2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "e2mgr/e2managererrors"
24 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
25 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30 type E2TInstancesManager struct {
31 rnibDataService services.RNibDataService
36 type IE2TInstancesManager interface {
37 GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error)
38 GetE2TInstances() ([]*entities.E2TInstance, error)
39 AddE2TInstance(e2tAddress string) error
40 RemoveE2TInstance(e2tInstance *entities.E2TInstance) error
41 SelectE2TInstance() (string, error)
42 AssociateRan(ranName string, e2tAddress string) error
43 DissociateRan(ranName string, e2tAddress string) error
46 func NewE2TInstancesManager(rnibDataService services.RNibDataService, logger *logger.Logger) *E2TInstancesManager {
47 return &E2TInstancesManager{
48 rnibDataService: rnibDataService,
53 func (m *E2TInstancesManager) GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error) {
54 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
57 m.logger.Errorf("#GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
60 return e2tInstance, err
63 func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error) {
64 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
67 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T addresses. error: %s", err)
71 if len(e2tAddresses) == 0 {
72 m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
73 return []*entities.E2TInstance{}, nil
76 e2tInstances, err := m.rnibDataService.GetE2TInstances(e2tAddresses)
79 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T instances list. error: %s", err)
80 return e2tInstances, err
83 if len(e2tInstances) == 0 {
84 m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T instances list")
85 return e2tInstances, nil
88 return e2tInstances, nil
91 func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2TInstance) *entities.E2TInstance {
92 var minInstance *entities.E2TInstance
93 minAssociatedRanCount := math.MaxInt32
95 for _, v := range e2tInstances {
96 if v.State == entities.Active && len(v.AssociatedRanList) < minAssociatedRanCount {
97 minAssociatedRanCount = len(v.AssociatedRanList)
105 func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
107 e2tInstance := entities.NewE2TInstance(e2tAddress)
108 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
111 m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed saving E2T instance. error: %s", e2tInstance.Address, err)
118 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
122 _, ok := err.(*common.ResourceNotFoundError)
125 m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err)
130 e2tAddresses = append(e2tAddresses, e2tInstance.Address)
132 err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
135 m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err)
139 m.logger.Infof("#AddE2TInstance - E2T Instance address: %s - successfully completed", e2tInstance.Address)
143 func (m *E2TInstancesManager) DissociateRan(ranName string, e2tAddress string) error {
148 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
151 m.logger.Errorf("#DissociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
155 i := 0 // output index
156 for _, v := range e2tInstance.AssociatedRanList {
158 // copy and increment index
159 e2tInstance.AssociatedRanList[i] = v
164 e2tInstance.AssociatedRanList = e2tInstance.AssociatedRanList[:i]
166 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
169 m.logger.Errorf("#DissociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
176 func (m *E2TInstancesManager) RemoveE2TInstance(e2tInstance *entities.E2TInstance) error {
179 func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
181 e2tInstances, err := m.GetE2TInstances()
184 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - failed retrieving E2T instances. error: %s", err)
185 return "", e2managererrors.NewRnibDbError()
188 if len(e2tInstances) == 0 {
189 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No E2T instance found")
190 return "", e2managererrors.NewE2TInstanceAbsenceError()
193 min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances)
196 m.logger.Errorf("#SelectE2TInstance - No active E2T instance found")
197 return "", e2managererrors.NewE2TInstanceAbsenceError()
200 return min.Address, nil
203 func (m *E2TInstancesManager) AssociateRan(ranName string, e2tAddress string) error {
208 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
211 m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
212 return e2managererrors.NewRnibDbError()
215 e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, ranName)
217 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
220 m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
221 return e2managererrors.NewRnibDbError()