Merge "RIC-398 - Fix all critical issues from static code analysis scan" into PI5
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_setup_request_notification_handler.go
index d71810e..d65516c 100644 (file)
@@ -132,21 +132,24 @@ func (h E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAdd
        return nodebInfo, nil
 }
 
+func (h E2SetupRequestNotificationHandler) setGnbFunctions(nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
+       ranFunctions := setupRequest.ExtractRanFunctionsList()
+
+       if ranFunctions != nil {
+               nodebInfo.GetGnb().RanFunctions = ranFunctions
+       }
+
+       return nil
+}
+
 func (h E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
        if nodebInfo.GetConnectionStatus() == entities.ConnectionStatus_SHUTTING_DOWN {
                h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s, connection status: %s - nodeB entity in incorrect state", ranName, nodebInfo.ConnectionStatus)
                return errors.New("nodeB entity in incorrect state")
        }
 
-       ranFunctions, err := setupRequest.ExtractRanFunctionsList()
-
-       if err != nil {
-               h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to update nodebInfo entity. Error: %s", ranName, err)
-               return err
-       }
-
-       nodebInfo.GetGnb().RanFunctions = ranFunctions
-       return nil
+       err := h.setGnbFunctions(nodebInfo, setupRequest)
+       return err
 }
 
 func (h E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(nodebInfo *entities.NodebInfo, req *models.NotificationRequest) {
@@ -160,6 +163,8 @@ func (h E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(nodebInfo
 
        responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
 
+       h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - payload: %s", responsePayload)
+
        msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, nodebInfo.RanName, responsePayload, req.TransactionId, req.GetMsgSrc())
        h.logger.Infof("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", nodebInfo.RanName, msg)
        _ = h.rmrSender.WhSend(msg)
@@ -168,12 +173,13 @@ func (h E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(nodebInfo
 
 func (h E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
 
-       ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicNearRtId)
+       plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
+
+       ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicId)
        if err != nil {
-               h.logger.Errorf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - failed to convert RicNearRtId value %s to 20 bit string . Error: %s", ranName, h.config.GlobalRicId.RicNearRtId, err)
                return
        }
-       successResponse := models.NewE2SetupSuccessResponseMessage(h.config.GlobalRicId.PlmnId, ricNearRtId, setupRequest)
+       successResponse := models.NewE2SetupSuccessResponseMessage(plmnId, ricNearRtId, setupRequest)
        h.logger.Debugf("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
 
        responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
@@ -183,11 +189,30 @@ func (h E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName stri
 
        responsePayload = replaceEmptyTagsWithSelfClosing(responsePayload)
 
+       h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - payload: %s", responsePayload)
+
        msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, req.TransactionId, req.GetMsgSrc())
        h.logger.Infof("#E2SetupRequestNotificationHandler.handleSuccessfulResponse - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
        _ = h.rmrSender.Send(msg)
 }
 
+func buildPlmnId(mmc string, mnc string) string{
+       var b strings.Builder
+
+       b.WriteByte(mmc[1])
+       b.WriteByte(mmc[0])
+       if len(mnc) == 2 {
+               b.WriteString("F")
+       } else {
+               b.WriteByte(mnc[2])
+       }
+       b.WriteByte(mmc[2])
+       b.WriteByte(mnc[1])
+       b.WriteByte(mnc[0])
+
+       return b.String()
+}
+
 func replaceEmptyTagsWithSelfClosing(responsePayload []byte) []byte {
        responseString := strings.NewReplacer(
                "<reject></reject>", "<reject/>",
@@ -222,8 +247,11 @@ func (h E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*m
        if len(e2tIpAddress) == 0 {
                return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
        }
+
+       h.logger.Infof("#E2SetupRequestNotificationHandler.parseSetupRequest - payload: %s", payload[pipInd+1:])
+
        setupRequest := &models.E2SetupRequestMessage{}
-       err := xml.Unmarshal(payload[pipInd+1:], &setupRequest.E2APPDU)
+       err := xml.Unmarshal(normalizeXml(payload[pipInd+1:]), &setupRequest.E2APPDU)
        if err != nil {
                return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
        }
@@ -231,7 +259,14 @@ func (h E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*m
        return setupRequest, e2tIpAddress, nil
 }
 
+func normalizeXml(payload []byte) []byte {
+       xmlStr := string(payload)
+       normalized := strings.NewReplacer("&lt;", "<", "&gt;", ">").Replace(xmlStr)
+       return []byte(normalized)
+}
+
 func (h E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
+
        var err error
        nodebInfo := &entities.NodebInfo{
                AssociatedE2TInstanceAddress: e2tAddress,
@@ -239,21 +274,23 @@ func (h E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAdd
                RanName:                      ranName,
                NodeType:                     entities.Node_GNB,
                Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
-               GlobalNbId: &entities.GlobalNbId{
-                       PlmnId: request.GetPlmnId(),
-                       NbId:   request.GetNbId(),
-               },
+               GlobalNbId:                   h.buildGlobalNbId(request),
        }
-       nodebInfo.GetGnb().RanFunctions, err = request.ExtractRanFunctionsList()
+
+       err = h.setGnbFunctions(nodebInfo, request)
        return nodebInfo, err
 }
 
+func (h E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {
+       return &entities.GlobalNbId{
+               PlmnId: setupRequest.GetPlmnId(),
+               NbId:   setupRequest.GetNbId(),
+       }
+}
+
 func (h E2SetupRequestNotificationHandler) buildNbIdentity(ranName string, setupRequest *models.E2SetupRequestMessage) *entities.NbIdentity {
        return &entities.NbIdentity{
                InventoryName: ranName,
-               GlobalNbId: &entities.GlobalNbId{
-                       PlmnId: setupRequest.GetPlmnId(),
-                       NbId:   setupRequest.GetNbId(),
-               },
+               GlobalNbId:    h.buildGlobalNbId(setupRequest),
        }
 }