RIC-194 Setup from RAN: On Routing Manager Failure, return Setup Failure
[ric-plt/e2mgr.git] / E2Manager / managers / e2t_association_manager.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package managers
21
22 import (
23         "e2mgr/clients"
24         "e2mgr/e2managererrors"
25         "e2mgr/logger"
26         "e2mgr/services"
27         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28 )
29
30 type E2TAssociationManager struct {
31         logger             *logger.Logger
32         rnibDataService    services.RNibDataService
33         e2tInstanceManager IE2TInstancesManager
34         rmClient           clients.IRoutingManagerClient
35 }
36
37 func NewE2TAssociationManager(logger *logger.Logger, rnibDataService services.RNibDataService, e2tInstanceManager IE2TInstancesManager, rmClient clients.IRoutingManagerClient) *E2TAssociationManager {
38         return &E2TAssociationManager{
39                 logger:             logger,
40                 rnibDataService:    rnibDataService,
41                 e2tInstanceManager: e2tInstanceManager,
42                 rmClient:           rmClient,
43         }
44 }
45
46 func (m *E2TAssociationManager) AssociateRan(e2tAddress string, nodebInfo *entities.NodebInfo) error {
47         ranName := nodebInfo.RanName
48         m.logger.Infof("#E2TAssociationManager.AssociateRan - Associating RAN %s to E2T Instance address: %s", ranName, e2tAddress)
49
50         err := m.associateRanAndUpdateNodeb(e2tAddress, nodebInfo)
51         if err != nil {
52                 m.logger.Errorf("#E2TAssociationManager.AssociateRan - RoutingManager failure: Failed to associate RAN %s to E2T %s. Error: %s", nodebInfo, e2tAddress, err)
53                 return err
54         }
55         err = m.e2tInstanceManager.AddRansToInstance(e2tAddress, []string{ranName})
56         if err != nil {
57                 m.logger.Errorf("#E2TAssociationManager.AssociateRan - RAN name: %s - Failed to add RAN to E2T instance %s. Error: %s", ranName, e2tAddress, err)
58                 return e2managererrors.NewRnibDbError()
59         }
60         m.logger.Infof("#E2TAssociationManager.AssociateRan - successfully associated RAN %s with E2T %s", ranName, e2tAddress)
61         return nil
62 }
63
64 func (m *E2TAssociationManager) associateRanAndUpdateNodeb(e2tAddress string, nodebInfo *entities.NodebInfo) error {
65
66         rmErr := m.rmClient.AssociateRanToE2TInstance(e2tAddress, nodebInfo.RanName)
67         if rmErr != nil {
68                 nodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
69         } else {
70                 nodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTED
71                 nodebInfo.AssociatedE2TInstanceAddress = e2tAddress
72                 nodebInfo.ConnectionAttempts = 0
73         }
74         rNibErr := m.rnibDataService.UpdateNodebInfo(nodebInfo)
75         if rNibErr != nil {
76                 m.logger.Errorf("#E2TAssociationManager.associateRanAndUpdateNodeb - RAN name: %s - Failed to update nodeb entity in rNib. Error: %s", nodebInfo.RanName, rNibErr)
77         }
78         var err error
79         if rmErr != nil {
80                 err = e2managererrors.NewRoutingManagerError()
81         } else if rNibErr != nil{
82                 err = e2managererrors.NewRnibDbError()
83         }
84         return err
85 }
86
87 func (m *E2TAssociationManager) DissociateRan(e2tAddress string, ranName string) error {
88         m.logger.Infof("#E2TAssociationManager.DissociateRan - Dissociating RAN %s from E2T Instance address: %s", ranName, e2tAddress)
89
90         nodebInfo, rnibErr := m.rnibDataService.GetNodeb(ranName)
91         if rnibErr != nil {
92                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RAN name: %s - Failed fetching RAN from rNib. Error: %s", ranName, rnibErr)
93                 return rnibErr
94         }
95
96         nodebInfo.AssociatedE2TInstanceAddress = ""
97         rnibErr = m.rnibDataService.UpdateNodebInfo(nodebInfo)
98         if rnibErr != nil {
99                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RAN name: %s - Failed to update RAN.AssociatedE2TInstanceAddress in rNib. Error: %s", ranName, rnibErr)
100                 return rnibErr
101         }
102
103         err := m.e2tInstanceManager.RemoveRanFromInstance(ranName, e2tAddress)
104         if err != nil {
105                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RAN name: %s - Failed to remove RAN from E2T instance %s. Error: %s", ranName, e2tAddress, err)
106                 return err
107         }
108
109         err = m.rmClient.DissociateRanE2TInstance(e2tAddress, ranName)
110         if err != nil {
111                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RoutingManager failure: Failed to dissociate RAN %s from E2T %s. Error: %s", ranName, e2tAddress, err)
112         } else {
113                 m.logger.Infof("#E2TAssociationManager.DissociateRan - successfully dissociated RAN %s from E2T %s", ranName, e2tAddress)
114         }
115         return nil
116 }
117
118 func (m *E2TAssociationManager) RemoveE2tInstance(e2tInstance *entities.E2TInstance) error {
119         m.logger.Infof("#E2TAssociationManager.RemoveE2tInstance -  Removing E2T %s and dessociating its associated RANs.", e2tInstance.Address)
120
121         err := m.rmClient.DeleteE2TInstance(e2tInstance.Address, e2tInstance.AssociatedRanList)
122         if err != nil {
123                 m.logger.Warnf("#E2TAssociationManager.RemoveE2tInstance - RoutingManager failure: Failed to delete E2T %s. Error: %s", e2tInstance.Address, err)
124                 // log and continue
125         }
126
127         err = m.e2tInstanceManager.RemoveE2TInstance(e2tInstance.Address)
128         if err != nil {
129                 m.logger.Errorf("#E2TAssociationManager.RemoveE2tInstance - Failed to remove E2T %s. Error: %s", e2tInstance.Address, err)
130                 return err
131         }
132
133         m.logger.Infof("#E2TAssociationManager.RemoveE2tInstance -  E2T %s successfully removed.", e2tInstance.Address)
134         return nil
135 }