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 GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error)
42 GetE2TInstances() ([]*entities.E2TInstance, error)
43 GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error)
44 AddE2TInstance(e2tAddress string) error
45 RemoveE2TInstance(e2tInstance *entities.E2TInstance) error
46 SelectE2TInstance() (string, error)
47 AddRanToInstance(ranName string, e2tAddress string) error
48 RemoveRanFromInstance(ranName string, e2tAddress string) error
49 ActivateE2TInstance(e2tInstance *entities.E2TInstance) error
50 ResetKeepAliveTimestamp(e2tAddress string) error
53 func NewE2TInstancesManager(rnibDataService services.RNibDataService, logger *logger.Logger) *E2TInstancesManager {
54 return &E2TInstancesManager{
55 rnibDataService: rnibDataService,
60 func (m *E2TInstancesManager) GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error) {
61 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
65 _, ok := err.(*common.ResourceNotFoundError)
68 m.logger.Errorf("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
70 m.logger.Infof("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s not found on DB", e2tAddress)
74 return e2tInstance, err
77 func (m *E2TInstancesManager) GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error) {
78 e2tAddresses, err := m.rnibDataService.GetE2TAddressesNoLogs()
81 _, ok := err.(*common.ResourceNotFoundError)
84 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T addresses. error: %s", err)
85 return nil, e2managererrors.NewRnibDbError()
88 return []*entities.E2TInstance{}, nil
91 if len(e2tAddresses) == 0 {
92 return []*entities.E2TInstance{}, nil
95 e2tInstances, err := m.rnibDataService.GetE2TInstancesNoLogs(e2tAddresses)
98 _, ok := err.(*common.ResourceNotFoundError)
101 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T instances list. error: %s", err)
103 return e2tInstances, err
106 return e2tInstances, nil
109 func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error) {
110 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
114 _, ok := err.(*common.ResourceNotFoundError)
117 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T addresses. error: %s", err)
118 return nil, e2managererrors.NewRnibDbError()
121 m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
122 return []*entities.E2TInstance{}, nil
125 if len(e2tAddresses) == 0 {
126 m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
127 return []*entities.E2TInstance{}, nil
130 e2tInstances, err := m.rnibDataService.GetE2TInstances(e2tAddresses)
133 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T instances list. error: %s", err)
134 return e2tInstances, e2managererrors.NewRnibDbError()
137 if len(e2tInstances) == 0 {
138 m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T instances list")
139 return e2tInstances, nil
142 return e2tInstances, nil
145 func (m *E2TInstancesManager) ResetKeepAliveTimestampsForAllE2TInstances() {
147 e2tInstances, err := m.GetE2TInstances()
150 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Couldn't reset timestamps due to a DB error")
154 if len(e2tInstances) == 0 {
155 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - No instances, ignoring reset")
159 for _, v := range e2tInstances {
161 if v.State != entities.Active {
165 v.KeepAliveTimestamp = time.Now().UnixNano()
167 err := m.rnibDataService.SaveE2TInstance(v)
170 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - E2T address: %s - failed resetting e2t instance keep alive timestamp. error: %s", v.Address, err)
174 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Done with reset")
177 func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2TInstance) *entities.E2TInstance {
178 var minInstance *entities.E2TInstance
179 minAssociatedRanCount := math.MaxInt32
181 for _, v := range e2tInstances {
182 if v.State == entities.Active && len(v.AssociatedRanList) < minAssociatedRanCount {
183 minAssociatedRanCount = len(v.AssociatedRanList)
191 func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
196 e2tInstance := entities.NewE2TInstance(e2tAddress)
197 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
200 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T instance. error: %s", e2tInstance.Address, err)
204 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
208 _, ok := err.(*common.ResourceNotFoundError)
211 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err)
216 e2tAddresses = append(e2tAddresses, e2tInstance.Address)
218 err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
221 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err)
225 m.logger.Infof("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - successfully added E2T instance", e2tInstance.Address)
229 func (m *E2TInstancesManager) RemoveRanFromInstance(ranName string, e2tAddress string) error {
234 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
237 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
238 return e2managererrors.NewRnibDbError()
241 i := 0 // output index
242 for _, v := range e2tInstance.AssociatedRanList {
244 // copy and increment index
245 e2tInstance.AssociatedRanList[i] = v
250 e2tInstance.AssociatedRanList = e2tInstance.AssociatedRanList[:i]
252 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
255 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
256 return e2managererrors.NewRnibDbError()
259 m.logger.Infof("#E2TInstancesManager.RemoveRanFromInstance - successfully dissociated RAN %s from E2T %s", ranName, e2tInstance.Address)
263 func (m *E2TInstancesManager) RemoveE2TInstance(e2tInstance *entities.E2TInstance) error {
266 func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
268 e2tInstances, err := m.GetE2TInstances()
274 if len(e2tInstances) == 0 {
275 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No E2T instance found")
276 return "", e2managererrors.NewE2TInstanceAbsenceError()
279 min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances)
282 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No active E2T instance found")
283 return "", e2managererrors.NewE2TInstanceAbsenceError()
286 m.logger.Infof("#E2TInstancesManager.SelectE2TInstance - successfully selected E2T instance. address: %s", min.Address)
287 return min.Address, nil
290 func (m *E2TInstancesManager) AddRanToInstance(ranName string, e2tAddress string) error {
295 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
298 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
299 return e2managererrors.NewRnibDbError()
302 e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, ranName)
304 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
307 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
308 return e2managererrors.NewRnibDbError()
311 m.logger.Infof("#E2TInstancesManager.AddRanToInstance - RAN %s was added successfully to E2T %s", ranName, e2tInstance.Address)
315 func (m E2TInstancesManager) ActivateE2TInstance(e2tInstance *entities.E2TInstance) error{
317 if e2tInstance == nil {
318 m.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - e2tInstance empty")
319 return e2managererrors.NewInternalError()
322 m.logger.Infof("#E2TInstancesManager.ActivateE2TInstance - E2T Address: %s - activate E2T instance", e2tInstance.Address)
324 e2tInstance.State = entities.Active
325 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
327 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
329 m.logger.Errorf("#E2TInstancesManager.ActivateE2TInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tInstance.Address, err)
335 func (m *E2TInstancesManager) ResetKeepAliveTimestamp(e2tAddress string) error {
340 e2tInstance, err := m.rnibDataService.GetE2TInstanceNoLogs(e2tAddress)
343 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
347 if e2tInstance.State == entities.ToBeDeleted || e2tInstance.State == entities.RoutingManagerFailure {
348 m.logger.Warnf("#E2TInstancesManager.ResetKeepAliveTimestamp - Ignore. This Instance is about to deleted")
353 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
354 err = m.rnibDataService.SaveE2TInstanceNoLogs(e2tInstance)
357 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)