blob: 02f8e1d19c77d01b3b62532846e75ca1d8a45cfa [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.
16//
17
Amichaie3623cf2019-09-11 16:59:16 +030018package services
19
20import (
21 "e2mgr/configuration"
22 "e2mgr/logger"
23 "e2mgr/rNibWriter"
Amichaie3623cf2019-09-11 16:59:16 +030024 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
25 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
ss412gde190682019-10-24 09:29:26 +030026 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
Amichaie3623cf2019-09-11 16:59:16 +030027 "net"
28 "time"
29)
30
31type RNibDataService interface {
32 SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error
33 UpdateNodebInfo(nodebInfo *entities.NodebInfo) error
34 SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error
35 GetNodeb(ranName string) (*entities.NodebInfo, error)
36 GetListNodebIds() ([]*entities.NbIdentity, error)
ss412gde190682019-10-24 09:29:26 +030037 PingRnib() bool
Amichaie3623cf2019-09-11 16:59:16 +030038}
39
40type rNibDataService struct {
41 logger *logger.Logger
42 rnibReaderProvider func() reader.RNibReader
43 rnibWriterProvider func() rNibWriter.RNibWriter
44 maxAttempts int
45 retryInterval time.Duration
46}
47
48func NewRnibDataService(logger *logger.Logger, config *configuration.Configuration, rnibReaderProvider func() reader.RNibReader, rnibWriterProvider func() rNibWriter.RNibWriter) *rNibDataService {
49 return &rNibDataService{
50 logger: logger,
51 rnibReaderProvider: rnibReaderProvider,
52 rnibWriterProvider: rnibWriterProvider,
53 maxAttempts: config.MaxRnibConnectionAttempts,
54 retryInterval: time.Duration(config.RnibRetryIntervalMs) * time.Millisecond,
55 }
56}
57
58func (w *rNibDataService) UpdateNodebInfo(nodebInfo *entities.NodebInfo) error {
59 w.logger.Infof("#RnibDataService.UpdateNodebInfo - nodebInfo: %s", nodebInfo)
60
61 err := w.retry("UpdateNodebInfo", func() (err error) {
62 err = w.rnibWriterProvider().UpdateNodebInfo(nodebInfo)
63 return
64 })
65
66 return err
67}
68
69func (w *rNibDataService) SaveNodeb(nbIdentity *entities.NbIdentity, nb *entities.NodebInfo) error {
70 w.logger.Infof("#RnibDataService.SaveNodeb - nbIdentity: %s, nodebInfo: %s", nbIdentity, nb)
71
72 err := w.retry("SaveNodeb", func() (err error) {
73 err = w.rnibWriterProvider().SaveNodeb(nbIdentity, nb)
74 return
75 })
76
77 return err
78}
79
80func (w *rNibDataService) SaveRanLoadInformation(inventoryName string, ranLoadInformation *entities.RanLoadInformation) error {
81 w.logger.Infof("#RnibDataService.SaveRanLoadInformation - inventoryName: %s, ranLoadInformation: %s", inventoryName, ranLoadInformation)
82
83 err := w.retry("SaveRanLoadInformation", func() (err error) {
84 err = w.rnibWriterProvider().SaveRanLoadInformation(inventoryName, ranLoadInformation)
85 return
86 })
87
88 return err
89}
90
91func (w *rNibDataService) GetNodeb(ranName string) (*entities.NodebInfo, error) {
ss412gde190682019-10-24 09:29:26 +030092 w.logger.Infof("#RnibDataService.GetNodeb - RAN name: %s", ranName)
Amichaie3623cf2019-09-11 16:59:16 +030093
94 var nodeb *entities.NodebInfo = nil
95
96 err := w.retry("GetNodeb", func() (err error) {
97 nodeb, err = w.rnibReaderProvider().GetNodeb(ranName)
98 return
99 })
100
101 return nodeb, err
102}
103
104func (w *rNibDataService) GetListNodebIds() ([]*entities.NbIdentity, error) {
105 w.logger.Infof("#RnibDataService.GetListNodebIds")
106
107 var nodeIds []*entities.NbIdentity = nil
108
109 err := w.retry("GetListNodebIds", func() (err error) {
110 nodeIds, err = w.rnibReaderProvider().GetListNodebIds()
111 return
112 })
113
114 return nodeIds, err
115}
116
ss412gde190682019-10-24 09:29:26 +0300117func (w *rNibDataService) PingRnib() bool {
118 err := w.retry("GetListNodebIds", func() (err error) {
119 _, err = w.rnibReaderProvider().GetListNodebIds()
120 return
121 })
122
123 return !isRnibConnectionError(err)
124}
125
Amichaie3623cf2019-09-11 16:59:16 +0300126func (w *rNibDataService) retry(rnibFunc string, f func() error) (err error) {
127 attempts := w.maxAttempts
128
129 for i := 1; ; i++ {
130 err = f()
131 if err == nil {
132 return
133 }
ss412gde190682019-10-24 09:29:26 +0300134 if !isRnibConnectionError(err) {
Amichaie3623cf2019-09-11 16:59:16 +0300135 return err
136 }
137 if i >= attempts {
138 w.logger.Errorf("#RnibDataService.retry - after %d attempts of %s, last error: %s", attempts, rnibFunc, err)
139 return err
140 }
141 time.Sleep(w.retryInterval)
142
143 w.logger.Infof("#RnibDataService.retry - retrying %d %s after error: %s", i, rnibFunc, err)
144 }
145}
146
ss412gde190682019-10-24 09:29:26 +0300147
148func isRnibConnectionError(err error) bool {
149 internalErr, ok := err.(*common.InternalError)
Amichaie3623cf2019-09-11 16:59:16 +0300150 if !ok {
151 return false
152 }
153 _, ok = internalErr.Err.(*net.OpError)
154
155 return ok
156}