-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparam.go
494 lines (423 loc) · 14.9 KB
/
param.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package mrnes
import (
"encoding/json"
"fmt"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"os"
"path"
)
// AttrbStruct holds the name of an attribute and a value for it
type AttrbStruct struct {
AttrbName, AttrbValue string
}
// A valueStruct type holds three different types a value might have,
// typically only one of these is used, and which one is known by context
type valueStruct struct {
intValue int
floatValue float64
stringValue string
boolValue bool
}
// CreateAttrbStruct is a constructor
func CreateAttrbStruct(attrbName, attrbValue string) *AttrbStruct {
as := new(AttrbStruct)
as.AttrbName = attrbName
as.AttrbValue = attrbValue
return as
}
// ValidateAttribute checks that the attribute named is one that associates with the parameter object type named
func ValidateAttribute(paramObj, attrbName string) bool {
_, present := ExpAttributes[paramObj]
if !present {
return false
}
// wildcard always checks out
if attrbName == "*" {
return true
}
// result is true if the name is in the list of attributes for the parameter object type
return slices.Contains(ExpAttributes[paramObj], attrbName)
}
// CompareAttrbs returns -1 if the first argument is strictly more general than the second,
// returns 1 if the second argument is strictly more general than the first, and 0 otherwise
func CompareAttrbs(attrbs1, attrbs2 []AttrbStruct) int {
// attrbs1 is strictly more general if its length is strictly less and every name it has
// is shared by attrbs2
if len(attrbs1) < len(attrbs2) {
for _, attrb1 := range attrbs1 {
attrbName := attrb1.AttrbName
found := false
for _, attrb2 := range attrbs2 {
if attrb2.AttrbName == attrbName {
found = true
break
}
}
// if attrbName was not found in attrb2 then attrbs1 cannot be strictly more general
// and (because len(attrbs1) < len(attrbs2)) it cannot be strictly less general
if !found {
return 0
}
}
// every attribute name in attrbs1 found in attrbs2, means attrbs1 is more general
return -1
}
if len(attrbs2) < len(attrbs1) {
for _, attrb2 := range attrbs2 {
attrbName := attrb2.AttrbName
found := false
for _, attrb1 := range attrbs1 {
if attrb1.AttrbName == attrbName {
found = true
break
}
}
// if attrbName was not found in attrb1 then attrbs2 cannot be strictly more general
// and (because len(attrbs2) < len(attrbs1)) it cannot be strictly less general
if !found {
return 0
}
}
// every attribute name in attrbs2 found in attrbs1, means attrbs2 is more general
return 1
}
return 0
}
// EqAttrbs determines whether the two attribute lists are exactly the same
func EqAttrbs(attrbs1, attrbs2 []AttrbStruct) bool {
if len(attrbs1) != len(attrbs2) {
return false
}
// see whether every attribute in attrbs1 is found in attrbs2
for _, attrb1 := range attrbs1 {
found := false
for _, attrb2 := range attrbs2 {
if attrb1.AttrbName == attrb2.AttrbName && attrb1.AttrbValue == attrb2.AttrbValue {
found = true
break
}
}
// if attrb1.AttrbName not found in attrb2 they can't be equal
if !found {
return false
}
}
// now see whether every attribute in attrbs2 is found in attrbs1
for _, attrb2 := range attrbs2 {
found := false
for _, attrb1 := range attrbs1 {
if attrb2.AttrbName == attrb1.AttrbName && attrb2.AttrbValue == attrb1.AttrbValue {
found = true
break
}
}
// if attrb2.AttrbName not found in attrb1 they can't be equal
if !found {
return false
}
}
// perfect match among attribute names
return true
}
// ExpParameter struct describes an input to experiment configuration at run-time. It specifies
// - ParamObj identifies the kind of thing being configured : Switch, Router, Endpoint, Interface, or Network
// - Attributes is a list of attributes, each of which are required for the parameter value to be applied.
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"`
Attributes []AttrbStruct `json:"attributes" yaml:"attributes"`
// ParameterType, e.g., "Bandwidth", "WiredLatency", "model"
Param string `json:"param" yaml:"param"`
// string-encoded value associated with type
Value string `json:"value" yaml:"value"`
}
// Eq returns a boolean flag indicating whether the two ExpParameters referenced in the call are the same
func (epp *ExpParameter) Eq(ep2 *ExpParameter) bool {
if epp.ParamObj != ep2.ParamObj {
return false
}
if !EqAttrbs(epp.Attributes, ep2.Attributes) {
return false
}
if epp.Param != ep2.Param {
return false
}
if epp.Value != ep2.Value {
return false
}
return true
}
// CreateExpParameter is a constructor. Completely fills in the struct with the [ExpParameter] attributes.
func CreateExpParameter(paramObj string, attributes []AttrbStruct, param, value string) *ExpParameter {
exptr := &ExpParameter{ParamObj: paramObj, Attributes: attributes, Param: param, Value: value}
return exptr
}
// AddAttribute includes another attribute to those associated with the ExpParameter.
// An error is returned if the attribute name (other than 'group') already exists
func (epp *ExpParameter) AddAttribute(attrbName, attrbValue string) error {
// check whether the attribute name is valid for this parameter
if !ValidateAttribute(epp.ParamObj, attrbName) {
return fmt.Errorf("attribute name %s not allowed for parameter object type %s",
attrbName, epp.ParamObj)
}
// check for duplication of attribute as given, and just return if already present
for _, attrb := range epp.Attributes {
if attrb.AttrbName == attrbName && attrb.AttrbValue == attrbValue {
return nil
}
}
// if the attribute name is not 'group', report an attribute name conflict
if attrbName != "group" {
for _, attrb := range epp.Attributes {
if attrb.AttrbName == attrbName {
return fmt.Errorf("attribute name %s already exists for parameter object", attrbName)
}
}
}
// create a new AttrbStruct and add it to the list
epp.Attributes = append(epp.Attributes, *CreateAttrbStruct(attrbName, attrbValue))
return nil
}
// 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"`
}
// AddExpParameter includes the argument ExpParameter to the the Parameter list of the referencing
// ExpCfg
func (excfg *ExpCfg) AddExpParameter(exparam *ExpParameter) {
excfg.Parameters = append(excfg.Parameters, *exparam)
}
// 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)
}
err := f.Close()
if err != nil {
panic(err)
}
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 {
excfg := &ExpCfg{Name: name, Parameters: make([]ExpParameter, 0)}
return excfg
}
// ValidateParameter returns an error if the paramObj, attributes, and param values don't
// make sense taken together within an ExpParameter.
func ValidateParameter(paramObj string, attributes []AttrbStruct, param string) error {
if ExpParamObjs == nil {
GetExpParamDesc()
}
// the paramObj string has to be recognized as one of the permitted ones (stored in list ExpParamObjs)
if !slices.Contains(ExpParamObjs, paramObj) {
panic(fmt.Errorf("parameter paramObj %s is not recognized", paramObj))
}
// check the validity of each attribute
for _, attrb := range attributes {
if !ValidateAttribute(paramObj, attrb.AttrbName) {
panic(fmt.Errorf("attribute %s not value for parameter object type %s", attrb.AttrbName, 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 (excfg *ExpCfg) AddParameter(paramObj string, attributes []AttrbStruct, param, value string) error {
// validate the offered parameter values
err := ValidateParameter(paramObj, attributes, param)
if err != nil {
return err
}
// create an ExpParameter with these values
excp := CreateExpParameter(paramObj, attributes, param, value)
// save it
excfg.Parameters = append(excfg.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 (excfg *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(*excfg)
} else if pathExt == ".json" || pathExt == ".JSON" {
bytes, merr = json.MarshalIndent(*excfg, "", "\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)
}
err := f.Close()
if err != nil {
panic(err)
}
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
}
// UpdateExpCfg copies the ExpCfg parameters in the file referenced by the
// 'updatefile' name into the file of ExpCfg parameters in the file referenced by the
// 'orgfile' name
func UpdateExpCfg(orgfile, updatefile string, useYAML bool, dict []byte) {
// read in the experiment file
expCfg, err := ReadExpCfg(orgfile, useYAML, dict)
if err != nil {
panic(err)
}
// read in the update parameters
updateCfg, err2 := ReadExpCfg(updatefile, useYAML, []byte{})
if err2 != nil {
panic(err2)
}
for _, update := range updateCfg.Parameters {
err := expCfg.AddParameter(update.ParamObj, update.Attributes, update.Param, update.Value)
if err != nil {
panic(err)
}
}
// write out the modified configuration
err = expCfg.WriteToFile(orgfile)
if err != nil {
panic(err)
}
}
// ExpParamObjs , ExpAttributes , and ExpParams hold 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", "Endpoint", "Interface", "Network"}
ExpAttributes = make(map[string][]string)
ExpAttributes["Switch"] = []string{"name", "group", "model", "*"}
ExpAttributes["Router"] = []string{"name", "group", "model", "*"}
ExpAttributes["Endpoint"] = []string{"name", "model", "group", "*"}
ExpAttributes["Interface"] = []string{"name", "group", "devtype", "devname", "media", "network", "*"}
ExpAttributes["Network"] = []string{"name", "group", "media", "scale", "*"}
ExpParams = make(map[string][]string)
ExpParams["Switch"] = []string{"model", "buffer", "trace"}
ExpParams["Router"] = []string{"model", "buffer", "trace"}
ExpParams["Endpoint"] = []string{"trace", "model", "interruptdelay", "bckgrndSrv"}
ExpParams["Network"] = []string{"latency", "bandwidth", "capacity", "drop", "trace"}
ExpParams["Interface"] = []string{"latency", "delay", "buffer", "bandwidth",
"MTU", "rsrvd", "drop", "trace"}
}
return ExpParamObjs, ExpAttributes, ExpParams
}