Skip to content

Commit

Permalink
using OC to create prefix-list
Browse files Browse the repository at this point in the history
  • Loading branch information
self-maurya committed Aug 8, 2024
1 parent 530b9a3 commit 638daf1
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 53 deletions.
25 changes: 12 additions & 13 deletions feature/experimental/policy/otg_tests/prefix_set_test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,20 @@ the DUT in one gnmi.Set using the `replace` option

### Add prefix-list from cli and then replace with gnmi

* Configure Prefix-list through CLI Config Sessions.
* Send a gNMI SET request that contains below prefixes under TAG_3_IPV4 prefix-set
```
ip prefix-list TAG_3_IPV4
seq 10 permit 10.240.31.48/28
seq 20 permit 10.244.187.32/28
seq 30 permit 173.36.128.0/20
seq 40 permit 173.37.128.0/20
seq 50 permit 173.38.128.0/20
seq 60 permit 173.39.128.0/20
seq 70 permit 173.40.128.0/20
seq 80 permit 173.41.128.0/20
seq 90 permit 173.42.128.0/20
seq 100 permit 173.43.128.0/20
10.240.31.48/28
10.244.187.32/28
173.36.128.0/20
173.37.128.0/20
173.38.128.0/20
173.39.128.0/20
173.40.128.0/20
173.41.128.0/20
173.42.128.0/20
173.43.128.0/20
```
* Perform octa restart or reboot the device.
* Perform openconfig agent(e.g, Octa for Arista) restart or reboot the device.
* Send a gNMI SET request that contains additional prefixes within the same
prefix-set, TAG_3_IPV4.
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@
package prefix_set_test

import (
"context"
"testing"

"github.com/openconfig/featureprofiles/internal/fptest"
"github.com/openconfig/featureprofiles/internal/helpers"
"github.com/openconfig/featureprofiles/internal/gnoi"
"github.com/openconfig/ondatra"
"github.com/openconfig/ondatra/gnmi"
"github.com/openconfig/ondatra/gnmi/oc"
)

const (
prefixSetA = "PFX_SET_A"
tag3IPv4 = "TAG_3_IPV4"
pfx1 = "10.240.31.48/28"
pfx2 = "173.36.128.0/20"
pfx3 = "173.36.144.0/20"
pfx4 = "10.240.31.64/28"
mskLen = "exact"
)

var (
processName = map[ondatra.Vendor]string{

Check failure on line 38 in feature/experimental/policy/otg_tests/prefix_set_test/prefix_set_test.go

View workflow job for this annotation

GitHub Actions / Static Analysis

var processName is unused (U1000)
ondatra.ARISTA: "Octa",
}
)

func TestMain(m *testing.M) {
fptest.RunTests(m)
}
Expand Down Expand Up @@ -98,47 +104,32 @@ func TestPrefixSet(t *testing.T) {
}
}

func TestPrefixSetWithCLIConfig(t *testing.T) {
func TestPrefixSetWithOCAgentRestart(t *testing.T) {
dut := ondatra.DUT(t, "dut")
ctx := context.Background()
cli := dut.RawAPIs().CLI(t)

switch dut.Vendor() {
case ondatra.ARISTA:
helpers.GnmiCLIConfig(t, dut, `
ip prefix-list TAG_3_IPV4
seq 10 permit 10.240.31.48/28
seq 20 permit 10.244.187.32/28
seq 30 permit 173.36.128.0/20
seq 40 permit 173.37.128.0/20
seq 50 permit 173.38.128.0/20
seq 60 permit 173.39.128.0/20
seq 70 permit 173.40.128.0/20
seq 80 permit 173.41.128.0/20
seq 90 permit 173.42.128.0/20
seq 100 permit 173.43.128.0/20
`,
)
helpers.GnmiCLIConfig(t, dut, `
management api gnmi
transport grpc default
operation set persistence
`,
)
cmd := "agent Octa terminate"
res, err := cli.RunCommand(ctx, "agent Octa terminate")
if err != nil {
t.Errorf("error executing command %q:\n%v", cmd, err)
}
if res.Error() != "" {
t.Errorf("error executing command %q:\n%v", cmd, res.Error())
}
}

dutOcRoot := &oc.Root{}
rp := dutOcRoot.GetOrCreateRoutingPolicy()
ds := rp.GetOrCreateDefinedSets()
v4PrefixSet := ds.GetOrCreatePrefixSet("TAG_3_IPV4")
v4PrefixSet := ds.GetOrCreatePrefixSet(tag3IPv4)
v4PrefixSet.GetOrCreatePrefix("10.240.31.48/28", mskLen)
v4PrefixSet.GetOrCreatePrefix("10.244.187.32/28", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.36.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.37.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.38.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.39.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.40.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.41.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.42.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.43.128.0/20", mskLen)
gnmi.Update(t, dut, gnmi.OC().RoutingPolicy().DefinedSets().PrefixSet(tag3IPv4).Config(), v4PrefixSet)
prefixSet := gnmi.Get[*oc.RoutingPolicy_DefinedSets_PrefixSet](t, dut, gnmi.OC().RoutingPolicy().DefinedSets().PrefixSet(tag3IPv4).State())
if len(prefixSet.Prefix) != 10 {
t.Errorf("Prefix set has %v prefixes, want 10", len(prefixSet.Prefix))
}

gnoi.TerminateOCAgent(t, dut, true)

v4PrefixSet = ds.GetOrCreatePrefixSet(tag3IPv4)
v4PrefixSet.SetMode(oc.PrefixSet_Mode_IPV4)
v4PrefixSet.GetOrCreatePrefix("173.49.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.46.128.0/20", mskLen)
Expand All @@ -163,8 +154,8 @@ func TestPrefixSetWithCLIConfig(t *testing.T) {
v4PrefixSet.GetOrCreatePrefix("173.48.128.0/20", mskLen)
v4PrefixSet.GetOrCreatePrefix("173.45.128.0/20", mskLen)

gnmi.Replace(t, dut, gnmi.OC().RoutingPolicy().DefinedSets().PrefixSet("TAG_3_IPV4").Config(), v4PrefixSet)
prefixSet := gnmi.Get[*oc.RoutingPolicy_DefinedSets_PrefixSet](t, dut, gnmi.OC().RoutingPolicy().DefinedSets().PrefixSet("TAG_3_IPV4").State())
gnmi.Replace(t, dut, gnmi.OC().RoutingPolicy().DefinedSets().PrefixSet(tag3IPv4).Config(), v4PrefixSet)
prefixSet = gnmi.Get[*oc.RoutingPolicy_DefinedSets_PrefixSet](t, dut, gnmi.OC().RoutingPolicy().DefinedSets().PrefixSet(tag3IPv4).State())
if len(prefixSet.Prefix) != 22 {
t.Errorf("Prefix set has %v prefixes, want 22", len(prefixSet.Prefix))
}
Expand Down
75 changes: 75 additions & 0 deletions internal/gnoi/gnoi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 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
//
// http://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 gnoi provides utilities for interacting with the gNOI API.
package gnoi

import (
"context"
"fmt"
"testing"
"time"

gpb "github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/ondatra"
"github.com/openconfig/ondatra/gnmi"
"github.com/openconfig/ondatra/gnmi/oc"
"github.com/openconfig/ygnmi/ygnmi"
)

var (
ocAgentTerminationCmd = map[ondatra.Vendor]string{
ondatra.ARISTA: "agent Octa terminate",
}
ocAgentDaemon = map[ondatra.Vendor]string{
ondatra.ARISTA: "Octa",
}
)

// TerminateOCAgent terminates the OpenConfig agent on the DUT.
func TerminateOCAgent(t *testing.T, dut *ondatra.DUTDevice, waitForRestart bool) error {
t.Helper()

ctx := context.Background()
cli := dut.RawAPIs().CLI(t)

cmd, ok := ocAgentTerminationCmd[dut.Vendor()]
if !ok {
t.Errorf("No command found for vendor %v", dut.Vendor())
}
res, err := cli.RunCommand(ctx, cmd)
if err != nil {
return fmt.Errorf("error executing command %q: %v", cmd, err)
}
if res.Error() != "" {
return fmt.Errorf("error executing command %q: %v", cmd, res.Error())
}

if ocAgent, ok := ocAgentDaemon[dut.Vendor()]; ok && waitForRestart {
gnmi.WatchAll(
t,
dut.GNMIOpts().WithYGNMIOpts(ygnmi.WithSubscriptionMode(gpb.SubscriptionMode_ON_CHANGE)),
gnmi.OC().System().ProcessAny().State(),
time.Minute,
func(p *ygnmi.Value[*oc.System_Process]) bool {
val, ok := p.Val()
if !ok {
return false
}
return val.GetName() == ocAgent
},
)
}
return nil
}

0 comments on commit 638daf1

Please sign in to comment.