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