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