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