From 68200440de42e4370b65d8c85616e37d3ea67f22 Mon Sep 17 00:00:00 2001 From: Abhijit Dhar Date: Tue, 29 Sep 2026 07:28:16 +0000 Subject: [PATCH 1/4] enhanced PushConfig function to make otg ports up during push config. Also provided alternative function to skip it if required PushConfigOpts() --- otg/otg.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/otg/otg.go b/otg/otg.go index 1106cccb..18a89766 100644 --- a/otg/otg.go +++ b/otg/otg.go @@ -56,8 +56,27 @@ func (o *OTG) String() string { return fmt.Sprintf("{ate: %s}", o.ate) } +// PushConfigOptions contains options for PushConfigOpts. +type PushConfigOptions struct { + // LinkUp, when true, brings up the links for the ports just configured + // after pushing the config. When false, the link-up step is skipped. + LinkUp bool +} + // PushConfig pushes config to the ATE. func (o *OTG) PushConfig(t testing.TB, cfg gosnappi.Config) { + t.Helper() + o.pushConfig(t, cfg, true) +} + +// PushConfigOpts pushes config to the ATE, using opts to control optional +// behavior. If opts is nil, the link-up step is skipped. +func (o *OTG) PushConfigOpts(t testing.TB, cfg gosnappi.Config, opts *PushConfigOptions) { + t.Helper() + o.pushConfig(t, cfg, opts != nil && opts.LinkUp) +} + +func (o *OTG) pushConfig(t testing.TB, cfg gosnappi.Config, linkUp bool) { t.Helper() t = events.ActionStarted(t, "Pushing config to %s", o.ate) warns, err := pushConfig(context.Background(), o.ate, cfg) @@ -67,6 +86,32 @@ func (o *OTG) PushConfig(t testing.TB, cfg gosnappi.Config) { if len(warns) > 0 { t.Logf("PushConfig(t) on %s non-fatal warnings: %v", o.ate, warns) } + // Bring up the links for the ports just configured, unless no ports are + // configured or the caller opted out of the link-up step. + if !linkUp || len(cfg.Ports().Items()) == 0 { + t.Logf("Skipping link-up step for %s", o.ate) + return + } + warnsLinkup, errLinkup := o.setLinkState(context.Background(), gosnappi.StatePortLinkState.UP) + if errLinkup != nil { + t.Fatalf("PushConfig(t) on %s: failed to set port link state up: %v", o.ate, errLinkup) + } + + if len(warnsLinkup) > 0 { + t.Logf("PushConfig(t) on %s non-fatal warnings when setting port link state up: %v", + o.ate, + warnsLinkup) + } +} + +func (o *OTG) setLinkState(ctx context.Context, state gosnappi.StatePortLinkStateEnum) ([]string, error) { + controlState := gosnappi.NewControlState() + controlState.Port().Link().SetState(state) + resp, err := o.setControlState(ctx, controlState) + if err != nil { + return nil, err + } + return resp.Warnings(), nil } func pushConfig(ctx context.Context, ate binding.ATE, cfg gosnappi.Config) ([]string, error) { From 023c42f7f0120e05781a8631293064b3effe9487 Mon Sep 17 00:00:00 2001 From: Abhijit Dhar Date: Mon, 2 Nov 2026 09:45:32 +0000 Subject: [PATCH 2/4] Log link state before and after link up --- otg/otg.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ otg/otg_test.go | 2 +- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/otg/otg.go b/otg/otg.go index 18a89766..62765900 100644 --- a/otg/otg.go +++ b/otg/otg.go @@ -24,6 +24,7 @@ package otg import ( "fmt" + "strings" "testing" "time" @@ -34,6 +35,7 @@ import ( "github.com/openconfig/ondatra/gnmi" "github.com/openconfig/ondatra/internal/events" "github.com/openconfig/ondatra/internal/rawapis" + "github.com/openconfig/ygnmi/ygnmi" gpb "github.com/openconfig/gnmi/proto/gnmi" ) @@ -92,7 +94,78 @@ func (o *OTG) pushConfig(t testing.TB, cfg gosnappi.Config, linkUp bool) { t.Logf("Skipping link-up step for %s", o.ate) return } + // logLinkStates logs each configured port's link state. With an expected + // state of "UP" or "DOWN", it first polls every pollIntervalMs until all + // ports reach that state or timeoutSec elapses; "NONE" logs immediately. + // Telemetry errors and timeouts are tolerated silently. + logLinkStates := func(when string, timeoutSec int, expectedState string, pollIntervalMs int) { + const queryTimeout = 10 * time.Second + ctx := context.Background() + gnmiClient, err := rawapis.FetchOTGGNMI(ctx, o.ate) + if err != nil { + return + } + client, err := ygnmi.NewClient(gnmiClient, ygnmi.WithTarget(o.ate.Name())) + if err != nil { + return + } + readStates := func(deadline time.Time) map[string]fmt.Stringer { + states := make(map[string]fmt.Stringer) + for _, cfgPort := range cfg.Ports().Items() { + timeout := queryTimeout + if !deadline.IsZero() { + timeout = max(min(queryTimeout, time.Until(deadline)), time.Second) + } + queryCtx, cancel := context.WithTimeout(ctx, timeout) + state, err := ygnmi.Get(queryCtx, client, gnmi.OTG().Port(cfgPort.Name()).Link().State()) + cancel() + if err == nil { + states[cfgPort.Name()] = state + } + } + return states + } + allInState := func(states map[string]fmt.Stringer, want string) bool { + for _, cfgPort := range cfg.Ports().Items() { + state, ok := states[cfgPort.Name()] + if !ok || state.String() != want { + return false + } + } + return true + } + + expected := strings.ToUpper(strings.TrimSpace(expectedState)) + var states map[string]fmt.Stringer + if expected == "UP" || expected == "DOWN" { + deadline := time.Now().Add(time.Duration(max(timeoutSec, 0)) * time.Second) + pollInterval := time.Duration(max(pollIntervalMs, 1)) * time.Millisecond + states = readStates(deadline) + for !allInState(states, expected) { + remaining := time.Until(deadline) + if remaining <= 0 { + break + } + time.Sleep(min(pollInterval, remaining)) + states = readStates(deadline) + } + } else { + states = readStates(time.Time{}) + } + + for _, cfgPort := range cfg.Ports().Items() { + if state, ok := states[cfgPort.Name()]; ok { + t.Logf("PushConfig(t) on: %s, %s : %v", + cfgPort.Name(), + when, + state) + } + } + } + + logLinkStates("before setting link state actual link is", 10, "UP", 500) warnsLinkup, errLinkup := o.setLinkState(context.Background(), gosnappi.StatePortLinkState.UP) + logLinkStates("after setting link state up actual link is", 10, "UP", 500) if errLinkup != nil { t.Fatalf("PushConfig(t) on %s: failed to set port link state up: %v", o.ate, errLinkup) } diff --git a/otg/otg_test.go b/otg/otg_test.go index 936449b1..38fdbc58 100644 --- a/otg/otg_test.go +++ b/otg/otg_test.go @@ -52,7 +52,7 @@ func TestFetchConfig(t *testing.T) { func TestPushConfig(t *testing.T) { wantCfg := gosnappi.NewConfig() wantCfg.Ports().Add().SetName("port1") - otgAPI.PushConfig(t, wantCfg) + otgAPI.PushConfigOpts(t, wantCfg, &PushConfigOptions{}) if gotCfg := fakeSnappi.config; wantCfg != gotCfg { t.Errorf("PushConfig got unexpected config %v, want %v", gotCfg, wantCfg) } From c9b9612821a977c04ca44eaa1f81228c6fc7c5fe Mon Sep 17 00:00:00 2001 From: abhijit-dhar Date: Thu, 12 Nov 2026 09:34:59 +0000 Subject: [PATCH 3/4] otg: skip redundant port-state wait after link-up --- otg/otg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otg/otg.go b/otg/otg.go index 62765900..ecfafad2 100644 --- a/otg/otg.go +++ b/otg/otg.go @@ -165,7 +165,7 @@ func (o *OTG) pushConfig(t testing.TB, cfg gosnappi.Config, linkUp bool) { logLinkStates("before setting link state actual link is", 10, "UP", 500) warnsLinkup, errLinkup := o.setLinkState(context.Background(), gosnappi.StatePortLinkState.UP) - logLinkStates("after setting link state up actual link is", 10, "UP", 500) + //logLinkStates("after setting link state up actual link is", 10, "UP", 500) if errLinkup != nil { t.Fatalf("PushConfig(t) on %s: failed to set port link state up: %v", o.ate, errLinkup) } From cc64f345f7e5b91177e147b5a8662aeae29a7329 Mon Sep 17 00:00:00 2001 From: abhijit-dhar Date: Wed, 18 Nov 2026 08:57:12 +0000 Subject: [PATCH 4/4] removed the logs that prints the otg link state --- otg/otg.go | 141 ++++++++++++++++++++++++++--------------------------- 1 file changed, 70 insertions(+), 71 deletions(-) diff --git a/otg/otg.go b/otg/otg.go index ecfafad2..af027ade 100644 --- a/otg/otg.go +++ b/otg/otg.go @@ -94,86 +94,85 @@ func (o *OTG) pushConfig(t testing.TB, cfg gosnappi.Config, linkUp bool) { t.Logf("Skipping link-up step for %s", o.ate) return } - // logLinkStates logs each configured port's link state. With an expected - // state of "UP" or "DOWN", it first polls every pollIntervalMs until all - // ports reach that state or timeoutSec elapses; "NONE" logs immediately. - // Telemetry errors and timeouts are tolerated silently. - logLinkStates := func(when string, timeoutSec int, expectedState string, pollIntervalMs int) { - const queryTimeout = 10 * time.Second - ctx := context.Background() - gnmiClient, err := rawapis.FetchOTGGNMI(ctx, o.ate) - if err != nil { - return - } - client, err := ygnmi.NewClient(gnmiClient, ygnmi.WithTarget(o.ate.Name())) - if err != nil { - return - } - readStates := func(deadline time.Time) map[string]fmt.Stringer { - states := make(map[string]fmt.Stringer) - for _, cfgPort := range cfg.Ports().Items() { - timeout := queryTimeout - if !deadline.IsZero() { - timeout = max(min(queryTimeout, time.Until(deadline)), time.Second) - } - queryCtx, cancel := context.WithTimeout(ctx, timeout) - state, err := ygnmi.Get(queryCtx, client, gnmi.OTG().Port(cfgPort.Name()).Link().State()) - cancel() - if err == nil { - states[cfgPort.Name()] = state - } + //logLinkStates(t, o.ate, cfg, "before setting link state actual link is", 10, "UP", 500) + warnsLinkup, errLinkup := o.setLinkState(context.Background(), gosnappi.StatePortLinkState.UP) + if errLinkup != nil { + t.Fatalf("PushConfig(t) on %s: failed to set port link state up: %v", o.ate, errLinkup) + } + // logLinkStates(t, o.ate, cfg, "after setting link state actual link is", 10, "UP", 500) + if len(warnsLinkup) > 0 { + t.Logf("PushConfig(t) on %s non-fatal warnings when setting port link state up: %v", + o.ate, + warnsLinkup) + } +} + +// logLinkStates logs each configured port's link state. With an expected +// state of "UP" or "DOWN", it first polls every pollIntervalMs until all +// ports reach that state or timeoutSec elapses; "NONE" logs immediately. +// Telemetry errors and timeouts are tolerated silently. +func logLinkStates(t testing.TB, ate binding.ATE, cfg gosnappi.Config, when string, timeoutSec int, expectedState string, pollIntervalMs int) { + const queryTimeout = 10 * time.Second + ctx := context.Background() + gnmiClient, err := rawapis.FetchOTGGNMI(ctx, ate) + if err != nil { + return + } + client, err := ygnmi.NewClient(gnmiClient, ygnmi.WithTarget(ate.Name())) + if err != nil { + return + } + readStates := func(deadline time.Time) map[string]fmt.Stringer { + states := make(map[string]fmt.Stringer) + for _, cfgPort := range cfg.Ports().Items() { + timeout := queryTimeout + if !deadline.IsZero() { + timeout = max(min(queryTimeout, time.Until(deadline)), time.Second) } - return states - } - allInState := func(states map[string]fmt.Stringer, want string) bool { - for _, cfgPort := range cfg.Ports().Items() { - state, ok := states[cfgPort.Name()] - if !ok || state.String() != want { - return false - } + queryCtx, cancel := context.WithTimeout(ctx, timeout) + state, err := ygnmi.Get(queryCtx, client, gnmi.OTG().Port(cfgPort.Name()).Link().State()) + cancel() + if err == nil { + states[cfgPort.Name()] = state } - return true } - - expected := strings.ToUpper(strings.TrimSpace(expectedState)) - var states map[string]fmt.Stringer - if expected == "UP" || expected == "DOWN" { - deadline := time.Now().Add(time.Duration(max(timeoutSec, 0)) * time.Second) - pollInterval := time.Duration(max(pollIntervalMs, 1)) * time.Millisecond - states = readStates(deadline) - for !allInState(states, expected) { - remaining := time.Until(deadline) - if remaining <= 0 { - break - } - time.Sleep(min(pollInterval, remaining)) - states = readStates(deadline) + return states + } + allInState := func(states map[string]fmt.Stringer, want string) bool { + for _, cfgPort := range cfg.Ports().Items() { + state, ok := states[cfgPort.Name()] + if !ok || state.String() != want { + return false } - } else { - states = readStates(time.Time{}) } - - for _, cfgPort := range cfg.Ports().Items() { - if state, ok := states[cfgPort.Name()]; ok { - t.Logf("PushConfig(t) on: %s, %s : %v", - cfgPort.Name(), - when, - state) + return true + } + + expected := strings.ToUpper(strings.TrimSpace(expectedState)) + var states map[string]fmt.Stringer + if expected == "UP" || expected == "DOWN" { + deadline := time.Now().Add(time.Duration(max(timeoutSec, 0)) * time.Second) + pollInterval := time.Duration(max(pollIntervalMs, 1)) * time.Millisecond + states = readStates(deadline) + for !allInState(states, expected) { + remaining := time.Until(deadline) + if remaining <= 0 { + break } + time.Sleep(min(pollInterval, remaining)) + states = readStates(deadline) } + } else { + states = readStates(time.Time{}) } - logLinkStates("before setting link state actual link is", 10, "UP", 500) - warnsLinkup, errLinkup := o.setLinkState(context.Background(), gosnappi.StatePortLinkState.UP) - //logLinkStates("after setting link state up actual link is", 10, "UP", 500) - if errLinkup != nil { - t.Fatalf("PushConfig(t) on %s: failed to set port link state up: %v", o.ate, errLinkup) - } - - if len(warnsLinkup) > 0 { - t.Logf("PushConfig(t) on %s non-fatal warnings when setting port link state up: %v", - o.ate, - warnsLinkup) + for _, cfgPort := range cfg.Ports().Items() { + if state, ok := states[cfgPort.Name()]; ok { + t.Logf("PushConfig(t) on: %s, %s : %v", + cfgPort.Name(), + when, + state) + } } }