4e8883edec1cca241c89e982e8dffc4d96e4e966
[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         "fmt"
24         "github.com/spf13/viper"
25 )
26
27 type Configuration struct {
28         Logging struct {
29                 LogLevel string
30         }
31         Http struct {
32                 Port int
33         }
34         Rmr struct {
35                 Port       int
36                 MaxMsgSize int
37         }
38         RoutingManager struct {
39                 BaseUrl string
40         }
41         Kubernetes struct {
42                 ConfigPath string
43                 KubeNamespace  string
44         }
45         NotificationResponseBuffer   int
46         BigRedButtonTimeoutSec       int
47         MaxRnibConnectionAttempts    int
48         RnibRetryIntervalMs          int
49         KeepAliveResponseTimeoutMs   int
50         KeepAliveDelayMs             int
51         E2TInstanceDeletionTimeoutMs int
52         GlobalRicId                  struct {
53                 PlmnId      string
54                 RicNearRtId 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.populateKubernetesConfig(viper.Sub("kubernetes"))
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         return &config
85 }
86
87 func (c *Configuration) populateLoggingConfig(logConfig *viper.Viper) {
88         if logConfig == nil {
89                 panic(fmt.Sprintf("#configuration.populateLoggingConfig - failed to populate logging configuration: The entry 'logging' not found\n"))
90         }
91         c.Logging.LogLevel = logConfig.GetString("logLevel")
92 }
93
94 func (c *Configuration) populateHttpConfig(httpConfig *viper.Viper) {
95         if httpConfig == nil {
96                 panic(fmt.Sprintf("#configuration.populateHttpConfig - failed to populate HTTP configuration: The entry 'http' not found\n"))
97         }
98         c.Http.Port = httpConfig.GetInt("port")
99 }
100
101 func (c *Configuration) populateRmrConfig(rmrConfig *viper.Viper) {
102         if rmrConfig == nil {
103                 panic(fmt.Sprintf("#configuration.populateRmrConfig - failed to populate RMR configuration: The entry 'rmr' not found\n"))
104         }
105         c.Rmr.Port = rmrConfig.GetInt("port")
106         c.Rmr.MaxMsgSize = rmrConfig.GetInt("maxMsgSize")
107 }
108
109 func (c *Configuration) populateRoutingManagerConfig(rmConfig *viper.Viper) {
110         if rmConfig == nil {
111                 panic(fmt.Sprintf("#configuration.populateRoutingManagerConfig - failed to populate Routing Manager configuration: The entry 'routingManager' not found\n"))
112         }
113         c.RoutingManager.BaseUrl = rmConfig.GetString("baseUrl")
114 }
115
116 func (c *Configuration) populateKubernetesConfig(rmConfig *viper.Viper) {
117         if rmConfig == nil {
118                 panic(fmt.Sprintf("#configuration.populateKubernetesConfig - failed to populate Kubernetes configuration: The entry 'kubernetes' not found\n"))
119         }
120         c.Kubernetes.ConfigPath = rmConfig.GetString("configPath")
121         c.Kubernetes.KubeNamespace = rmConfig.GetString("kubeNamespace")
122 }
123
124 func (c *Configuration) populateGlobalRicIdConfig(globalRicIdConfig *viper.Viper) {
125         if globalRicIdConfig == nil {
126                 panic(fmt.Sprintf("#configuration.populateGlobalRicIdConfig - failed to populate Global RicId configuration: The entry 'globalRicId' not found\n"))
127         }
128         c.GlobalRicId.PlmnId = globalRicIdConfig.GetString("plmnId")
129         c.GlobalRicId.RicNearRtId = globalRicIdConfig.GetString("ricNearRtId")
130 }
131
132 func (c *Configuration) String() string {
133         return fmt.Sprintf("{logging.logLevel: %s, http.port: %d, rmr: { port: %d, maxMsgSize: %d}, routingManager.baseUrl: %s, "+
134                 "notificationResponseBuffer: %d, bigRedButtonTimeoutSec: %d, maxRnibConnectionAttempts: %d, "+
135                 "rnibRetryIntervalMs: %d, keepAliveResponseTimeoutMs: %d, keepAliveDelayMs: %d, e2tInstanceDeletionTimeoutMs: %d, "+
136                 "globalRicId: { plmnId: %s, ricNearRtId: %s, kubernetes: {configPath: %s, kubeNamespace: %s}}",
137                 c.Logging.LogLevel,
138                 c.Http.Port,
139                 c.Rmr.Port,
140                 c.Rmr.MaxMsgSize,
141                 c.RoutingManager.BaseUrl,
142                 c.NotificationResponseBuffer,
143                 c.BigRedButtonTimeoutSec,
144                 c.MaxRnibConnectionAttempts,
145                 c.RnibRetryIntervalMs,
146                 c.KeepAliveResponseTimeoutMs,
147                 c.KeepAliveDelayMs,
148                 c.E2TInstanceDeletionTimeoutMs,
149                 c.GlobalRicId.PlmnId,
150                 c.GlobalRicId.RicNearRtId,
151                 c.Kubernetes.ConfigPath,
152                 c.Kubernetes.KubeNamespace,
153         )
154 }