RIC-193 - Setup from RAN: set connection status to CONNECTED in case RM failure
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_setup_request_notification_handler.go
index 907e448..8271e64 100644 (file)
@@ -34,6 +34,8 @@ import (
        "fmt"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
+       "strconv"
+       "strings"
 )
 
 type E2SetupRequestNotificationHandler struct {
@@ -58,7 +60,7 @@ func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configu
 
 func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest){
        ranName := request.RanName
-       h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2 Setup Request. Payload: %x", ranName, request.Payload)
+       h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - received E2_SETUP_REQUEST. Payload: %x", ranName, request.Payload)
 
        setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
        if err != nil {
@@ -67,6 +69,7 @@ func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRe
        }
 
        h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - E2T Address: %s - handling E2_SETUP_REQUEST", e2tIpAddress)
+       h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_REQUEST has been parsed successfully %+v", setupRequest)
 
        _, err = h.e2tInstancesManager.GetE2TInstance(e2tIpAddress)
 
@@ -106,44 +109,64 @@ func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRe
                        return
                }
        }
+       nodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTED
        err = h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
        if err != nil{
                h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
+               err = h.rNibDataService.UpdateNodebInfo(nodebInfo)
+               if err != nil {
+                       h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - Failed to update nodebInfo entity. Error: %s", ranName, err)
+               }
+               return
+       }
+
+       ricNearRtId, err := convertTo20BitString(h.config.GlobalRicId.RicNearRtId)
+       if err != nil{
+               h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - 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()
-       successResponse.SetPlmnId(h.config.GlobalRicId.PlmnId)
-       successResponse.SetRicId(h.config.GlobalRicId.RicNearRtId)
-       successResponse.SetExtractRanFunctionsIDList(setupRequest)
-       responsePayload, err := xml.Marshal(successResponse)
+       successResponse := models.NewE2SetupSuccessResponseMessage(h.config.GlobalRicId.PlmnId, ricNearRtId,setupRequest)
+       h.logger.Debugf("#E2SetupRequestNotificationHandler.Handle - E2_SETUP_RESPONSE has been built successfully %+v", successResponse)
 
+       responsePayload, err := xml.Marshal(&successResponse.E2APPDU)
        if err != nil{
-               h.logger.Warnf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - Error marshalling E2 Setup Response. Response: %x", ranName, responsePayload)
+               h.logger.Warnf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
        }
+
+       responsePayload = replaceCriticalityTagsWithSelfClosing(responsePayload)
+
        msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_RESP, ranName, responsePayload, request.TransactionId)
-       h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - E2 Setup Request has been built. Message: %x", ranName, msg)
-       err = h.rmrSender.Send(msg)
+       h.logger.Infof("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - RIC_E2_SETUP_RESP message has been built successfully. Message: %x", ranName, msg)
+       _ = h.rmrSender.Send(msg)
 }
 
-func (h E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte)(*models.E2SetupRequestMessage, string, error){
+func replaceCriticalityTagsWithSelfClosing(responsePayload []byte) []byte {
+       responseString := strings.Replace(string(responsePayload), "<reject></reject>", "<reject/>", -1)
+       responseString = strings.Replace(responseString, "<ignore></ignore>", "<ignore/>", -1)
+       return []byte(responseString)
+}
 
-       colonInd := bytes.IndexByte(payload, ':')
-       if colonInd < 0 {
-               return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request, failed extract E2T IP Address: no ':' separator found")
+func convertTo20BitString(ricNearRtId string) (string, error){
+       r, err := strconv.ParseUint(ricNearRtId, 16, 32)
+       if err != nil{
+               return "", err
        }
+       return fmt.Sprintf("%020b", r)[:20], nil
+}
 
-       e2tIpAddress := string(payload[:colonInd])
-       if len(e2tIpAddress) == 0 {
-               return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
-       }
+func (h E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte)(*models.E2SetupRequestMessage, string, error){
 
        pipInd := bytes.IndexByte(payload, '|')
        if pipInd < 0 {
                return nil, "", errors.New( "#E2SetupRequestNotificationHandler.parseSetupRequest - Error parsing E2 Setup Request failed extract Payload: no | separator found")
        }
 
+       e2tIpAddress := string(payload[:pipInd])
+       if len(e2tIpAddress) == 0 {
+               return nil, "", errors.New("#E2SetupRequestNotificationHandler.parseSetupRequest - Empty E2T Address received")
+       }
        setupRequest := &models.E2SetupRequestMessage{}
-       err := xml.Unmarshal(payload[pipInd + 1:], &setupRequest)
+       err := xml.Unmarshal(payload[pipInd + 1:], &setupRequest.E2APPDU)
        if err != nil {
                return nil, "", errors.New(fmt.Sprintf("#E2SetupRequestNotificationHandler.parseSetupRequest - Error unmarshalling E2 Setup Request payload: %x", payload))
        }
@@ -159,6 +182,10 @@ 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(),
+               },
        }
        nodebInfo.GetGnb().RanFunctions, err = request.GetExtractRanFunctionsList()
        return nodebInfo, err