Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
FolderOrigin-RevId: /usr/local/google/home/gdennis/copybara/temp/folder-destination2722440756953871467/.
  • Loading branch information
GGN Engprod Team authored and greg-dennis committed May 13, 2022
1 parent b6a6148 commit 88dd577
Show file tree
Hide file tree
Showing 6 changed files with 40,593 additions and 40,272 deletions.
5 changes: 1 addition & 4 deletions ate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package ondatra

import (
"github.com/open-traffic-generator/snappi/gosnappi"
"github.com/openconfig/ondatra/binding"
)

Expand All @@ -28,9 +27,7 @@ type ATEDevice struct {
// OTG returns a handle to the OTG API.
func (a *ATEDevice) OTG() *OTG {
if a.otg == nil {
// TODO: Replace with a Binding.DialOTG method.
api := gosnappi.NewApi()
a.otg = &OTG{ate: a.res.(binding.ATE), api: api}
a.otg = &OTG{ate: a.res.(binding.ATE)}
}
return a.otg
}
Expand Down
6 changes: 3 additions & 3 deletions dut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func initDUTFakes(t *testing.T) {
}
}

func TestPushConfig(t *testing.T) {
func TestConfigPush(t *testing.T) {
initDUTFakes(t)
dutArista := DUT(t, "dut_arista")
testsPass := []struct {
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestPushConfig(t *testing.T) {
}
}

func TestPushConfigErrors(t *testing.T) {
func TestConfigPushErrors(t *testing.T) {
initDUTFakes(t)
dutArista := DUT(t, "dut_arista")
testsFail := []struct {
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestPushConfigErrors(t *testing.T) {
}
}

func TestAppendConfig(t *testing.T) {
func TestConfigAppend(t *testing.T) {
initDUTFakes(t)
gotConfig = ""
gotReset = false
Expand Down
135 changes: 135 additions & 0 deletions internal/otg/otg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package otg provides Open Traffic Generator API business logic.
package otg

import (
"fmt"
"sync"

"github.com/open-traffic-generator/snappi/gosnappi"
"github.com/openconfig/ondatra/binding"
)

var (
mu sync.Mutex
apis = make(map[binding.ATE]gosnappi.GosnappiApi)
)

func fetchAPI(ate binding.ATE) (gosnappi.GosnappiApi, error) {
mu.Lock()
defer mu.Unlock()
api, ok := apis[ate]
if !ok {
var err error
api, err = ate.DialOTG()
if err != nil {
return nil, err
}
apis[ate] = api
}
return api, nil
}

// NewConfig constructs a new config for the specified ATE.
func NewConfig(ate binding.ATE) (gosnappi.Config, error) {
api, err := fetchAPI(ate)
if err != nil {
return nil, err
}
return api.NewConfig(), nil
}

// FetchConfig fetches and returns the config on the specified ATE.
func FetchConfig(ate binding.ATE) (gosnappi.Config, error) {
api, err := fetchAPI(ate)
if err != nil {
return nil, err
}
return api.GetConfig()
}

// PushConfig pushes a config to the specified ATE.
// The first return value are any non-fatal warnings encountered.
func PushConfig(ate binding.ATE, cfg gosnappi.Config) ([]string, error) {
api, err := fetchAPI(ate)
if err != nil {
return nil, err
}
for _, cfgPort := range cfg.Ports().Items() {
bindPort, ok := ate.Ports()[cfgPort.Name()]
if !ok {
var tbPorts []string
for p := range ate.Ports() {
tbPorts = append(tbPorts, p)
}
return nil, fmt.Errorf("port %s in config is not one of the ATE ports %v", cfgPort.Name(), tbPorts)
}
cfgPort.SetLocation(bindPort.Name)
}
resp, err := api.SetConfig(cfg)
if err != nil {
return nil, err
}
return resp.Warnings(), nil
}

// StartProtocols starts protocol on the specified ATE.
// The first return value are any non-fatal warnings encountered.
func StartProtocols(ate binding.ATE) ([]string, error) {
return setProtocolState(ate, gosnappi.ProtocolStateState.START)
}

// StopProtocols stops protocol on the specified ATE.
// The first return value are any non-fatal warnings encountered.
func StopProtocols(ate binding.ATE) ([]string, error) {
return setProtocolState(ate, gosnappi.ProtocolStateState.STOP)
}

func setProtocolState(ate binding.ATE, state gosnappi.ProtocolStateStateEnum) ([]string, error) {
api, err := fetchAPI(ate)
if err != nil {
return nil, err
}
resp, err := api.SetProtocolState(api.NewProtocolState().SetState(state))
if err != nil {
return nil, err
}
return resp.Warnings(), nil
}

// StartTraffic starts traffic on the specified ATE.
// The first return value are any non-fatal warnings encountered.
func StartTraffic(ate binding.ATE) ([]string, error) {
return setTransmitState(ate, gosnappi.TransmitStateState.START)
}

// StopTraffic stops traffic on the specified ATE.
// The first return value are any non-fatal warnings encountered.
func StopTraffic(ate binding.ATE) ([]string, error) {
return setTransmitState(ate, gosnappi.TransmitStateState.STOP)
}

func setTransmitState(ate binding.ATE, state gosnappi.TransmitStateStateEnum) ([]string, error) {
api, err := fetchAPI(ate)
if err != nil {
return nil, err
}
resp, err := api.SetTransmitState(api.NewTransmitState().SetState(state))
if err != nil {
return nil, err
}
return resp.Warnings(), nil
}
75 changes: 36 additions & 39 deletions otg.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,109 +16,106 @@ package ondatra

import (
"fmt"
"strconv"
"testing"

"github.com/open-traffic-generator/snappi/gosnappi"
"github.com/openconfig/ondatra/binding"
"github.com/openconfig/ondatra/internal/debugger"
"github.com/openconfig/ondatra/internal/otg"
)

// OTG provides the Open Traffic Generator API to an ATE.
type OTG struct {
ate binding.ATE
api gosnappi.GosnappiApi
}

func (o *OTG) String() string {
return fmt.Sprintf("{ate: %s}", o.ate)
}

// NewConfig creates a new OTG config.
func (o *OTG) NewConfig() gosnappi.Config {
return o.api.NewConfig()
func (o *OTG) NewConfig(t testing.TB) gosnappi.Config {
t.Helper()
debugger.ActionStarted(t, "Creating new config for %s", o.ate)
cfg, err := otg.NewConfig(o.ate)
if err != nil {
t.Fatalf("NewConfig(t) on %s: %v", o.ate, err)
}
return cfg
}

// PushConfig pushes config to the ATE.
func (o *OTG) PushConfig(t testing.TB, cfg gosnappi.Config) {
t.Helper()
const locFormat = "service-ate%d.ceos-simple.svc.cluster.local:5555+service-ate%d.ceos-simple.svc.cluster.local:50071"
for _, p := range cfg.Ports().Items() {
portNum, err := strconv.Atoi(p.Name()[1:])
if err != nil {
t.Fatal(err)
}
p.SetLocation(fmt.Sprintf(locFormat, portNum, portNum))
}
debugger.ActionStarted(t, "Pushing config to %s", o.ate)
resp, err := o.api.SetConfig(cfg)
warns, err := otg.PushConfig(o.ate, cfg)
if err != nil {
t.Fatalf("PushConfig(t) on %s: %v", o, err)
t.Fatalf("PushConfig(t) on %s: %v", o.ate, err)
}
if warns := resp.Warnings(); len(warns) > 0 {
t.Logf("PushConfig(t) on %s non-fatal warnings: %v", o, warns)
if len(warns) > 0 {
t.Logf("PushConfig(t) on %s non-fatal warnings: %v", o.ate, warns)
}
}

// FetchConfig fetches config from the ATE.
func (o *OTG) FetchConfig(t testing.TB) gosnappi.Config {
t.Helper()
debugger.ActionStarted(t, "Fetching config from %s", o.ate)
cfg, err := o.api.GetConfig()
cfg, err := otg.FetchConfig(o.ate)
if err != nil {
t.Fatalf("FetchConfig(t) from %s: %v", o, err)
t.Fatalf("FetchConfig(t) on %s: %v", o.ate, err)
}
return cfg
}

// StartProtocols starts protocols on the ATE.
func (o *OTG) StartProtocols(t testing.TB) {
t.Helper()
debugger.ActionStarted(t, "Starting protocols on %s", o.ate)
started := o.api.NewProtocolState().SetState(gosnappi.ProtocolStateState.START)
resp, err := o.api.SetProtocolState(started)
warns, err := otg.StartProtocols(o.ate)
if err != nil {
t.Fatalf("StartProtocols(t) from %s: %v", o, err)
t.Fatalf("StartProtocols(t) on %s: %v", o.ate, err)
}
if warns := resp.Warnings(); len(warns) > 0 {
t.Logf("StartProtocols(t) on %s non-fatal warnings: %v", o, warns)
if len(warns) > 0 {
t.Logf("StartProtocols(t) on %s non-fatal warnings: %v", o.ate, warns)
}
}

// StopProtocols stops protocols on the ATE.
func (o *OTG) StopProtocols(t testing.TB) {
debugger.ActionStarted(t, "Stopping rotocols on %s", o.ate)
stopped := o.api.NewProtocolState().SetState(gosnappi.ProtocolStateState.STOP)
resp, err := o.api.SetProtocolState(stopped)
t.Helper()
debugger.ActionStarted(t, "Stopping protocols on %s", o.ate)
warns, err := otg.StopProtocols(o.ate)
if err != nil {
t.Fatalf("StopProtocols(t) from %s: %v", o, err)
t.Fatalf("StopProtocols(t) on %s: %v", o.ate, err)
}
if warns := resp.Warnings(); len(warns) > 0 {
t.Logf("StopProtocols(t) on %s non-fatal warnings: %v", o, warns)
if len(warns) > 0 {
t.Logf("StopProtocols(t) on %s non-fatal warnings: %v", o.ate, warns)
}
}

// StartTraffic starts traffic on the ATE.
func (o *OTG) StartTraffic(t testing.TB) {
t.Helper()
debugger.ActionStarted(t, "Starting traffic on %s", o.ate)
started := o.api.NewTransmitState().SetState(gosnappi.TransmitStateState.START)
resp, err := o.api.SetTransmitState(started)
warns, err := otg.StartTraffic(o.ate)
if err != nil {
t.Fatalf("StartTraffic(t) from %s: %v", o, err)
t.Fatalf("StartTraffic(t) on %s: %v", o.ate, err)
}
if warns := resp.Warnings(); len(warns) > 0 {
t.Logf("StartTraffic(t) on %s non-fatal warnings: %v", o, warns)
if len(warns) > 0 {
t.Logf("StartTraffic(t) on %s non-fatal warnings: %v", o.ate, warns)
}
}

// StopTraffic stops traffic on the ATE.
func (o *OTG) StopTraffic(t testing.TB) {
t.Helper()
debugger.ActionStarted(t, "Stopping traffic on %s", o.ate)
stopped := o.api.NewTransmitState().SetState(gosnappi.TransmitStateState.STOP)
resp, err := o.api.SetTransmitState(stopped)
warns, err := otg.StopTraffic(o.ate)
if err != nil {
t.Fatalf("StopTraffic(t) from %s: %v", o, err)
t.Fatalf("StopTraffic(t) on %s: %v", o.ate, err)
}
if warns := resp.Warnings(); len(warns) > 0 {
t.Logf("StopTraffic(t) on %s non-fatal warnings: %v", o, warns)
if len(warns) > 0 {
t.Logf("StopTraffic(t) on %s non-fatal warnings: %v", o.ate, warns)
}
}
Loading

0 comments on commit 88dd577

Please sign in to comment.