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