blob: 72f9eb8c9fa7594cb863cd151cf5b54a566907f6 [file] [log] [blame]
ss412g07ef76d2019-08-12 17:26:40 +03001//
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
18package rNibWriter
19
20import (
ss412g07ef76d2019-08-12 17:26:40 +030021 "errors"
22 "fmt"
is005q19e72a52019-08-26 17:56:18 +030023 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
24 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
ss412g07ef76d2019-08-12 17:26:40 +030025 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
26 "github.com/golang/protobuf/proto"
27)
28
ss412g07ef76d2019-08-12 17:26:40 +030029var writerPool *common.Pool
30
31type rNibWriterInstance struct {
32 sdl *common.ISdlInstance
33 namespace string
34}
is005q19e72a52019-08-26 17:56:18 +030035
ss412g07ef76d2019-08-12 17:26:40 +030036/*
37RNibWriter interface allows saving data to the redis DB
is005q19e72a52019-08-26 17:56:18 +030038*/
ss412g07ef76d2019-08-12 17:26:40 +030039type RNibWriter interface {
40 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) common.IRNibError
is005q19e72a52019-08-26 17:56:18 +030041 UpdateNodebInfo(nodebInfo *entities.NodebInfo) common.IRNibError
ss412g07ef76d2019-08-12 17:26:40 +030042 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError
43}
is005q19e72a52019-08-26 17:56:18 +030044
ss412g07ef76d2019-08-12 17:26:40 +030045/*
46Init initializes the infrastructure required for the RNibWriter instance
is005q19e72a52019-08-26 17:56:18 +030047*/
ss412g07ef76d2019-08-12 17:26:40 +030048func Init(namespace string, poolSize int) {
49 initPool(poolSize,
50 func() interface{} {
51 var sdlI common.ISdlInstance = sdlgo.NewSdlInstance(namespace, sdlgo.NewDatabase())
52 return &rNibWriterInstance{sdl: &sdlI, namespace: namespace}
53 },
54 func(obj interface{}) {
55 (*obj.(*rNibWriterInstance).sdl).Close()
56 })
57}
is005q19e72a52019-08-26 17:56:18 +030058
ss412g07ef76d2019-08-12 17:26:40 +030059/*
60InitPool initializes the writer's instances pool
is005q19e72a52019-08-26 17:56:18 +030061*/
ss412g07ef76d2019-08-12 17:26:40 +030062func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) {
63 writerPool = common.NewPool(poolSize, newObj, destroyObj)
64}
is005q19e72a52019-08-26 17:56:18 +030065
ss412g07ef76d2019-08-12 17:26:40 +030066/*
67GetRNibWriter returns RNibWriter instance from the pool
is005q19e72a52019-08-26 17:56:18 +030068*/
ss412g07ef76d2019-08-12 17:26:40 +030069func GetRNibWriter() RNibWriter {
70 return writerPool.Get().(RNibWriter)
71}
is005q19e72a52019-08-26 17:56:18 +030072
ss412g07ef76d2019-08-12 17:26:40 +030073/*
74SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
is005q19e72a52019-08-26 17:56:18 +030075*/
ss412g07ef76d2019-08-12 17:26:40 +030076func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) common.IRNibError {
77
78 isNotEmptyIdentity := isNotEmpty(nbIdentity)
79
is005q19e72a52019-08-26 17:56:18 +030080 if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN {
81 return common.NewValidationError(errors.New(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity)))
ss412g07ef76d2019-08-12 17:26:40 +030082 }
83 defer writerPool.Put(w)
84 data, err := proto.Marshal(entity)
85 if err != nil {
86 return common.NewInternalError(err)
87 }
88 var pairs []interface{}
89 key, rNibErr := common.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
is005q19e72a52019-08-26 17:56:18 +030090 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +030091 return rNibErr
92 }
93 pairs = append(pairs, key, data)
94
95 if isNotEmptyIdentity {
96 key, rNibErr = common.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
is005q19e72a52019-08-26 17:56:18 +030097 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +030098 return rNibErr
99 }
100 pairs = append(pairs, key, data)
101 }
102
103 if entity.GetEnb() != nil {
104 pairs, rNibErr = appendEnbCells(nbIdentity, entity.GetEnb().GetServedCells(), pairs)
is005q19e72a52019-08-26 17:56:18 +0300105 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +0300106 return rNibErr
107 }
108 }
109 if entity.GetGnb() != nil {
110 pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs)
is005q19e72a52019-08-26 17:56:18 +0300111 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +0300112 return rNibErr
113 }
114 }
115 err = (*w.sdl).Set(pairs)
116 if err != nil {
117 return common.NewInternalError(err)
118 }
ns019t57c3fae2019-08-12 20:08:49 +0300119
120 ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName}
121
is005q19e72a52019-08-26 17:56:18 +0300122 if isNotEmptyIdentity {
ns019t57c3fae2019-08-12 20:08:49 +0300123 nbIdData, err := proto.Marshal(ranNameIdentity)
ss412g07ef76d2019-08-12 17:26:40 +0300124 if err != nil {
125 return common.NewInternalError(err)
126 }
ns019t57c3fae2019-08-12 20:08:49 +0300127 err = (*w.sdl).RemoveMember(entities.Node_UNKNOWN.String(), nbIdData)
ss412g07ef76d2019-08-12 17:26:40 +0300128 if err != nil {
129 return common.NewInternalError(err)
130 }
ns019t57c3fae2019-08-12 20:08:49 +0300131 } else {
132 nbIdentity = ranNameIdentity
133 }
134
135 nbIdData, err := proto.Marshal(nbIdentity)
136 if err != nil {
137 return common.NewInternalError(err)
138 }
139 err = (*w.sdl).AddMember(entity.GetNodeType().String(), nbIdData)
140 if err != nil {
141 return common.NewInternalError(err)
ss412g07ef76d2019-08-12 17:26:40 +0300142 }
143 return nil
144}
145
146/*
is005q19e72a52019-08-26 17:56:18 +0300147UpdateNodebInfo...
148*/
149func (w *rNibWriterInstance) UpdateNodebInfo(nodebInfo *entities.NodebInfo) common.IRNibError {
150
151 defer writerPool.Put(w)
152
153 nodebNameKey, rNibErr := common.ValidateAndBuildNodeBNameKey(nodebInfo.GetRanName())
154
155 if rNibErr != nil {
156 return rNibErr
157 }
158
is005q26473192019-08-28 17:51:37 +0300159 nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId())
is005q19e72a52019-08-26 17:56:18 +0300160
161 data, err := proto.Marshal(nodebInfo)
162
163 if err != nil {
164 return common.NewInternalError(err)
165 }
166
167 var pairs []interface{}
is005q26473192019-08-28 17:51:37 +0300168 pairs = append(pairs, nodebNameKey, data)
169
170 if buildNodebIdKeyError == nil {
171 pairs = append(pairs, nodebIdKey, data)
172 }
173
is005q19e72a52019-08-26 17:56:18 +0300174 err = (*w.sdl).Set(pairs)
175
176 if err != nil {
177 return common.NewInternalError(err)
178 }
179
180 return nil
181}
182
183/*
ss412g07ef76d2019-08-12 17:26:40 +0300184SaveRanLoadInformation stores ran load information for the provided ran
185*/
186func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError {
187
188 defer writerPool.Put(w)
189
is005q19e72a52019-08-26 17:56:18 +0300190 key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName)
ss412g07ef76d2019-08-12 17:26:40 +0300191
192 if rnibErr != nil {
193 return rnibErr
194 }
195
196 data, err := proto.Marshal(ranLoadInformation)
197
198 if err != nil {
199 return common.NewInternalError(err)
200 }
201
202 var pairs []interface{}
203 pairs = append(pairs, key, data)
204
205 err = (*w.sdl).Set(pairs)
206
207 if err != nil {
208 return common.NewInternalError(err)
209 }
210
211 return nil
212}
213
214/*
215Close closes writer's pool
is005q19e72a52019-08-26 17:56:18 +0300216*/
217func Close() {
ss412g07ef76d2019-08-12 17:26:40 +0300218 writerPool.Close()
219}
220
221func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, common.IRNibError) {
222 for _, cell := range cells {
is005q19e72a52019-08-26 17:56:18 +0300223 cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}}
ss412g07ef76d2019-08-12 17:26:40 +0300224 cellData, err := proto.Marshal(&cellEntity)
225 if err != nil {
226 return pairs, common.NewInternalError(err)
227 }
228 key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId())
is005q19e72a52019-08-26 17:56:18 +0300229 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +0300230 return pairs, rNibErr
231 }
232 pairs = append(pairs, key, cellData)
233 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci())
is005q19e72a52019-08-26 17:56:18 +0300234 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +0300235 return pairs, rNibErr
236 }
237 pairs = append(pairs, key, cellData)
238 }
239 return pairs, nil
240}
241
is005q19e72a52019-08-26 17:56:18 +0300242func appendGnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, common.IRNibError) {
ss412g07ef76d2019-08-12 17:26:40 +0300243 for _, cell := range cells {
is005q19e72a52019-08-26 17:56:18 +0300244 cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}}
ss412g07ef76d2019-08-12 17:26:40 +0300245 cellData, err := proto.Marshal(&cellEntity)
246 if err != nil {
247 return pairs, common.NewInternalError(err)
248 }
249 key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
is005q19e72a52019-08-26 17:56:18 +0300250 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +0300251 return pairs, rNibErr
252 }
253 pairs = append(pairs, key, cellData)
254 key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci())
is005q19e72a52019-08-26 17:56:18 +0300255 if rNibErr != nil {
ss412g07ef76d2019-08-12 17:26:40 +0300256 return pairs, rNibErr
257 }
258 pairs = append(pairs, key, cellData)
259 }
260 return pairs, nil
261}
262
263func isNotEmpty(nbIdentity *entities.NbIdentity) bool {
264 return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""
ns019t57c3fae2019-08-12 20:08:49 +0300265}