[RIC-587] Update E2 Setup existing nodeb behavior
[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         ranConnectStatusChangeManager IRanConnectStatusChangeManager
36 }
37
38 func NewE2TAssociationManager(logger *logger.Logger, rnibDataService services.RNibDataService, e2tInstanceManager IE2TInstancesManager, rmClient clients.IRoutingManagerClient, ranConnectStatusChangeManager IRanConnectStatusChangeManager) *E2TAssociationManager {
39         return &E2TAssociationManager{
40                 logger:                        logger,
41                 rnibDataService:               rnibDataService,
42                 e2tInstanceManager:            e2tInstanceManager,
43                 rmClient:                      rmClient,
44                 ranConnectStatusChangeManager: ranConnectStatusChangeManager,
45         }
46 }
47
48 func (m *E2TAssociationManager) AssociateRan(e2tAddress string, nodebInfo *entities.NodebInfo) (bool, error) {
49         ranName := nodebInfo.RanName
50         m.logger.Infof("#E2TAssociationManager.AssociateRan - Associating RAN %s to E2T Instance address: %s", ranName, e2tAddress)
51
52         ranStatusChangePublished, err := m.associateRanAndUpdateNodeb(e2tAddress, nodebInfo)
53         if err != nil {
54                 m.logger.Errorf("#E2TAssociationManager.AssociateRan - RoutingManager failure: Failed to associate RAN %s to E2T %s. Error: %s", nodebInfo, e2tAddress, err)
55                 return ranStatusChangePublished, err
56         }
57         err = m.e2tInstanceManager.AddRansToInstance(e2tAddress, []string{ranName})
58         if err != nil {
59                 m.logger.Errorf("#E2TAssociationManager.AssociateRan - RAN name: %s - Failed to add RAN to E2T instance %s. Error: %s", ranName, e2tAddress, err)
60                 return ranStatusChangePublished, e2managererrors.NewRnibDbError()
61         }
62         m.logger.Infof("#E2TAssociationManager.AssociateRan - successfully associated RAN %s with E2T %s", ranName, e2tAddress)
63         return ranStatusChangePublished, nil
64 }
65
66 func (m *E2TAssociationManager) associateRanAndUpdateNodeb(e2tAddress string, nodebInfo *entities.NodebInfo) (bool, error) {
67
68         rmErr := m.rmClient.AssociateRanToE2TInstance(e2tAddress, nodebInfo.RanName)
69
70         if rmErr != nil {
71                 ranStatusChangePublished, _ := m.ranConnectStatusChangeManager.ChangeStatus(nodebInfo, entities.ConnectionStatus_DISCONNECTED)
72                 return ranStatusChangePublished, e2managererrors.NewRoutingManagerError()
73         }
74
75         ranStatusChangePublished, rnibErr := m.ranConnectStatusChangeManager.ChangeStatus(nodebInfo, entities.ConnectionStatus_CONNECTED)
76
77         if rnibErr != nil {
78                 return ranStatusChangePublished, e2managererrors.NewRnibDbError()
79         }
80
81         nodebInfo.AssociatedE2TInstanceAddress = e2tAddress
82         rnibErr = m.rnibDataService.UpdateNodebInfo(nodebInfo)
83
84         if rnibErr != nil {
85                 m.logger.Errorf("#E2TAssociationManager.associateRanAndUpdateNodeb - RAN name: %s - Failed updating nodeb. Error: %s", nodebInfo.RanName, rnibErr)
86                 return ranStatusChangePublished, e2managererrors.NewRnibDbError()
87         }
88
89         return ranStatusChangePublished, nil
90 }
91
92 func (m *E2TAssociationManager) DissociateRan(e2tAddress string, ranName string) error {
93         m.logger.Infof("#E2TAssociationManager.DissociateRan - Dissociating RAN %s from E2T Instance address: %s", ranName, e2tAddress)
94
95         nodebInfo, rnibErr := m.rnibDataService.GetNodeb(ranName)
96         if rnibErr != nil {
97                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RAN name: %s - Failed fetching RAN from rNib. Error: %s", ranName, rnibErr)
98                 return rnibErr
99         }
100
101         nodebInfo.AssociatedE2TInstanceAddress = ""
102         rnibErr = m.rnibDataService.UpdateNodebInfo(nodebInfo)
103         if rnibErr != nil {
104                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RAN name: %s - Failed to update RAN.AssociatedE2TInstanceAddress in rNib. Error: %s", ranName, rnibErr)
105                 return rnibErr
106         }
107
108         err := m.e2tInstanceManager.RemoveRanFromInstance(ranName, e2tAddress)
109         if err != nil {
110                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RAN name: %s - Failed to remove RAN from E2T instance %s. Error: %s", ranName, e2tAddress, err)
111                 return err
112         }
113
114         err = m.rmClient.DissociateRanE2TInstance(e2tAddress, ranName)
115
116         if err != nil {
117                 m.logger.Errorf("#E2TAssociationManager.DissociateRan - RoutingManager failure: Failed to dissociate RAN %s from E2T %s. Error: %s", ranName, e2tAddress, err)
118         } else {
119                 m.logger.Infof("#E2TAssociationManager.DissociateRan - successfully dissociated RAN %s from E2T %s", ranName, e2tAddress)
120         }
121         return nil
122 }
123
124 func (m *E2TAssociationManager) RemoveE2tInstance(e2tInstance *entities.E2TInstance) error {
125         m.logger.Infof("#E2TAssociationManager.RemoveE2tInstance -  Removing E2T %s and dessociating its associated RANs.", e2tInstance.Address)
126
127         err := m.rmClient.DeleteE2TInstance(e2tInstance.Address, e2tInstance.AssociatedRanList)
128         if err != nil {
129                 m.logger.Warnf("#E2TAssociationManager.RemoveE2tInstance - RoutingManager failure: Failed to delete E2T %s. Error: %s", e2tInstance.Address, err)
130                 // log and continue
131         }
132
133         err = m.e2tInstanceManager.RemoveE2TInstance(e2tInstance.Address)
134         if err != nil {
135                 m.logger.Errorf("#E2TAssociationManager.RemoveE2tInstance - Failed to remove E2T %s. Error: %s", e2tInstance.Address, err)
136                 return err
137         }
138
139         m.logger.Infof("#E2TAssociationManager.RemoveE2tInstance -  E2T %s successfully removed.", e2tInstance.Address)
140         return nil
141 }