Merge R4 branch to master
[ric-plt/e2mgr.git] / E2Manager / managers / e2t_shutdown_manager.go
index ddf3406..2bb5d0f 100644 (file)
 package managers
 
 import (
+       "e2mgr/configuration"
        "e2mgr/logger"
        "e2mgr/services"
+       "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
+       "time"
 )
 
-
 type IE2TShutdownManager interface {
        Shutdown(e2tInstance *entities.E2TInstance) error
 }
 
 type E2TShutdownManager struct {
-       logger              *logger.Logger
-       rnibDataService     services.RNibDataService
-       e2TInstancesManager IE2TInstancesManager
+       logger                *logger.Logger
+       config                *configuration.Configuration
+       rnibDataService       services.RNibDataService
+       e2TInstancesManager   IE2TInstancesManager
+       e2tAssociationManager *E2TAssociationManager
 }
 
-func NewE2TShutdownManager(logger *logger.Logger, rnibDataService services.RNibDataService, e2TInstancesManager IE2TInstancesManager) E2TShutdownManager {
-       return E2TShutdownManager{
-               logger:              logger,
-               rnibDataService:     rnibDataService,
-               e2TInstancesManager: e2TInstancesManager,
+func NewE2TShutdownManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, e2TInstancesManager IE2TInstancesManager, e2tAssociationManager *E2TAssociationManager) *E2TShutdownManager {
+       return &E2TShutdownManager{
+               logger:                logger,
+               config:                config,
+               rnibDataService:       rnibDataService,
+               e2TInstancesManager:   e2TInstancesManager,
+               e2tAssociationManager: e2tAssociationManager,
        }
 }
 
-func (h E2TShutdownManager) Shutdown(e2tInstance *entities.E2TInstance) error{
-       h.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s is Dead, RIP", e2tInstance.Address)
+func (m E2TShutdownManager) Shutdown(e2tInstance *entities.E2TInstance) error {
+       m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s is Dead, RIP", e2tInstance.Address)
+
+       isE2tInstanceBeingDeleted := m.isE2tInstanceAlreadyBeingDeleted(e2tInstance)
+       if isE2tInstanceBeingDeleted {
+               m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s is already being deleted", e2tInstance.Address)
+               return nil
+       }
+
+       err := m.markE2tInstanceToBeDeleted(e2tInstance)
+       if err != nil {
+               m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to mark E2T %s as 'ToBeDeleted'.", e2tInstance.Address)
+               return err
+       }
 
+       err = m.clearNodebsAssociation(e2tInstance.AssociatedRanList)
+       if err != nil {
+               m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to clear nodebs association to E2T %s.", e2tInstance.Address)
+               return err
+       }
+
+       err = m.e2tAssociationManager.RemoveE2tInstance(e2tInstance)
+       if err != nil {
+               m.logger.Errorf("#E2TShutdownManager.Shutdown - Failed to remove E2T %s.", e2tInstance.Address)
+               return err
+       }
+
+
+       m.logger.Infof("#E2TShutdownManager.Shutdown - E2T %s was shutdown successfully.", e2tInstance.Address)
        return nil
 }
+
+func (m E2TShutdownManager) clearNodebsAssociation(ranNamesToBeDissociated []string) error {
+       for _, ranName := range ranNamesToBeDissociated {
+               nodeb, err := m.rnibDataService.GetNodeb(ranName)
+               if err != nil {
+                       m.logger.Warnf("#E2TShutdownManager.associateAndSetupNodebs - Failed to get nodeb %s from db.", ranName)
+                       _, ok := err.(*common.ResourceNotFoundError)
+                       if !ok {
+                               continue
+                       }
+                       return err
+               }
+               nodeb.AssociatedE2TInstanceAddress = ""
+               nodeb.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
+
+               err = m.rnibDataService.UpdateNodebInfo(nodeb)
+               if err != nil {
+                       m.logger.Errorf("#E2TShutdownManager.associateAndSetupNodebs - Failed to save nodeb %s from db.", ranName)
+                       return err
+               }
+       }
+       return nil
+}
+
+func (m E2TShutdownManager) markE2tInstanceToBeDeleted(e2tInstance *entities.E2TInstance) error {
+       e2tInstance.State = entities.ToBeDeleted
+       e2tInstance.DeletionTimestamp = time.Now().UnixNano()
+
+       return m.rnibDataService.SaveE2TInstance(e2tInstance)
+}
+
+func (m E2TShutdownManager) isE2tInstanceAlreadyBeingDeleted(e2tInstance *entities.E2TInstance) bool {
+       delta := time.Now().UnixNano() - e2tInstance.DeletionTimestamp
+       timestampNanosec := int64(time.Duration(m.config.E2TInstanceDeletionTimeoutMs) * time.Millisecond)
+
+       return e2tInstance.State == entities.ToBeDeleted && delta <= timestampNanosec
+}