Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +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 writer |
| 19 | |
| 20 | import ( |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 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" |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 24 | "github.com/golang/protobuf/proto" |
| 25 | ) |
| 26 | |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 27 | type rNibWriterInstance struct { |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 28 | sdl rnibcommon.ISdlInstance |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /* |
| 32 | RNibWriter interface allows saving data to the redis BD |
| 33 | */ |
| 34 | type RNibWriter interface { |
Mohamed Abukar | e17b3da | 2019-09-09 16:40:44 +0300 | [diff] [blame] | 35 | SaveNodeb(nbIdentity *rnibentities.NbIdentity, nb *rnibentities.NodebInfo) error |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /* |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 39 | GetRNibWriter returns reference to RNibWriter |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 40 | */ |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 41 | |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 42 | func GetRNibWriter(sdl rnibcommon.ISdlInstance) RNibWriter { |
| 43 | return &rNibWriterInstance{sdl: sdl} |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
| 47 | SaveNodeb saves nodeB entity data in the redis DB according to the specified data model |
| 48 | */ |
Mohamed Abukar | e17b3da | 2019-09-09 16:40:44 +0300 | [diff] [blame] | 49 | func (w *rNibWriterInstance) SaveNodeb(nbIdentity *rnibentities.NbIdentity, entity *rnibentities.NodebInfo) error { |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 50 | isNotEmptyIdentity := isNotEmpty(nbIdentity) |
| 51 | |
| 52 | if isNotEmptyIdentity && entity.GetNodeType() == rnibentities.Node_UNKNOWN { |
Mohamed Abukar | e17b3da | 2019-09-09 16:40:44 +0300 | [diff] [blame] | 53 | return rnibcommon.NewValidationError(fmt.Sprintf("#rNibWriter.saveNodeB - Unknown responding node type, entity: %v", entity)) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 54 | } |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 55 | data, err := proto.Marshal(entity) |
| 56 | if err != nil { |
| 57 | return rnibcommon.NewInternalError(err) |
| 58 | } |
| 59 | var pairs []interface{} |
| 60 | key, rNibErr := rnibcommon.ValidateAndBuildNodeBNameKey(nbIdentity.InventoryName) |
| 61 | if rNibErr != nil { |
| 62 | return rNibErr |
| 63 | } |
| 64 | pairs = append(pairs, key, data) |
| 65 | |
| 66 | if isNotEmptyIdentity { |
| 67 | key, rNibErr = rnibcommon.ValidateAndBuildNodeBIdKey(entity.GetNodeType().String(), nbIdentity.GlobalNbId.GetPlmnId(), nbIdentity.GlobalNbId.GetNbId()) |
| 68 | if rNibErr != nil { |
| 69 | return rNibErr |
| 70 | } |
| 71 | pairs = append(pairs, key, data) |
| 72 | } |
| 73 | |
| 74 | if entity.GetEnb() != nil { |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 75 | pairs, rNibErr = appendEnbCells(nbIdentity.InventoryName, entity.GetEnb().GetServedCells(), pairs) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 76 | if rNibErr != nil { |
| 77 | return rNibErr |
| 78 | } |
| 79 | } |
| 80 | if entity.GetGnb() != nil { |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 81 | pairs, rNibErr = appendGnbCells(nbIdentity.InventoryName, entity.GetGnb().GetServedNrCells(), pairs) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 82 | if rNibErr != nil { |
| 83 | return rNibErr |
| 84 | } |
| 85 | } |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 86 | err = w.sdl.Set(pairs) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 87 | if err != nil { |
| 88 | return rnibcommon.NewInternalError(err) |
| 89 | } |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 90 | |
| 91 | ranNameIdentity := &rnibentities.NbIdentity{InventoryName: nbIdentity.InventoryName} |
| 92 | |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 93 | if isNotEmptyIdentity { |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 94 | nbIdData, err := proto.Marshal(ranNameIdentity) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 95 | if err != nil { |
| 96 | return rnibcommon.NewInternalError(err) |
| 97 | } |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 98 | err = w.sdl.RemoveMember(rnibentities.Node_UNKNOWN.String(), nbIdData) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 99 | if err != nil { |
| 100 | return rnibcommon.NewInternalError(err) |
| 101 | } |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 102 | } else { |
| 103 | nbIdentity = ranNameIdentity |
| 104 | } |
| 105 | |
| 106 | nbIdData, err := proto.Marshal(nbIdentity) |
| 107 | if err != nil { |
| 108 | return rnibcommon.NewInternalError(err) |
| 109 | } |
| 110 | err = w.sdl.AddMember(entity.GetNodeType().String(), nbIdData) |
| 111 | if err != nil { |
| 112 | return rnibcommon.NewInternalError(err) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 113 | } |
| 114 | return nil |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | Close closes writer's pool |
| 119 | */ |
| 120 | func CloseWriter() { |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 121 | } |
| 122 | |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 123 | func appendEnbCells(inventoryName string, cells []*rnibentities.ServedCellInfo, pairs []interface{}) ([]interface{}, error) { |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 124 | for _, cell := range cells { |
| 125 | cellEntity := rnibentities.Cell{Type: rnibentities.Cell_LTE_CELL, Cell: &rnibentities.Cell_ServedCellInfo{ServedCellInfo: cell}} |
| 126 | cellData, err := proto.Marshal(&cellEntity) |
| 127 | if err != nil { |
| 128 | return pairs, rnibcommon.NewInternalError(err) |
| 129 | } |
| 130 | key, rNibErr := rnibcommon.ValidateAndBuildCellIdKey(cell.GetCellId()) |
| 131 | if rNibErr != nil { |
| 132 | return pairs, rNibErr |
| 133 | } |
| 134 | pairs = append(pairs, key, cellData) |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 135 | key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetPci()) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 136 | if rNibErr != nil { |
| 137 | return pairs, rNibErr |
| 138 | } |
| 139 | pairs = append(pairs, key, cellData) |
| 140 | } |
| 141 | return pairs, nil |
| 142 | } |
| 143 | |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 144 | func appendGnbCells(inventoryName string, cells []*rnibentities.ServedNRCell, pairs []interface{}) ([]interface{}, error) { |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 145 | for _, cell := range cells { |
| 146 | cellEntity := rnibentities.Cell{Type: rnibentities.Cell_NR_CELL, Cell: &rnibentities.Cell_ServedNrCell{ServedNrCell: cell}} |
| 147 | cellData, err := proto.Marshal(&cellEntity) |
| 148 | if err != nil { |
| 149 | return pairs, rnibcommon.NewInternalError(err) |
| 150 | } |
| 151 | key, rNibErr := rnibcommon.ValidateAndBuildNrCellIdKey(cell.GetServedNrCellInformation().GetCellId()) |
| 152 | if rNibErr != nil { |
| 153 | return pairs, rNibErr |
| 154 | } |
| 155 | pairs = append(pairs, key, cellData) |
Mohamed Abukar | fbf8bcf | 2020-05-14 18:33:54 +0300 | [diff] [blame] | 156 | key, rNibErr = rnibcommon.ValidateAndBuildCellNamePciKey(inventoryName, cell.GetServedNrCellInformation().GetNrPci()) |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 157 | if rNibErr != nil { |
| 158 | return pairs, rNibErr |
| 159 | } |
| 160 | pairs = append(pairs, key, cellData) |
| 161 | } |
| 162 | return pairs, nil |
| 163 | } |
| 164 | |
| 165 | func isNotEmpty(nbIdentity *rnibentities.NbIdentity) bool { |
| 166 | return nbIdentity.GlobalNbId != nil && nbIdentity.GlobalNbId.PlmnId != "" && nbIdentity.GlobalNbId.NbId != "" |
| 167 | } |