[RIC-431] REDESIGN - Add RanListManager methods | Add/Update RnibWriter methods
[ric-plt/e2mgr.git] / E2Manager / managers / ran_list_manager.go
1 //
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
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package managers
21
22 import (
23         "e2mgr/logger"
24         "e2mgr/services"
25         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
26         "sync"
27 )
28
29 type ranListManagerInstance struct {
30         logger          *logger.Logger
31         rnibDataService services.RNibDataService
32         mux             sync.Mutex
33         nbIdentityMap   map[string]*entities.NbIdentity
34 }
35
36 type RanListManager interface {
37         InitNbIdentityMap() error
38         AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error
39         UpdateNbIdentityConnectionStatus(nodeType entities.Node_Type, ranName string, connectionStatus entities.ConnectionStatus) error
40         RemoveNbIdentity(nodeType entities.Node_Type, ranName string) error
41         GetNbIdentityList() []*entities.NbIdentity
42         UpdateRanState(nodebInfo *entities.NodebInfo) error // TODO: replace with UpdateNbIdentityConnectionStatus
43 }
44
45 func NewRanListManager(logger *logger.Logger, rnibDataService services.RNibDataService) RanListManager {
46         return &ranListManagerInstance{
47                 logger:          logger,
48                 rnibDataService: rnibDataService,
49                 nbIdentityMap:   make(map[string]*entities.NbIdentity),
50         }
51 }
52
53 // TODO: replace with UpdateNbIdentityConnectionStatus
54 func (m *ranListManagerInstance) UpdateRanState(nodebInfo *entities.NodebInfo) error {
55         m.logger.Infof("#ranListManagerInstance.UpdateRanState - RAN name: %s - Updating state...", nodebInfo.RanName)
56         return nil
57 }
58
59 func (m *ranListManagerInstance) InitNbIdentityMap() error {
60         nbIds, err := m.rnibDataService.GetListNodebIds()
61
62         if err != nil {
63                 m.logger.Errorf("#ranListManagerInstance.InitRanList - Failed fetching RAN list from DB. error: %s", err)
64                 return err
65         }
66
67         for _, v := range nbIds {
68                 m.nbIdentityMap[v.InventoryName] = v
69         }
70
71         m.logger.Infof("#ranListManagerInstance.InitRanList - Successfully initiated nodeb identity map")
72         return nil
73 }
74
75 func (m *ranListManagerInstance) AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
76         m.mux.Lock()
77         defer m.mux.Unlock()
78
79         m.nbIdentityMap[nbIdentity.InventoryName] = nbIdentity
80
81         err := m.rnibDataService.AddNbIdentity(nodeType, nbIdentity)
82
83         if err != nil {
84                 m.logger.Errorf("#ranListManagerInstance.AddNbIdentity - RAN name: %s - Failed adding nodeb identity to DB. error: %s", nbIdentity.InventoryName, err)
85                 return err
86         }
87
88         m.logger.Infof("#ranListManagerInstance.AddNbIdentity - RAN name: %s - Successfully added nodeb identity", nbIdentity.InventoryName)
89         return nil
90 }
91
92 func (m *ranListManagerInstance) UpdateNbIdentityConnectionStatus(nodeType entities.Node_Type, ranName string, connectionStatus entities.ConnectionStatus) error {
93         //TODO: implement
94         return nil
95 }
96
97 func (m *ranListManagerInstance) RemoveNbIdentity(nodeType entities.Node_Type, ranName string) error {
98         //TODO: implement
99         return nil
100 }
101
102 func (m *ranListManagerInstance) GetNbIdentityList() []*entities.NbIdentity {
103         nbIds := make([]*entities.NbIdentity, len(m.nbIdentityMap))
104         for _, v := range m.nbIdentityMap {
105                 nbIds = append(nbIds, v)
106         }
107
108         return nbIds
109 }