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