blob: 2ab34d2f1b17add8987dff40f1368346825a256a [file] [log] [blame]
ss412gde190682019-10-24 09:29:26 +03001//
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.
nm755n15d39822019-11-28 16:56:00 +000016
17// This source code is part of the near-RT RIC (RAN Intelligent Controller)
18// platform project (RICP).
19
Amichaie3623cf2019-09-11 16:59:16 +030020package services
21
22import (
23 "e2mgr/configuration"
24 "e2mgr/logger"
25 "e2mgr/rNibWriter"
ss412gefcb4522019-12-02 16:59:19 +020026 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
Amichaie3623cf2019-09-11 16:59:16 +030027 "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
33type 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)
ss412gde190682019-10-24 09:29:26 +030039 PingRnib() bool
ss412gefcb4522019-12-02 16:59:19 +020040 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)
Amichaif846c592020-01-08 16:45:07 +020049 RemoveE2TInstance(e2tAddress string) error
idanshal08bbf912020-04-20 17:11:33 +030050 UpdateGnbCells(nodebInfo *entities.NodebInfo, servedNrCells []*entities.ServedNRCell) error
idanshald916e2a2020-04-22 15:56:43 +030051 RemoveServedNrCells(inventoryName string, servedNrCells []*entities.ServedNRCell) error
ss412ga07b8592020-06-16 21:51:33 +030052 GetGeneralConfiguration() (*entities.GeneralConfiguration, error)
53 UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error
54 SaveGeneralConfiguration(config *entities.GeneralConfiguration) error
Amichai7e2e4932020-07-07 15:49:07 +000055 RemoveEnb(nodebInfo *entities.NodebInfo) error
Amichaie3623cf2019-09-11 16:59:16 +030056}
57
58type rNibDataService struct {
Idan Shalom14eb5132020-07-06 09:41:46 +000059 logger *logger.Logger
60 rnibReader reader.RNibReader
61 rnibWriter rNibWriter.RNibWriter
62 maxAttempts int
63 retryInterval time.Duration
Amichaie3623cf2019-09-11 16:59:16 +030064}
65
ss412gefcb4522019-12-02 16:59:19 +020066func NewRnibDataService(logger *logger.Logger, config *configuration.Configuration, rnibReader reader.RNibReader, rnibWriter rNibWriter.RNibWriter) *rNibDataService {
Amichaie3623cf2019-09-11 16:59:16 +030067 return &rNibDataService{
Idan Shalom14eb5132020-07-06 09:41:46 +000068 logger: logger,
69 rnibReader: rnibReader,
70 rnibWriter: rnibWriter,
71 maxAttempts: config.MaxRnibConnectionAttempts,
72 retryInterval: time.Duration(config.RnibRetryIntervalMs) * time.Millisecond,
Amichaie3623cf2019-09-11 16:59:16 +030073 }
74}
75
idanshald916e2a2020-04-22 15:56:43 +030076func (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
Amichai7e2e4932020-07-07 15:49:07 +000085func (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
idanshal08bbf912020-04-20 17:11:33 +030094func (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
Amichaie3623cf2019-09-11 16:59:16 +0300105func (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) {
ss412gefcb4522019-12-02 16:59:19 +0200109 err = w.rnibWriter.UpdateNodebInfo(nodebInfo)
Amichaie3623cf2019-09-11 16:59:16 +0300110 return
111 })
112
113 return err
114}
115
116func (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) {
ss412gefcb4522019-12-02 16:59:19 +0200120 err = w.rnibWriter.SaveNodeb(nbIdentity, nb)
Amichaie3623cf2019-09-11 16:59:16 +0300121 return
122 })
123
124 return err
125}
126
127func (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) {
ss412gefcb4522019-12-02 16:59:19 +0200131 err = w.rnibWriter.SaveRanLoadInformation(inventoryName, ranLoadInformation)
Amichaie3623cf2019-09-11 16:59:16 +0300132 return
133 })
134
135 return err
136}
137
138func (w *rNibDataService) GetNodeb(ranName string) (*entities.NodebInfo, error) {
Amichaie3623cf2019-09-11 16:59:16 +0300139
140 var nodeb *entities.NodebInfo = nil
141
142 err := w.retry("GetNodeb", func() (err error) {
ss412gefcb4522019-12-02 16:59:19 +0200143 nodeb, err = w.rnibReader.GetNodeb(ranName)
Amichaie3623cf2019-09-11 16:59:16 +0300144 return
145 })
146
is005q5c953322020-01-05 16:22:47 +0200147 if err == nil {
Amichai380b7a22020-01-14 16:38:06 +0200148 w.logger.Infof("#RnibDataService.GetNodeb - RAN name: %s, connection status: %s, associated E2T: %s", nodeb.RanName, nodeb.ConnectionStatus, nodeb.AssociatedE2TInstanceAddress)
is005q5c953322020-01-05 16:22:47 +0200149 }
150
Amichaie3623cf2019-09-11 16:59:16 +0300151 return nodeb, err
152}
153
154func (w *rNibDataService) GetListNodebIds() ([]*entities.NbIdentity, error) {
Amichaie3623cf2019-09-11 16:59:16 +0300155 var nodeIds []*entities.NbIdentity = nil
156
157 err := w.retry("GetListNodebIds", func() (err error) {
ss412gefcb4522019-12-02 16:59:19 +0200158 nodeIds, err = w.rnibReader.GetListNodebIds()
Amichaie3623cf2019-09-11 16:59:16 +0300159 return
160 })
161
is005q5c953322020-01-05 16:22:47 +0200162 if err == nil {
163 w.logger.Infof("#RnibDataService.GetListNodebIds - RANs count: %d", len(nodeIds))
164 }
165
Amichaie3623cf2019-09-11 16:59:16 +0300166 return nodeIds, err
167}
168
ss412gefcb4522019-12-02 16:59:19 +0200169func (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
184func (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
195func (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
207func (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
219func (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
235func (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
247func (w *rNibDataService) SaveE2TInstance(e2tInstance *entities.E2TInstance) error {
Irina49144462020-04-07 16:37:20 +0300248 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)
ss412gefcb4522019-12-02 16:59:19 +0200249
250 return w.SaveE2TInstanceNoLogs(e2tInstance)
251}
252
253func (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
263func (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
Amichaif846c592020-01-08 16:45:07 +0200274func (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
ss412ga07b8592020-06-16 21:51:33 +0300285func (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
ss412ga07b8592020-06-16 21:51:33 +0300293 return generalConfiguration, err
294}
295
296func (w *rNibDataService) SaveGeneralConfiguration(config *entities.GeneralConfiguration) error {
ss412ga07b8592020-06-16 21:51:33 +0300297 err := w.retry("SaveGeneralConfiguration", func() (err error) {
298 err = w.rnibWriter.SaveGeneralConfiguration(config)
299 return
300 })
301
302 return err
303}
304
ss412gde190682019-10-24 09:29:26 +0300305func (w *rNibDataService) PingRnib() bool {
306 err := w.retry("GetListNodebIds", func() (err error) {
ss412gefcb4522019-12-02 16:59:19 +0200307 _, err = w.rnibReader.GetListNodebIds()
ss412gde190682019-10-24 09:29:26 +0300308 return
309 })
310
311 return !isRnibConnectionError(err)
312}
313
ss412ga07b8592020-06-16 21:51:33 +0300314func (w *rNibDataService) UpdateNodebInfoOnConnectionStatusInversion(nodebInfo *entities.NodebInfo, event string) error {
Amichai7e2e4932020-07-07 15:49:07 +0000315 w.logger.Infof("#RnibDataService.UpdateNodebInfoOnConnectionStatusInversion - event: %s, nodebInfo: %s", event, nodebInfo)
ss412ga07b8592020-06-16 21:51:33 +0300316
317 err := w.retry("UpdateNodebInfoOnConnectionStatusInversion", func() (err error) {
Amichai7e2e4932020-07-07 15:49:07 +0000318 err = w.rnibWriter.UpdateNodebInfoOnConnectionStatusInversion(nodebInfo, event)
ss412ga07b8592020-06-16 21:51:33 +0300319 return
320 })
321
322 return err
323}
324
Amichaie3623cf2019-09-11 16:59:16 +0300325func (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 }
ss412gde190682019-10-24 09:29:26 +0300333 if !isRnibConnectionError(err) {
Amichaie3623cf2019-09-11 16:59:16 +0300334 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
ss412gde190682019-10-24 09:29:26 +0300346func isRnibConnectionError(err error) bool {
347 internalErr, ok := err.(*common.InternalError)
Amichaie3623cf2019-09-11 16:59:16 +0300348 if !ok {
349 return false
350 }
351 _, ok = internalErr.Err.(*net.OpError)
352
353 return ok
354}