Add R5 content to master
[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 RanConnectStatusChangeManager struct {
35         logger          *logger.Logger
36         rnibDataService services.RNibDataService
37         ranListManager  RanListManager
38         ranAlarmService services.RanAlarmService
39 }
40
41 func NewRanConnectStatusChangeManager(logger *logger.Logger, rnibDataService services.RNibDataService, ranListManager RanListManager, ranAlarmService services.RanAlarmService) *RanConnectStatusChangeManager {
42         return &RanConnectStatusChangeManager{
43                 logger:          logger,
44                 rnibDataService: rnibDataService,
45                 ranListManager:  ranListManager,
46                 ranAlarmService: ranAlarmService,
47         }
48 }
49
50 func (m *RanConnectStatusChangeManager) ChangeStatus(nodebInfo *entities.NodebInfo, nextStatus entities.ConnectionStatus) error {
51         m.logger.Infof("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s, currentStatus: %s, nextStatus: %s", nodebInfo.RanName, nodebInfo.GetConnectionStatus(), nextStatus)
52
53         // set the proper event
54         event := m.setEvent(nodebInfo, nextStatus)
55         isConnectivityEvent := event != NONE_RAW_EVENT
56
57         // only after determining event we set next status
58         nodebInfo.ConnectionStatus = nextStatus;
59         if !isConnectivityEvent {
60                 err := m.updateNodebInfo(nodebInfo)
61                 if err != nil {
62                         return err
63                 }
64         } else {
65                 err := m.updateNodebInfoOnConnectionStatusInversion(nodebInfo, event)
66                 if err != nil {
67                         return err
68                 }
69         }
70
71         // in any case, update RanListManager
72         m.logger.Infof("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s, updating RanListManager... status: %s", nodebInfo.RanName, nodebInfo.GetConnectionStatus())
73         err := m.ranListManager.UpdateRanState(nodebInfo)
74         if err != nil {
75                 m.logger.Errorf("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s - Failed updating RAN's connection status by RanListManager. Error: %v", nodebInfo.RanName, err)
76                 // log and proceed...
77         }
78
79         if isConnectivityEvent {
80                 m.logger.Infof("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s, setting alarm at RanAlarmService... event: %s", nodebInfo.RanName, event)
81                 err := m.ranAlarmService.SetConnectivityChangeAlarm(nodebInfo)
82                 if err != nil {
83                         m.logger.Errorf("#RanConnectStatusChangeManager.ChangeStatus - RAN name: %s - Failed setting an alarm by RanAlarmService. Error: %v", nodebInfo.RanName, err)
84                         // log and proceed...
85                 }
86         }
87
88         return nil
89 }
90
91 func (m *RanConnectStatusChangeManager) updateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error {
92
93         err := m.rnibDataService.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, event)
94
95         if err != nil {
96                 m.logger.Errorf("#RanConnectStatusChangeManager.updateNodebInfoOnConnectionStatusInversion - RAN name: %s - Failed updating RAN's connection status in rNib. Error: %v", nodebInfo.RanName, err)
97                 return err
98         }
99
100         m.logger.Infof("#RanConnectStatusChangeManager.updateNodebInfoOnConnectionStatusInversion - RAN name: %s - Successfully updated rNib.", nodebInfo.RanName)
101         return nil
102 }
103
104 func (m *RanConnectStatusChangeManager) updateNodebInfo(nodebInfo *entities.NodebInfo) error {
105
106         err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
107
108         if err != nil {
109                 m.logger.Errorf("#RanConnectStatusChangeManager.updateNodebInfo - RAN name: %s - Failed updating RAN's connection status in rNib. Error: %v", nodebInfo.RanName, err)
110                 return err
111         }
112
113         m.logger.Infof("#RanConnectStatusChangeManager.updateNodebInfo - RAN name: %s - Successfully updated rNib.", nodebInfo.RanName)
114         return nil
115 }
116
117 func (m *RanConnectStatusChangeManager) setEvent(nodebInfo *entities.NodebInfo, nextState entities.ConnectionStatus) string {
118         currentConnectionStatus := nodebInfo.GetConnectionStatus()
119
120         var event string
121         if currentConnectionStatus != entities.ConnectionStatus_CONNECTED && nextState == entities.ConnectionStatus_CONNECTED {
122                 event = nodebInfo.RanName + "_" + CONNECTED_RAW_EVENT
123         } else if currentConnectionStatus == entities.ConnectionStatus_CONNECTED && nextState != entities.ConnectionStatus_CONNECTED {
124                 event = nodebInfo.RanName + "_" + DISCONNECTED_RAW_EVENT
125         } else {
126                 event = NONE_RAW_EVENT
127         }
128
129         m.logger.Infof("#RanConnectStatusChangeManager.setEvent - Connectivity Event for RAN %s is: %s", nodebInfo.RanName, event)
130         return event
131 }