603458e7e5013b41d882ba91791c29aef4f5ad79
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_setup_request_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         "bytes"
24         "e2mgr/logger"
25         "e2mgr/managers"
26         "e2mgr/models"
27         "e2mgr/rmrCgo"
28         "e2mgr/services"
29         "e2mgr/services/rmrsender"
30         "e2mgr/utils"
31         "encoding/xml"
32         "errors"
33         "fmt"
34         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
35         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
36 )
37
38 type E2SetupRequestNotificationHandler struct {
39         logger                 *logger.Logger
40         e2tInstancesManager    managers.IE2TInstancesManager
41         rmrSender              *rmrsender.RmrSender
42         rNibDataService       services.RNibDataService
43         e2tAssociationManager *managers.E2TAssociationManager
44 }
45
46 func NewE2SetupRequestNotificationHandler(logger *logger.Logger, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager) E2SetupRequestNotificationHandler {
47         return E2SetupRequestNotificationHandler{
48                 logger:                 logger,
49                 e2tInstancesManager:    e2tInstancesManager,
50                 rmrSender: rmrSender,
51                 rNibDataService: rNibDataService,
52                 e2tAssociationManager: e2tAssociationManager,
53         }
54 }
55
56 func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest){
57         ranName := request.RanName
58         h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2 Setup Request. Payload: %x", ranName, request.Payload)
59
60         setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
61         if err != nil {
62                 h.logger.Errorf(err.Error())
63                 return
64         }
65
66         h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
67
68         _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
69
70         if err != nil {
71                 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
72                 return
73         }
74
75         nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
76         if err != nil{
77                 if _, ok := err.(*common.ResourceNotFoundError); ok{
78                         nbIdentity := h.buildNbIdentity(ranName, setupRequest)
79                         nodebInfo = h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
80                         err = h.rNibDataService.SaveNodeb(nbIdentity, nodebInfo)
81                         if err != nil{
82                                 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to save nodebInfo entity. Error: %s", ranName, err)
83                                 return
84                         }
85                 } else{
86                         h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
87                         return
88                 }
89
90         } else {
91                 if nodebInfo.ConnectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
92                         h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", nodebInfo.RanName, nodebInfo.ConnectionStatus)
93                         h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - Summary: elapsed time for receiving and handling setup request message from E2 terminator: %f ms", utils.ElapsedTime(request.StartTime))
94                         return
95                 }
96                 h.updateNodeBFunctions(nodebInfo, setupRequest)
97         }
98         err = h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
99         if err != nil{
100                 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
101                 return
102         }
103         successResponse := &models.E2SetupSuccessResponseMessage{}
104         successResponse.SetPlmnId(setupRequest.GetPlmnId())
105         successResponse.SetNbId("&" + fmt.Sprintf("%020b", 0xf0))
106         responsePayload, err := xml.Marshal(successResponse)
107         if err != nil{
108                 h.logger.Warnf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - Error marshalling E2 Setup Response. Response: %x", ranName, responsePayload)
109         }
110         msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, request.TransactionId)
111         h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - E2 Setup Request has been built. Message: %x", ranName, msg)
112         //TODO err = h.rmrSender.Send(msg)
113
114 }
115
116 func (h E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte)(*models.E2SetupRequestMessage, string, error){
117
118         colonInd := bytes.IndexByte(payload, ':')
119         if colonInd < 0 {
120                 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request, failed extract E2T IP Address: no ':' separator found")
121         }
122
123         e2tIpAddress := string(payload[:colonInd])
124         if len(e2tIpAddress) == 0 {
125                 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
126         }
127
128         pipInd := bytes.IndexByte(payload, '|')
129         if pipInd < 0 {
130                 return nil, "", errors.New( "#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
131         }
132
133         setupRequest := &models.E2SetupRequestMessage{}
134         err := xml.Unmarshal(payload[pipInd + 1:], &setupRequest)
135         if err != nil {
136                 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %s")
137         }
138
139         return setupRequest, e2tIpAddress, nil
140 }
141
142 func (h E2SetupRequestNotificationHandler) updateNodeBFunctions(nodeB *entities.NodebInfo, request *models.E2SetupRequestMessage){
143         //TODO the function should be implemented in the scope of the US 192 "Save the entire Setup request in RNIB"
144 }
145
146 func (h E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) *entities.NodebInfo{
147         nodebInfo := &entities.NodebInfo{
148                 AssociatedE2TInstanceAddress: e2tAddress,
149                 ConnectionStatus: entities.ConnectionStatus_CONNECTED,
150                 RanName: ranName,
151                 NodeType: entities.Node_GNB,
152                 Configuration: &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
153         }
154         h.updateNodeBFunctions(nodebInfo, request)
155         return nodebInfo
156 }
157
158 func (h E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage)*entities.NbIdentity{
159         return &entities.NbIdentity{
160                 InventoryName:ranName,
161                 GlobalNbId: &entities.GlobalNbId{
162                         PlmnId: setupRequest.GetPlmnId(),
163                         NbId:   setupRequest.GetNbId(),
164                 },
165         }
166 }