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