[RICPLT-2585] Init fixes | Init UTs..........
[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         e2tInstancesManager IE2TInstancesManager
37 }
38
39 func NewRanReconnectionManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, ranSetupManager *RanSetupManager, e2tInstancesManager IE2TInstancesManager) *RanReconnectionManager {
40         return &RanReconnectionManager{
41                 logger:              logger,
42                 config:              config,
43                 rnibDataService:     rnibDataService,
44                 ranSetupManager:     ranSetupManager,
45                 e2tInstancesManager: e2tInstancesManager,
46         }
47 }
48
49 func (m *RanReconnectionManager) isRanExceededConnectionAttempts(nodebInfo *entities.NodebInfo) bool {
50         return int(nodebInfo.GetConnectionAttempts()) >= m.config.MaxConnectionAttempts
51 }
52
53 func (m *RanReconnectionManager) ReconnectRan(inventoryName string) error {
54         nodebInfo, rnibErr := m.rnibDataService.GetNodeb(inventoryName)
55
56         if rnibErr != nil {
57                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed fetching RAN from rNib. Error: %v", inventoryName, rnibErr)
58                 return rnibErr
59         }
60
61         m.logger.Infof("#RanReconnectionManager.ReconnectRan - RAN name: %s - RAN's connection status: %s, RAN's connection attempts: %d", nodebInfo.RanName, nodebInfo.ConnectionStatus, nodebInfo.ConnectionAttempts)
62
63         if !m.canReconnectRan(nodebInfo) {
64                 e2tAddress := nodebInfo.AssociatedE2TInstanceAddress
65                 err := m.updateUnconnectableRan(nodebInfo)
66
67                 if err != nil {
68                         return err
69                 }
70
71                 if m.isRanExceededConnectionAttempts(nodebInfo) {
72                         return m.e2tInstancesManager.DeassociateRan(nodebInfo.RanName, e2tAddress)
73                 }
74
75                 return nil
76         }
77
78         err := m.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
79
80         if err != nil {
81                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed executing setup. Error: %v", inventoryName, err)
82                 return err
83         }
84
85         return nil
86 }
87
88 func (m *RanReconnectionManager) canReconnectRan(nodebInfo *entities.NodebInfo) bool {
89         connectionStatus := nodebInfo.GetConnectionStatus()
90         return connectionStatus != entities.ConnectionStatus_SHUT_DOWN && connectionStatus != entities.ConnectionStatus_SHUTTING_DOWN &&
91                 int(nodebInfo.GetConnectionAttempts()) < m.config.MaxConnectionAttempts
92 }
93
94 func (m *RanReconnectionManager) updateNodebInfo(nodebInfo *entities.NodebInfo, connectionStatus entities.ConnectionStatus, resetE2tAddress bool) error {
95
96         nodebInfo.ConnectionStatus = connectionStatus;
97
98         if resetE2tAddress {
99                 nodebInfo.AssociatedE2TInstanceAddress = ""
100         }
101
102         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
103
104         if err != nil {
105                 m.logger.Errorf("#RanReconnectionManager.updateNodebInfo - RAN name: %s - Failed updating RAN's connection status to %s in rNib. Error: %v", nodebInfo.RanName, connectionStatus, err)
106                 return err
107         }
108
109         m.logger.Infof("#RanReconnectionManager.updateNodebInfo - RAN name: %s - Successfully updated rNib. RAN's current connection status: %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)
110         return nil
111 }
112
113 func (m *RanReconnectionManager) updateUnconnectableRan(nodebInfo *entities.NodebInfo) error {
114         connectionStatus := nodebInfo.GetConnectionStatus()
115
116         if connectionStatus == entities.ConnectionStatus_SHUT_DOWN {
117                 m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUT_DOWN", nodebInfo.RanName)
118                 return nil
119         }
120
121         if connectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
122                 m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUTTING_DOWN", nodebInfo.RanName)
123                 return m.updateNodebInfo(nodebInfo, entities.ConnectionStatus_SHUT_DOWN, false)
124         }
125
126         if m.isRanExceededConnectionAttempts(nodebInfo) {
127                 m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: RAN's connection attempts exceeded the limit (%d)", nodebInfo.RanName, m.config.MaxConnectionAttempts)
128                 return m.updateNodebInfo(nodebInfo, entities.ConnectionStatus_DISCONNECTED, true)
129         }
130
131         return nil
132 }