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