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