X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=E2Manager%2Fmanagers%2Fe2t_instances_manager.go;h=d4bff658ce75eb42f738670f58c429cce298e944;hb=622ede7fa6952e84a311654683d8eccc6f0eed25;hp=52e9b25143f1266b0947e48b7606a2c3fcfb39f8;hpb=8adac1461257b94f8d4f50b33cdef7ecbba36cb9;p=ric-plt%2Fe2mgr.git diff --git a/E2Manager/managers/e2t_instances_manager.go b/E2Manager/managers/e2t_instances_manager.go index 52e9b25..d4bff65 100644 --- a/E2Manager/managers/e2t_instances_manager.go +++ b/E2Manager/managers/e2t_instances_manager.go @@ -1,11 +1,29 @@ +// +// Copyright 2019 AT&T Intellectual Property +// Copyright 2019 Nokia +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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. +// + package managers import ( + "e2mgr/e2managererrors" "e2mgr/logger" "e2mgr/services" - "fmt" "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common" "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities" + "math" "sync" ) @@ -17,11 +35,12 @@ type E2TInstancesManager struct { type IE2TInstancesManager interface { GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error) + GetE2TInstances() ([]*entities.E2TInstance, error) AddE2TInstance(e2tAddress string) error RemoveE2TInstance(e2tInstance *entities.E2TInstance) error - SelectE2TInstance(e2tInstance *entities.E2TInstance) (string, error) + SelectE2TInstance() (string, error) AssociateRan(ranName string, e2tAddress string) error - DeassociateRan(ranName string, e2tAddress string) error + DissociateRan(ranName string, e2tAddress string) error } func NewE2TInstancesManager(rnibDataService services.RNibDataService, logger *logger.Logger) *E2TInstancesManager { @@ -35,19 +54,63 @@ func (m *E2TInstancesManager) GetE2TInstance(e2tAddress string) (*entities.E2TIn e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress) if err != nil { - m.logger.Errorf("#GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err) + + _, ok := err.(*common.ResourceNotFoundError) + + if !ok { + m.logger.Errorf("#GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err) + } else { + m.logger.Infof("#GetE2TInstance - E2T Instance address: %s not found on DB", e2tAddress) + } } return e2tInstance, err } -func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error { +func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error) { + e2tAddresses, err := m.rnibDataService.GetE2TAddresses() - if len(e2tAddress) == 0 { - m.logger.Errorf("#AddE2TInstance - Empty E2T address received") - return fmt.Errorf("empty E2T address") + if err != nil { + m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T addresses. error: %s", err) + return nil, err } + if len(e2tAddresses) == 0 { + m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list") + return []*entities.E2TInstance{}, nil + } + + e2tInstances, err := m.rnibDataService.GetE2TInstances(e2tAddresses) + + if err != nil { + m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T instances list. error: %s", err) + return e2tInstances, err + } + + if len(e2tInstances) == 0 { + m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T instances list") + return e2tInstances, nil + } + + return e2tInstances, nil +} + +func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2TInstance) *entities.E2TInstance { + var minInstance *entities.E2TInstance + minAssociatedRanCount := math.MaxInt32 + + for _, v := range e2tInstances { + if v.State == entities.Active && len(v.AssociatedRanList) < minAssociatedRanCount { + minAssociatedRanCount = len(v.AssociatedRanList) + minInstance = v + } + } + + return minInstance +} + +func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error { + e2tInstance := entities.NewE2TInstance(e2tAddress) err := m.rnibDataService.SaveE2TInstance(e2tInstance) @@ -59,70 +122,40 @@ func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error { m.mux.Lock() defer m.mux.Unlock() - e2tInfoList, err := m.rnibDataService.GetE2TInfoList() + e2tAddresses, err := m.rnibDataService.GetE2TAddresses() if err != nil { _, ok := err.(*common.ResourceNotFoundError) if !ok { - m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed retrieving E2TInfoList. error: %s", e2tInstance.Address, err) + m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err) return err } } - e2tInstanceInfo := entities.NewE2TInstanceInfo(e2tInstance.Address) - e2tInfoList = append(e2tInfoList, e2tInstanceInfo) + e2tAddresses = append(e2tAddresses, e2tInstance.Address) - err = m.rnibDataService.SaveE2TInfoList(e2tInfoList) + err = m.rnibDataService.SaveE2TAddresses(e2tAddresses) if err != nil { - m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed saving E2TInfoList. error: %s", e2tInstance.Address, err) + m.logger.Errorf("#AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err) return err } - m.logger.Infof("#AddE2TInstance - E2T Instance address: %s - successfully completed", e2tInstance.Address) + m.logger.Infof("#AddE2TInstance - E2T Instance address: %s - successfully added E2T instance", e2tInstance.Address) return nil } -func (m *E2TInstancesManager) DeassociateRan(ranName string, e2tAddress string) error { +func (m *E2TInstancesManager) DissociateRan(ranName string, e2tAddress string) error { m.mux.Lock() defer m.mux.Unlock() - e2tInfoList, err := m.rnibDataService.GetE2TInfoList() - - if err != nil { - m.logger.Errorf("#DeassociateRan - E2T Instance address: %s - Failed retrieving E2TInfoList. error: %s", e2tAddress, err) - return err - } - - isE2TInstanceFound := false - - for _, e2tInfoInstance := range e2tInfoList { - if e2tInfoInstance.Address == e2tAddress { - e2tInfoInstance.AssociatedRanCount-- - isE2TInstanceFound = true - break - } - } - - if !isE2TInstanceFound { - m.logger.Warnf("#DeassociateRan - E2T Instance address: %s - E2TInstance not found in E2TInfoList.", e2tAddress) - return nil - } - - err = m.rnibDataService.SaveE2TInfoList(e2tInfoList) - - if err != nil { - m.logger.Errorf("#DeassociateRan - E2T Instance address: %s - Failed saving E2TInfoList. error: %s", e2tAddress, err) - return err - } - e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress) if err != nil { - m.logger.Errorf("#DeassociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err) + m.logger.Errorf("#DissociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err) return err } @@ -140,48 +173,52 @@ func (m *E2TInstancesManager) DeassociateRan(ranName string, e2tAddress string) err = m.rnibDataService.SaveE2TInstance(e2tInstance) if err != nil { - m.logger.Errorf("#DeassociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err) + m.logger.Errorf("#DissociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err) return err } + m.logger.Infof("#DissociateRan - successfully dissociated RAN %s from E2T %s", ranName, e2tInstance.Address) return nil } func (m *E2TInstancesManager) RemoveE2TInstance(e2tInstance *entities.E2TInstance) error { return nil } -func (m *E2TInstancesManager) SelectE2TInstance(e2tInstance *entities.E2TInstance) (string, error) { - return "", nil -} +func (m *E2TInstancesManager) SelectE2TInstance() (string, error) { -func (m *E2TInstancesManager) AssociateRan(ranName string, e2tAddress string) error { - - e2tInfoList, err := m.rnibDataService.GetE2TInfoList() + e2tInstances, err := m.GetE2TInstances() if err != nil { - m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed retrieving E2TInfoList. error: %s", e2tAddress, err) - return err + m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - failed retrieving E2T instances. error: %s", err) + return "", e2managererrors.NewRnibDbError() } - for _, e2tInfoInstance := range e2tInfoList { - if e2tInfoInstance.Address == e2tAddress { - e2tInfoInstance.AssociatedRanCount++ - break; - } + if len(e2tInstances) == 0 { + m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No E2T instance found") + return "", e2managererrors.NewE2TInstanceAbsenceError() } - err = m.rnibDataService.SaveE2TInfoList(e2tInfoList) + min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances) - if err != nil { - m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed saving E2TInfoList. error: %s", e2tAddress, err) - return err + if min == nil { + m.logger.Errorf("#SelectE2TInstance - No active E2T instance found") + return "", e2managererrors.NewE2TInstanceAbsenceError() } + m.logger.Infof("#SelectE2TInstance - successfully selected E2T instance. address: %s", min.Address) + return min.Address, nil +} + +func (m *E2TInstancesManager) AssociateRan(ranName string, e2tAddress string) error { + + m.mux.Lock() + defer m.mux.Unlock() + e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress) if err != nil { m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err) - return err + return e2managererrors.NewRnibDbError() } e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, ranName) @@ -190,8 +227,9 @@ func (m *E2TInstancesManager) AssociateRan(ranName string, e2tAddress string) er if err != nil { m.logger.Errorf("#AssociateRan - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err) - return err + return e2managererrors.NewRnibDbError() } + m.logger.Infof("#AssociateRan - successfully associated RAN %s with E2T %s", ranName, e2tInstance.Address) return nil }