-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
174 lines (154 loc) · 5.71 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
package diameter
import (
"encoding/json"
"errors"
"time"
)
type DiameterConfig struct {
RequestTimeout *Duration `json:"requestTimeout,omitempty"`
MaxRetransmits *uint `json:"maxRetransmits,omitempty"`
RetransmitInterval *Duration `json:"retransmitInterval,omitempty"`
EnableWatchdog *bool `json:"enableWatchdog,omitempty"`
WatchdogInterval *Duration `json:"watchdogInterval,omitempty"`
WatchdogStream *uint `json:"watchdogStream,omitempty"`
SupportedVendorID *[]uint32 `json:"supportedVendorID,omitempty"`
AcctApplicationID *[]uint32 `json:"acctApplicationId,omitempty"`
AuthApplicationId *[]uint32 `json:"authApplicationId,omitempty"`
VendorSpecificApplicationID *[]VendorSpecificApplicationIDConfig `json:"vendorSpecificApplicationId,omitempty"`
CapabilityExchange *CapabilityExchangeConfig `json:"capabilityExchange,omitempty"`
TransportProtocol *string `josn:"transportProtocol,omitempty"`
TLS *TLSConfig `json:"tls,omitempty"`
}
type VendorSpecificApplicationIDConfig struct {
VendorID *uint32 `json:"vendorId,omitempty"`
AuthApplicationID *uint32 `json:"authApplicationId,omitempty"`
AcctApplicationID *uint32 `json:"acctApplicationId,omitempty"`
}
type CapabilityExchangeConfig struct {
VendorID *uint32 `json:"vendorID"`
ProductName *string `json:"productName,omitempty"`
OriginHost *string `json:"originHost,omitempty"`
OriginRealm *string `json:"originRealm,omitempty"`
FirmwareRevision *uint32 `json:"firmwareRevision,omitempty"`
HostIPAddresses *[]string `json:"hostIPAddresses,omitempty"`
}
type TLSConfig struct {
Enable bool `json:"enable"`
Cert string `json:"cert,omitempty"`
Key string `json:"key,omitempty"`
}
func parseConfig(arg map[string]interface{}) (*DiameterConfig, error) {
var config DiameterConfig
if b, err := json.Marshal(arg); err != nil {
return nil, err
} else {
if err = json.Unmarshal(b, &config); err != nil {
return nil, err
}
}
setDiameterConfigDefaults(&config)
return &config, nil
}
func setDiameterConfigDefaults(config *DiameterConfig) {
// Default values
var defaultRequestTimeout = Duration{1 * time.Second}
var defaultMaxRetransmits uint = 1
var defaultRetransmitInterval = Duration{1 * time.Second}
var defaultEnableWatchdog = true
var defaultWatchdogInterval = Duration{5 * time.Second}
var defaultWatchdogStream uint = 0
var defaultVendorID uint32 = 13
var defaultProductName = "xk6-diameter"
var defaultOriginHost = "origin.host"
var defaultOriginRealm = "origin.realm"
var defaultFirmwareRevision uint32 = 1
var defaultHostIPAddresses = []string{"127.0.0.1"}
var defaultSupportedVendorID = []uint32{}
var defaultAcctApplicationID = []uint32{}
var defaultAuthApplicationID = []uint32{}
var defaultVendorSpecificApplicationID = []VendorSpecificApplicationIDConfig{}
var defaultTransportProtocol = "tcp"
// Set defaults for DiameterConfig
if config.RequestTimeout == nil {
config.RequestTimeout = &defaultRequestTimeout
}
if config.MaxRetransmits == nil {
config.MaxRetransmits = &defaultMaxRetransmits
}
if config.RetransmitInterval == nil {
config.RetransmitInterval = &defaultRetransmitInterval
}
if config.EnableWatchdog == nil {
config.EnableWatchdog = &defaultEnableWatchdog
}
if config.WatchdogInterval == nil {
config.WatchdogInterval = &defaultWatchdogInterval
}
if config.WatchdogStream == nil {
config.WatchdogStream = &defaultWatchdogStream
}
if config.SupportedVendorID == nil {
config.SupportedVendorID = &defaultSupportedVendorID
}
if config.AcctApplicationID == nil {
config.AcctApplicationID = &defaultAcctApplicationID
}
if config.AuthApplicationId == nil {
config.AuthApplicationId = &defaultAuthApplicationID
}
if config.VendorSpecificApplicationID == nil {
config.VendorSpecificApplicationID = &defaultVendorSpecificApplicationID
}
// Set defaults for CapabilityExchangeConfig
if config.CapabilityExchange == nil {
config.CapabilityExchange = &CapabilityExchangeConfig{}
}
if config.CapabilityExchange.VendorID == nil {
config.CapabilityExchange.VendorID = &defaultVendorID
}
if config.CapabilityExchange.ProductName == nil {
config.CapabilityExchange.ProductName = &defaultProductName
}
if config.CapabilityExchange.OriginHost == nil {
config.CapabilityExchange.OriginHost = &defaultOriginHost
}
if config.CapabilityExchange.OriginRealm == nil {
config.CapabilityExchange.OriginRealm = &defaultOriginRealm
}
if config.CapabilityExchange.FirmwareRevision == nil {
config.CapabilityExchange.FirmwareRevision = &defaultFirmwareRevision
}
if config.CapabilityExchange.HostIPAddresses == nil {
config.CapabilityExchange.HostIPAddresses = &defaultHostIPAddresses
}
if config.TransportProtocol == nil {
config.TransportProtocol = &defaultTransportProtocol
}
// Set defaults for TLSConfig
if config.TLS == nil {
config.TLS = &TLSConfig{}
}
}
type Duration struct {
time.Duration
}
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}