f27f810f0cb7d301a8477b07b335411199de97fa
[ric-plt/e2mgr.git] / E2Manager / managers / ran_reconnection_manager.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 package managers
19
20 import (
21         "e2mgr/configuration"
22         "e2mgr/logger"
23         "e2mgr/services"
24         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
25 )
26
27 type IRanReconnectionManager interface {
28         ReconnectRan(inventoryName string) error
29 }
30
31 type RanReconnectionManager struct {
32         logger          *logger.Logger
33         config          *configuration.Configuration
34         rnibDataService services.RNibDataService
35         ranSetupManager *RanSetupManager
36 }
37
38 func NewRanReconnectionManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, ranSetupManager *RanSetupManager) *RanReconnectionManager {
39         return &RanReconnectionManager{
40                 logger:          logger,
41                 config:          config,
42                 rnibDataService: rnibDataService,
43                 ranSetupManager: ranSetupManager,
44         }
45 }
46
47 func (m *RanReconnectionManager) ReconnectRan(inventoryName string) error {
48         nodebInfo, rnibErr := m.rnibDataService.GetNodeb(inventoryName)
49
50         if rnibErr != nil {
51                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed fetching RAN from rNib. Error: %v", inventoryName, rnibErr)
52                 return rnibErr
53         }
54
55         m.logger.Infof("#RanReconnectionManager.ReconnectRan - RAN name: %s - RAN's connection status: %s, RAN's connection attempts: %d", nodebInfo.RanName, nodebInfo.ConnectionStatus, nodebInfo.ConnectionAttempts)
56
57         if !m.canReconnectRan(nodebInfo) {
58                 return m.setConnectionStatusOfUnconnectableRan(nodebInfo)
59         }
60
61         err := m.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
62
63         if err != nil {
64                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed executing setup. Error: %v", inventoryName, err)
65                 return err
66         }
67
68         return nil
69 }
70
71 func (m *RanReconnectionManager) canReconnectRan(nodebInfo *entities.NodebInfo) bool {
72         connectionStatus := nodebInfo.GetConnectionStatus()
73         return connectionStatus != entities.ConnectionStatus_SHUT_DOWN && connectionStatus != entities.ConnectionStatus_SHUTTING_DOWN &&
74                 int(nodebInfo.GetConnectionAttempts()) < m.config.MaxConnectionAttempts
75 }
76
77 func (m *RanReconnectionManager) updateNodebInfoStatus(nodebInfo *entities.NodebInfo, connectionStatus entities.ConnectionStatus) error {
78         if nodebInfo.ConnectionStatus == connectionStatus {
79                 return nil
80         }
81
82         nodebInfo.ConnectionStatus = connectionStatus;
83         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
84
85         if err != nil {
86                 m.logger.Errorf("#RanReconnectionManager.updateNodebInfoStatus - RAN name: %s - Failed updating RAN's connection status to %s in rNib. Error: %v", nodebInfo.RanName, connectionStatus, err)
87                 return err
88         }
89
90         m.logger.Infof("#RanReconnectionManager.updateNodebInfoStatus - RAN name: %s - Successfully updated rNib. RAN's current connection status: %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)
91         return nil
92 }
93
94 func (m *RanReconnectionManager) setConnectionStatusOfUnconnectableRan(nodebInfo *entities.NodebInfo) error {
95         connectionStatus := nodebInfo.GetConnectionStatus()
96
97         if connectionStatus == entities.ConnectionStatus_SHUT_DOWN {
98                 m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUT_DOWN", nodebInfo.RanName)
99                 return nil
100         }
101
102         if connectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
103                 m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUTTING_DOWN", nodebInfo.RanName)
104                 return m.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_SHUT_DOWN)
105         }
106
107         if int(nodebInfo.GetConnectionAttempts()) >= m.config.MaxConnectionAttempts {
108                 m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: RAN's connection attempts exceeded the limit (%d)", nodebInfo.RanName, m.config.MaxConnectionAttempts)
109                 return m.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_DISCONNECTED)
110         }
111
112         return nil
113 }