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(e2tInstance *entities.E2TInstance) error
47 SelectE2TInstance() (string, error)
48 AddRanToInstance(ranName string, e2tAddress string) error
49 RemoveRanFromInstance(ranName string, e2tAddress string) error
50 ActivateE2TInstance(e2tInstance *entities.E2TInstance) error
51 ResetKeepAliveTimestamp(e2tAddress string) error
52 ClearRansOfAllE2TInstances() 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(e2tInstance *entities.E2TInstance) error {
276 func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
278 e2tInstances, err := m.GetE2TInstances()
284 if len(e2tInstances) == 0 {
285 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No E2T instance found")
286 return "", e2managererrors.NewE2TInstanceAbsenceError()
289 min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances)
292 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No active E2T instance found")
293 return "", e2managererrors.NewE2TInstanceAbsenceError()
296 m.logger.Infof("#E2TInstancesManager.SelectE2TInstance - successfully selected E2T instance. address: %s", min.Address)
297 return min.Address, nil
300 func (m *E2TInstancesManager) AddRanToInstance(ranName string, e2tAddress string) error {
305 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
308 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
309 return e2managererrors.NewRnibDbError()
312 e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, ranName)
314 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
317 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
318 return e2managererrors.NewRnibDbError()
321 m.logger.Infof("#E2TInstancesManager.AddRanToInstance - RAN %s was added successfully to E2T %s", ranName, e2tInstance.Address)
325 func (m E2TInstancesManager) ActivateE2TInstance(e2tInstance *entities.E2TInstance) error {
327 if e2tInstance == nil {
328 m.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - e2tInstance empty")
329 return e2managererrors.NewInternalError()
332 m.logger.Infof("#E2TInstancesManager.ActivateE2TInstance - E2T Address: %s - activate E2T instance", e2tInstance.Address)
334 e2tInstance.State = entities.Active
335 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
337 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
339 m.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tInstance.Address, err)
345 func (m *E2TInstancesManager) ResetKeepAliveTimestamp(e2tAddress string) error {
350 e2tInstance, err := m.rnibDataService.GetE2TInstanceNoLogs(e2tAddress)
353 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
357 if e2tInstance.State == entities.ToBeDeleted || e2tInstance.State == entities.RoutingManagerFailure {
358 m.logger.Warnf("#E2TInstancesManager.ResetKeepAliveTimestamp - Ignore. This Instance is about to deleted")
363 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
364 err = m.rnibDataService.SaveE2TInstanceNoLogs(e2tInstance)
367 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
374 func (m *E2TInstancesManager) ClearRansOfAllE2TInstances() error {
379 e2tInstances, err := m.GetE2TInstances()
385 if len(e2tInstances) == 0 {
386 m.logger.Errorf("#E2TInstancesManager.ClearRansOfAllE2TInstances - No E2T instances to clear associated RANs from")
390 for _, v := range e2tInstances {
391 v.AssociatedRanList = []string{}
392 err := m.rnibDataService.SaveE2TInstance(v)
395 m.logger.Errorf("#E2TInstancesManager.ClearRansOfAllE2TInstances - e2t address: %s - failed saving e2t instance. error: %s", v.Address, err)