Sync from Azure to LF
[ric-plt/resource-status-manager.git] / RSM / 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         "rsm/enums"
24 )
25
26 type Configuration struct {
27         Logging struct {
28                 LogLevel string
29         }
30         Http struct {
31                 Port int
32         }
33         Rmr struct {
34                 Port             int
35                 MaxMsgSize       int
36                 ReadyIntervalSec int
37         }
38         Rnib struct {
39                 MaxRnibConnectionAttempts int
40                 RnibRetryIntervalMs       int
41         }
42         ResourceStatusParams struct {
43                 EnableResourceStatus         bool
44                 PartialSuccessAllowed        bool
45                 PrbPeriodic                  bool
46                 TnlLoadIndPeriodic           bool
47                 HwLoadIndPeriodic            bool
48                 AbsStatusPeriodic            bool
49                 RsrpMeasurementPeriodic      bool
50                 CsiPeriodic                  bool
51                 PeriodicityMs                enums.ReportingPeriodicity
52                 PeriodicityRsrpMeasurementMs enums.ReportingPeriodicityRSRPMR
53                 PeriodicityCsiMs             enums.ReportingPeriodicityCSIR
54         }
55 }
56
57 func ParseConfiguration() (*Configuration, error) {
58         viper.SetConfigType("yaml")
59         viper.SetConfigName("configuration")
60         viper.AddConfigPath("RSM/resources/")
61         viper.AddConfigPath("./resources/")     //For production
62         viper.AddConfigPath("../resources/")    //For test under Docker
63         viper.AddConfigPath("../../resources/") //For test under Docker
64         if err := viper.ReadInConfig(); err != nil {
65                 return nil, fmt.Errorf("#configuration.parseConfiguration - failed to read configuration file: %s\n", err)
66         }
67
68         config := Configuration{}
69         if err := config.fillRmrConfig(viper.Sub("rmr")); err != nil {
70                 return nil, err
71         }
72         if err := config.fillHttpConfig(viper.Sub("http")); err != nil {
73                 return nil, err
74         }
75         if err := config.fillLoggingConfig(viper.Sub("logging")); err != nil {
76                 return nil, err
77         }
78         if err := config.fillRnibConfig(viper.Sub("rnib")); err != nil {
79                 return nil, err
80         }
81         if err := config.fillResourceStatusParamsConfig(viper.Sub("resourceStatusParams")); err != nil {
82                 return nil, err
83         }
84
85         return &config, nil
86 }
87
88 func (c *Configuration) fillLoggingConfig(logConfig *viper.Viper) error {
89         if logConfig == nil {
90                 return fmt.Errorf("#configuration.fillLoggingConfig - failed to fill logging configuration: The entry 'logging' not found\n")
91         }
92         c.Logging.LogLevel = logConfig.GetString("logLevel")
93         return nil
94 }
95
96 func (c *Configuration) fillHttpConfig(httpConfig *viper.Viper) error {
97         if httpConfig == nil {
98                 return fmt.Errorf("#configuration.fillHttpConfig - failed to fill HTTP configuration: The entry 'http' not found\n")
99         }
100         c.Http.Port = httpConfig.GetInt("port")
101         return nil
102 }
103
104 func (c *Configuration) fillRmrConfig(rmrConfig *viper.Viper) error {
105         if rmrConfig == nil {
106                 return fmt.Errorf("#configuration.fillRmrConfig - failed to fill RMR configuration: The entry 'rmr' not found\n")
107         }
108         c.Rmr.Port = rmrConfig.GetInt("port")
109         c.Rmr.MaxMsgSize = rmrConfig.GetInt("maxMsgSize")
110         c.Rmr.ReadyIntervalSec = rmrConfig.GetInt("readyIntervalSec")
111         return nil
112 }
113
114 func (c *Configuration) fillRnibConfig(rnibConfig *viper.Viper) error {
115         if rnibConfig == nil {
116                 return fmt.Errorf("#configuration.fillRnibConfig - failed to fill RNib configuration: The entry 'rnib' not found\n")
117         }
118         c.Rnib.MaxRnibConnectionAttempts = rnibConfig.GetInt("maxRnibConnectionAttempts")
119         c.Rnib.RnibRetryIntervalMs = rnibConfig.GetInt("rnibRetryIntervalMs")
120         return nil
121 }
122
123 func (c *Configuration) fillResourceStatusParamsConfig(chConfig *viper.Viper) error {
124         if chConfig == nil {
125                 return fmt.Errorf("#configuration.fillResourceStatusParamsConfig - failed to fill resourceStatusParams configuration: The entry 'resourceStatusParams' not found\n")
126         }
127         c.ResourceStatusParams.EnableResourceStatus = chConfig.GetBool("enableResourceStatus")
128         c.ResourceStatusParams.PartialSuccessAllowed = chConfig.GetBool("partialSuccessAllowed")
129         c.ResourceStatusParams.PrbPeriodic = chConfig.GetBool("prbPeriodic")
130         c.ResourceStatusParams.TnlLoadIndPeriodic = chConfig.GetBool("tnlLoadIndPeriodic")
131         c.ResourceStatusParams.HwLoadIndPeriodic = chConfig.GetBool("hwLoadIndPeriodic")
132         c.ResourceStatusParams.AbsStatusPeriodic = chConfig.GetBool("absStatusPeriodic")
133         c.ResourceStatusParams.RsrpMeasurementPeriodic = chConfig.GetBool("rsrpMeasurementPeriodic")
134         c.ResourceStatusParams.CsiPeriodic = chConfig.GetBool("csiPeriodic")
135         if err := setPeriodicityMs(c, chConfig.GetInt("periodicityMs")); err != nil {
136                 return err
137         }
138         if err := setPeriodicityRsrpMeasurementMs(c, chConfig.GetInt("periodicityRsrpMeasurementMs")); err != nil {
139                 return err
140         }
141         if err := setPeriodicityCsiMs(c, chConfig.GetInt("periodicityCsiMs")); err != nil {
142                 return err
143         }
144         return nil
145 }
146
147 func setPeriodicityMs(c *Configuration, periodicityMs int) error {
148         v, ok := enums.ReportingPeriodicityValues[periodicityMs]
149
150         if !ok {
151                 return fmt.Errorf("Invalid configuration value supplied for PeriodicityMs. Received: %d. Should be one of: %v\n", periodicityMs, enums.GetReportingPeriodicityValuesAsKeys())
152         }
153
154         c.ResourceStatusParams.PeriodicityMs = v
155         return nil
156 }
157
158 func setPeriodicityRsrpMeasurementMs(c *Configuration, periodicityRsrpMeasurementMs int) error {
159         v, ok := enums.ReportingPeriodicityRsrPmrValues[periodicityRsrpMeasurementMs]
160
161         if !ok {
162                 return fmt.Errorf("Invalid configuration value supplied for PeriodicityRsrpMeasurementMs. Received: %d. Should be one of: %v\n", periodicityRsrpMeasurementMs, enums.GetReportingPeriodicityRsrPmrValuesAsKeys())
163         }
164
165         c.ResourceStatusParams.PeriodicityRsrpMeasurementMs = v
166         return nil
167 }
168
169 func setPeriodicityCsiMs(c *Configuration, periodicityCsiMs int) error {
170         v, ok := enums.ReportingPeriodicityCsirValues[periodicityCsiMs]
171
172         if !ok {
173                 return fmt.Errorf("Invalid configuration value supplied for PeriodicityCsiMs. Received: %d. Should be one of: %v\n", periodicityCsiMs, enums.GetReportingPeriodicityCsirValuesAsKeys())
174         }
175
176         c.ResourceStatusParams.PeriodicityCsiMs = v
177         return nil
178 }
179
180 func (c *Configuration) String() string {
181         return fmt.Sprintf("{logging.logLevel: %s, http.port: %d, rmr.port: %d, rmr.maxMsgSize: %d, rmr.readyIntervalSec: %d, rnib.maxRnibConnectionAttempts: %d, rnib.rnibRetryIntervalMs: %d, "+
182                 "resourceStatusParams.enableResourceStatus: %t, resourceStatusParams.partialSuccessAllowed: %t, resourceStatusParams.prbPeriodic: %t, "+
183                 "resourceStatusParams.tnlLoadIndPeriodic: %t, resourceStatusParams.hwLoadIndPeriodic: %t, resourceStatusParams.absStatusPeriodic: %t,"+
184                 "resourceStatusParams.rsrpMeasurementPeriodic: %t, resourceStatusParams.csiPeriodic: %t, resourceStatusParams.periodicityMs: %s, "+
185                 "resourceStatusParams.periodicityRsrpMeasurementMs: %s, resourceStatusParams.periodicityCsiMs: %s}",
186                 c.Logging.LogLevel,
187                 c.Http.Port,
188                 c.Rmr.Port,
189                 c.Rmr.MaxMsgSize,
190                 c.Rmr.ReadyIntervalSec,
191                 c.Rnib.MaxRnibConnectionAttempts,
192                 c.Rnib.RnibRetryIntervalMs,
193                 c.ResourceStatusParams.EnableResourceStatus,
194                 c.ResourceStatusParams.PartialSuccessAllowed,
195                 c.ResourceStatusParams.PrbPeriodic,
196                 c.ResourceStatusParams.TnlLoadIndPeriodic,
197                 c.ResourceStatusParams.HwLoadIndPeriodic,
198                 c.ResourceStatusParams.AbsStatusPeriodic,
199                 c.ResourceStatusParams.RsrpMeasurementPeriodic,
200                 c.ResourceStatusParams.CsiPeriodic,
201
202                 c.ResourceStatusParams.PeriodicityMs,
203                 c.ResourceStatusParams.PeriodicityRsrpMeasurementMs,
204                 c.ResourceStatusParams.PeriodicityCsiMs,
205         )
206 }