RIC-244 - Eliminate Setup/Reset request from dashboard & connectionAttempts
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / setup_response_notification_handler.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 rmrmsghandlers
21
22 import (
23         "e2mgr/enums"
24         "e2mgr/logger"
25         "e2mgr/managers"
26         "e2mgr/models"
27         "e2mgr/rmrCgo"
28         "e2mgr/services"
29         "e2mgr/utils"
30         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
31 )
32
33 type SetupResponseNotificationHandler struct {
34         logger                 *logger.Logger
35         rnibDataService        services.RNibDataService
36         setupResponseManager   managers.ISetupResponseManager
37         ranStatusChangeManager managers.IRanStatusChangeManager
38         msgType                int
39 }
40
41 var msgTypeToMsgName = map[int]string{
42         rmrCgo.RIC_X2_SETUP_RESP:         "X2 Setup Response",
43         rmrCgo.RIC_X2_SETUP_FAILURE:      "X2 Setup Failure Response",
44         rmrCgo.RIC_ENDC_X2_SETUP_RESP:    "ENDC Setup Response",
45         rmrCgo.RIC_ENDC_X2_SETUP_FAILURE: "ENDC Setup Failure Response",
46 }
47
48 func NewSetupResponseNotificationHandler(logger *logger.Logger, rnibDataService services.RNibDataService, setupResponseManager managers.ISetupResponseManager, ranStatusChangeManager managers.IRanStatusChangeManager, msgType int) SetupResponseNotificationHandler {
49         return SetupResponseNotificationHandler{
50                 logger: logger,
51                 rnibDataService:        rnibDataService,
52                 setupResponseManager:   setupResponseManager,
53                 ranStatusChangeManager: ranStatusChangeManager,
54                 msgType:                msgType,
55         }
56 }
57
58 func (h SetupResponseNotificationHandler) Handle(request *models.NotificationRequest) {
59         msgName := msgTypeToMsgName[h.msgType]
60         h.logger.Infof("#SetupResponseNotificationHandler - RAN name: %s - Received %s notification", request.RanName, msgName)
61
62         inventoryName := request.RanName
63
64         nodebInfo, rnibErr := h.rnibDataService.GetNodeb(inventoryName)
65
66         if rnibErr != nil {
67                 h.logger.Errorf("#SetupResponseNotificationHandler - RAN name: %s - Error fetching RAN from rNib: %v", request.RanName, rnibErr)
68                 return
69         }
70
71         if !isConnectionStatusValid(nodebInfo.ConnectionStatus) {
72                 h.logger.Errorf("#SetupResponseNotificationHandler - RAN name: %s - Invalid RAN connection status: %s", request.RanName, nodebInfo.ConnectionStatus)
73                 return
74         }
75
76         nbIdentity := &entities.NbIdentity{InventoryName: inventoryName}
77         err := h.setupResponseManager.PopulateNodebByPdu(h.logger, nbIdentity, nodebInfo, request.Payload)
78
79         if err != nil {
80                 return
81         }
82
83         rnibErr = h.rnibDataService.SaveNodeb(nbIdentity, nodebInfo)
84
85         if rnibErr != nil {
86                 h.logger.Errorf("#SetupResponseNotificationHandler - RAN name: %s - Error saving RAN to rNib: %v", request.RanName, rnibErr)
87                 return
88         }
89
90         h.logger.Infof("#SetupResponseNotificationHandler - RAN name: %s - Successfully saved RAN to rNib", request.RanName)
91         h.logger.Infof("#SetupResponseNotificationHandler - Summary: elapsed time for receiving and handling setup response message from E2 terminator: %f ms", utils.ElapsedTime(request.StartTime))
92
93         if !isSuccessSetupResponseMessage(h.msgType) {
94                 return
95         }
96
97         _ = h.ranStatusChangeManager.Execute(rmrCgo.RAN_CONNECTED, enums.RIC_TO_RAN, nodebInfo)
98 }
99
100 func isConnectionStatusValid(connectionStatus entities.ConnectionStatus) bool {
101         return connectionStatus == entities.ConnectionStatus_CONNECTING || connectionStatus == entities.ConnectionStatus_CONNECTED
102 }
103
104 func isSuccessSetupResponseMessage(msgType int) bool {
105         return msgType == rmrCgo.RIC_X2_SETUP_RESP || msgType == rmrCgo.RIC_ENDC_X2_SETUP_RESP
106 }