Copy latest code
[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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20
21 package managers
22
23 import (
24         "e2mgr/configuration"
25         "e2mgr/logger"
26         "e2mgr/services"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28 )
29
30 type IRanReconnectionManager interface {
31         ReconnectRan(inventoryName string) error
32 }
33
34 type RanReconnectionManager struct {
35         logger              *logger.Logger
36         config              *configuration.Configuration
37         rnibDataService     services.RNibDataService
38         ranSetupManager     *RanSetupManager
39         e2tInstancesManager IE2TInstancesManager
40 }
41
42 func NewRanReconnectionManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, ranSetupManager *RanSetupManager, e2tInstancesManager IE2TInstancesManager) *RanReconnectionManager {
43         return &RanReconnectionManager{
44                 logger:              logger,
45                 config:              config,
46                 rnibDataService:     rnibDataService,
47                 ranSetupManager:     ranSetupManager,
48                 e2tInstancesManager: e2tInstancesManager,
49         }
50 }
51
52 func (m *RanReconnectionManager) isRanExceededConnectionAttempts(nodebInfo *entities.NodebInfo) bool {
53         return int(nodebInfo.GetConnectionAttempts()) >= m.config.MaxConnectionAttempts
54 }
55
56 func (m *RanReconnectionManager) ReconnectRan(inventoryName string) error {
57         nodebInfo, rnibErr := m.rnibDataService.GetNodeb(inventoryName)
58
59         if rnibErr != nil {
60                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed fetching RAN from rNib. Error: %v", inventoryName, rnibErr)
61                 return rnibErr
62         }
63
64         m.logger.Infof("#RanReconnectionManager.ReconnectRan - RAN name: %s - RAN's connection status: %s, RAN's connection attempts: %d", nodebInfo.RanName, nodebInfo.ConnectionStatus, nodebInfo.ConnectionAttempts)
65
66         if !m.canReconnectRan(nodebInfo) {
67                 e2tAddress := nodebInfo.AssociatedE2TInstanceAddress
68                 err := m.updateUnconnectableRan(nodebInfo)
69
70                 if err != nil {
71                         return err
72                 }
73
74                 if m.isRanExceededConnectionAttempts(nodebInfo) {
75                         return m.e2tInstancesManager.DissociateRan(nodebInfo.RanName, e2tAddress)
76                 }
77
78                 return nil
79         }
80
81         err := m.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
82
83         if err != nil {
84                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed executing setup. Error: %v", inventoryName, err)
85                 return err
86         }
87
88         return nil
89 }
90
91 func (m *RanReconnectionManager) canReconnectRan(nodebInfo *entities.NodebInfo) bool {
92         connectionStatus := nodebInfo.GetConnectionStatus()
93         return connectionStatus != entities.ConnectionStatus_SHUT_DOWN && connectionStatus != entities.ConnectionStatus_SHUTTING_DOWN &&
94                 int(nodebInfo.GetConnectionAttempts()) < m.config.MaxConnectionAttempts
95 }
96
97 func (m *RanReconnectionManager) updateNodebInfo(nodebInfo *entities.NodebInfo, connectionStatus entities.ConnectionStatus, resetE2tAddress bool) error {
98
99         nodebInfo.ConnectionStatus = connectionStatus;
100
101         if resetE2tAddress {
102                 nodebInfo.AssociatedE2TInstanceAddress = ""
103         }
104
105         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
106
107         if err != nil {
108                 m.logger.Errorf("#RanReconnectionManager.updateNodebInfo - RAN name: %s - Failed updating RAN's connection status to %s in rNib. Error: %v", nodebInfo.RanName, connectionStatus, err)
109                 return err
110         }
111
112         m.logger.Infof("#RanReconnectionManager.updateNodebInfo - RAN name: %s - Successfully updated rNib. RAN's current connection status: %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)
113         return nil
114 }
115
116 func (m *RanReconnectionManager) updateUnconnectableRan(nodebInfo *entities.NodebInfo) error {
117         connectionStatus := nodebInfo.GetConnectionStatus()
118
119         if connectionStatus == entities.ConnectionStatus_SHUT_DOWN {
120                 m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUT_DOWN", nodebInfo.RanName)
121                 return nil
122         }
123
124         if connectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
125                 m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUTTING_DOWN", nodebInfo.RanName)
126                 return m.updateNodebInfo(nodebInfo, entities.ConnectionStatus_SHUT_DOWN, false)
127         }
128
129         if m.isRanExceededConnectionAttempts(nodebInfo) {
130                 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)
131                 return m.updateNodebInfo(nodebInfo, entities.ConnectionStatus_DISCONNECTED, true)
132         }
133
134         return nil
135 }