[RIC-431] REDESIGN - Modify Setup Flows to call AddNbIdentity | Modify UTs
[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.InitNbIdentityMap - 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.InitNbIdentityMap - Successfully initiated nodeb identity map")
72         m.logger.Debugf("#ranListManagerInstance.InitNbIdentityMap - nodeb Identity map: %s", m.nbIdentityMap)
73         return nil
74 }
75
76 func (m *ranListManagerInstance) AddNbIdentity(nodeType entities.Node_Type, nbIdentity *entities.NbIdentity) error {
77         m.mux.Lock()
78         defer m.mux.Unlock()
79
80         m.nbIdentityMap[nbIdentity.InventoryName] = nbIdentity
81
82         err := m.rnibDataService.AddNbIdentity(nodeType, nbIdentity)
83
84         if err != nil {
85                 m.logger.Errorf("#ranListManagerInstance.AddNbIdentity - RAN name: %s - Failed adding nodeb identity to DB. error: %s", nbIdentity.InventoryName, err)
86                 return err
87         }
88
89         m.logger.Infof("#ranListManagerInstance.AddNbIdentity - RAN name: %s - Successfully added nodeb identity", nbIdentity.InventoryName)
90         m.logger.Debugf("#ranListManagerInstance.AddNbIdentity - nodeb Identity map: %s", m.nbIdentityMap)
91         return nil
92 }
93
94 func (m *ranListManagerInstance) UpdateNbIdentityConnectionStatus(nodeType entities.Node_Type, ranName string, connectionStatus entities.ConnectionStatus) error {
95         //TODO: implement
96         return nil
97 }
98
99 func (m *ranListManagerInstance) RemoveNbIdentity(nodeType entities.Node_Type, ranName string) error {
100         //TODO: implement
101         return nil
102 }
103
104 func (m *ranListManagerInstance) GetNbIdentityList() []*entities.NbIdentity {
105         nbIds := make([]*entities.NbIdentity, len(m.nbIdentityMap))
106         for _, v := range m.nbIdentityMap {
107                 nbIds = append(nbIds, v)
108         }
109
110         return nbIds
111 }