Skip to content

Commit

Permalink
Merge pull request #63 from open-traffic-generator/otg-v1.0
Browse files Browse the repository at this point in the history
Upgrading to gosnappi v1.0.0
  • Loading branch information
bortok authored Feb 20, 2024
2 parents a957be5 + 77ba5b0 commit d21d22d
Show file tree
Hide file tree
Showing 17 changed files with 514 additions and 82 deletions.
2 changes: 1 addition & 1 deletion cmd/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func newBgp(config gosnappi.Config) {
}

// Print the OTG configuration constructed
otgYaml, err := config.ToYaml()
otgYaml, err := config.Marshal().ToYaml()
if err != nil {
log.Fatal(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ func init() {
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func readOtgStdin(api gosnappi.GosnappiApi) gosnappi.Config {
func readOtgStdin(api gosnappi.Api) gosnappi.Config {
otgbytes, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
otg := string(otgbytes)

config := api.NewConfig()
err = config.FromYaml(otg) // Thus YAML is assumed by default, and as a superset of JSON, it works for JSON format too
config := gosnappi.NewConfig()
err = config.Unmarshal().FromYaml(otg) // Thus YAML is assumed by default, and as a superset of JSON, it works for JSON format too
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,8 @@ func init() {
}

func createDevice() {
// Create a new API handle
api := gosnappi.NewApi()

// Create a flow
newDevice(api.NewConfig())
newDevice(gosnappi.NewConfig())
}

func addDevice() {
Expand Down Expand Up @@ -147,7 +144,7 @@ func newDevice(config gosnappi.Config) {
SetPrefix(devicePrefixv4)

// Print the OTG configuration constructed
otgYaml, err := config.ToYaml()
otgYaml, err := config.Marshal().ToYaml()
if err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 2 additions & 7 deletions cmd/flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,8 @@ func init() {
}

func createFlow() {
// Create a new API handle
api := gosnappi.NewApi()

// Create a flow
newFlow(api.NewConfig())
newFlow(gosnappi.NewConfig())
}

func addFlow() {
Expand Down Expand Up @@ -350,14 +347,12 @@ func newFlow(config gosnappi.Config) {
if flowDstMacExplicit {
if flowDstMac == "auto" {
log.Debugf("Device-bound flow %s will use \"auto\" mode for the destination MAC", flowName)
eth.Dst().SetChoice("auto")
} else {
log.Debugf("Device-bound flow %s will use an explicitly defined destination MAC %s", flowName, flowDstMac)
eth.Dst().SetValue(flowDstMac)
}
} else {
log.Debugf("Device-bound flow %s will use \"auto\" mode for the destination MAC by default", flowName)
eth.Dst().SetChoice("auto")
}
} else {
portRx := otgGetOrCreatePort(config, flowRxPort, flowRxLocation)
Expand Down Expand Up @@ -420,7 +415,7 @@ func newFlow(config gosnappi.Config) {
}

// Print traffic configuration constructed
otgYaml, err := config.ToYaml()
otgYaml, err := config.Marshal().ToYaml()
if err != nil {
log.Fatal(err)
}
Expand Down
42 changes: 23 additions & 19 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func init() {
runCmd.Flags().StringVarP(&protoMode, "protocols", "", "auto", "Protocols control mode:\n \"auto\" - detect, start and stop\n \"ignore\" - do not detect, start or stop,\n \"keep\" - detect, start but do not stop\n ")
}

func initOTG() (gosnappi.GosnappiApi, gosnappi.Config) {
func initOTG() (gosnappi.Api, gosnappi.Config) {
var otgbytes []byte
var err error
if otgFile != "" { // Read OTG config from file
Expand All @@ -187,12 +187,12 @@ func initOTG() (gosnappi.GosnappiApi, gosnappi.Config) {
api.NewHttpTransport().SetLocation(otgURL).SetVerify(!otgIgnoreX509)

// Create a new traffic configuration that will be set on traffic generator
config := api.NewConfig()
config := gosnappi.NewConfig()
// These are mutually exclusive parameters
if otgJson {
err = config.FromJson(otg)
err = config.Unmarshal().FromJson(otg)
} else {
err = config.FromYaml(otg) // Thus YAML is assumed by default, and as a superset of JSON, it actually works for JSON format too
err = config.Unmarshal().FromYaml(otg) // Thus YAML is assumed by default, and as a superset of JSON, it actually works for JSON format too
}
if err != nil {
log.Fatal(err)
Expand All @@ -201,21 +201,21 @@ func initOTG() (gosnappi.GosnappiApi, gosnappi.Config) {
return api, config
}

func applyConfig(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.GosnappiApi, gosnappi.Config) {
func applyConfig(api gosnappi.Api, config gosnappi.Config) (gosnappi.Api, gosnappi.Config) {
log.Info("Applying OTG config...")
res, err := api.SetConfig(config)
checkResponse(api, res, err)
log.Info("ready.")
return api, config
}

func startProtocols(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.GosnappiApi, gosnappi.Config) {
func startProtocols(api gosnappi.Api, config gosnappi.Config) (gosnappi.Api, gosnappi.Config) {
if protoMode == "ignore" {
return api, config
}
if len(config.Devices().Items()) > 0 { // TODO also if LAGs are configured
log.Info("Starting protocols...")
ps := api.NewControlState()
ps := gosnappi.NewControlState()
ps.Protocol().All().SetState(gosnappi.StateProtocolAllState.START)
res, err := api.SetControlState(ps)
checkResponse(api, res, err)
Expand Down Expand Up @@ -250,7 +250,7 @@ func startProtocols(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.
}

// Wait for configured protocols to come up
req := api.NewMetricsRequest()
req := gosnappi.NewMetricsRequest()
for {
var protocolState = make(map[string]bool)
for p, c := range configuredProtocols {
Expand Down Expand Up @@ -315,11 +315,11 @@ func startProtocols(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.
return api, config
}

func runTraffic(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.GosnappiApi, gosnappi.Config) {
func runTraffic(api gosnappi.Api, config gosnappi.Config) (gosnappi.Api, gosnappi.Config) {
// start transmitting configured flows
// TODO check we have traffic flows
log.Info("Starting traffic...")
ts := api.NewControlState()
ts := gosnappi.NewControlState()
ts.Traffic().FlowTransmit().SetState(gosnappi.StateTrafficFlowTransmitState.START)
res, err := api.SetControlState(ts)
checkResponse(api, res, err)
Expand All @@ -329,7 +329,7 @@ func runTraffic(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.Gosn
log.Infof("Total packets to transmit: %d, ETA is: %s\n", targetTx, trafficETA)

// use port metrics to initially determine if traffic is running
req := api.NewMetricsRequest()
req := gosnappi.NewMetricsRequest()
req.Port()
metrics, err := api.GetMetrics(req)
checkResponse(api, metrics, err)
Expand Down Expand Up @@ -373,27 +373,27 @@ func runTraffic(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.Gosn
return api, config
}

func stopTraffic(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.GosnappiApi, gosnappi.Config) {
func stopTraffic(api gosnappi.Api, config gosnappi.Config) (gosnappi.Api, gosnappi.Config) {
// stop transmitting traffic
// TODO consider defer
log.Info("Stopping traffic...")
ts := api.NewControlState()
ts := gosnappi.NewControlState()
ts.Traffic().FlowTransmit().SetState(gosnappi.StateTrafficFlowTransmitState.STOP)
res, err := api.SetControlState(ts)
checkResponse(api, res, err)
log.Info("stopped.")
return api, config
}

func stopProtocols(api gosnappi.GosnappiApi, config gosnappi.Config) (gosnappi.GosnappiApi, gosnappi.Config) {
func stopProtocols(api gosnappi.Api, config gosnappi.Config) (gosnappi.Api, gosnappi.Config) {
// stop protocols
if protoMode == "ignore" || protoMode == "keep" {
return api, config
}
// TODO consider defer
if len(config.Devices().Items()) > 0 { // TODO also if LAGs are configured
log.Info("Stopping protocols...")
ps := api.NewControlState()
ps := gosnappi.NewControlState()
ps.Protocol().All().SetState(gosnappi.StateProtocolAllState.STOP)
res, err := api.SetControlState(ps)
checkResponse(api, res, err)
Expand Down Expand Up @@ -479,9 +479,9 @@ func isTrafficRunningWithETA(mr gosnappi.MetricsResponse, targetTx uint64, start
}

// check for OTG error and print it
func checkOTGError(api gosnappi.GosnappiApi, err error) {
func checkOTGError(api gosnappi.Api, err error) {
if err != nil {
errData, ok := api.FromError(err)
errData, ok := gosnappi.FromError(err)
// helper function to parse error
// returns a bool with err, indicating weather the error was of otg error format
if ok {
Expand All @@ -502,7 +502,7 @@ func checkOTGError(api gosnappi.GosnappiApi, err error) {
}

// print otg api response content
func checkResponse(api gosnappi.GosnappiApi, res interface{}, err error) {
func checkResponse(api gosnappi.Api, res interface{}, err error) {
checkOTGError(api, err)
switch v := res.(type) {
case gosnappi.MetricsResponse:
Expand Down Expand Up @@ -535,7 +535,11 @@ func printMetricsResponse(mr gosnappi.MetricsResponse, err error) {
}

func printMetricsResponseRawJson(mr gosnappi.MetricsResponse) {
j, err := otgMetricsResponseToJson(mr.Msg())
p, err := mr.Marshal().ToProto()
if err != nil {
log.Fatal(err)
}
j, err := otgMetricsResponseToJson(p)
if err == nil {
fmt.Println(string(j))
} else {
Expand Down
8 changes: 6 additions & 2 deletions cmd/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func transformStdInWithTemplate(t string) {
text := scanner.Text()

mr := gosnappi.NewMetricsResponse()
err := mr.FromJson(text)
err := mr.Unmarshal().FromJson(text)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -185,7 +185,11 @@ func transformMetricsResponse(mr gosnappi.MetricsResponse, tmpl string) {
log.Fatal(err)
}

err = t.Execute(os.Stdout, mr.Msg())
msg, err := mr.Marshal().ToProto()
if err != nil {
log.Fatal(err)
}
err = t.Execute(os.Stdout, msg)
if err != nil {
log.Fatal(err)
}
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ require (
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.13.2
github.com/open-traffic-generator/snappi/gosnappi v1.0.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.32.0
)

require (
Expand All @@ -25,11 +25,11 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.61.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit d21d22d

Please sign in to comment.