Integrate RNIB with SDL SyncStorage to xapp-frame
[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         "github.com/golang/protobuf/proto"
25 )
26
27 type rNibWriterInstance struct {
28         sdl        rnibcommon.ISdlInstance //Deprecated: Will be removed in a future release and replaced by sdlStorage
29         sdlStorage rnibcommon.ISdlSyncStorage
30         ns         string
31 }
32
33 /*
34 RNibWriter interface allows saving data to the redis BD
35 */
36 type RNibWriter interface {
37         SaveNodeb(nbIdentity *rnibentities.NbIdentity, nb *rnibentities.NodebInfo) error
38 }
39
40 /*
41 GetNewRNibWriter returns reference to RNibWriter
42 */
43 func GetNewRNibWriter(sdlStorage rnibcommon.ISdlSyncStorage) RNibWriter {
44         return &rNibWriterInstance{
45                 sdl:        nil,
46                 sdlStorage: sdlStorage,
47                 ns:         rnibcommon.GetRNibNamespace(),
48         }
49 }
50
51 //GetRNibWriter returns reference to RNibWriter
52 //Deprecated: Will be removed in a future release, please use GetNewRNibWriter instead.
53 func GetRNibWriter(sdl rnibcommon.ISdlInstance) RNibWriter {
54         return &rNibWriterInstance{
55                 sdl:        sdl,
56                 sdlStorage: nil,
57                 ns:         "",
58         }
59 }
60
61 /*
62 SaveNodeb saves nodeB entity data in the redis DB according to the specified data model
63 */
64 func (w *rNibWriterInstance) SaveNodeb(nbIdentity *rnibentities.NbIdentity, entity *rnibentities.NodebInfo) error {
65         isNotEmptyIdentity := isNotEmpty(nbIdentity)
66
67         if isNotEmptyIdentity && entity.GetNodeType() == rnibentities.Node_UNKNOWN {
68                 return rnibcommon.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity))
69         }
70         data, err := proto.Marshal(entity)
71         if err != nil {
72                 return rnibcommon.NewInternalError(err)
73         }
74         var pairs []interface{}
75         key, rNibErr := rnibcommon.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName)
76         if rNibErr != nil {
77                 return rNibErr
78         }
79         pairs = append(pairs, key, data)
80
81         if isNotEmptyIdentity {
82                 key, rNibErr = rnibcommon.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId())
83                 if rNibErr != nil {
84                         return rNibErr
85                 }
86                 pairs = append(pairs, key, data)
87         }
88
89         if entity.GetEnb() != nil {
90                 pairs, rNibErr = appendEnbCells(nbIdentity.InventoryName, entity.GetEnb().GetServedCells(), pairs)
91                 if rNibErr != nil {
92                         return rNibErr
93                 }
94         }
95         if entity.GetGnb() != nil {
96                 pairs, rNibErr = appendGnbCells(nbIdentity.InventoryName, entity.GetGnb().GetServedNrCells(), pairs)
97                 if rNibErr != nil {
98                         return rNibErr
99                 }
100         }
101         if w.sdlStorage != nil {
102                 err = w.sdlStorage.Set(w.ns, pairs)
103         } else {
104                 err = w.sdl.Set(pairs)
105         }
106         if err != nil {
107                 return rnibcommon.NewInternalError(err)
108         }
109
110         ranNameIdentity := &rnibentities.NbIdentity{InventoryName: nbIdentity.InventoryName}
111
112         if isNotEmptyIdentity {
113                 nbIdData, err := proto.Marshal(ranNameIdentity)
114                 if err != nil {
115                         return rnibcommon.NewInternalError(err)
116                 }
117                 if w.sdlStorage != nil {
118                         err = w.sdlStorage.RemoveMember(w.ns, rnibentities.Node_UNKNOWN.String(), nbIdData)
119                 } else {
120                         err = w.sdl.RemoveMember(rnibentities.Node_UNKNOWN.String(), nbIdData)
121                 }
122                 if err != nil {
123                         return rnibcommon.NewInternalError(err)
124                 }
125         } else {
126                 nbIdentity = ranNameIdentity
127         }
128
129         nbIdData, err := proto.Marshal(nbIdentity)
130         if err != nil {
131                 return rnibcommon.NewInternalError(err)
132         }
133         if w.sdlStorage != nil {
134                 err = w.sdlStorage.AddMember(w.ns, entity.GetNodeType().String(), nbIdData)
135         } else {
136                 err = w.sdl.AddMember(entity.GetNodeType().String(), nbIdData)
137         }
138         if err != nil {
139                 return rnibcommon.NewInternalError(err)
140         }
141         return nil
142 }
143
144 /*
145 Close closes writer's pool
146 */
147 func CloseWriter() {
148 }
149
150 func appendEnbCells(inventoryName string, cells []*rnibentities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) {
151         for _, cell := range cells {
152                 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_LTE_CELL, Cell: &rnibentities.Cell_ServedCellInfo{ServedCellInfo: cell}}
153                 cellData, err := proto.Marshal(&cellEntity)
154                 if err != nil {
155                         return pairs, rnibcommon.NewInternalError(err)
156                 }
157                 key, rNibErr := rnibcommon.ValidateAndBuildCellIdKey(cell.GetCellId())
158                 if rNibErr != nil {
159                         return pairs, rNibErr
160                 }
161                 pairs = append(pairs, key, cellData)
162                 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci())
163                 if rNibErr != nil {
164                         return pairs, rNibErr
165                 }
166                 pairs = append(pairs, key, cellData)
167         }
168         return pairs, nil
169 }
170
171 func appendGnbCells(inventoryName string, cells []*rnibentities.ServedNRCell, pairs []interface{}) ([]interface{}, error) {
172         for _, cell := range cells {
173                 cellEntity := rnibentities.Cell{Type: rnibentities.Cell_NR_CELL, Cell: &rnibentities.Cell_ServedNrCell{ServedNrCell: cell}}
174                 cellData, err := proto.Marshal(&cellEntity)
175                 if err != nil {
176                         return pairs, rnibcommon.NewInternalError(err)
177                 }
178                 key, rNibErr := rnibcommon.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId())
179                 if rNibErr != nil {
180                         return pairs, rNibErr
181                 }
182                 pairs = append(pairs, key, cellData)
183                 key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci())
184                 if rNibErr != nil {
185                         return pairs, rNibErr
186                 }
187                 pairs = append(pairs, key, cellData)
188         }
189         return pairs, nil
190 }
191
192 func isNotEmpty(nbIdentity *rnibentities.NbIdentity) bool {
193         return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != ""
194 }