ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 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 ( |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 21 | "errors" |
| 22 | "fmt" |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 23 | "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" |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 25 | "gerrit.o-ran-sc.org/r/ric-plt/sdlgo" |
| 26 | "github.com/golang/protobuf/proto" |
| 27 | ) |
| 28 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 29 | var writerPool *common.Pool |
| 30 | |
| 31 | type rNibWriterInstance struct { |
| 32 | sdl *common.ISdlInstance |
| 33 | namespace string |
| 34 | } |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 35 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 36 | /* |
| 37 | RNibWriter interface allows saving data to the redis DB |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 38 | */ |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 39 | type RNibWriter interface { |
| 40 | SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) common.IRNibError |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 41 | UpdateNodebInfo(nodebInfo *entities.NodebInfo) common.IRNibError |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 42 | SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError |
| 43 | } |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 44 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 45 | /* |
| 46 | Init initializes the infrastructure required for the RNibWriter instance |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 47 | */ |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 48 | func 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 | } |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 58 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 59 | /* |
| 60 | InitPool initializes the writer's instances pool |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 61 | */ |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 62 | func initPool(poolSize int, newObj func() interface{}, destroyObj func(interface{})) { |
| 63 | writerPool = common.NewPool(poolSize, newObj, destroyObj) |
| 64 | } |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 65 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 66 | /* |
| 67 | GetRNibWriter returns RNibWriter instance from the pool |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 68 | */ |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 69 | func GetRNibWriter() RNibWriter { |
| 70 | return writerPool.Get().(RNibWriter) |
| 71 | } |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 72 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 73 | /* |
| 74 | SaveNodeb saves nodeB entity data in the redis DB according to the specified data model |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 75 | */ |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 76 | func (w *rNibWriterInstance) SaveNodeb(nbIdentity *entities.NbIdentity, entity *entities.NodebInfo) common.IRNibError { |
| 77 | |
| 78 | isNotEmptyIdentity := isNotEmpty(nbIdentity) |
| 79 | |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 80 | if isNotEmptyIdentity && entity.GetNodeType() == entities.Node_UNKNOWN { |
| 81 | return common.NewValidationError(errors.New(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 82 | } |
| 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) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 90 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 91 | 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()) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 97 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 98 | 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) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 105 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 106 | return rNibErr |
| 107 | } |
| 108 | } |
| 109 | if entity.GetGnb() != nil { |
| 110 | pairs, rNibErr = appendGnbCells(nbIdentity, entity.GetGnb().GetServedNrCells(), pairs) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 111 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 112 | return rNibErr |
| 113 | } |
| 114 | } |
| 115 | err = (*w.sdl).Set(pairs) |
| 116 | if err != nil { |
| 117 | return common.NewInternalError(err) |
| 118 | } |
ns019t | 57c3fae | 2019-08-12 20:08:49 +0300 | [diff] [blame] | 119 | |
| 120 | ranNameIdentity := &entities.NbIdentity{InventoryName: nbIdentity.InventoryName} |
| 121 | |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 122 | if isNotEmptyIdentity { |
ns019t | 57c3fae | 2019-08-12 20:08:49 +0300 | [diff] [blame] | 123 | nbIdData, err := proto.Marshal(ranNameIdentity) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 124 | if err != nil { |
| 125 | return common.NewInternalError(err) |
| 126 | } |
ns019t | 57c3fae | 2019-08-12 20:08:49 +0300 | [diff] [blame] | 127 | err = (*w.sdl).RemoveMember(entities.Node_UNKNOWN.String(), nbIdData) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 128 | if err != nil { |
| 129 | return common.NewInternalError(err) |
| 130 | } |
ns019t | 57c3fae | 2019-08-12 20:08:49 +0300 | [diff] [blame] | 131 | } 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) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | /* |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 147 | UpdateNodebInfo... |
| 148 | */ |
| 149 | func (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 | |
is005q | 2647319 | 2019-08-28 17:51:37 +0300 | [diff] [blame] | 159 | nodebIdKey, buildNodebIdKeyError := common.ValidateAndBuildNodeBIdKey(nodebInfo.GetNodeType().String(), nodebInfo.GlobalNbId.GetPlmnId(), nodebInfo.GlobalNbId.GetNbId()) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 160 | |
| 161 | data, err := proto.Marshal(nodebInfo) |
| 162 | |
| 163 | if err != nil { |
| 164 | return common.NewInternalError(err) |
| 165 | } |
| 166 | |
| 167 | var pairs []interface{} |
is005q | 2647319 | 2019-08-28 17:51:37 +0300 | [diff] [blame] | 168 | pairs = append(pairs, nodebNameKey, data) |
| 169 | |
| 170 | if buildNodebIdKeyError == nil { |
| 171 | pairs = append(pairs, nodebIdKey, data) |
| 172 | } |
| 173 | |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 174 | err = (*w.sdl).Set(pairs) |
| 175 | |
| 176 | if err != nil { |
| 177 | return common.NewInternalError(err) |
| 178 | } |
| 179 | |
| 180 | return nil |
| 181 | } |
| 182 | |
| 183 | /* |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 184 | SaveRanLoadInformation stores ran load information for the provided ran |
| 185 | */ |
| 186 | func (w *rNibWriterInstance) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) common.IRNibError { |
| 187 | |
| 188 | defer writerPool.Put(w) |
| 189 | |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 190 | key, rnibErr := common.ValidateAndBuildRanLoadInformationKey(inventoryName) |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 191 | |
| 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 | /* |
| 215 | Close closes writer's pool |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 216 | */ |
| 217 | func Close() { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 218 | writerPool.Close() |
| 219 | } |
| 220 | |
| 221 | func appendEnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedCellInfo, pairs []interface{}) ([]interface{}, common.IRNibError) { |
| 222 | for _, cell := range cells { |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 223 | cellEntity := entities.Cell{Type: entities.Cell_LTE_CELL, Cell: &entities.Cell_ServedCellInfo{ServedCellInfo: cell}} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 224 | cellData, err := proto.Marshal(&cellEntity) |
| 225 | if err != nil { |
| 226 | return pairs, common.NewInternalError(err) |
| 227 | } |
| 228 | key, rNibErr := common.ValidateAndBuildCellIdKey(cell.GetCellId()) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 229 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 230 | return pairs, rNibErr |
| 231 | } |
| 232 | pairs = append(pairs, key, cellData) |
| 233 | key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetPci()) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 234 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 235 | return pairs, rNibErr |
| 236 | } |
| 237 | pairs = append(pairs, key, cellData) |
| 238 | } |
| 239 | return pairs, nil |
| 240 | } |
| 241 | |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 242 | func appendGnbCells(nbIdentity *entities.NbIdentity, cells []*entities.ServedNRCell, pairs []interface{}) ([]interface{}, common.IRNibError) { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 243 | for _, cell := range cells { |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 244 | cellEntity := entities.Cell{Type: entities.Cell_NR_CELL, Cell: &entities.Cell_ServedNrCell{ServedNrCell: cell}} |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 245 | cellData, err := proto.Marshal(&cellEntity) |
| 246 | if err != nil { |
| 247 | return pairs, common.NewInternalError(err) |
| 248 | } |
| 249 | key, rNibErr := common.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId()) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 250 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 251 | return pairs, rNibErr |
| 252 | } |
| 253 | pairs = append(pairs, key, cellData) |
| 254 | key, rNibErr = common.ValidateAndBuildCellNamePciKey(nbIdentity.InventoryName, cell.GetServedNrCellInformation().GetNrPci()) |
is005q | 19e72a5 | 2019-08-26 17:56:18 +0300 | [diff] [blame] | 255 | if rNibErr != nil { |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 256 | return pairs, rNibErr |
| 257 | } |
| 258 | pairs = append(pairs, key, cellData) |
| 259 | } |
| 260 | return pairs, nil |
| 261 | } |
| 262 | |
| 263 | func isNotEmpty(nbIdentity *entities.NbIdentity) bool { |
| 264 | return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != "" |
ns019t | 57c3fae | 2019-08-12 20:08:49 +0300 | [diff] [blame] | 265 | } |