forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
61 lines (54 loc) · 1.69 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
package tg
import (
"errors"
"fmt"
"reflect"
"github.com/usnistgov/ndn-dpdk/app/fetch"
"github.com/usnistgov/ndn-dpdk/app/fileserver"
"github.com/usnistgov/ndn-dpdk/app/tgconsumer"
"github.com/usnistgov/ndn-dpdk/app/tgproducer"
"github.com/usnistgov/ndn-dpdk/iface"
)
// Config describes traffic generator configuration.
type Config struct {
Face iface.LocatorWrapper `json:"face"`
Producer *tgproducer.Config `json:"producer,omitempty"`
FileServer *fileserver.Config `json:"fileServer,omitempty"`
Consumer *tgconsumer.Config `json:"consumer,omitempty"`
Fetcher *fetch.Config `json:"fetcher,omitempty"`
}
// Validate applies defaults and validates the configuration.
func (cfg *Config) Validate() error {
errs := []error{}
hasProducer, hasConsumer := "", ""
type validator interface {
Validate() error
}
validate := func(field string, v validator, sameKind *string) {
if reflect.ValueOf(v).IsNil() {
return
}
if sameKind != nil {
if *sameKind != "" {
errs = append(errs, fmt.Errorf("%s and %s cannot coexist", *sameKind, field))
}
*sameKind = field
}
if e := v.Validate(); e != nil {
errs = append(errs, fmt.Errorf("%s %w", field, e))
}
}
if cfg.Face.Locator == nil {
errs = append(errs, errors.New("face is missing"))
} else {
validate("face", &cfg.Face, nil)
}
validate("producer", cfg.Producer, &hasProducer)
validate("fileServer", cfg.FileServer, &hasProducer)
validate("consumer", cfg.Consumer, &hasConsumer)
validate("fetcher", cfg.Fetcher, &hasConsumer)
if hasProducer == "" && hasConsumer == "" {
errs = append(errs, errors.New("at least one producer or consumer module should be enabled"))
}
return errors.Join(errs...)
}