Merge "Split build steps out of Dockerfile to script"
[ric-plt/e2mgr.git] / E2Manager / managers / ran_reconnection_manager.go
index 70acee0..b733142 100644 (file)
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
-//
+
+//  This source code is part of the near-RT RIC (RAN Intelligent Controller)
+//  platform project (RICP).
+
 
 package managers
 
 import (
        "e2mgr/configuration"
        "e2mgr/logger"
-       "e2mgr/rNibWriter"
        "e2mgr/services"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
-       "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
 )
 
 type IRanReconnectionManager interface {
@@ -31,25 +32,29 @@ type IRanReconnectionManager interface {
 }
 
 type RanReconnectionManager struct {
-       logger             *logger.Logger
-       config             *configuration.Configuration
-       rnibReaderProvider func() reader.RNibReader
-       rnibWriterProvider func() rNibWriter.RNibWriter
-       ranSetupManager    *RanSetupManager
+       logger                *logger.Logger
+       config                *configuration.Configuration
+       rnibDataService       services.RNibDataService
+       ranSetupManager       *RanSetupManager
+       e2tAssociationManager *E2TAssociationManager
 }
 
-func NewRanReconnectionManager(logger *logger.Logger, config *configuration.Configuration, rnibReaderProvider func() reader.RNibReader, rnibWriterProvider func() rNibWriter.RNibWriter, rmrService *services.RmrService) *RanReconnectionManager {
+func NewRanReconnectionManager(logger *logger.Logger, config *configuration.Configuration, rnibDataService services.RNibDataService, ranSetupManager *RanSetupManager, e2tAssociationManager *E2TAssociationManager) *RanReconnectionManager {
        return &RanReconnectionManager{
-               logger:             logger,
-               config:             config,
-               rnibReaderProvider: rnibReaderProvider,
-               rnibWriterProvider: rnibWriterProvider,
-               ranSetupManager:    NewRanSetupManager(logger,rmrService,rnibWriterProvider),
+               logger:                logger,
+               config:                config,
+               rnibDataService:       rnibDataService,
+               ranSetupManager:       ranSetupManager,
+               e2tAssociationManager: e2tAssociationManager,
        }
 }
 
+func (m *RanReconnectionManager) isRanExceededConnectionAttempts(nodebInfo *entities.NodebInfo) bool {
+       return int(nodebInfo.GetConnectionAttempts()) >= m.config.MaxConnectionAttempts
+}
+
 func (m *RanReconnectionManager) ReconnectRan(inventoryName string) error {
-       nodebInfo, rnibErr := m.rnibReaderProvider().GetNodeb(inventoryName)
+       nodebInfo, rnibErr := m.rnibDataService.GetNodeb(inventoryName)
 
        if rnibErr != nil {
                m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed fetching RAN from rNib. Error: %v", inventoryName, rnibErr)
@@ -59,11 +64,21 @@ func (m *RanReconnectionManager) ReconnectRan(inventoryName string) error {
        m.logger.Infof("#RanReconnectionManager.ReconnectRan - RAN name: %s - RAN's connection status: %s, RAN's connection attempts: %d", nodebInfo.RanName, nodebInfo.ConnectionStatus, nodebInfo.ConnectionAttempts)
 
        if !m.canReconnectRan(nodebInfo) {
-               return m.setConnectionStatusOfUnconnectableRan(nodebInfo)
-       }
+               e2tAddress := nodebInfo.AssociatedE2TInstanceAddress
+               err := m.updateUnconnectableRan(nodebInfo)
 
+               if err != nil {
+                       return err
+               }
 
-       err := m.ranSetupManager.ExecuteSetup(nodebInfo)
+               if m.isRanExceededConnectionAttempts(nodebInfo) {
+                       return m.e2tAssociationManager.DissociateRan(e2tAddress, nodebInfo.RanName)
+               }
+
+               return nil
+       }
+
+       err := m.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
 
        if err != nil {
                m.logger.Errorf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Failed executing setup. Error: %v", inventoryName, err)
@@ -79,39 +94,37 @@ func (m *RanReconnectionManager) canReconnectRan(nodebInfo *entities.NodebInfo)
                int(nodebInfo.GetConnectionAttempts()) < m.config.MaxConnectionAttempts
 }
 
-func (m *RanReconnectionManager) updateNodebInfoStatus(nodebInfo *entities.NodebInfo, connectionStatus entities.ConnectionStatus) error {
-       if nodebInfo.ConnectionStatus == connectionStatus {
-               return nil
-       }
+func (m *RanReconnectionManager) updateNodebInfo(nodebInfo *entities.NodebInfo, connectionStatus entities.ConnectionStatus) error {
 
        nodebInfo.ConnectionStatus = connectionStatus;
-       err := m.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
+
+       err := m.rnibDataService.UpdateNodebInfo(nodebInfo)
 
        if err != nil {
-               m.logger.Errorf("#RanReconnectionManager.updateNodebInfoStatus - RAN name: %s - Failed updating RAN's connection status to %s in rNib. Error: %v", nodebInfo.RanName, connectionStatus, err)
+               m.logger.Errorf("#RanReconnectionManager.updateNodebInfo - RAN name: %s - Failed updating RAN's connection status to %s in rNib. Error: %v", nodebInfo.RanName, connectionStatus, err)
                return err
        }
 
-       m.logger.Infof("#RanReconnectionManager.updateNodebInfoStatus - RAN name: %s - Successfully updated rNib. RAN's current connection status: %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)
+       m.logger.Infof("#RanReconnectionManager.updateNodebInfo - RAN name: %s - Successfully updated rNib. RAN's current connection status: %s", nodebInfo.RanName, nodebInfo.ConnectionStatus)
        return nil
 }
 
-func (m *RanReconnectionManager) setConnectionStatusOfUnconnectableRan(nodebInfo *entities.NodebInfo) error {
+func (m *RanReconnectionManager) updateUnconnectableRan(nodebInfo *entities.NodebInfo) error {
        connectionStatus := nodebInfo.GetConnectionStatus()
 
        if connectionStatus == entities.ConnectionStatus_SHUT_DOWN {
-               m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUT_DOWN", nodebInfo.RanName)
+               m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUT_DOWN", nodebInfo.RanName)
                return nil
        }
 
        if connectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
-               m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUTTING_DOWN", nodebInfo.RanName)
-               return m.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_SHUT_DOWN)
+               m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: connection status is SHUTTING_DOWN", nodebInfo.RanName)
+               return m.updateNodebInfo(nodebInfo, entities.ConnectionStatus_SHUT_DOWN)
        }
 
-       if int(nodebInfo.GetConnectionAttempts()) >= m.config.MaxConnectionAttempts {
-               m.logger.Warnf("#RanReconnectionManager.ReconnectRan - RAN name: %s - Cannot reconnect RAN. Reason: RAN's connection attempts exceeded the limit (%d)", nodebInfo.RanName, m.config.MaxConnectionAttempts)
-               return m.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_DISCONNECTED)
+       if m.isRanExceededConnectionAttempts(nodebInfo) {
+               m.logger.Warnf("#RanReconnectionManager.updateUnconnectableRan - RAN name: %s - Cannot reconnect RAN. Reason: RAN's connection attempts exceeded the limit (%d)", nodebInfo.RanName, m.config.MaxConnectionAttempts)
+               return m.updateNodebInfo(nodebInfo, entities.ConnectionStatus_DISCONNECTED)
        }
 
        return nil