blob: 7f7c379a8e49bbf714cd75e44ec605d05b0a3f5e [file] [log] [blame]
ss412gefcb4522019-12-02 16:59:19 +02001//
2// Copyright 2019 AT&T Intellectual Property
3// Copyright 2019 Nokia
4//
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
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
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.
16//
ss412g697bea22019-12-24 22:38:19 +020017
Amichai804065d2019-12-24 14:52:24 +020018// This source code is part of the near-RT RIC (RAN Intelligent Controller)
19// platform project (RICP).
ss412gefcb4522019-12-02 16:59:19 +020020
21package managers
22
23import (
24 "e2mgr/e2managererrors"
25 "e2mgr/logger"
26 "e2mgr/services"
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"
29 "math"
30 "sync"
31 "time"
32)
33
34type E2TInstancesManager struct {
35 rnibDataService services.RNibDataService
36 logger *logger.Logger
37 mux sync.Mutex
38}
39
40type IE2TInstancesManager interface {
is005q90d22a62020-01-01 11:55:49 +020041 GetE2TAddresses() ([]string, error)
ss412gefcb4522019-12-02 16:59:19 +020042 GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error)
43 GetE2TInstances() ([]*entities.E2TInstance, error)
44 GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error)
45 AddE2TInstance(e2tAddress string) error
Amichaif846c592020-01-08 16:45:07 +020046 RemoveE2TInstance(e2tAddress string) error
ss412gefcb4522019-12-02 16:59:19 +020047 SelectE2TInstance() (string, error)
Amichai804065d2019-12-24 14:52:24 +020048 AddRanToInstance(ranName string, e2tAddress string) error
49 RemoveRanFromInstance(ranName string, e2tAddress string) error
ss412gefcb4522019-12-02 16:59:19 +020050 ResetKeepAliveTimestamp(e2tAddress string) error
is005q90d22a62020-01-01 11:55:49 +020051 ClearRansOfAllE2TInstances() error
Amichaif846c592020-01-08 16:45:07 +020052 SetE2tInstanceState(e2tAddress string, currentState entities.E2TInstanceState, newState entities.E2TInstanceState) error
ss412gefcb4522019-12-02 16:59:19 +020053}
54
55func NewE2TInstancesManager(rnibDataService services.RNibDataService, logger *logger.Logger) *E2TInstancesManager {
56 return &E2TInstancesManager{
57 rnibDataService: rnibDataService,
58 logger: logger,
59 }
60}
61
62func (m *E2TInstancesManager) GetE2TInstance(e2tAddress string) (*entities.E2TInstance, error) {
63 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
64
65 if err != nil {
66
67 _, ok := err.(*common.ResourceNotFoundError)
68
69 if !ok {
70 m.logger.Errorf("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
71 } else {
72 m.logger.Infof("#E2TInstancesManager.GetE2TInstance - E2T Instance address: %s not found on DB", e2tAddress)
73 }
74 }
75
76 return e2tInstance, err
77}
78
79func (m *E2TInstancesManager) GetE2TInstancesNoLogs() ([]*entities.E2TInstance, error) {
80 e2tAddresses, err := m.rnibDataService.GetE2TAddressesNoLogs()
81
82 if err != nil {
83 _, ok := err.(*common.ResourceNotFoundError)
84
85 if !ok {
86 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T addresses. error: %s", err)
87 return nil, e2managererrors.NewRnibDbError()
88 }
89
90 return []*entities.E2TInstance{}, nil
91 }
92
93 if len(e2tAddresses) == 0 {
94 return []*entities.E2TInstance{}, nil
95 }
96
97 e2tInstances, err := m.rnibDataService.GetE2TInstancesNoLogs(e2tAddresses)
98
99 if err != nil {
100 _, ok := err.(*common.ResourceNotFoundError)
101
102 if !ok {
103 m.logger.Errorf("#E2TInstancesManager.GetE2TInstancesNoLogs - Failed retrieving E2T instances list. error: %s", err)
104 }
105 return e2tInstances, err
106 }
107
108 return e2tInstances, nil
109}
110
is005q90d22a62020-01-01 11:55:49 +0200111func (m *E2TInstancesManager) GetE2TAddresses() ([]string, error) {
ss412gefcb4522019-12-02 16:59:19 +0200112 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
113
114 if err != nil {
115
116 _, ok := err.(*common.ResourceNotFoundError)
117
118 if !ok {
is005q90d22a62020-01-01 11:55:49 +0200119 m.logger.Errorf("#E2TInstancesManager.GetE2TAddresses - Failed retrieving E2T addresses. error: %s", err)
ss412gefcb4522019-12-02 16:59:19 +0200120 return nil, e2managererrors.NewRnibDbError()
121 }
122
is005q90d22a62020-01-01 11:55:49 +0200123 }
124
125 return e2tAddresses, nil
126}
127
128func (m *E2TInstancesManager) GetE2TInstances() ([]*entities.E2TInstance, error) {
129 e2tAddresses, err := m.GetE2TAddresses()
130
131 if err != nil {
132 return nil, e2managererrors.NewRnibDbError()
ss412gefcb4522019-12-02 16:59:19 +0200133 }
134
135 if len(e2tAddresses) == 0 {
136 m.logger.Infof("#E2TInstancesManager.GetE2TInstances - Empty E2T addresses list")
137 return []*entities.E2TInstance{}, nil
138 }
139
140 e2tInstances, err := m.rnibDataService.GetE2TInstances(e2tAddresses)
141
142 if err != nil {
143 m.logger.Errorf("#E2TInstancesManager.GetE2TInstances - Failed retrieving E2T instances list. error: %s", err)
144 return e2tInstances, e2managererrors.NewRnibDbError()
145 }
146
147 if len(e2tInstances) == 0 {
148 m.logger.Warnf("#E2TInstancesManager.GetE2TInstances - Empty E2T instances list")
149 return e2tInstances, nil
150 }
151
152 return e2tInstances, nil
153}
154
155func (m *E2TInstancesManager) ResetKeepAliveTimestampsForAllE2TInstances() {
156
157 e2tInstances, err := m.GetE2TInstances()
158
159 if err != nil {
160 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Couldn't reset timestamps due to a DB error")
161 return
162 }
163
164 if len(e2tInstances) == 0 {
165 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - No instances, ignoring reset")
166 return
167 }
168
169 for _, v := range e2tInstances {
170
171 if v.State != entities.Active {
172 continue
173 }
174
175 v.KeepAliveTimestamp = time.Now().UnixNano()
176
177 err := m.rnibDataService.SaveE2TInstance(v)
178
179 if err != nil {
180 m.logger.Errorf("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - E2T address: %s - failed resetting e2t instance keep alive timestamp. error: %s", v.Address, err)
181 }
182 }
183
184 m.logger.Infof("E2TInstancesManager.ResetKeepAliveTimestampForAllE2TInstances - Done with reset")
185}
186
187func findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances []*entities.E2TInstance) *entities.E2TInstance {
188 var minInstance *entities.E2TInstance
189 minAssociatedRanCount := math.MaxInt32
190
191 for _, v := range e2tInstances {
192 if v.State == entities.Active && len(v.AssociatedRanList) < minAssociatedRanCount {
193 minAssociatedRanCount = len(v.AssociatedRanList)
194 minInstance = v
195 }
196 }
197
198 return minInstance
199}
200
201func (m *E2TInstancesManager) AddE2TInstance(e2tAddress string) error {
202
203 m.mux.Lock()
204 defer m.mux.Unlock()
205
206 e2tInstance := entities.NewE2TInstance(e2tAddress)
207 err := m.rnibDataService.SaveE2TInstance(e2tInstance)
208
209 if err != nil {
210 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T instance. error: %s", e2tInstance.Address, err)
211 return err
212 }
213
214 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
215
216 if err != nil {
217
218 _, ok := err.(*common.ResourceNotFoundError)
219
220 if !ok {
221 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tInstance.Address, err)
222 return err
223 }
224 }
225
226 e2tAddresses = append(e2tAddresses, e2tInstance.Address)
227
228 err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
229
230 if err != nil {
231 m.logger.Errorf("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tInstance.Address, err)
232 return err
233 }
234
235 m.logger.Infof("#E2TInstancesManager.AddE2TInstance - E2T Instance address: %s - successfully added E2T instance", e2tInstance.Address)
236 return nil
237}
238
Amichai804065d2019-12-24 14:52:24 +0200239func (m *E2TInstancesManager) RemoveRanFromInstance(ranName string, e2tAddress string) error {
ss412gefcb4522019-12-02 16:59:19 +0200240
241 m.mux.Lock()
242 defer m.mux.Unlock()
243
244 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
245
246 if err != nil {
Amichai804065d2019-12-24 14:52:24 +0200247 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
Amichai0c747ac2019-12-25 14:06:40 +0200248 return e2managererrors.NewRnibDbError()
ss412gefcb4522019-12-02 16:59:19 +0200249 }
250
251 i := 0 // output index
252 for _, v := range e2tInstance.AssociatedRanList {
253 if v != ranName {
254 // copy and increment index
255 e2tInstance.AssociatedRanList[i] = v
256 i++
257 }
258 }
259
260 e2tInstance.AssociatedRanList = e2tInstance.AssociatedRanList[:i]
261
262 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
263
264 if err != nil {
Amichai804065d2019-12-24 14:52:24 +0200265 m.logger.Errorf("#E2TInstancesManager.RemoveRanFromInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
Amichai0c747ac2019-12-25 14:06:40 +0200266 return e2managererrors.NewRnibDbError()
ss412gefcb4522019-12-02 16:59:19 +0200267 }
268
Amichai804065d2019-12-24 14:52:24 +0200269 m.logger.Infof("#E2TInstancesManager.RemoveRanFromInstance - successfully dissociated RAN %s from E2T %s", ranName, e2tInstance.Address)
ss412gefcb4522019-12-02 16:59:19 +0200270 return nil
271}
272
Amichaif846c592020-01-08 16:45:07 +0200273func (m *E2TInstancesManager) RemoveE2TInstance(e2tAddress string) error {
274 m.mux.Lock()
275 defer m.mux.Unlock()
276
277 err := m.rnibDataService.RemoveE2TInstance(e2tAddress)
278 if err != nil {
279 m.logger.Errorf("#E2TInstancesManager.RemoveE2TInstance - E2T Instance address: %s - Failed removing E2TInstance. error: %s", e2tAddress, err)
280 return e2managererrors.NewRnibDbError()
281 }
282
283 e2tAddresses, err := m.rnibDataService.GetE2TAddresses()
284
285 if err != nil {
286 m.logger.Errorf("#E2TInstancesManager.RemoveE2TInstance - E2T Instance address: %s - Failed retrieving E2T addresses list. error: %s", e2tAddress, err)
287 return e2managererrors.NewRnibDbError()
288 }
289
290 e2tAddresses = m.removeAddressFromList(e2tAddresses, e2tAddress)
291
292 err = m.rnibDataService.SaveE2TAddresses(e2tAddresses)
293 if err != nil {
294 m.logger.Errorf("#E2TInstancesManager.RemoveE2TInstance - E2T Instance address: %s - Failed saving E2T addresses list. error: %s", e2tAddress, err)
295 return e2managererrors.NewRnibDbError()
296 }
297
ss412gefcb4522019-12-02 16:59:19 +0200298 return nil
299}
Amichaif846c592020-01-08 16:45:07 +0200300
301func (m *E2TInstancesManager) removeAddressFromList(e2tAddresses []string, addressToRemove string) []string {
302 newAddressList := []string{}
303
304 for _, address := range e2tAddresses {
305 if address != addressToRemove {
306 newAddressList = append(newAddressList, address)
307 }
308 }
309
310 return newAddressList
311}
312
ss412gefcb4522019-12-02 16:59:19 +0200313func (m *E2TInstancesManager) SelectE2TInstance() (string, error) {
314
315 e2tInstances, err := m.GetE2TInstances()
316
317 if err != nil {
318 return "", err
319 }
320
321 if len(e2tInstances) == 0 {
322 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No E2T instance found")
323 return "", e2managererrors.NewE2TInstanceAbsenceError()
324 }
325
326 min := findActiveE2TInstanceWithMinimumAssociatedRans(e2tInstances)
327
328 if min == nil {
329 m.logger.Errorf("#E2TInstancesManager.SelectE2TInstance - No active E2T instance found")
330 return "", e2managererrors.NewE2TInstanceAbsenceError()
331 }
332
333 m.logger.Infof("#E2TInstancesManager.SelectE2TInstance - successfully selected E2T instance. address: %s", min.Address)
334 return min.Address, nil
335}
336
Amichai804065d2019-12-24 14:52:24 +0200337func (m *E2TInstancesManager) AddRanToInstance(ranName string, e2tAddress string) error {
ss412gefcb4522019-12-02 16:59:19 +0200338
339 m.mux.Lock()
340 defer m.mux.Unlock()
341
342 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
343
344 if err != nil {
Amichai804065d2019-12-24 14:52:24 +0200345 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
ss412gefcb4522019-12-02 16:59:19 +0200346 return e2managererrors.NewRnibDbError()
347 }
348
349 e2tInstance.AssociatedRanList = append(e2tInstance.AssociatedRanList, ranName)
350
351 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
352
353 if err != nil {
Amichai804065d2019-12-24 14:52:24 +0200354 m.logger.Errorf("#E2TInstancesManager.AddRanToInstance - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
ss412gefcb4522019-12-02 16:59:19 +0200355 return e2managererrors.NewRnibDbError()
356 }
357
Amichai804065d2019-12-24 14:52:24 +0200358 m.logger.Infof("#E2TInstancesManager.AddRanToInstance - RAN %s was added successfully to E2T %s", ranName, e2tInstance.Address)
ss412gefcb4522019-12-02 16:59:19 +0200359 return nil
360}
361
ss412gefcb4522019-12-02 16:59:19 +0200362func (m *E2TInstancesManager) ResetKeepAliveTimestamp(e2tAddress string) error {
363
364 m.mux.Lock()
365 defer m.mux.Unlock()
366
367 e2tInstance, err := m.rnibDataService.GetE2TInstanceNoLogs(e2tAddress)
368
369 if err != nil {
370 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
371 return err
372 }
373
374 if e2tInstance.State == entities.ToBeDeleted || e2tInstance.State == entities.RoutingManagerFailure {
375 m.logger.Warnf("#E2TInstancesManager.ResetKeepAliveTimestamp - Ignore. This Instance is about to deleted")
376 return nil
377
378 }
379
380 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
381 err = m.rnibDataService.SaveE2TInstanceNoLogs(e2tInstance)
382
383 if err != nil {
384 m.logger.Errorf("#E2TInstancesManager.ResetKeepAliveTimestamp - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tAddress, err)
385 return err
386 }
387
388 return nil
389}
is005q90d22a62020-01-01 11:55:49 +0200390
Amichaif846c592020-01-08 16:45:07 +0200391func (m *E2TInstancesManager) SetE2tInstanceState(e2tAddress string, currentState entities.E2TInstanceState, newState entities.E2TInstanceState) error {
392 m.mux.Lock()
393 defer m.mux.Unlock()
394
395 e2tInstance, err := m.rnibDataService.GetE2TInstance(e2tAddress)
396
397 if err != nil {
398 m.logger.Errorf("#E2TInstancesManager.SetE2tInstanceState - E2T Instance address: %s - Failed retrieving E2TInstance. error: %s", e2tAddress, err)
399 return e2managererrors.NewRnibDbError()
400 }
401
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()
405 }
406
407 e2tInstance.State = newState
408 if (newState == entities.Active) {
409 e2tInstance.KeepAliveTimestamp = time.Now().UnixNano()
410 }
411
412 err = m.rnibDataService.SaveE2TInstance(e2tInstance)
413 if err != nil {
414 m.logger.Errorf("#E2TInstancesManager.SetE2tInstanceState - E2T Instance address: %s - Failed saving E2TInstance. error: %s", e2tInstance.Address, err)
415 return err
416 }
417
418 m.logger.Infof("#E2TInstancesManager.SetE2tInstanceState - E2T Instance address: %s - State change: %s --> %s", e2tAddress, currentState, newState)
419
420 return nil
421}
422
is005q90d22a62020-01-01 11:55:49 +0200423func (m *E2TInstancesManager) ClearRansOfAllE2TInstances() error {
is005q5c953322020-01-05 16:22:47 +0200424 m.logger.Infof("#E2TInstancesManager.ClearRansOfAllE2TInstances - Going to clear associated RANs from E2T instances")
is005q90d22a62020-01-01 11:55:49 +0200425 m.mux.Lock()
426 defer m.mux.Unlock()
427
428 e2tInstances, err := m.GetE2TInstances()
429
430 if err != nil {
431 return err
432 }
433
434 if len(e2tInstances) == 0 {
435 m.logger.Errorf("#E2TInstancesManager.ClearRansOfAllE2TInstances - No E2T instances to clear associated RANs from")
436 return nil
437 }
438
439 for _, v := range e2tInstances {
440 v.AssociatedRanList = []string{}
441 err := m.rnibDataService.SaveE2TInstance(v)
442
443 if err != nil {
444 m.logger.Errorf("#E2TInstancesManager.ClearRansOfAllE2TInstances - e2t address: %s - failed saving e2t instance. error: %s", v.Address, err)
445 }
446 }
447
448 return nil
449}