[RIC-396] Reject E2 Setup when ricEnabled = false
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_setup_request_notification_handler.go
index d65516c..7420a29 100644 (file)
@@ -38,6 +38,11 @@ import (
        "strings"
 )
 
+var (
+       emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention",
+               "v60s", "v20s", "v10s", "v5s", "v2s", "v1s"}
+)
+
 type E2SetupRequestNotificationHandler struct {
        logger                *logger.Logger
        config                *configuration.Configuration
@@ -47,8 +52,8 @@ type E2SetupRequestNotificationHandler struct {
        e2tAssociationManager *managers.E2TAssociationManager
 }
 
-func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configuration.Configuration, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager) E2SetupRequestNotificationHandler {
-       return E2SetupRequestNotificationHandler{
+func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configuration.Configuration, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager) *E2SetupRequestNotificationHandler {
+       return &E2SetupRequestNotificationHandler{
                logger:                logger,
                config:                config,
                e2tInstancesManager:   e2tInstancesManager,
@@ -58,10 +63,23 @@ func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configu
        }
 }
 
-func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRequest) {
+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)
 
+       generalConfiguration, err := h.rNibDataService.GetGeneralConfiguration()
+
+       if err != nil {
+               h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - Failed retrieving e2m general configuration. error: %s", err)
+               return
+       }
+
+       if !generalConfiguration.EnableRic {
+               cause := models.Cause{Misc: &models.CauseMisc{OmIntervention: &struct{}{}}}
+               h.handleUnsuccessfulResponse(ranName, request, cause)
+               return
+       }
+
        setupRequest, e2tIpAddress, err := h.parseSetupRequest(request.Payload)
        if err != nil {
                h.logger.Errorf(err.Error())
@@ -104,7 +122,8 @@ func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRe
 
                h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to associate E2T to nodeB entity. Error: %s", ranName, err)
                if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
-                       h.handleUnsuccessfulResponse(nodebInfo, request)
+                       cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
+                       h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause)
                }
                return
        }
@@ -112,7 +131,7 @@ func (h E2SetupRequestNotificationHandler) Handle(request *models.NotificationRe
        h.handleSuccessfulResponse(ranName, request, setupRequest)
 }
 
-func (h E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
+func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
 
        nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
 
@@ -132,7 +151,7 @@ func (h E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAdd
        return nodebInfo, nil
 }
 
-func (h E2SetupRequestNotificationHandler) setGnbFunctions(nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
+func (h *E2SetupRequestNotificationHandler) setGnbFunctions(nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
        ranFunctions := setupRequest.ExtractRanFunctionsList()
 
        if ranFunctions != nil {
@@ -142,7 +161,7 @@ func (h E2SetupRequestNotificationHandler) setGnbFunctions(nodebInfo *entities.N
        return nil
 }
 
-func (h E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) error {
+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")
@@ -152,26 +171,25 @@ func (h E2SetupRequestNotificationHandler) handleExistingRan(ranName string, nod
        return err
 }
 
-func (h E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(nodebInfo *entities.NodebInfo, req *models.NotificationRequest) {
-       failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s)
+func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause) {
+       failureResponse := models.NewE2SetupFailureResponseMessage(models.TimeToWaitEnum.V60s, cause)
        h.logger.Debugf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - E2_SETUP_RESPONSE has been built successfully %+v", failureResponse)
 
        responsePayload, err := xml.Marshal(&failureResponse.E2APPDU)
        if err != nil {
-               h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", nodebInfo.RanName, responsePayload)
+               h.logger.Warnf("#E2SetupRequestNotificationHandler.handleUnsuccessfulResponse - RAN name: %s - Error marshalling RIC_E2_SETUP_RESP. Payload: %s", ranName, responsePayload)
        }
 
        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)
+       msg := models.NewRmrMessage(rmrCgo.RIC_E2_SETUP_FAILURE, 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", ranName, msg)
        _ = h.rmrSender.WhSend(msg)
 
 }
 
-func (h E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
+func (h *E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName string, req *models.NotificationRequest, setupRequest *models.E2SetupRequestMessage) {
 
        plmnId := buildPlmnId(h.config.GlobalRicId.Mcc, h.config.GlobalRicId.Mnc)
 
@@ -196,7 +214,7 @@ func (h E2SetupRequestNotificationHandler) handleSuccessfulResponse(ranName stri
        _ = h.rmrSender.Send(msg)
 }
 
-func buildPlmnId(mmc string, mnc string) string{
+func buildPlmnId(mmc string, mnc string) string {
        var b strings.Builder
 
        b.WriteByte(mmc[1])
@@ -214,17 +232,17 @@ func buildPlmnId(mmc string, mnc string) string{
 }
 
 func replaceEmptyTagsWithSelfClosing(responsePayload []byte) []byte {
-       responseString := strings.NewReplacer(
-               "<reject></reject>", "<reject/>",
-               "<ignore></ignore>", "<ignore/>",
-               "<transport-resource-unavailable></transport-resource-unavailable>", "<transport-resource-unavailable/>",
-               "<v60s></v60s>", "<v60s/>",
-               "<v20s></v20s>", "<v20s/>",
-               "<v10s></v10s>", "<v10s/>",
-               "<v5s></v5s>", "<v5s/>",
-               "<v2s></v2s>", "<v2s/>",
-               "<v1s></v1s>", "<v1s/>",
-       ).Replace(string(responsePayload))
+
+       emptyTagVsSelfClosingTagPairs := make([]string, len(emptyTagsToReplaceToSelfClosingTags)*2)
+
+       j := 0
+
+       for i := 0; i < len(emptyTagsToReplaceToSelfClosingTags); i++ {
+               emptyTagVsSelfClosingTagPairs[j] = fmt.Sprintf("<%[1]s></%[1]s>", emptyTagsToReplaceToSelfClosingTags[i])
+               emptyTagVsSelfClosingTagPairs[j+1] = fmt.Sprintf("<%s/>", emptyTagsToReplaceToSelfClosingTags[i])
+               j += 2
+       }
+       responseString := strings.NewReplacer(emptyTagVsSelfClosingTagPairs...).Replace(string(responsePayload))
        return []byte(responseString)
 }
 
@@ -236,7 +254,7 @@ func convertTo20BitString(ricNearRtId string) (string, error) {
        return fmt.Sprintf("%020b", r)[:20], nil
 }
 
-func (h E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
+func (h *E2SetupRequestNotificationHandler) parseSetupRequest(payload []byte) (*models.E2SetupRequestMessage, string, error) {
 
        pipInd := bytes.IndexByte(payload, '|')
        if pipInd < 0 {