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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
23 "e2mgr/e2managererrors"
26 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
27 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
33 type E2TInstancesManager struct {
34 rnibDataService services.RNibDataService
39 type IE2TInstancesManager interface {
40 GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error)
41 GetE2TInstances() ([]*entities.E2TInstance, error)
42 GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error)
43 AddE2TInstance(e2tAddress string) error
44 RemoveE2TInstance(e2tInstance *entities.E2TInstance) error
45 SelectE2TInstance() (string, error)
46 AddRanToInstance(ranName string, e2tAddress string) error
47 RemoveRanFromInstance(ranName string, e2tAddress string) error
48 ActivateE2TInstance(e2tInstance *entities.E2TInstance) error
49 ResetKeepAliveTimestamp(e2tAddress string) error
52 func NewE2TInstancesManager(rnibDataService services.RNibDataService, logger *logger.Logger) *E2TInstancesManager {
53 return &E2TInstancesManager{
54 rnibDataService: rnibDataService,
59 func (m *E2TInstancesManager) GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error) {
60 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
64 _, ok := err.(*common.ResourceNotFoundError)
67 m.logger.Errorf("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
69 m.logger.Infof("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s not found on DB", e2tAddress)
73 return e2tInstance, err
76 func (m *E2TInstancesManager) GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error) {
77 e2tAddresses, err := m.rnibDataService.GetE2TAddressesNoLogs()
80 _, ok := err.(*common.ResourceNotFoundError)
83 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T addresses. error: %s", err)
84 return nil, e2managererrors.NewRnibDbError()
87 return []*entities.E2TInstance{}, nil
90 if len(e2tAddresses) == 0 {
91 return []*entities.E2TInstance{}, nil
94 e2tInstances, err := m.rnibDataService.GetE2TInstancesNoLogs(e2tAddresses)
97 _, ok := err.(*common.ResourceNotFoundError)
100 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T instances list. error: %s", err)
102 return e2tInstances, err
105 return e2tInstances, nil
108 func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error) {
109 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
113 _, ok := err.(*common.ResourceNotFoundError)
116 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T addresses. error: %s", err)
117 return nil, e2managererrors.NewRnibDbError()
120 m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
121 return []*entities.E2TInstance{}, nil
124 if len(e2tAddresses) == 0 {
125 m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
126 return []*entities.E2TInstance{}, nil
129 e2tInstances, err := m.rnibDataService.GetE2TInstances(e2tAddresses)
132 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T instances list. error: %s", err)
133 return e2tInstances, e2managererrors.NewRnibDbError()
136 if len(e2tInstances) == 0 {
137 m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T instances list")
138 return e2tInstances, nil
141 return e2tInstances, nil
144 func (m *E2TInstancesManager) ResetKeepAliveTimestampsForAllE2TInstances() {
146 e2tInstances, err := m.GetE2TInstances()
149 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Couldn't reset timestamps due to a DB error")
153 if len(e2tInstances) == 0 {
154 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - No instances, ignoring reset")
158 for _, v := range e2tInstances {
160 if v.State != entities.Active {
164 v.KeepAliveTimestamp = time.Now().UnixNano()
166 err := m.rnibDataService.SaveE2TInstance(v)
169 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - E2T address: %s - failed resetting e2t instance keep alive timestamp. error: %s", v.Address, err)
173 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Done with reset")
176 func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2TInstance) *entities.E2TInstance {
177 var minInstance *entities.E2TInstance
178 minAssociatedRanCount := math.MaxInt32
180 for _, v := range e2tInstances {
181 if v.State == entities.Active && len(v.AssociatedRanList) < minAssociatedRanCount {
182 minAssociatedRanCount = len(v.AssociatedRanList)
190 func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
195 e2tInstance := entities.NewE2TInstance(e2tAddress)
196 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
199 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T instance. error: %s", e2tInstance.Address, err)
203 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
207 _, ok := err.(*common.ResourceNotFoundError)
210 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err)
215 e2tAddresses = append(e2tAddresses, e2tInstance.Address)
217 err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
220 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err)
224 m.logger.Infof("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - successfully added E2T instance", e2tInstance.Address)
228 func (m *E2TInstancesManager) RemoveRanFromInstance(ranName string, e2tAddress string) error {
233 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
236 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
240 i := 0 // output index
241 for _, v := range e2tInstance.AssociatedRanList {
243 // copy and increment index
244 e2tInstance.AssociatedRanList[i] = v
249 e2tInstance.AssociatedRanList = e2tInstance.AssociatedRanList[:i]
251 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
254 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
258 m.logger.Infof("#E2TInstancesManager.RemoveRanFromInstance - successfully dissociated RAN %s from E2T %s", ranName, e2tInstance.Address)
262 func (m *E2TInstancesManager) RemoveE2TInstance(e2tInstance *entities.E2TInstance) error {
265 func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
267 e2tInstances, err := m.GetE2TInstances()
273 if len(e2tInstances) == 0 {
274 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No E2T instance found")
275 return "", e2managererrors.NewE2TInstanceAbsenceError()
278 min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances)
281 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No active E2T instance found")
282 return "", e2managererrors.NewE2TInstanceAbsenceError()
285 m.logger.Infof("#E2TInstancesManager.SelectE2TInstance - successfully selected E2T instance. address: %s", min.Address)
286 return min.Address, nil
289 func (m *E2TInstancesManager) AddRanToInstance(ranName string, e2tAddress string) error {
294 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
297 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
298 return e2managererrors.NewRnibDbError()
301 e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, ranName)
303 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
306 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
307 return e2managererrors.NewRnibDbError()
310 m.logger.Infof("#E2TInstancesManager.AddRanToInstance - RAN %s was added successfully to E2T %s", ranName, e2tInstance.Address)
314 func (m E2TInstancesManager) ActivateE2TInstance(e2tInstance *entities.E2TInstance) error{
316 if e2tInstance == nil {
317 m.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - e2tInstance empty")
318 return e2managererrors.NewInternalError()
321 m.logger.Infof("#E2TInstancesManager.ActivateE2TInstance - E2T Address: %s - activate E2T instance", e2tInstance.Address)
323 e2tInstance.State = entities.Active
324 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
326 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
328 m.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tInstance.Address, err)
334 func (m *E2TInstancesManager) ResetKeepAliveTimestamp(e2tAddress string) error {
339 e2tInstance, err := m.rnibDataService.GetE2TInstanceNoLogs(e2tAddress)
342 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
346 if e2tInstance.State == entities.ToBeDeleted || e2tInstance.State == entities.RoutingManagerFailure {
347 m.logger.Warnf("#E2TInstancesManager.ResetKeepAliveTimestamp - Ignore. This Instance is about to deleted")
352 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
353 err = m.rnibDataService.SaveE2TInstanceNoLogs(e2tInstance)
356 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)