push code back with legal issues fix
[ric-plt/e2mgr.git] / E2Manager / rNibWriter / rNibWriter.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
18 package rNibWriter
19
20 import (
21         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
22         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
23         "errors"
24         "fmt"
25         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
26         "github.com/golang/protobuf/proto"
27 )
28
29
30
31 var writerPool *common.Pool
32
33 type rNibWriterInstance struct {
34         sdl       *common.ISdlInstance
35         namespace string
36 }
37 /*
38 RNibWriter interface allows saving data to the redis DB
39  */
40 type RNibWriter interface {
41         SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) common.IRNibError
42         SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError
43 }
44 /*
45 Init initializes the infrastructure required for the RNibWriter instance
46  */
47 func Init(namespace string, poolSize int) {
48         initPool(poolSize,
49                 func() interface{} {
50                         var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
51                         return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
52                 },
53                 func(obj interface{}) {
54                         (*obj.(*rNibWriterInstance).sdl).Close()
55                 })
56 }
57 /*
58 InitPool initializes the writer's instances pool
59  */
60 func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
61         writerPool = common.NewPool(poolSize, newObj, destroyObj)
62 }
63 /*
64 GetRNibWriter returns RNibWriter instance from the pool
65  */
66 func GetRNibWriter() RNibWriter {
67         return writerPool.Get().(RNibWriter)
68 }
69 /*
70 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
71  */
72 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) common.IRNibError {
73
74         isNotEmptyIdentity := isNotEmpty(nbIdentity)
75
76         if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN{
77                 return common.NewValidationError(errors.New( fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity)))
78         }
79         defer writerPool.Put(w)
80         data, err := proto.Marshal(entity)
81         if err != nil {
82                 return common.NewInternalError(err)
83         }
84         var pairs []interface{}
85         key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
86         if rNibErr != nil{
87                 return rNibErr
88         }
89         pairs = append(pairs, key, data)
90
91         if isNotEmptyIdentity {
92                 key, rNibErr = common.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
93                 if rNibErr != nil{
94                         return rNibErr
95                 }
96                 pairs = append(pairs, key, data)
97         }
98
99         if entity.GetEnb() != nil {
100                 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
101                 if rNibErr != nil{
102                         return rNibErr
103                 }
104         }
105         if entity.GetGnb() != nil {
106                 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
107                 if rNibErr != nil{
108                         return rNibErr
109                 }
110         }
111         err = (*w.sdl).Set(pairs)
112         if err != nil {
113                 return common.NewInternalError(err)
114         }
115         if isNotEmptyIdentity {
116                 nbIdData, err := proto.Marshal(nbIdentity)
117                 if err != nil {
118                         return common.NewInternalError(err)
119                 }
120                 err = (*w.sdl).AddMember(entity.GetNodeType().String(), nbIdData)
121                 if err != nil {
122                         return common.NewInternalError(err)
123                 }
124         }
125         return nil
126 }
127
128 /*
129 SaveRanLoadInformation stores ran load information for the provided ran
130 */
131 func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError {
132
133         defer writerPool.Put(w)
134
135         key, rnibErr:= common.ValidateAndBuildRanLoadInformationKey(inventoryName)
136
137         if rnibErr != nil {
138                 return rnibErr
139         }
140
141         data, err := proto.Marshal(ranLoadInformation)
142
143         if err != nil {
144                 return common.NewInternalError(err)
145         }
146
147         var pairs []interface{}
148         pairs = append(pairs, key, data)
149
150         err = (*w.sdl).Set(pairs)
151
152         if err != nil {
153                 return common.NewInternalError(err)
154         }
155
156         return nil
157 }
158
159 /*
160 Close closes writer's pool
161  */
162 func Close(){
163         writerPool.Close()
164 }
165
166 func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, common.IRNibError) {
167         for _, cell := range cells {
168                 cellEntity := entities.Cell{Type:entities.Cell_LTE_CELL, Cell:&entities.Cell_ServedCellInfo{ServedCellInfo:cell}}
169                 cellData, err := proto.Marshal(&cellEntity)
170                 if err != nil {
171                         return pairs, common.NewInternalError(err)
172                 }
173                 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
174                 if rNibErr != nil{
175                         return pairs, rNibErr
176                 }
177                 pairs = append(pairs, key, cellData)
178                 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
179                 if rNibErr != nil{
180                         return pairs, rNibErr
181                 }
182                 pairs = append(pairs, key, cellData)
183         }
184         return pairs, nil
185 }
186
187 func appendGnbCells(nbIdentity *entities.NbIdentity, cells  []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, common.IRNibError) {
188         for _, cell := range cells {
189                 cellEntity := entities.Cell{Type:entities.Cell_NR_CELL, Cell:&entities.Cell_ServedNrCell{ServedNrCell:cell}}
190                 cellData, err := proto.Marshal(&cellEntity)
191                 if err != nil {
192                         return pairs, common.NewInternalError(err)
193                 }
194                 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
195                 if rNibErr != nil{
196                         return pairs, rNibErr
197                 }
198                 pairs = append(pairs, key, cellData)
199                 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
200                 if rNibErr != nil{
201                         return pairs, rNibErr
202                 }
203                 pairs = append(pairs, key, cellData)
204         }
205         return pairs, nil
206 }
207
208 func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
209         return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""
210 }