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