87a656ec3041b2cff9907be763c08498cb91c4e7
[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/rNibWriter"
24         "e2mgr/services"
25         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
26         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
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         rnibReaderProvider func() reader.RNibReader
38         rnibWriterProvider func() rNibWriter.RNibWriter
39         ranSetupManager    *RanSetupManager
40 }
41
42 func NewRanReconnectionManager(logger *logger.Logger, config *configuration.Configuration, rnibReaderProvider func() reader.RNibReader, rnibWriterProvider func() rNibWriter.RNibWriter, rmrService *services.RmrService) *RanReconnectionManager {
43         return &RanReconnectionManager{
44                 logger:             logger,
45                 config:             config,
46                 rnibReaderProvider: rnibReaderProvider,
47                 rnibWriterProvider: rnibWriterProvider,
48                 ranSetupManager:    NewRanSetupManager(logger,rmrService,rnibWriterProvider),
49         }
50 }
51
52 func (m *RanReconnectionManager) ReconnectRan(inventoryName string) error {
53         nodebInfo, rnibErr := m.rnibReaderProvider().GetNodeb(inventoryName)
54
55         if rnibErr != nil {
56                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed fetching RAN from rNib. Error: %v", inventoryName, rnibErr)
57                 return rnibErr
58         }
59
60         m.logger.Infof("#RanReconnectionManager.ReconnectRan - RAN name: %s - RAN's connection status: %s, RAN's connection attempts: %d", nodebInfo.RanName, nodebInfo.ConnectionStatus, nodebInfo.ConnectionAttempts)
61
62         if !m.canReconnectRan(nodebInfo) {
63                 return m.setConnectionStatusOfUnconnectableRan(nodebInfo)
64         }
65
66
67         err := m.ranSetupManager.ExecuteSetup(nodebInfo)
68
69         if err != nil {
70                 m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed executing setup. Error: %v", inventoryName, err)
71                 return err
72         }
73
74         return nil
75 }
76
77 func (m *RanReconnectionManager) canReconnectRan(nodebInfo *entities.NodebInfo) bool {
78         connectionStatus := nodebInfo.GetConnectionStatus()
79         return connectionStatus != entities.ConnectionStatus_SHUT_DOWN && connectionStatus != entities.ConnectionStatus_SHUTTING_DOWN &&
80                 int(nodebInfo.GetConnectionAttempts()) < m.config.MaxConnectionAttempts
81 }
82
83 func (m *RanReconnectionManager) updateNodebInfoStatus(nodebInfo *entities.NodebInfo, connectionStatus entities.ConnectionStatus) common.IRNibError {
84         if nodebInfo.ConnectionStatus == connectionStatus {
85                 return nil
86         }
87
88         nodebInfo.ConnectionStatus = connectionStatus;
89         err := m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
90
91         if err != nil {
92                 m.logger.Errorf("#RanReconnectionManager.updateNodebInfoStatus - RAN name: %s - Failed updating RAN's connection status to %s in rNib. Error: %v", nodebInfo.RanName, connectionStatus, err)
93                 return err
94         }
95
96         m.logger.Infof("#RanReconnectionManager.updateNodebInfoStatus - RAN name: %s - Successfully updated rNib. RAN's current connection status: %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)
97         return nil
98 }
99
100 func (m *RanReconnectionManager) setConnectionStatusOfUnconnectableRan(nodebInfo *entities.NodebInfo) common.IRNibError {
101         connectionStatus := nodebInfo.GetConnectionStatus()
102
103         if connectionStatus == entities.ConnectionStatus_SHUT_DOWN {
104                 m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUT_DOWN", nodebInfo.RanName)
105                 return nil
106         }
107
108         if connectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
109                 m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUTTING_DOWN", nodebInfo.RanName)
110                 return m.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_SHUT_DOWN)
111         }
112
113         if int(nodebInfo.GetConnectionAttempts()) >= m.config.MaxConnectionAttempts {
114                 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)
115                 return m.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_DISCONNECTED)
116         }
117
118         return nil
119 }