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. |
| 16 | // |
| 17 | |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 18 | package services |
| 19 | |
| 20 | import ( |
| 21 | "e2mgr/configuration" |
| 22 | "e2mgr/logger" |
| 23 | "e2mgr/rNibWriter" |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 24 | "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" |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [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 | "net" |
| 28 | "time" |
| 29 | ) |
| 30 | |
| 31 | type 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) |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame^] | 37 | PingRnib() bool |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type 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 | |
| 48 | func 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 | |
| 58 | func (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 | |
| 69 | func (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 | |
| 80 | func (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 | |
| 91 | func (w *rNibDataService) GetNodeb(ranName string) (*entities.NodebInfo, error) { |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame^] | 92 | w.logger.Infof("#RnibDataService.GetNodeb - RAN name: %s", ranName) |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 93 | |
| 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 | |
| 104 | func (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 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame^] | 117 | func (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 | |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 126 | func (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 | } |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame^] | 134 | if !isRnibConnectionError(err) { |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 135 | 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 | |
ss412g | de19068 | 2019-10-24 09:29:26 +0300 | [diff] [blame^] | 147 | |
| 148 | func isRnibConnectionError(err error) bool { |
| 149 | internalErr, ok := err.(*common.InternalError) |
Amichai | e3623cf | 2019-09-11 16:59:16 +0300 | [diff] [blame] | 150 | if !ok { |
| 151 | return false |
| 152 | } |
| 153 | _, ok = internalErr.Err.(*net.OpError) |
| 154 | |
| 155 | return ok |
| 156 | } |