/
config.go
262 lines (224 loc) · 7.21 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* CHF Configuration Factory
*/
package factory
import (
"errors"
"fmt"
"strconv"
"sync"
"github.com/asaskevich/govalidator"
"github.com/free5gc/chf/internal/logger"
)
const (
ChfDefaultTLSKeyLogPath = "./log/chfsslkey.log"
ChfDefaultTLSPemPath = "./cert/chf.pem"
ChfDefaultTLSKeyPath = "./cert/chf.key"
ChfDefaultConfigPath = "./config/chfcfg.yaml"
ChfSbiDefaultIPv4 = "127.0.0.113"
ChfSbiDefaultPort = 8000
ChfSbiDefaultScheme = "https"
ChfDefaultNrfUri = "https://127.0.0.10:8000"
CgfDefaultCdrFilePath = "/tmp"
ConvergedChargingResUriPrefix = "/nchf-convergedcharging/v3"
)
type Config struct {
Info *Info `yaml:"info" valid:"required"`
Configuration *Configuration `yaml:"configuration" valid:"required"`
Logger *Logger `yaml:"logger" valid:"required"`
sync.RWMutex
}
func (c *Config) Validate() (bool, error) {
if configuration := c.Configuration; configuration != nil {
if result, err := configuration.validate(); err != nil {
return result, err
}
}
result, err := govalidator.ValidateStruct(c)
return result, appendInvalid(err)
}
type Info struct {
Version string `yaml:"version,omitempty" valid:"required,in(1.0.3)"`
Description string `yaml:"description,omitempty" valid:"-"`
}
type Configuration struct {
ChfName string `yaml:"chfName,omitempty" valid:"required, type(string)"`
Sbi *Sbi `yaml:"sbi,omitempty" valid:"required"`
ServiceNameList []string `yaml:"serviceNameList,omitempty" valid:"required"`
NrfUri string `yaml:"nrfUri,omitempty" valid:"required, url"`
NrfCertPem string `yaml:"nrfCertPem,omitempty" valid:"optional"`
Mongodb *Mongodb `yaml:"mongodb" valid:"required"`
VolumeLimit int32 `yaml:"volumeLimit,omitempty" valid:"optional"`
VolumeLimitPDU int32 `yaml:"volumeLimitPDU,omitempty" valid:"optional"`
ReserveQuotaRatio int32 `yaml:"reserveQuotaRatio,omitempty" valid:"optional"`
VolumeThresholdRate float32 `yaml:"volumeThresholdRate,omitempty" valid:"optional"`
QuotaValidityTime int32 `yaml:"quotaValidityTime,omitempty" valid:"optional"`
RfDiameter *Diameter `yaml:"rfDiameter,omitempty" valid:"required"`
AbmfDiameter *Diameter `yaml:"abmfDiameter,omitempty" valid:"required"`
Cgf *Cgf `yaml:"cgf,omitempty" valid:"required"`
}
type Logger struct {
Enable bool `yaml:"enable" valid:"type(bool)"`
Level string `yaml:"level" valid:"required,in(trace|debug|info|warn|error|fatal|panic)"`
ReportCaller bool `yaml:"reportCaller" valid:"type(bool)"`
}
func (c *Configuration) validate() (bool, error) {
if sbi := c.Sbi; sbi != nil {
if result, err := sbi.validate(); err != nil {
return result, err
}
}
for index, serviceName := range c.ServiceNameList {
switch {
case serviceName == "nchf-convergedcharging":
default:
err := errors.New("Invalid serviceNameList[" + strconv.Itoa(index) + "]: " +
serviceName + ", should be nchf-convergedcharging.")
return false, err
}
}
result, err := govalidator.ValidateStruct(c)
return result, appendInvalid(err)
}
type Service struct {
ServiceName string `yaml:"serviceName" valid:"required, service"`
SuppFeat string `yaml:"suppFeat,omitempty" valid:"-"`
}
type Diameter struct {
Protocol string `yaml:"protocol" valid:"required"`
HostIPv4 string `yaml:"hostIPv4,omitempty" valid:"required,host"`
Port int `yaml:"port,omitempty" valid:"required,port"`
Tls *Tls `yaml:"tls,omitempty" valid:"optional"`
}
type Cgf struct {
HostIPv4 string `yaml:"hostIPv4,omitempty" valid:"required,host"`
Port int `yaml:"port,omitempty" valid:"required,port"`
ListenPort int `yaml:"listenPort,omitempty" valid:"required,port"`
Tls *Tls `yaml:"tls,omitempty" valid:"optional"`
CdrFilePath string `yaml:"cdrFilePath,omitempty" valid:"optional"`
}
type Sbi struct {
Scheme string `yaml:"scheme" valid:"required,scheme"`
RegisterIPv4 string `yaml:"registerIPv4,omitempty" valid:"required,host"` // IP that is registered at NRF.
BindingIPv4 string `yaml:"bindingIPv4,omitempty" valid:"required,host"` // IP used to run the server in the node.
Port int `yaml:"port,omitempty" valid:"required,port"`
Tls *Tls `yaml:"tls,omitempty" valid:"optional"`
}
func (s *Sbi) validate() (bool, error) {
govalidator.TagMap["scheme"] = govalidator.Validator(func(str string) bool {
return str == "https" || str == "http"
})
if tls := s.Tls; tls != nil {
if result, err := tls.validate(); err != nil {
return result, err
}
}
result, err := govalidator.ValidateStruct(s)
return result, appendInvalid(err)
}
type Tls struct {
Pem string `yaml:"pem,omitempty" valid:"type(string),minstringlength(1),required"`
Key string `yaml:"key,omitempty" valid:"type(string),minstringlength(1),required"`
}
func (t *Tls) validate() (bool, error) {
result, err := govalidator.ValidateStruct(t)
return result, err
}
type Mongodb struct {
Name string `yaml:"name" valid:"required, type(string)"`
Url string `yaml:"url" valid:"required"`
}
// Commenting the unused function
// func (m *Mongodb) validate() (bool, error) {
// pattern := `[-a-zA-Z0-9@:%._\+~#=]{1,256}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`
// if result := govalidator.StringMatches(m.Url, pattern); !result {
// err := fmt.Errorf("Invalid Url: %s", m.Url)
// return result, err
// }
// result, err := govalidator.ValidateStruct(m)
// return result, err
// }
func appendInvalid(err error) error {
var errs govalidator.Errors
if err == nil {
return nil
}
es := err.(govalidator.Errors).Errors()
for _, e := range es {
errs = append(errs, fmt.Errorf("Invalid %w", e))
}
return error(errs)
}
func (c *Config) GetVersion() string {
c.RLock()
defer c.RUnlock()
if c.Info.Version != "" {
return c.Info.Version
}
return ""
}
func (c *Config) SetLogEnable(enable bool) {
c.Lock()
defer c.Unlock()
if c.Logger == nil {
logger.CfgLog.Warnf("Logger should not be nil")
c.Logger = &Logger{
Enable: enable,
Level: "info",
}
} else {
c.Logger.Enable = enable
}
}
func (c *Config) SetLogLevel(level string) {
c.Lock()
defer c.Unlock()
if c.Logger == nil {
logger.CfgLog.Warnf("Logger should not be nil")
c.Logger = &Logger{
Level: level,
}
} else {
c.Logger.Level = level
}
}
func (c *Config) SetLogReportCaller(reportCaller bool) {
c.Lock()
defer c.Unlock()
if c.Logger == nil {
logger.CfgLog.Warnf("Logger should not be nil")
c.Logger = &Logger{
Level: "info",
ReportCaller: reportCaller,
}
} else {
c.Logger.ReportCaller = reportCaller
}
}
func (c *Config) GetLogEnable() bool {
c.RLock()
defer c.RUnlock()
if c.Logger == nil {
logger.CfgLog.Warnf("Logger should not be nil")
return false
}
return c.Logger.Enable
}
func (c *Config) GetLogLevel() string {
c.RLock()
defer c.RUnlock()
if c.Logger == nil {
logger.CfgLog.Warnf("Logger should not be nil")
return "info"
}
return c.Logger.Level
}
func (c *Config) GetLogReportCaller() bool {
c.RLock()
defer c.RUnlock()
if c.Logger == nil {
logger.CfgLog.Warnf("Logger should not be nil")
return false
}
return c.Logger.ReportCaller
}