2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 // Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 // platform project (RICP).
21 package rmrmsghandlers
26 "e2mgr/e2managererrors"
32 "e2mgr/services/rmrsender"
37 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
38 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
44 emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention", "request-id-unknown",
45 "v60s", "v20s", "v10s", "v5s", "v2s", "v1s"}
46 gnbTypesMap = map[string]entities.GnbType{
47 "gnb": entities.GnbType_GNB,
48 "en_gnb": entities.GnbType_EN_GNB,
50 enbTypesMap = map[string]entities.EnbType{
51 "enB_macro": entities.EnbType_MACRO_ENB,
52 "enB_home": entities.EnbType_HOME_ENB,
53 "enB_shortmacro": entities.EnbType_SHORT_MACRO_ENB,
54 "enB_longmacro": entities.EnbType_LONG_MACRO_ENB,
55 "ng_enB_macro": entities.EnbType_MACRO_NG_ENB,
56 "ng_enB_shortmacro": entities.EnbType_SHORT_MACRO_NG_ENB,
57 "ng_enB_longmacro": entities.EnbType_LONG_MACRO_NG_ENB,
61 type E2SetupRequestNotificationHandler struct {
63 config *configuration.Configuration
64 e2tInstancesManager managers.IE2TInstancesManager
65 rmrSender *rmrsender.RmrSender
66 rNibDataService services.RNibDataService
67 e2tAssociationManager *managers.E2TAssociationManager
68 ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
69 ranListManager managers.RanListManager
72 func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configuration.Configuration, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager, ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager, ranListManager managers.RanListManager) *E2SetupRequestNotificationHandler {
73 return &E2SetupRequestNotificationHandler{
76 e2tInstancesManager: e2tInstancesManager,
78 rNibDataService: rNibDataService,
79 e2tAssociationManager: e2tAssociationManager,
80 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
81 ranListManager: ranListManager,
85 func (h *E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
86 ranName := request.RanName
87 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
89 generalConfiguration, err := h.rNibDataService.GetGeneralConfiguration()
92 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - Failed retrieving e2m general configuration - quitting e2 setup flow. error: %s", err)
96 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - got general configuration from rnib - enableRic: %t", generalConfiguration.EnableRic)
98 if !generalConfiguration.EnableRic {
99 cause := models.Cause{Misc: &models.CauseMisc{OmIntervention: &struct{}{}}}
100 h.handleUnsuccessfulResponse(ranName, request, cause)
104 setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
106 h.logger.Errorf(err.Error())
110 h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
111 h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_REQUEST has been parsed successfully %+v", setupRequest)
113 _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
116 h.logger.Errorf("#E2TermInitNotificationHandler.Handle - Failed retrieving E2TInstance. error: %s", err)
120 nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
122 var functionsModified bool
126 if _, ok := err.(*common.ResourceNotFoundError); !ok {
127 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
131 if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
132 if _, ok := err.(*e2managererrors.UnknownSetupRequestRanNameError); ok {
133 cause := models.Cause{RicRequest: &models.CauseRic{RequestIdUnknown: &struct{}{}}}
134 h.handleUnsuccessfulResponse(ranName, request, cause)
141 functionsModified, err = h.handleExistingRan(ranName, nodebInfo, setupRequest)
148 ranStatusChangePublished, err := h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
152 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
153 if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
155 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
159 cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
160 h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause)
165 if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
169 h.handleSuccessfulResponse(ranName, request, setupRequest)
172 func (h *E2SetupRequestNotificationHandler) handleUpdateAndPublishNodebInfo(functionsModified bool, ranStatusChangePublished bool, nodebInfo *entities.NodebInfo) error {
174 if ranStatusChangePublished || !functionsModified {
178 err := h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
181 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
185 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Successfully executed UpdateNodebInfoAndPublish", nodebInfo.RanName)
190 func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
192 nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
194 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed building nodebInfo. Error: %s", ranName, err)
198 err = h.rNibDataService.SaveNodeb(nodebInfo)
200 h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed saving nodebInfo. Error: %s", ranName, err)
204 nbIdentity := h.buildNbIdentity(ranName, setupRequest)
206 err = h.ranListManager.AddNbIdentity(nodebInfo.GetNodeType(), nbIdentity)
212 return nodebInfo, nil
215 func (h *E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) (bool, error) {
216 if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
217 h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
218 return false, errors.New("nodeB entity in incorrect state")
221 nodebInfo.SetupFromNetwork = true
223 if nodebInfo.NodeType == entities.Node_ENB {
227 setupMessageRanFuncs := setupRequest.ExtractRanFunctionsList()
229 if setupMessageRanFuncs == nil || (len(setupMessageRanFuncs) == 0 && len(nodebInfo.GetGnb().RanFunctions) == 0) {
233 nodebInfo.GetGnb().RanFunctions = setupMessageRanFuncs
237 func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause) {
238 failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s, cause)
239 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
241 responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
243 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
246 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload,emptyTagsToReplaceToSelfClosingTags)
248 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - payload: %s", responsePayload)
249 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
250 h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
251 _ = h.rmrSender.WhSend(msg)
255 func (h *E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
257 plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
259 ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicId)
263 successResponse := models.NewE2SetupSuccessResponseMessage(plmnId, ricNearRtId, setupRequest)
264 h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
266 responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
268 h.logger.Warnf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
271 responsePayload = utils.ReplaceEmptyTagsWithSelfClosing(responsePayload,emptyTagsToReplaceToSelfClosingTags)
273 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - payload: %s", responsePayload)
275 msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
276 h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
277 _ = h.rmrSender.Send(msg)
280 func buildPlmnId(mmc string, mnc string) string {
281 var b strings.Builder
297 func convertTo20BitString(ricNearRtId string) (string, error) {
298 r, err := strconv.ParseUint(ricNearRtId, 16, 32)
302 return fmt.Sprintf("%020b", r)[:20], nil
305 func (h *E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
307 pipInd := bytes.IndexByte(payload, '|')
309 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
312 e2tIpAddress := string(payload[:pipInd])
313 if len(e2tIpAddress) == 0 {
314 return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
317 h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
319 setupRequest := &models.E2SetupRequestMessage{}
320 err := xml.Unmarshal(utils.NormalizeXml(payload[pipInd+1:]), &setupRequest.E2APPDU)
322 return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
325 return setupRequest, e2tIpAddress, nil
328 func (h *E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
329 nodebInfo := &entities.NodebInfo{
330 AssociatedE2TInstanceAddress: e2tAddress,
332 GlobalNbId: h.buildGlobalNbId(request),
333 SetupFromNetwork: true,
335 err := h.setNodeTypeAndConfiguration(nodebInfo)
340 if nodebInfo.NodeType == entities.Node_ENB {
341 return nodebInfo, nil
344 ranFuncs := request.ExtractRanFunctionsList()
347 nodebInfo.GetGnb().RanFunctions = ranFuncs
350 return nodebInfo, nil
353 func (h *E2SetupRequestNotificationHandler) setNodeTypeAndConfiguration(nodebInfo *entities.NodebInfo) error {
354 for k, v := range gnbTypesMap {
355 if strings.HasPrefix(nodebInfo.RanName, k) {
356 nodebInfo.NodeType = entities.Node_GNB
357 nodebInfo.Configuration = &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{GnbType: v}}
361 for k, v := range enbTypesMap {
362 if strings.HasPrefix(nodebInfo.RanName, k) {
363 nodebInfo.NodeType = entities.Node_ENB
364 nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &entities.Enb{EnbType: v}}
369 return e2managererrors.NewUnknownSetupRequestRanNameError(nodebInfo.RanName)
372 func (h *E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
373 return &entities.GlobalNbId{
374 PlmnId: setupRequest.GetPlmnId(),
375 NbId: setupRequest.GetNbId(),
379 func (h *E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
380 return &entities.NbIdentity{
381 InventoryName: ranName,
382 GlobalNbId: h.buildGlobalNbId(setupRequest),