[RIC-587] Update E2 Setup existing nodeb behavior
[ric-plt/e2mgr.git] / E2Manager / managers / ran_connect_status_change_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 package managers
21
22 import (
23         "e2mgr/logger"
24         "e2mgr/services"
25         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
26 )
27
28 const (
29         CONNECTED_RAW_EVENT    = "CONNECTED"
30         DISCONNECTED_RAW_EVENT = "DISCONNECTED"
31         NONE_RAW_EVENT         = "NONE"
32 )
33
34 type IRanConnectStatusChangeManager interface {
35         ChangeStatus(nodebInfo *entities.NodebInfo, nextStatus entities.ConnectionStatus) (bool, error)
36 }
37
38 type RanConnectStatusChangeManager struct {
39         logger          *logger.Logger
40         rnibDataService services.RNibDataService
41         ranListManager  RanListManager
42         ranAlarmService services.RanAlarmService
43 }
44
45 func NewRanConnectStatusChangeManager(logger *logger.Logger, rnibDataService services.RNibDataService, ranListManager RanListManager, ranAlarmService services.RanAlarmService) *RanConnectStatusChangeManager {
46         return &RanConnectStatusChangeManager{
47                 logger:          logger,
48                 rnibDataService: rnibDataService,
49                 ranListManager:  ranListManager,
50                 ranAlarmService: ranAlarmService,
51         }
52 }
53
54 func (m *RanConnectStatusChangeManager) ChangeStatus(nodebInfo *entities.NodebInfo, nextStatus entities.ConnectionStatus) (bool, error) {
55         m.logger.Infof("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s, currentStatus: %s, nextStatus: %s", nodebInfo.RanName, nodebInfo.GetConnectionStatus(), nextStatus)
56
57         var ranStatusChangePublished bool
58
59         // set the proper event
60         event := m.setEvent(nodebInfo, nextStatus)
61         isConnectivityEvent := event != NONE_RAW_EVENT
62
63         // only after determining event we set next status
64         nodebInfo.ConnectionStatus = nextStatus;
65         if !isConnectivityEvent {
66                 err := m.updateNodebInfo(nodebInfo)
67                 if err != nil {
68                         return ranStatusChangePublished, err
69                 }
70         } else {
71                 err := m.updateNodebInfoOnConnectionStatusInversion(nodebInfo, event)
72                 if err != nil {
73                         return ranStatusChangePublished, err
74                 }
75                 ranStatusChangePublished = true
76         }
77
78         // in any case, update RanListManager
79         m.logger.Infof("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s, updating RanListManager... status: %s", nodebInfo.RanName, nodebInfo.GetConnectionStatus())
80         err := m.ranListManager.UpdateRanState(nodebInfo)
81         if err != nil {
82                 m.logger.Errorf("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s - Failed updating RAN's connection status by RanListManager. Error: %v", nodebInfo.RanName, err)
83                 // log and proceed...
84         }
85
86         if isConnectivityEvent {
87                 m.logger.Infof("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s, setting alarm at RanAlarmService... event: %s", nodebInfo.RanName, event)
88                 err := m.ranAlarmService.SetConnectivityChangeAlarm(nodebInfo)
89                 if err != nil {
90                         m.logger.Errorf("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s - Failed setting an alarm by RanAlarmService. Error: %v", nodebInfo.RanName, err)
91                         // log and proceed...
92                 }
93         }
94
95         return ranStatusChangePublished, nil
96 }
97
98 func (m *RanConnectStatusChangeManager) updateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error {
99
100         err := m.rnibDataService.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, event)
101
102         if err != nil {
103                 m.logger.Errorf("#RanConnectStatusChangeManager.updateNodebInfoOnConnectionStatusInversion - RAN name: %s - Failed updating RAN's connection status in rNib. Error: %v", nodebInfo.RanName, err)
104                 return err
105         }
106
107         m.logger.Infof("#RanConnectStatusChangeManager.updateNodebInfoOnConnectionStatusInversion - RAN name: %s - Successfully updated rNib.", nodebInfo.RanName)
108         return nil
109 }
110
111 func (m *RanConnectStatusChangeManager) updateNodebInfo(nodebInfo *entities.NodebInfo) error {
112
113         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
114
115         if err != nil {
116                 m.logger.Errorf("#RanConnectStatusChangeManager.updateNodebInfo - RAN name: %s - Failed updating RAN's connection status in rNib. Error: %v", nodebInfo.RanName, err)
117                 return err
118         }
119
120         m.logger.Infof("#RanConnectStatusChangeManager.updateNodebInfo - RAN name: %s - Successfully updated rNib.", nodebInfo.RanName)
121         return nil
122 }
123
124 func (m *RanConnectStatusChangeManager) setEvent(nodebInfo *entities.NodebInfo, nextState entities.ConnectionStatus) string {
125         currentConnectionStatus := nodebInfo.GetConnectionStatus()
126
127         var event string
128         if currentConnectionStatus != entities.ConnectionStatus_CONNECTED && nextState == entities.ConnectionStatus_CONNECTED {
129                 event = nodebInfo.RanName + "_" + CONNECTED_RAW_EVENT
130         } else if currentConnectionStatus == entities.ConnectionStatus_CONNECTED && nextState != entities.ConnectionStatus_CONNECTED {
131                 event = nodebInfo.RanName + "_" + DISCONNECTED_RAW_EVENT
132         } else {
133                 event = NONE_RAW_EVENT
134         }
135
136         m.logger.Infof("#RanConnectStatusChangeManager.setEvent - Connectivity Event for RAN %s is: %s", nodebInfo.RanName, event)
137         return event
138 }