[RICPLT-2787] Add new e2t controller and GetE2TInstances API
[ric-plt/e2mgr.git] / E2Manager / managers / e2t_instances_manager.go
index c9bca3c..74f6a51 100644 (file)
@@ -25,6 +25,7 @@ import (
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
        "math"
        "sync"
+       "time"
 )
 
 type E2TInstancesManager struct {
@@ -36,11 +37,14 @@ type E2TInstancesManager struct {
 type IE2TInstancesManager interface {
        GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error)
        GetE2TInstances() ([]*entities.E2TInstance, error)
+       GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error)
        AddE2TInstance(e2tAddress string) error
        RemoveE2TInstance(e2tInstance *entities.E2TInstance) error
        SelectE2TInstance() (string, error)
        AssociateRan(ranName string, e2tAddress string) error
        DissociateRan(ranName string, e2tAddress string) error
+       ActivateE2TInstance(e2tInstance *entities.E2TInstance) error
+       ResetKeepAliveTimestamp(e2tAddress string) error
 }
 
 func NewE2TInstancesManager(rnibDataService services.RNibDataService, logger *logger.Logger) *E2TInstancesManager {
@@ -54,22 +58,69 @@ func (m *E2TInstancesManager) GetE2TInstance(e2tAddress string) (*entities.E2TIn
        e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
 
        if err != nil {
-               m.logger.Errorf("#GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
+
+               _, ok := err.(*common.ResourceNotFoundError)
+
+               if !ok {
+                       m.logger.Errorf("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
+               } else {
+                       m.logger.Infof("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s not found on DB", e2tAddress)
+               }
        }
 
        return e2tInstance, err
 }
 
+func (m *E2TInstancesManager) GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error) {
+       e2tAddresses, err := m.rnibDataService.GetE2TAddressesNoLogs()
+
+       if err != nil {
+               _, ok := err.(*common.ResourceNotFoundError)
+
+               if !ok {
+                       m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T addresses. error: %s", err)
+                       return nil, e2managererrors.NewRnibDbError()
+               }
+
+               return []*entities.E2TInstance{}, nil
+       }
+
+       if len(e2tAddresses) == 0 {
+               return []*entities.E2TInstance{}, nil
+       }
+
+       e2tInstances, err := m.rnibDataService.GetE2TInstancesNoLogs(e2tAddresses)
+
+       if err != nil {
+               _, ok := err.(*common.ResourceNotFoundError)
+
+               if !ok {
+                       m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T instances list. error: %s", err)
+               }
+               return e2tInstances, err
+       }
+
+       return e2tInstances, nil
+}
+
 func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error) {
        e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
 
        if err != nil {
-               m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T addresses. error: %s", err)
-               return nil, err
+
+               _, ok := err.(*common.ResourceNotFoundError)
+
+               if !ok {
+                       m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T addresses. error: %s", err)
+                       return nil, e2managererrors.NewRnibDbError()
+               }
+
+               m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
+               return []*entities.E2TInstance{}, nil
        }
 
        if len(e2tAddresses) == 0 {
-               m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
+               m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
                return []*entities.E2TInstance{}, nil
        }
 
@@ -77,7 +128,7 @@ func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error)
 
        if err != nil {
                m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T instances list. error: %s", err)
-               return e2tInstances, err
+               return e2tInstances, e2managererrors.NewRnibDbError()
        }
 
        if len(e2tInstances) == 0 {
@@ -88,6 +139,38 @@ func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error)
        return e2tInstances, nil
 }
 
+func (m *E2TInstancesManager) ResetKeepAliveTimestampsForAllE2TInstances() {
+
+       e2tInstances, err := m.GetE2TInstances()
+
+       if err != nil {
+               m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Couldn't reset timestamps due to a DB error")
+               return
+       }
+
+       if len(e2tInstances) == 0 {
+               m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - No instances, ignoring reset")
+               return
+       }
+
+       for _, v := range e2tInstances {
+
+               if v.State != entities.Active {
+                       continue
+               }
+
+               v.KeepAliveTimestamp = time.Now().UnixNano()
+
+               err := m.rnibDataService.SaveE2TInstance(v)
+
+               if err != nil {
+                       m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - E2T address: %s - failed resetting e2t instance keep alive timestamp. error: %s", v.Address, err)
+               }
+       }
+
+       m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Done with reset")
+}
+
 func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2TInstance) *entities.E2TInstance {
        var minInstance *entities.E2TInstance
        minAssociatedRanCount := math.MaxInt32
@@ -104,17 +187,17 @@ func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2T
 
 func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
 
+       m.mux.Lock()
+       defer m.mux.Unlock()
+
        e2tInstance := entities.NewE2TInstance(e2tAddress)
        err := m.rnibDataService.SaveE2TInstance(e2tInstance)
 
        if err != nil {
-               m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed saving E2T instance. error: %s", e2tInstance.Address, err)
+               m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T instance. error: %s", e2tInstance.Address, err)
                return err
        }
 
-       m.mux.Lock()
-       defer m.mux.Unlock()
-
        e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
 
        if err != nil {
@@ -122,7 +205,7 @@ func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
                _, ok := err.(*common.ResourceNotFoundError)
 
                if !ok {
-                       m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err)
+                       m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err)
                        return err
                }
        }
@@ -132,11 +215,11 @@ func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
        err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
 
        if err != nil {
-               m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err)
+               m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err)
                return err
        }
 
-       m.logger.Infof("#AddE2TInstance - E2T Instance address: %s - successfully completed", e2tInstance.Address)
+       m.logger.Infof("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - successfully added E2T instance", e2tInstance.Address)
        return nil
 }
 
@@ -148,7 +231,7 @@ func (m *E2TInstancesManager) DissociateRan(ranName string, e2tAddress string) e
        e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
 
        if err != nil {
-               m.logger.Errorf("#DissociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
+               m.logger.Errorf("#E2TInstancesManager.DissociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
                return err
        }
 
@@ -166,10 +249,11 @@ func (m *E2TInstancesManager) DissociateRan(ranName string, e2tAddress string) e
        err = m.rnibDataService.SaveE2TInstance(e2tInstance)
 
        if err != nil {
-               m.logger.Errorf("#DissociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
+               m.logger.Errorf("#E2TInstancesManager.DissociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
                return err
        }
 
+       m.logger.Infof("#E2TInstancesManager.DissociateRan - successfully dissociated RAN %s from E2T %s", ranName, e2tInstance.Address)
        return nil
 }
 
@@ -181,8 +265,7 @@ func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
        e2tInstances, err := m.GetE2TInstances()
 
        if err != nil {
-               m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - failed retrieving E2T instances. error: %s", err)
-               return "", e2managererrors.NewRnibDbError()
+               return "", err
        }
 
        if len(e2tInstances) == 0 {
@@ -193,10 +276,11 @@ func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
        min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances)
 
        if min == nil {
-               m.logger.Errorf("#SelectE2TInstance - No active E2T instance found")
+               m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No active E2T instance found")
                return "", e2managererrors.NewE2TInstanceAbsenceError()
        }
 
+       m.logger.Infof("#E2TInstancesManager.SelectE2TInstance - successfully selected E2T instance. address: %s", min.Address)
        return min.Address, nil
 }
 
@@ -208,7 +292,7 @@ func (m *E2TInstancesManager) AssociateRan(ranName string, e2tAddress string) er
        e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
 
        if err != nil {
-               m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
+               m.logger.Errorf("#E2TInstancesManager.AssociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
                return e2managererrors.NewRnibDbError()
        }
 
@@ -217,9 +301,59 @@ func (m *E2TInstancesManager) AssociateRan(ranName string, e2tAddress string) er
        err = m.rnibDataService.SaveE2TInstance(e2tInstance)
 
        if err != nil {
-               m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
+               m.logger.Errorf("#E2TInstancesManager.AssociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
                return e2managererrors.NewRnibDbError()
        }
 
+       m.logger.Infof("#E2TInstancesManager.AssociateRan - successfully associated RAN %s with E2T %s", ranName, e2tInstance.Address)
+       return nil
+}
+
+func (h E2TInstancesManager) ActivateE2TInstance(e2tInstance *entities.E2TInstance) error{
+
+       if e2tInstance == nil {
+               h.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - e2tInstance empty")
+               return e2managererrors.NewInternalError()
+       }
+
+       h.logger.Infof("#E2TInstancesManager.ActivateE2TInstance - E2T Address: %s - activate E2T instance", e2tInstance.Address)
+
+       e2tInstance.State = entities.Active
+       e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
+
+       err := h.rnibDataService.SaveE2TInstance(e2tInstance)
+       if err != nil {
+               h.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tInstance.Address, err)
+               return err
+       }
+       return nil
+}
+
+func (m *E2TInstancesManager) ResetKeepAliveTimestamp(e2tAddress string) error {
+
+       m.mux.Lock()
+       defer m.mux.Unlock()
+
+       e2tInstance, err := m.rnibDataService.GetE2TInstanceNoLogs(e2tAddress)
+
+       if err != nil {
+               m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
+               return err
+       }
+
+       if e2tInstance.State == entities.ToBeDeleted || e2tInstance.State == entities.RoutingManagerFailure {
+               m.logger.Warnf("#E2TInstancesManager.ResetKeepAliveTimestamp - Ignore. This Instance is about to deleted")
+               return nil
+
+       }
+
+       e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
+       err = m.rnibDataService.SaveE2TInstanceNoLogs(e2tInstance)
+
+       if err != nil {
+               m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
+               return err
+       }
+
        return nil
 }