[RICPLT-2727] Update RoutingMangaerClient UTs + others....
[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
18 package configuration
19
20 import (
21         "fmt"
22         "github.com/spf13/viper"
23 )
24
25 type Configuration struct {
26         Logging struct {
27                 LogLevel string
28         }
29         Http struct {
30                 Port int
31         }
32         Rmr struct {
33                 Port       int
34                 MaxMsgSize int
35         }
36         RoutingManager struct {
37                 BaseUrl    string
38         }
39         NotificationResponseBuffer   int
40         BigRedButtonTimeoutSec       int
41         MaxConnectionAttempts        int
42         MaxRnibConnectionAttempts    int
43         RnibRetryIntervalMs          int
44         KeepAliveResponseTimeoutMs       int
45         KeepAliveDelayMs             int
46 }
47
48 func ParseConfiguration() *Configuration {
49         viper.SetConfigType("yaml")
50         viper.SetConfigName("configuration")
51         viper.AddConfigPath("E2Manager/resources/")
52         viper.AddConfigPath("./resources/")     //For production
53         viper.AddConfigPath("../resources/")    //For test under Docker
54         viper.AddConfigPath("../../resources/") //For test under Docker
55         err := viper.ReadInConfig()
56         if err != nil {
57                 panic(fmt.Sprintf("#configuration.ParseConfiguration - failed to read configuration file: %s\n", err))
58         }
59
60         config := Configuration{}
61         config.populateRmrConfig(viper.Sub("rmr"))
62         config.populateHttpConfig(viper.Sub("http"))
63         config.populateLoggingConfig(viper.Sub("logging"))
64         config.populateRoutingManagerConfig(viper.Sub("routingManager"))
65         config.NotificationResponseBuffer = viper.GetInt("notificationResponseBuffer")
66         config.BigRedButtonTimeoutSec = viper.GetInt("bigRedButtonTimeoutSec")
67         config.MaxConnectionAttempts = viper.GetInt("maxConnectionAttempts")
68         config.MaxRnibConnectionAttempts = viper.GetInt("maxRnibConnectionAttempts")
69         config.RnibRetryIntervalMs = viper.GetInt("rnibRetryIntervalMs")
70         config.KeepAliveResponseTimeoutMs = viper.GetInt("keepAliveResponseTimeoutMs")
71         config.KeepAliveDelayMs = viper.GetInt("KeepAliveDelayMs")
72         return &config
73 }
74
75 func (c *Configuration) populateLoggingConfig(logConfig *viper.Viper) {
76         if logConfig == nil {
77                 panic(fmt.Sprintf("#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n"))
78         }
79         c.Logging.LogLevel = logConfig.GetString("logLevel")
80 }
81
82 func (c *Configuration) populateHttpConfig(httpConfig *viper.Viper) {
83         if httpConfig == nil {
84                 panic(fmt.Sprintf("#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n"))
85         }
86         c.Http.Port = httpConfig.GetInt("port")
87 }
88
89 func (c *Configuration) populateRmrConfig(rmrConfig *viper.Viper) {
90         if rmrConfig == nil {
91                 panic(fmt.Sprintf("#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n"))
92         }
93         c.Rmr.Port = rmrConfig.GetInt("port")
94         c.Rmr.MaxMsgSize = rmrConfig.GetInt("maxMsgSize")
95 }
96
97 func (c *Configuration) populateRoutingManagerConfig(rmConfig *viper.Viper) {
98         if rmConfig == nil {
99                 panic(fmt.Sprintf("#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n"))
100         }
101         c.RoutingManager.BaseUrl = rmConfig.GetString("baseUrl")
102 }