ss412g | de19068 | 2019-10-24 09:29:26 +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. |
nm755n | 15d3982 | 2019-11-28 16:56:00 +0000 | [diff] [blame] | 16 | |
| 17 | // This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | // platform project (RICP). |
| 19 | |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 20 | package services |
| 21 | |
| 22 | import ( |
| 23 | "e2mgr/configuration" |
| 24 | "e2mgr/logger" |
| 25 | "e2mgr/rNibWriter" |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 26 | "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common" |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities" |
| 28 | "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader" |
| 29 | "net" |
| 30 | "time" |
| 31 | ) |
| 32 | |
| 33 | type RNibDataService interface { |
| 34 | SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error |
| 35 | UpdateNodebInfo(nodebInfo *entities.NodebInfo) error |
| 36 | SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error |
| 37 | GetNodeb(ranName string) (*entities.NodebInfo, error) |
| 38 | GetListNodebIds() ([]*entities.NbIdentity, error) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 39 | PingRnib() bool |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 40 | GetE2TInstance(address string) (*entities.E2TInstance, error) |
| 41 | GetE2TInstances(addresses []string) ([]*entities.E2TInstance, error) |
| 42 | GetE2TAddresses() ([]string, error) |
| 43 | SaveE2TInstance(e2tInstance *entities.E2TInstance) error |
| 44 | SaveE2TAddresses(addresses []string) error |
| 45 | GetE2TInstanceNoLogs(address string) (*entities.E2TInstance, error) |
| 46 | GetE2TInstancesNoLogs(addresses []string) ([]*entities.E2TInstance, error) |
| 47 | SaveE2TInstanceNoLogs(e2tInstance *entities.E2TInstance) error |
| 48 | GetE2TAddressesNoLogs() ([]string, error) |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 49 | RemoveE2TInstance(e2tAddress string) error |
idanshal | 08bbf91 | 2020-04-20 17:11:33 +0300 | [diff] [blame] | 50 | UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error |
idanshal | d916e2a | 2020-04-22 15:56:43 +0300 | [diff] [blame] | 51 | RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 52 | GetGeneralConfiguration() (*entities.GeneralConfiguration, error) |
| 53 | UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error |
| 54 | SaveGeneralConfiguration(config *entities.GeneralConfiguration) error |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame^] | 55 | RemoveEnb(nodebInfo *entities.NodebInfo) error |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | type rNibDataService struct { |
Idan Shalom | 14eb513 | 2020-07-06 09:41:46 +0000 | [diff] [blame] | 59 | logger *logger.Logger |
| 60 | rnibReader reader.RNibReader |
| 61 | rnibWriter rNibWriter.RNibWriter |
| 62 | maxAttempts int |
| 63 | retryInterval time.Duration |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 64 | } |
| 65 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 66 | func NewRnibDataService(logger *logger.Logger, config *configuration.Configuration, rnibReader reader.RNibReader, rnibWriter rNibWriter.RNibWriter) *rNibDataService { |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 67 | return &rNibDataService{ |
Idan Shalom | 14eb513 | 2020-07-06 09:41:46 +0000 | [diff] [blame] | 68 | logger: logger, |
| 69 | rnibReader: rnibReader, |
| 70 | rnibWriter: rnibWriter, |
| 71 | maxAttempts: config.MaxRnibConnectionAttempts, |
| 72 | retryInterval: time.Duration(config.RnibRetryIntervalMs) * time.Millisecond, |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
idanshal | d916e2a | 2020-04-22 15:56:43 +0300 | [diff] [blame] | 76 | func (w *rNibDataService) RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error { |
| 77 | err := w.retry("RemoveServedNrCells", func() (err error) { |
| 78 | err = w.rnibWriter.RemoveServedNrCells(inventoryName, servedNrCells) |
| 79 | return |
| 80 | }) |
| 81 | |
| 82 | return err |
| 83 | } |
| 84 | |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame^] | 85 | func (w *rNibDataService) RemoveEnb(nodebInfo *entities.NodebInfo) error { |
| 86 | err := w.retry("RemoveEnb", func() (err error) { |
| 87 | err = w.rnibWriter.RemoveEnb(nodebInfo) |
| 88 | return |
| 89 | }) |
| 90 | |
| 91 | return err |
| 92 | } |
| 93 | |
idanshal | 08bbf91 | 2020-04-20 17:11:33 +0300 | [diff] [blame] | 94 | func (w *rNibDataService) UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error { |
| 95 | w.logger.Infof("#RnibDataService.UpdateGnbCells - nodebInfo: %s, servedNrCells: %s", nodebInfo, servedNrCells) |
| 96 | |
| 97 | err := w.retry("UpdateGnbCells", func() (err error) { |
| 98 | err = w.rnibWriter.UpdateGnbCells(nodebInfo, servedNrCells) |
| 99 | return |
| 100 | }) |
| 101 | |
| 102 | return err |
| 103 | } |
| 104 | |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 105 | func (w *rNibDataService) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error { |
| 106 | w.logger.Infof("#RnibDataService.UpdateNodebInfo - nodebInfo: %s", nodebInfo) |
| 107 | |
| 108 | err := w.retry("UpdateNodebInfo", func() (err error) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 109 | err = w.rnibWriter.UpdateNodebInfo(nodebInfo) |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 110 | return |
| 111 | }) |
| 112 | |
| 113 | return err |
| 114 | } |
| 115 | |
| 116 | func (w *rNibDataService) SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error { |
| 117 | w.logger.Infof("#RnibDataService.SaveNodeb - nbIdentity: %s, nodebInfo: %s", nbIdentity, nb) |
| 118 | |
| 119 | err := w.retry("SaveNodeb", func() (err error) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 120 | err = w.rnibWriter.SaveNodeb(nbIdentity, nb) |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 121 | return |
| 122 | }) |
| 123 | |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | func (w *rNibDataService) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error { |
| 128 | w.logger.Infof("#RnibDataService.SaveRanLoadInformation - inventoryName: %s, ranLoadInformation: %s", inventoryName, ranLoadInformation) |
| 129 | |
| 130 | err := w.retry("SaveRanLoadInformation", func() (err error) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 131 | err = w.rnibWriter.SaveRanLoadInformation(inventoryName, ranLoadInformation) |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 132 | return |
| 133 | }) |
| 134 | |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | func (w *rNibDataService) GetNodeb(ranName string) (*entities.NodebInfo, error) { |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 139 | |
| 140 | var nodeb *entities.NodebInfo = nil |
| 141 | |
| 142 | err := w.retry("GetNodeb", func() (err error) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 143 | nodeb, err = w.rnibReader.GetNodeb(ranName) |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 144 | return |
| 145 | }) |
| 146 | |
is005q | 5c95332 | 2020-01-05 16:22:47 +0200 | [diff] [blame] | 147 | if err == nil { |
Amichai | 380b7a2 | 2020-01-14 16:38:06 +0200 | [diff] [blame] | 148 | w.logger.Infof("#RnibDataService.GetNodeb - RAN name: %s, connection status: %s, associated E2T: %s", nodeb.RanName, nodeb.ConnectionStatus, nodeb.AssociatedE2TInstanceAddress) |
is005q | 5c95332 | 2020-01-05 16:22:47 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 151 | return nodeb, err |
| 152 | } |
| 153 | |
| 154 | func (w *rNibDataService) GetListNodebIds() ([]*entities.NbIdentity, error) { |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 155 | var nodeIds []*entities.NbIdentity = nil |
| 156 | |
| 157 | err := w.retry("GetListNodebIds", func() (err error) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 158 | nodeIds, err = w.rnibReader.GetListNodebIds() |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 159 | return |
| 160 | }) |
| 161 | |
is005q | 5c95332 | 2020-01-05 16:22:47 +0200 | [diff] [blame] | 162 | if err == nil { |
| 163 | w.logger.Infof("#RnibDataService.GetListNodebIds - RANs count: %d", len(nodeIds)) |
| 164 | } |
| 165 | |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 166 | return nodeIds, err |
| 167 | } |
| 168 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 169 | func (w *rNibDataService) GetE2TInstance(address string) (*entities.E2TInstance, error) { |
| 170 | var e2tInstance *entities.E2TInstance = nil |
| 171 | |
| 172 | err := w.retry("GetE2TInstance", func() (err error) { |
| 173 | e2tInstance, err = w.rnibReader.GetE2TInstance(address) |
| 174 | return |
| 175 | }) |
| 176 | |
| 177 | if err == nil { |
| 178 | w.logger.Infof("#RnibDataService.GetE2TInstance - E2T instance address: %s, state: %s, associated RANs count: %d, keep Alive ts: %d", e2tInstance.Address, e2tInstance.State, len(e2tInstance.AssociatedRanList), e2tInstance.KeepAliveTimestamp) |
| 179 | } |
| 180 | |
| 181 | return e2tInstance, err |
| 182 | } |
| 183 | |
| 184 | func (w *rNibDataService) GetE2TInstanceNoLogs(address string) (*entities.E2TInstance, error) { |
| 185 | var e2tInstance *entities.E2TInstance = nil |
| 186 | |
| 187 | err := w.retry("GetE2TInstance", func() (err error) { |
| 188 | e2tInstance, err = w.rnibReader.GetE2TInstance(address) |
| 189 | return |
| 190 | }) |
| 191 | |
| 192 | return e2tInstance, err |
| 193 | } |
| 194 | |
| 195 | func (w *rNibDataService) GetE2TInstances(addresses []string) ([]*entities.E2TInstance, error) { |
| 196 | w.logger.Infof("#RnibDataService.GetE2TInstances - addresses: %s", addresses) |
| 197 | var e2tInstances []*entities.E2TInstance = nil |
| 198 | |
| 199 | err := w.retry("GetE2TInstance", func() (err error) { |
| 200 | e2tInstances, err = w.rnibReader.GetE2TInstances(addresses) |
| 201 | return |
| 202 | }) |
| 203 | |
| 204 | return e2tInstances, err |
| 205 | } |
| 206 | |
| 207 | func (w *rNibDataService) GetE2TInstancesNoLogs(addresses []string) ([]*entities.E2TInstance, error) { |
| 208 | |
| 209 | var e2tInstances []*entities.E2TInstance = nil |
| 210 | |
| 211 | err := w.retry("GetE2TInstance", func() (err error) { |
| 212 | e2tInstances, err = w.rnibReader.GetE2TInstances(addresses) |
| 213 | return |
| 214 | }) |
| 215 | |
| 216 | return e2tInstances, err |
| 217 | } |
| 218 | |
| 219 | func (w *rNibDataService) GetE2TAddresses() ([]string, error) { |
| 220 | |
| 221 | var e2tAddresses []string = nil |
| 222 | |
| 223 | err := w.retry("GetE2TAddresses", func() (err error) { |
| 224 | e2tAddresses, err = w.rnibReader.GetE2TAddresses() |
| 225 | return |
| 226 | }) |
| 227 | |
| 228 | if err == nil { |
| 229 | w.logger.Infof("#RnibDataService.GetE2TAddresses - addresses: %s", e2tAddresses) |
| 230 | } |
| 231 | |
| 232 | return e2tAddresses, err |
| 233 | } |
| 234 | |
| 235 | func (w *rNibDataService) GetE2TAddressesNoLogs() ([]string, error) { |
| 236 | |
| 237 | var e2tAddresses []string = nil |
| 238 | |
| 239 | err := w.retry("GetE2TAddresses", func() (err error) { |
| 240 | e2tAddresses, err = w.rnibReader.GetE2TAddresses() |
| 241 | return |
| 242 | }) |
| 243 | |
| 244 | return e2tAddresses, err |
| 245 | } |
| 246 | |
| 247 | func (w *rNibDataService) SaveE2TInstance(e2tInstance *entities.E2TInstance) error { |
Irina | 4914446 | 2020-04-07 16:37:20 +0300 | [diff] [blame] | 248 | w.logger.Infof("#RnibDataService.SaveE2TInstance - E2T instance address: %s, podName: %s, state: %s, associated RANs count: %d, keep Alive ts: %d", e2tInstance.Address, e2tInstance.PodName, e2tInstance.State, len(e2tInstance.AssociatedRanList), e2tInstance.KeepAliveTimestamp) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 249 | |
| 250 | return w.SaveE2TInstanceNoLogs(e2tInstance) |
| 251 | } |
| 252 | |
| 253 | func (w *rNibDataService) SaveE2TInstanceNoLogs(e2tInstance *entities.E2TInstance) error { |
| 254 | |
| 255 | err := w.retry("SaveE2TInstance", func() (err error) { |
| 256 | err = w.rnibWriter.SaveE2TInstance(e2tInstance) |
| 257 | return |
| 258 | }) |
| 259 | |
| 260 | return err |
| 261 | } |
| 262 | |
| 263 | func (w *rNibDataService) SaveE2TAddresses(addresses []string) error { |
| 264 | w.logger.Infof("#RnibDataService.SaveE2TAddresses - addresses: %s", addresses) |
| 265 | |
| 266 | err := w.retry("SaveE2TAddresses", func() (err error) { |
| 267 | err = w.rnibWriter.SaveE2TAddresses(addresses) |
| 268 | return |
| 269 | }) |
| 270 | |
| 271 | return err |
| 272 | } |
| 273 | |
Amichai | f846c59 | 2020-01-08 16:45:07 +0200 | [diff] [blame] | 274 | func (w *rNibDataService) RemoveE2TInstance(e2tAddress string) error { |
| 275 | w.logger.Infof("#RnibDataService.RemoveE2TInstance - e2tAddress: %s", e2tAddress) |
| 276 | |
| 277 | err := w.retry("RemoveE2TInstance", func() (err error) { |
| 278 | err = w.rnibWriter.RemoveE2TInstance(e2tAddress) |
| 279 | return |
| 280 | }) |
| 281 | |
| 282 | return err |
| 283 | } |
| 284 | |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 285 | func (w *rNibDataService) GetGeneralConfiguration() (*entities.GeneralConfiguration, error) { |
| 286 | var generalConfiguration *entities.GeneralConfiguration = nil |
| 287 | |
| 288 | err := w.retry("GetGeneralConfiguration", func() (err error) { |
| 289 | generalConfiguration, err = w.rnibReader.GetGeneralConfiguration() |
| 290 | return |
| 291 | }) |
| 292 | |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 293 | return generalConfiguration, err |
| 294 | } |
| 295 | |
| 296 | func (w *rNibDataService) SaveGeneralConfiguration(config *entities.GeneralConfiguration) error { |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 297 | err := w.retry("SaveGeneralConfiguration", func() (err error) { |
| 298 | err = w.rnibWriter.SaveGeneralConfiguration(config) |
| 299 | return |
| 300 | }) |
| 301 | |
| 302 | return err |
| 303 | } |
| 304 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 305 | func (w *rNibDataService) PingRnib() bool { |
| 306 | err := w.retry("GetListNodebIds", func() (err error) { |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 307 | _, err = w.rnibReader.GetListNodebIds() |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 308 | return |
| 309 | }) |
| 310 | |
| 311 | return !isRnibConnectionError(err) |
| 312 | } |
| 313 | |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 314 | func (w *rNibDataService) UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error { |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame^] | 315 | w.logger.Infof("#RnibDataService.UpdateNodebInfoOnConnectionStatusInversion - event: %s, nodebInfo: %s", event, nodebInfo) |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 316 | |
| 317 | err := w.retry("UpdateNodebInfoOnConnectionStatusInversion", func() (err error) { |
Amichai | 7e2e493 | 2020-07-07 15:49:07 +0000 | [diff] [blame^] | 318 | err = w.rnibWriter.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, event) |
ss412g | a07b859 | 2020-06-16 21:51:33 +0300 | [diff] [blame] | 319 | return |
| 320 | }) |
| 321 | |
| 322 | return err |
| 323 | } |
| 324 | |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 325 | func (w *rNibDataService) retry(rnibFunc string, f func() error) (err error) { |
| 326 | attempts := w.maxAttempts |
| 327 | |
| 328 | for i := 1; ; i++ { |
| 329 | err = f() |
| 330 | if err == nil { |
| 331 | return |
| 332 | } |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 333 | if !isRnibConnectionError(err) { |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 334 | return err |
| 335 | } |
| 336 | if i >= attempts { |
| 337 | w.logger.Errorf("#RnibDataService.retry - after %d attempts of %s, last error: %s", attempts, rnibFunc, err) |
| 338 | return err |
| 339 | } |
| 340 | time.Sleep(w.retryInterval) |
| 341 | |
| 342 | w.logger.Infof("#RnibDataService.retry - retrying %d %s after error: %s", i, rnibFunc, err) |
| 343 | } |
| 344 | } |
| 345 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame] | 346 | func isRnibConnectionError(err error) bool { |
| 347 | internalErr, ok := err.(*common.InternalError) |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 348 | if !ok { |
| 349 | return false |
| 350 | } |
| 351 | _, ok = internalErr.Err.(*net.OpError) |
| 352 | |
| 353 | return ok |
| 354 | } |