Skip to content

Commit

Permalink
checkOTGError for run with gosnappi 0.11.6
Browse files Browse the repository at this point in the history
  • Loading branch information
bortok committed Apr 19, 2023
1 parent f7d0cff commit 7f4d44d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ otgen

dist/
.DS_Store
*.bak
51 changes: 29 additions & 22 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ func initOTG() (gosnappi.GosnappiApi, gosnappi.Config) {

func applyConfig(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.GosnappiApi, gosnappi.Config) {
log.Info("Applying OTG config...")
res, err := api.SetConfig(config)
checkResponse(res, err)
_, err := api.SetConfig(config)
checkOTGError(api, err)
log.Info("ready.")
return api, config
}
Expand All @@ -217,8 +217,8 @@ func startProtocols(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.
if len(config.Devices().Items()) > 0 { // TODO also if LAGs are configured
log.Info("Starting protocols...")
ps := api.NewProtocolState().SetState(gosnappi.ProtocolStateState.START)
res, err := api.SetProtocolState(ps)
checkResponse(res, err)
_, err := api.SetProtocolState(ps)
checkOTGError(api, err)
log.Info("waiting for protocols to come up...")

// Detect protocols present in the configuration
Expand Down Expand Up @@ -320,8 +320,8 @@ func runTraffic(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.Gosn
// TODO check we have traffic flows
log.Info("Starting traffic...")
ts := api.NewTransmitState().SetState(gosnappi.TransmitStateState.START)
res, err := api.SetTransmitState(ts)
checkResponse(res, err)
_, err := api.SetTransmitState(ts)
checkOTGError(api, err)
log.Info("started...")

targetTx, trafficETA := calculateTrafficTargets(config)
Expand All @@ -331,7 +331,7 @@ func runTraffic(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.Gosn
req := api.NewMetricsRequest()
req.Port()
metrics, err := api.GetMetrics(req)
checkResponse(metrics, err)
checkOTGError(api, err)

start := time.Now()

Expand Down Expand Up @@ -377,8 +377,8 @@ func stopTraffic(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.Gos
// TODO consider defer
log.Info("Stopping traffic...")
ts := api.NewTransmitState().SetState(gosnappi.TransmitStateState.STOP)
res, err := api.SetTransmitState(ts)
checkResponse(res, err)
_, err := api.SetTransmitState(ts)
checkOTGError(api, err)
log.Info("stopped.")
return api, config
}
Expand All @@ -392,8 +392,8 @@ func stopProtocols(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.G
if len(config.Devices().Items()) > 0 { // TODO also if LAGs are configured
log.Info("Stopping protocols...")
ps := api.NewProtocolState().SetState(gosnappi.ProtocolStateState.STOP)
res, err := api.SetProtocolState(ps)
checkResponse(res, err)
_, err := api.SetProtocolState(ps)
checkOTGError(api, err)
log.Info("stopped.")
}
return api, config
Expand Down Expand Up @@ -475,19 +475,26 @@ func isTrafficRunningWithETA(mr gosnappi.MetricsResponse, targetTx int64, start
return trafficRunning
}

// print otg api response content
func checkResponse(res interface{}, err error) {
// check for OTG error and print it
func checkOTGError(api gosnappi.GosnappiApi, err error) {
if err != nil {
log.Fatal(err)
}
switch v := res.(type) {
case gosnappi.MetricsResponse:
case gosnappi.ResponseWarning:
for _, w := range v.Warnings() {
log.Warn("WARNING:", w)
errData, ok := api.FromError(err)
// helper function to parse error
// returns a bool with err, indicating weather the error was of otg error format
if ok {
log.Errorf("OTG API error code: %d", errData.Code())
if errData.HasKind() {
log.Errorf("OTG API error kind: %v", errData.Kind())
}
log.Errorf("OTG API error messages:")
for _, e := range errData.Errors() {
log.Errorf(e)
}
log.Fatalln("Fatal OTG error, exiting...")
} else {
log.Errorf("Fatal OTG error: %v\n", err)
log.Fatalln("Exiting...")
}
default:
log.Fatal("Unknown response type:", v)
}
}

Expand Down
17 changes: 4 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
module github.com/open-traffic-generator/otgen

go 1.17
go 1.19

require (
github.com/drone/envsubst v1.0.3
github.com/golang/protobuf v1.5.3
github.com/gosuri/uilive v0.0.4
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mum4k/termdash v0.18.0
github.com/olekukonko/tablewriter v0.0.5
github.com/open-traffic-generator/snappi/gosnappi v0.10.9
github.com/openconfig/gnmi v0.9.1
github.com/open-traffic-generator/snappi/gosnappi v0.11.6
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.8.0
google.golang.org/protobuf v1.30.0
)

require (
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.1 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.5.4 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/openconfig/goyang v0.0.0-20200115183954-d0a48929f0ea // indirect
github.com/openconfig/grpctunnel v0.0.0-20220819142823-6f5422b8ca70 // indirect
github.com/openconfig/ygot v0.6.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/grpc v1.54.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 7f4d44d

Please sign in to comment.