[RIC-587] Update E2 Setup existing nodeb behavior
[ric-plt/e2mgr.git] / E2Manager / handlers / rmrmsghandlers / e2_setup_request_notification_handler.go
index d229e8c..259dcc3 100644 (file)
@@ -39,8 +39,21 @@ import (
 )
 
 var (
-       emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention",
+       emptyTagsToReplaceToSelfClosingTags = []string{"reject", "ignore", "transport-resource-unavailable", "om-intervention", "request-id-unknown",
                "v60s", "v20s", "v10s", "v5s", "v2s", "v1s"}
+       gnbTypesMap = map[string]entities.GnbType{
+               "gnb":    entities.GnbType_GNB,
+               "en_gnb": entities.GnbType_EN_GNB,
+       }
+       enbTypesMap = map[string]entities.EnbType{
+               "enB_macro":         entities.EnbType_MACRO_ENB,
+               "enB_home":          entities.EnbType_HOME_ENB,
+               "enB_shortmacro":    entities.EnbType_SHORT_MACRO_ENB,
+               "enB_longmacro":     entities.EnbType_LONG_MACRO_ENB,
+               "ng_enB_macro":      entities.EnbType_MACRO_NG_ENB,
+               "ng_enB_shortmacro": entities.EnbType_SHORT_MACRO_NG_ENB,
+               "ng_enB_longmacro":  entities.EnbType_LONG_MACRO_NG_ENB,
+       }
 )
 
 type E2SetupRequestNotificationHandler struct {
@@ -51,9 +64,10 @@ type E2SetupRequestNotificationHandler struct {
        rNibDataService               services.RNibDataService
        e2tAssociationManager         *managers.E2TAssociationManager
        ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager
+       ranListManager                managers.RanListManager
 }
 
-func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configuration.Configuration, e2tInstancesManager managers.IE2TInstancesManager, rmrSender *rmrsender.RmrSender, rNibDataService services.RNibDataService, e2tAssociationManager *managers.E2TAssociationManager, ranConnectStatusChangeManager managers.IRanConnectStatusChangeManager) *E2SetupRequestNotificationHandler {
+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 {
        return &E2SetupRequestNotificationHandler{
                logger:                        logger,
                config:                        config,
@@ -62,6 +76,7 @@ func NewE2SetupRequestNotificationHandler(logger *logger.Logger, config *configu
                rNibDataService:               rNibDataService,
                e2tAssociationManager:         e2tAssociationManager,
                ranConnectStatusChangeManager: ranConnectStatusChangeManager,
+               ranListManager:                ranListManager,
        }
 }
 
@@ -102,82 +117,117 @@ func (h *E2SetupRequestNotificationHandler) Handle(request *models.NotificationR
 
        nodebInfo, err := h.rNibDataService.GetNodeb(ranName)
 
+       var functionsModified bool
+
        if err != nil {
 
                if _, ok := err.(*common.ResourceNotFoundError); !ok {
                        h.logger.Errorf("#E2SetupRequestNotificationHandler.Handle - RAN name: %s - failed to retrieve nodebInfo entity. Error: %s", ranName, err)
                        return
-
                }
 
                if nodebInfo, err = h.handleNewRan(ranName, e2tIpAddress, setupRequest); err != nil {
+                       if _, ok := err.(*e2managererrors.UnknownSetupRequestRanNameError); ok {
+                               cause := models.Cause{RicRequest: &models.CauseRic{RequestIdUnknown: &struct{}{}}}
+                               h.handleUnsuccessfulResponse(ranName, request, cause)
+                       }
                        return
                }
 
        } else {
-               if err = h.handleExistingRan(ranName, nodebInfo, setupRequest); err != nil {
+
+               functionsModified, err = h.handleExistingRan(ranName, nodebInfo, setupRequest)
+
+               if err != nil {
                        return
                }
        }
 
-       err = h.e2tAssociationManager.AssociateRan(e2tIpAddress, nodebInfo)
+       ranStatusChangePublished, 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)
                if _, ok := err.(*e2managererrors.RoutingManagerError); ok {
+
+                       if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
+                               return
+                       }
+
                        cause := models.Cause{Transport: &models.CauseTransport{TransportResourceUnavailable: &struct{}{}}}
                        h.handleUnsuccessfulResponse(nodebInfo.RanName, request, cause)
                }
                return
        }
 
+       if err = h.handleUpdateAndPublishNodebInfo(functionsModified, ranStatusChangePublished, nodebInfo); err != nil {
+               return
+       }
+
        h.handleSuccessfulResponse(ranName, request, setupRequest)
 }
 
-func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
+func (h *E2SetupRequestNotificationHandler) handleUpdateAndPublishNodebInfo(functionsModified bool, ranStatusChangePublished bool, nodebInfo *entities.NodebInfo) error {
 
-       nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
+       if ranStatusChangePublished || !functionsModified {
+               return nil
+       }
+
+       err := h.rNibDataService.UpdateNodebInfoAndPublish(nodebInfo)
 
        if err != nil {
-               h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed to build nodebInfo entity. Error: %s", ranName, err)
-               return nil, err
+               h.logger.Errorf("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Failed at UpdateNodebInfoAndPublish. error: %s", nodebInfo.RanName, err)
+               return err
        }
 
-       nbIdentity := h.buildNbIdentity(ranName, setupRequest)
-       err = h.rNibDataService.SaveNodeb(nbIdentity, nodebInfo)
+       h.logger.Infof("#E2SetupRequestNotificationHandler.handleUpdateAndPublishNodebInfo - RAN name: %s - Successfully executed UpdateNodebInfoAndPublish", nodebInfo.RanName)
+       return nil
 
+}
+
+func (h *E2SetupRequestNotificationHandler) handleNewRan(ranName string, e2tIpAddress string, setupRequest *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
+
+       nodebInfo, err := h.buildNodebInfo(ranName, e2tIpAddress, setupRequest)
        if err != nil {
-               h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed to save nodebInfo entity. Error: %s", ranName, err)
+               h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed building nodebInfo. Error: %s", ranName, err)
                return nil, err
        }
 
-       err = h.ranConnectStatusChangeManager.ChangeStatus(nodebInfo, entities.ConnectionStatus_CONNECTED)
-
+       err = h.rNibDataService.SaveNodeb(nodebInfo)
        if err != nil {
+               h.logger.Errorf("#E2SetupRequestNotificationHandler.handleNewRan - RAN name: %s - failed saving nodebInfo. Error: %s", ranName, err)
                return nil, err
        }
 
-       return nodebInfo, nil
-}
+       nbIdentity := h.buildNbIdentity(ranName, setupRequest)
 
-func (h *E2SetupRequestNotificationHandler) setGnbFunctions(nodebInfo *entities.NodebInfo, setupRequest *models.E2SetupRequestMessage) {
-       ranFunctions := setupRequest.ExtractRanFunctionsList()
+       err = h.ranListManager.AddNbIdentity(nodebInfo.GetNodeType(), nbIdentity)
 
-       if ranFunctions != nil {
-               nodebInfo.GetGnb().RanFunctions = ranFunctions
+       if err != nil {
+               return nil, err
        }
+
+       return nodebInfo, 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) (bool, 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")
+               return false, errors.New("nodeB entity in incorrect state")
        }
 
-       h.setGnbFunctions(nodebInfo, setupRequest)
+       if nodebInfo.NodeType == entities.Node_ENB {
+               return false, nil
+       }
+
+       setupMessageRanFuncs := setupRequest.ExtractRanFunctionsList()
 
-       return h.rNibDataService.UpdateNodebInfo(nodebInfo)
+       if setupMessageRanFuncs == nil || (len(setupMessageRanFuncs) == 0 && len(nodebInfo.GetGnb().RanFunctions) == 0) {
+               return false, nil
+       }
+
+       nodebInfo.GetGnb().RanFunctions = setupMessageRanFuncs
+       return true, nil
 }
 
 func (h *E2SetupRequestNotificationHandler) handleUnsuccessfulResponse(ranName string, req *models.NotificationRequest, cause models.Cause) {
@@ -293,18 +343,47 @@ func normalizeXml(payload []byte) []byte {
 }
 
 func (h *E2SetupRequestNotificationHandler) buildNodebInfo(ranName string, e2tAddress string, request *models.E2SetupRequestMessage) (*entities.NodebInfo, error) {
-
-       var err error
        nodebInfo := &entities.NodebInfo{
                AssociatedE2TInstanceAddress: e2tAddress,
                RanName:                      ranName,
-               NodeType:                     entities.Node_GNB,
-               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
                GlobalNbId:                   h.buildGlobalNbId(request),
+               SetupFromNetwork:             true,
+       }
+       err := h.setNodeTypeAndConfiguration(nodebInfo)
+       if err != nil {
+               return nil, err
+       }
+
+       if nodebInfo.NodeType == entities.Node_ENB {
+               return nodebInfo, nil
+       }
+
+       ranFuncs := request.ExtractRanFunctionsList()
+
+       if ranFuncs != nil {
+               nodebInfo.GetGnb().RanFunctions = ranFuncs
+       }
+
+       return nodebInfo, nil
+}
+
+func (h *E2SetupRequestNotificationHandler) setNodeTypeAndConfiguration(nodebInfo *entities.NodebInfo) error {
+       for k, v := range gnbTypesMap {
+               if strings.HasPrefix(nodebInfo.RanName, k) {
+                       nodebInfo.NodeType = entities.Node_GNB
+                       nodebInfo.Configuration = &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{GnbType: v}}
+                       return nil
+               }
+       }
+       for k, v := range enbTypesMap {
+               if strings.HasPrefix(nodebInfo.RanName, k) {
+                       nodebInfo.NodeType = entities.Node_ENB
+                       nodebInfo.Configuration = &entities.NodebInfo_Enb{Enb: &entities.Enb{EnbType: v}}
+                       return nil
+               }
        }
 
-       h.setGnbFunctions(nodebInfo, request)
-       return nodebInfo, err
+       return e2managererrors.NewUnknownSetupRequestRanNameError(nodebInfo.RanName)
 }
 
 func (h *E2SetupRequestNotificationHandler) buildGlobalNbId(setupRequest *models.E2SetupRequestMessage) *entities.GlobalNbId {