-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesc-params.go
308 lines (255 loc) · 10.1 KB
/
desc-params.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package mrnesbits
// file desc-params.go holds structs, methods, and data structures used in specifying
// assignment of performance parameters to mrnesbits/mrnes models
import (
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"os"
"path"
"strings"
)
// An ExpParameter struct describes an input to experiment configuration at run-time. It specified
// - ParamObj identifies the kind of thing being configured : Switch, Router, Host, Interface, or Network
// - Attribute identifies a class of objects of that type to which the configuration parameter should apply.
// May be "*" for a wild-card, may be "name%%xxyy" where "xxyy" is the object's identifier, may be
// a comma-separated list of other attributes, documented [here]
type ExpParameter struct {
// Type of thing being configured
ParamObj string `json:"paramObj" yaml:"paramObj"`
// attribute identifier for this parameter
Attribute string `json:"attribute" yaml:"attribute"`
// ParameterType, e.g., "Bandwidth", "WiredLatency", "CPU"
Param string `json:"param" yaml:"param"`
// string-encoded value associated with type
Value string `json:"value" yaml:"value"`
}
// CreateExpParameter is a constructor. Completely fills in the struct with the [ExpParameter] attributes.
func CreateExpParameter(paramObj, attribute, param, value string) *ExpParameter {
exptr := &ExpParameter{ParamObj: paramObj, Attribute: attribute, Param: param, Value: value}
return exptr
}
// An ExpCfg structure holds all of the ExpParameters for a named experiment
type ExpCfg struct {
// Name is an identifier for a group of [ExpParameters]. No particular interpretation of this string is
// used, except as a referencing label when moving an ExpCfg into or out of a dictionary
Name string `json:"expname" yaml:"expname"`
// Parameters is a list of all the [ExpParameter] objects presented to the simulator for an experiment.
Parameters []ExpParameter `json:"parameters" yaml:"parameters"`
}
// An ExpCfgDict is a dictionary that holds [ExpCfg] objects in a map indexed by their Name.
type ExpCfgDict struct {
DictName string `json:"dictname" yaml:"dictname"`
Cfgs map[string]ExpCfg `json:"cfgs" yaml:"cfgs"`
}
// CreateExpCfgDict is a constructor. Saves a name for the dictionary, and initializes the slice of ExpCfg objects
func CreateExpCfgDict(name string) *ExpCfgDict {
ecd := new(ExpCfgDict)
ecd.DictName = name
ecd.Cfgs = make(map[string]ExpCfg)
return ecd
}
// AddExpCfg adds the offered ExpCfg to the dictionary, optionally returning
// an error if an ExpCfg with the same Name is already saved.
func (ecd *ExpCfgDict) AddExpCfg(ec *ExpCfg, overwrite bool) error {
// allow for overwriting duplication?
if !overwrite {
_, present := ecd.Cfgs[ec.Name]
if present {
return fmt.Errorf("attempt to overwrite template ExpCfg %s", ec.Name)
}
}
// save it
ecd.Cfgs[ec.Name] = *ec
return nil
}
// RecoverExpCfg returns an ExpCfg from the dictionary, with name equal to the input parameter.
// It returns also a flag denoting whether the identified ExpCfg has an entry in the dictionary.
func (ecd *ExpCfgDict) RecoverExpCfg(name string) (*ExpCfg, bool) {
ec, present := ecd.Cfgs[name]
if present {
return &ec, true
}
return nil, false
}
// WriteToFile stores the ExpCfgDict struct to the file whose name is given.
// Serialization to json or to yaml is selected based on the extension of this name.
func (ecd *ExpCfgDict) WriteToFile(filename string) error {
pathExt := path.Ext(filename)
var bytes []byte
var merr error = nil
if pathExt == ".yaml" || pathExt == ".YAML" || pathExt == ".yml" {
bytes, merr = yaml.Marshal(*ecd)
} else if pathExt == ".json" || pathExt == ".JSON" {
bytes, merr = json.MarshalIndent(*ecd, "", "\t")
}
if merr != nil {
panic(merr)
}
f, cerr := os.Create(filename)
if cerr != nil {
panic(cerr)
}
_, werr := f.WriteString(string(bytes[:]))
if werr != nil {
panic(werr)
}
f.Close()
return werr
}
// ReadExpCfgDict deserializes a byte slice holding a representation of an ExpCfgDict struct.
// If the input argument of dict (those bytes) is empty, the file whose name is given is read
// to acquire them. A deserialized representation is returned, or an error if one is generated
// from a file read or the deserialization.
func ReadExpCfgDict(filename string, useYAML bool, dict []byte) (*ExpCfgDict, error) {
var err error
if len(dict) == 0 {
dict, err = os.ReadFile(filename)
if err != nil {
return nil, err
}
}
example := ExpCfgDict{}
if useYAML {
err = yaml.Unmarshal(dict, &example)
} else {
err = json.Unmarshal(dict, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// CreateExpCfg is a constructor. Saves the offered Name and initializes the slice of ExpParameters.
func CreateExpCfg(name string) *ExpCfg {
expcfg := &ExpCfg{Name: name, Parameters: make([]ExpParameter, 0)}
return expcfg
}
// ValidateParameter returns an error if the paramObj, attribute, and param values don't
// make sense taken together within an ExpParameter.
func ValidateParameter(paramObj, attribute, param string) error {
// the paramObj string has to be recognized as one of the permitted ones (stored in list ExpParamObjs)
if !slices.Contains(ExpParamObjs, paramObj) {
return fmt.Errorf("paramater paramObj %s is not recognized", paramObj)
}
// Start the analysis of the attribute by splitting it by comma
attrbList := strings.Split(attribute, ",")
// every elemental attribute needs to be a name or "*", or recognized as a legitimate attribute
// for the associated paramObj
for _, attrb := range attrbList {
// if name is present it is the only acceptable attribute in the comma-separated list
if strings.Contains(attrb, "name%%") {
if len(attrbList) != 1 {
return fmt.Errorf("name paramater attribute %s paramObj %s is included with more attributes", attrb, paramObj)
}
// otherwise OK
return nil
}
// if "*" is present it is the only acceptable attribute in the comma-separated list
if strings.Contains(attrb, "*") {
if len(attrbList) != 1 {
return fmt.Errorf("name paramater attribute * paramObj %s is included with more attributes", paramObj)
}
// otherwise OK
return nil
}
// otherwise check the legitmacy of the individual attribute. Whole string is invalidate if one component is invalid.
if !slices.Contains(ExpAttributes[paramObj], attrb) {
return fmt.Errorf("paramater attribute %s is not recognized for paramObj %s", attrb, paramObj)
}
}
// comma-separated attribute is OK, make sure the type of param is consistent with the paramObj
if !slices.Contains(ExpParams[paramObj], param) {
return fmt.Errorf("paramater %s is not recognized for paramObj %s", param, paramObj)
}
// it's all good
return nil
}
// AddParameter accepts the four values in an ExpParameter, creates one, and adds to the ExpCfg's list.
// Returns an error if the parameters are not validated.
func (expcfg *ExpCfg) AddParameter(paramObj, attribute, param, value string) error {
// validate the offered parameter values
err := ValidateParameter(paramObj, attribute, param)
if err != nil {
return err
}
// create an ExpParameter with these values
excp := CreateExpParameter(paramObj, attribute, param, value)
// save it
expcfg.Parameters = append(expcfg.Parameters, *excp)
return nil
}
// WriteToFile stores the ExpCfg struct to the file whose name is given.
// Serialization to json or to yaml is selected based on the extension of this name.
func (expcfg *ExpCfg) WriteToFile(filename string) error {
pathExt := path.Ext(filename)
var bytes []byte
var merr error = nil
if pathExt == ".yaml" || pathExt == ".YAML" || pathExt == ".yml" {
bytes, merr = yaml.Marshal(*expcfg)
} else if pathExt == ".json" || pathExt == ".JSON" {
bytes, merr = json.MarshalIndent(*expcfg, "", "\t")
}
if merr != nil {
panic(merr)
}
f, cerr := os.Create(filename)
if cerr != nil {
panic(cerr)
}
_, werr := f.WriteString(string(bytes[:]))
if werr != nil {
panic(werr)
}
f.Close()
return werr
}
// ReadExpCfg deserializes a byte slice holding a representation of an ExpCfg struct.
// If the input argument of dict (those bytes) is empty, the file whose name is given is read
// to acquire them. A deserialized representation is returned, or an error if one is generated
// from a file read or the deserialization.
func ReadExpCfg(filename string, useYAML bool, dict []byte) (*ExpCfg, error) {
var err error
if len(dict) == 0 {
dict, err = os.ReadFile(filename)
if err != nil {
return nil, err
}
}
example := ExpCfg{}
if useYAML {
err = yaml.Unmarshal(dict, &example)
} else {
err = json.Unmarshal(dict, &example)
}
if err != nil {
return nil, err
}
return &example, nil
}
// ExpParamObjs holds descriptions of the types of objects
// that are initialized by an exp file for each the attributes of the object that can be tested for to determine
// whether the object is to receive the configuration parameter, and the parameter types defined for each object type
var ExpParamObjs []string
var ExpAttributes map[string][]string
var ExpParams map[string][]string
// GetExpParamDesc returns ExpParamObjs, ExpAttributes, and ExpParams after ensuring that they have been build
func GetExpParamDesc() ([]string, map[string][]string, map[string][]string) {
if ExpParamObjs == nil {
ExpParamObjs = []string{"Switch", "Router", "Host", "Interface", "Network"}
ExpAttributes = make(map[string][]string)
ExpAttributes["Switch"] = []string{"model", "*"}
ExpAttributes["Router"] = []string{"model", "wired", "wireless", "*"}
ExpAttributes["Host"] = []string{"*"}
ExpAttributes["Interface"] = []string{"Switch", "Host", "Router", "wired", "wireless", "*"}
ExpAttributes["Network"] = []string{"wired", "wireless", "LAN", "WAN", "T3", "T2", "T1", "*"}
ExpParams = make(map[string][]string)
ExpParams["Switch"] = []string{"execTime", "buffer"}
ExpParams["Route"] = []string{"execTime", "buffer"}
ExpParams["Host"] = []string{"CPU"}
ExpParams["Network"] = []string{"media", "latency", "bandwidth", "capacity"}
ExpParams["Interface"] = []string{"media", "latency", "bandwidth", "packetSize"}
}
return ExpParamObjs, ExpAttributes, ExpParams
}