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.
18 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 // platform project (RICP).
24 "e2mgr/e2managererrors"
27 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
34 type E2TInstancesManager struct {
35 rnibDataService services.RNibDataService
40 type IE2TInstancesManager interface {
41 GetE2TAddresses() ([]string, error)
42 GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error)
43 GetE2TInstances() ([]*entities.E2TInstance, error)
44 GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error)
45 AddE2TInstance(e2tAddress string) error
46 RemoveE2TInstance(e2tAddress string) error
47 SelectE2TInstance() (string, error)
48 AddRansToInstance(e2tAddress string, ranNames []string) error
49 RemoveRanFromInstance(ranName string, e2tAddress string) error
50 ResetKeepAliveTimestamp(e2tAddress string) error
51 ClearRansOfAllE2TInstances() error
52 SetE2tInstanceState(e2tAddress string, currentState entities.E2TInstanceState, newState entities.E2TInstanceState) error
55 func NewE2TInstancesManager(rnibDataService services.RNibDataService, logger *logger.Logger) *E2TInstancesManager {
56 return &E2TInstancesManager{
57 rnibDataService: rnibDataService,
62 func (m *E2TInstancesManager) GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error) {
63 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
67 _, ok := err.(*common.ResourceNotFoundError)
70 m.logger.Errorf("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
72 m.logger.Infof("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s not found on DB", e2tAddress)
76 return e2tInstance, err
79 func (m *E2TInstancesManager) GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error) {
80 e2tAddresses, err := m.rnibDataService.GetE2TAddressesNoLogs()
83 _, ok := err.(*common.ResourceNotFoundError)
86 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T addresses. error: %s", err)
87 return nil, e2managererrors.NewRnibDbError()
90 return []*entities.E2TInstance{}, nil
93 if len(e2tAddresses) == 0 {
94 return []*entities.E2TInstance{}, nil
97 e2tInstances, err := m.rnibDataService.GetE2TInstancesNoLogs(e2tAddresses)
100 _, ok := err.(*common.ResourceNotFoundError)
103 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T instances list. error: %s", err)
105 return e2tInstances, err
108 return e2tInstances, nil
111 func (m *E2TInstancesManager) GetE2TAddresses() ([]string, error) {
112 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
116 _, ok := err.(*common.ResourceNotFoundError)
119 m.logger.Errorf("#E2TInstancesManager.GetE2TAddresses - Failed retrieving E2T addresses. error: %s", err)
120 return nil, e2managererrors.NewRnibDbError()
125 return e2tAddresses, nil
128 func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error) {
129 e2tAddresses, err := m.GetE2TAddresses()
132 return nil, e2managererrors.NewRnibDbError()
135 if len(e2tAddresses) == 0 {
136 m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
137 return []*entities.E2TInstance{}, nil
140 e2tInstances, err := m.rnibDataService.GetE2TInstances(e2tAddresses)
143 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T instances list. error: %s", err)
144 return e2tInstances, e2managererrors.NewRnibDbError()
147 if len(e2tInstances) == 0 {
148 m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T instances list")
149 return e2tInstances, nil
152 return e2tInstances, nil
155 func (m *E2TInstancesManager) ResetKeepAliveTimestampsForAllE2TInstances() {
157 e2tInstances, err := m.GetE2TInstances()
160 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Couldn't reset timestamps due to a DB error")
164 if len(e2tInstances) == 0 {
165 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - No instances, ignoring reset")
169 for _, v := range e2tInstances {
171 if v.State != entities.Active {
175 v.KeepAliveTimestamp = time.Now().UnixNano()
177 err := m.rnibDataService.SaveE2TInstance(v)
180 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - E2T address: %s - failed resetting e2t instance keep alive timestamp. error: %s", v.Address, err)
184 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Done with reset")
187 func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2TInstance) *entities.E2TInstance {
188 var minInstance *entities.E2TInstance
189 minAssociatedRanCount := math.MaxInt32
191 for _, v := range e2tInstances {
192 if v.State == entities.Active && len(v.AssociatedRanList) < minAssociatedRanCount {
193 minAssociatedRanCount = len(v.AssociatedRanList)
201 func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
206 e2tInstance := entities.NewE2TInstance(e2tAddress)
207 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
210 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T instance. error: %s", e2tInstance.Address, err)
214 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
218 _, ok := err.(*common.ResourceNotFoundError)
221 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err)
226 e2tAddresses = append(e2tAddresses, e2tInstance.Address)
228 err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
231 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err)
235 m.logger.Infof("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - successfully added E2T instance", e2tInstance.Address)
239 func (m *E2TInstancesManager) RemoveRanFromInstance(ranName string, e2tAddress string) error {
244 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
247 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
248 return e2managererrors.NewRnibDbError()
251 i := 0 // output index
252 for _, v := range e2tInstance.AssociatedRanList {
254 // copy and increment index
255 e2tInstance.AssociatedRanList[i] = v
260 e2tInstance.AssociatedRanList = e2tInstance.AssociatedRanList[:i]
262 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
265 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
266 return e2managererrors.NewRnibDbError()
269 m.logger.Infof("#E2TInstancesManager.RemoveRanFromInstance - successfully dissociated RAN %s from E2T %s", ranName, e2tInstance.Address)
273 func (m *E2TInstancesManager) RemoveE2TInstance(e2tAddress string) error {
277 err := m.rnibDataService.RemoveE2TInstance(e2tAddress)
279 m.logger.Errorf("#E2TInstancesManager.RemoveE2TInstance - E2T Instance address: %s - Failed removing E2TInstance. error: %s", e2tAddress, err)
280 return e2managererrors.NewRnibDbError()
283 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
286 m.logger.Errorf("#E2TInstancesManager.RemoveE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tAddress, err)
287 return e2managererrors.NewRnibDbError()
290 e2tAddresses = m.removeAddressFromList(e2tAddresses, e2tAddress)
292 err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
294 m.logger.Errorf("#E2TInstancesManager.RemoveE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tAddress, err)
295 return e2managererrors.NewRnibDbError()
301 func (m *E2TInstancesManager) removeAddressFromList(e2tAddresses []string, addressToRemove string) []string {
302 newAddressList := []string{}
304 for _, address := range e2tAddresses {
305 if address != addressToRemove {
306 newAddressList = append(newAddressList, address)
310 return newAddressList
313 func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
315 e2tInstances, err := m.GetE2TInstances()
321 if len(e2tInstances) == 0 {
322 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No E2T instance found")
323 return "", e2managererrors.NewE2TInstanceAbsenceError()
326 min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances)
329 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No active E2T instance found")
330 return "", e2managererrors.NewE2TInstanceAbsenceError()
333 m.logger.Infof("#E2TInstancesManager.SelectE2TInstance - successfully selected E2T instance. address: %s", min.Address)
334 return min.Address, nil
337 func (m *E2TInstancesManager) AddRansToInstance(e2tAddress string, ranNames []string) error {
342 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
345 m.logger.Errorf("#E2TInstancesManager.AddRansToInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
346 return e2managererrors.NewRnibDbError()
349 e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, ranNames...)
351 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
354 m.logger.Errorf("#E2TInstancesManager.AddRansToInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
355 return e2managererrors.NewRnibDbError()
358 m.logger.Infof("#E2TInstancesManager.AddRansToInstance - RAN %s were added successfully to E2T %s", ranNames, e2tInstance.Address)
362 func (m *E2TInstancesManager) ResetKeepAliveTimestamp(e2tAddress string) error {
367 e2tInstance, err := m.rnibDataService.GetE2TInstanceNoLogs(e2tAddress)
370 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
374 if e2tInstance.State == entities.ToBeDeleted {
375 m.logger.Warnf("#E2TInstancesManager.ResetKeepAliveTimestamp - Ignore. This Instance is about to be deleted")
380 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
381 err = m.rnibDataService.SaveE2TInstanceNoLogs(e2tInstance)
384 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
391 func (m *E2TInstancesManager) SetE2tInstanceState(e2tAddress string, currentState entities.E2TInstanceState, newState entities.E2TInstanceState) error {
395 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
398 m.logger.Errorf("#E2TInstancesManager.SetE2tInstanceState - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
399 return e2managererrors.NewRnibDbError()
402 if (currentState != e2tInstance.State) {
403 m.logger.Warnf("#E2TInstancesManager.SetE2tInstanceState - E2T Instance address: %s - Current state is not: %s", e2tAddress, currentState)
404 return e2managererrors.NewInternalError()
407 e2tInstance.State = newState
408 if (newState == entities.Active) {
409 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
412 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
414 m.logger.Errorf("#E2TInstancesManager.SetE2tInstanceState - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tInstance.Address, err)
418 m.logger.Infof("#E2TInstancesManager.SetE2tInstanceState - E2T Instance address: %s - State change: %s --> %s", e2tAddress, currentState, newState)
423 func (m *E2TInstancesManager) ClearRansOfAllE2TInstances() error {
424 m.logger.Infof("#E2TInstancesManager.ClearRansOfAllE2TInstances - Going to clear associated RANs from E2T instances")
428 e2tInstances, err := m.GetE2TInstances()
434 if len(e2tInstances) == 0 {
435 m.logger.Errorf("#E2TInstancesManager.ClearRansOfAllE2TInstances - No E2T instances to clear associated RANs from")
439 for _, v := range e2tInstances {
440 v.AssociatedRanList = []string{}
441 err := m.rnibDataService.SaveE2TInstance(v)
444 m.logger.Errorf("#E2TInstancesManager.ClearRansOfAllE2TInstances - e2t address: %s - failed saving e2t instance. error: %s", v.Address, err)