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