RIC-961:implement Xn and X2 component IDs correctly in E2M
[ric-plt/e2mgr.git] / E2Manager / configuration / configuration.go
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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package configuration
21
22 import (
23         "errors"
24         "fmt"
25         "github.com/spf13/viper"
26         "strconv"
27 )
28
29 type RnibWriterConfig struct {
30         StateChangeMessageChannel     string
31         RanManipulationMessageChannel string
32 }
33
34 type Configuration struct {
35         Logging struct {
36                 LogLevel string
37         }
38         Http struct {
39                 Port int
40         }
41         Rmr struct {
42                 Port       int
43                 MaxMsgSize int
44         }
45         RoutingManager struct {
46                 BaseUrl string
47         }
48
49         NotificationResponseBuffer   int
50         BigRedButtonTimeoutSec       int
51         MaxRnibConnectionAttempts    int
52         RnibRetryIntervalMs          int
53         KeepAliveResponseTimeoutMs   int
54         KeepAliveDelayMs             int
55         E2TInstanceDeletionTimeoutMs int
56         GlobalRicId                  struct {
57                 RicId string
58                 Mcc   string
59                 Mnc   string
60         }
61         RnibWriter RnibWriterConfig
62 }
63
64 func ParseConfiguration() *Configuration {
65         viper.SetConfigType("yaml")
66         viper.SetConfigName("configuration")
67         viper.AddConfigPath("E2Manager/resources/")
68         viper.AddConfigPath("./resources/")     //For production
69         viper.AddConfigPath("../resources/")    //For test under Docker
70         viper.AddConfigPath("../../resources/") //For test under Docker
71         err := viper.ReadInConfig()
72         if err != nil {
73                 panic(fmt.Sprintf("#configuration.ParseConfiguration - failed to read configuration file: %s\n", err))
74         }
75
76         config := Configuration{}
77         config.populateRmrConfig(viper.Sub("rmr"))
78         config.populateHttpConfig(viper.Sub("http"))
79         config.populateLoggingConfig(viper.Sub("logging"))
80         config.populateRoutingManagerConfig(viper.Sub("routingManager"))
81         config.NotificationResponseBuffer = viper.GetInt("notificationResponseBuffer")
82         config.BigRedButtonTimeoutSec = viper.GetInt("bigRedButtonTimeoutSec")
83         config.MaxRnibConnectionAttempts = viper.GetInt("maxRnibConnectionAttempts")
84         config.RnibRetryIntervalMs = viper.GetInt("rnibRetryIntervalMs")
85         config.KeepAliveResponseTimeoutMs = viper.GetInt("keepAliveResponseTimeoutMs")
86         config.KeepAliveDelayMs = viper.GetInt("KeepAliveDelayMs")
87         config.E2TInstanceDeletionTimeoutMs = viper.GetInt("e2tInstanceDeletionTimeoutMs")
88         config.populateGlobalRicIdConfig(viper.Sub("globalRicId"))
89         config.populateRnibWriterConfig(viper.Sub("rnibWriter"))
90         return &config
91 }
92
93 func (c *Configuration) populateLoggingConfig(logConfig *viper.Viper) {
94         if logConfig == nil {
95                 panic(fmt.Sprintf("#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n"))
96         }
97         c.Logging.LogLevel = logConfig.GetString("logLevel")
98 }
99
100 func (c *Configuration) populateHttpConfig(httpConfig *viper.Viper) {
101         if httpConfig == nil {
102                 panic(fmt.Sprintf("#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n"))
103         }
104         c.Http.Port = httpConfig.GetInt("port")
105 }
106
107 func (c *Configuration) populateRmrConfig(rmrConfig *viper.Viper) {
108         if rmrConfig == nil {
109                 panic(fmt.Sprintf("#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n"))
110         }
111         c.Rmr.Port = rmrConfig.GetInt("port")
112         c.Rmr.MaxMsgSize = rmrConfig.GetInt("maxMsgSize")
113 }
114
115 func (c *Configuration) populateRoutingManagerConfig(rmConfig *viper.Viper) {
116         if rmConfig == nil {
117                 panic(fmt.Sprintf("#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n"))
118         }
119         c.RoutingManager.BaseUrl = rmConfig.GetString("baseUrl")
120 }
121
122 func (c *Configuration) populateRnibWriterConfig(rnibWriterConfig *viper.Viper) {
123         if rnibWriterConfig == nil {
124                 panic(fmt.Sprintf("#configuration.populateRnibWriterConfig - failed to populate Rnib Writer configuration: The entry 'rnibWriter' not found\n"))
125         }
126         c.RnibWriter.StateChangeMessageChannel = rnibWriterConfig.GetString("stateChangeMessageChannel")
127         c.RnibWriter.RanManipulationMessageChannel = rnibWriterConfig.GetString("ranManipulationMessageChannel")
128 }
129
130 func (c *Configuration) populateGlobalRicIdConfig(globalRicIdConfig *viper.Viper) {
131         err := validateGlobalRicIdConfig(globalRicIdConfig)
132         if err != nil {
133                 panic(err.Error())
134         }
135         c.GlobalRicId.RicId = globalRicIdConfig.GetString("ricId")
136         c.GlobalRicId.Mcc = globalRicIdConfig.GetString("mcc")
137         c.GlobalRicId.Mnc = globalRicIdConfig.GetString("mnc")
138 }
139
140 func validateGlobalRicIdConfig(globalRicIdConfig *viper.Viper) error {
141         if globalRicIdConfig == nil {
142                 return errors.New("#configuration.validateGlobalRicIdConfig - failed to populate Global RicId configuration: The entry 'globalRicId' not found\n")
143         }
144
145         err := validateRicId(globalRicIdConfig.GetString("ricId"))
146
147         if err != nil {
148                 return err
149         }
150
151         err = validateMcc(globalRicIdConfig.GetString("mcc"))
152
153         if err != nil {
154                 return err
155         }
156
157         err = validateMnc(globalRicIdConfig.GetString("mnc"))
158
159         if err != nil {
160                 return err
161         }
162
163         return nil
164 }
165
166 func validateMcc(mcc string) error {
167
168         if len(mcc) == 0 {
169                 return errors.New("#configuration.validateMcc - mcc is missing or empty\n")
170         }
171
172         if len(mcc) != 3 {
173                 return errors.New("#configuration.validateMcc - mcc is not 3 digits\n")
174         }
175
176         mccInt, err := strconv.Atoi(mcc)
177
178         if err != nil {
179                 return errors.New("#configuration.validateMcc - mcc is not a number\n")
180         }
181
182         if mccInt < 0 {
183                 return errors.New("#configuration.validateMcc - mcc is negative\n")
184         }
185         return nil
186 }
187
188 func validateMnc(mnc string) error {
189
190         if len(mnc) == 0 {
191                 return errors.New("#configuration.validateMnc - mnc is missing or empty\n")
192         }
193
194         if len(mnc) < 2 || len(mnc) > 3 {
195                 return errors.New("#configuration.validateMnc - mnc is not 2 or 3 digits\n")
196         }
197
198         mncAsInt, err := strconv.Atoi(mnc)
199
200         if err != nil {
201                 return errors.New("#configuration.validateMnc - mnc is not a number\n")
202         }
203
204         if mncAsInt < 0 {
205                 return errors.New("#configuration.validateMnc - mnc is negative\n")
206         }
207
208         return nil
209 }
210
211 func validateRicId(ricId string) error {
212
213         if len(ricId) == 0 {
214                 return errors.New("#configuration.validateRicId - ricId is missing or empty\n")
215         }
216
217         if len(ricId) != 5 {
218                 return errors.New("#configuration.validateRicId - ricId length should be 5 hex characters\n")
219         }
220
221         _, err := strconv.ParseUint(ricId, 16, 64)
222         if err != nil {
223                 return errors.New("#configuration.validateRicId - ricId is not hex number\n")
224         }
225
226         return nil
227 }
228
229 func (c *Configuration) String() string {
230         return fmt.Sprintf("{logging.logLevel: %s, http.port: %d, rmr: { port: %d, maxMsgSize: %d}, routingManager.baseUrl: %s, "+
231                 "notificationResponseBuffer: %d, bigRedButtonTimeoutSec: %d, maxRnibConnectionAttempts: %d, "+
232                 "rnibRetryIntervalMs: %d, keepAliveResponseTimeoutMs: %d, keepAliveDelayMs: %d, e2tInstanceDeletionTimeoutMs: %d, "+
233                 "globalRicId: { ricId: %s, mcc: %s, mnc: %s}, rnibWriter: { stateChangeMessageChannel: %s, ranManipulationChannel: %s}",
234                 c.Logging.LogLevel,
235                 c.Http.Port,
236                 c.Rmr.Port,
237                 c.Rmr.MaxMsgSize,
238                 c.RoutingManager.BaseUrl,
239                 c.NotificationResponseBuffer,
240                 c.BigRedButtonTimeoutSec,
241                 c.MaxRnibConnectionAttempts,
242                 c.RnibRetryIntervalMs,
243                 c.KeepAliveResponseTimeoutMs,
244                 c.KeepAliveDelayMs,
245                 c.E2TInstanceDeletionTimeoutMs,
246                 c.GlobalRicId.RicId,
247                 c.GlobalRicId.Mcc,
248                 c.GlobalRicId.Mnc,
249                 c.RnibWriter.StateChangeMessageChannel,
250                 c.RnibWriter.RanManipulationMessageChannel,
251         )
252 }