Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework for reconciler implementation. #194

Merged
merged 4 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/openconfig/gribi v1.0.0
github.com/openconfig/lemming v0.3.2-0.20230914210403-c6484d12af0a
github.com/openconfig/testt v0.0.0-20220311054427-efbb1a32ec07
github.com/openconfig/ygot v0.29.10
github.com/openconfig/ygot v0.29.11-0.20230922181344-6c3af4108a57
go.uber.org/atomic v1.10.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d
google.golang.org/grpc v1.58.0-dev
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ github.com/openconfig/ygnmi v0.8.7/go.mod h1:7up6qc9l9G4+Cfo37gzO0D7+2g4yqyW+xzh
github.com/openconfig/ygot v0.6.0/go.mod h1:o30svNf7O0xK+R35tlx95odkDmZWS9JyWWQSmIhqwAs=
github.com/openconfig/ygot v0.10.4/go.mod h1:oCQNdXnv7dWc8scTDgoFkauv1wwplJn5HspHcjlxSAQ=
github.com/openconfig/ygot v0.13.2/go.mod h1:kJN0yCXIH07dOXvNBEFm3XxXdnDD5NI6K99tnD5x49c=
github.com/openconfig/ygot v0.29.10 h1:FRZXxyeCdiJXz6uat5uOm3Hlg+PUu2N0mY+eiva12MI=
github.com/openconfig/ygot v0.29.10/go.mod h1:RNnn1ytQ8GZV5LPts36l0cyoRjsYYpruiruJEvmU2sg=
github.com/openconfig/ygot v0.29.11-0.20230922181344-6c3af4108a57 h1:z7bNYTNR1HzxYQNVwBUaf0veA4j4vuTBW7mTLRW4M1w=
github.com/openconfig/ygot v0.29.11-0.20230922181344-6c3af4108a57/go.mod h1:RNnn1ytQ8GZV5LPts36l0cyoRjsYYpruiruJEvmU2sg=
github.com/p4lang/p4runtime v1.4.0-rc.5.0.20220728214547-13f0d02a521e h1:AfZKoikDXbZ7zWvO/lvCRzLo7i6lM+gNleYVMxPiWyQ=
github.com/p4lang/p4runtime v1.4.0-rc.5.0.20220728214547-13f0d02a521e/go.mod h1:m9laObIMXM9N1ElGXijc66/MSM5eheZJLRLxg/TG+fU=
github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
Expand Down
94 changes: 91 additions & 3 deletions rib/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
import (
"fmt"

"github.com/openconfig/ygot/ygot"

aftpb "github.com/openconfig/gribi/v1/proto/gribi_aft"
spb "github.com/openconfig/gribi/v1/proto/service"
"github.com/openconfig/ygot/ygot"
wpb "github.com/openconfig/ygot/proto/ywrapper"
)

// RIBFromGetResponses returns a RIB from a slice of gRIBI GetResponse messages.
// FromGetResponses returns a RIB from a slice of gRIBI GetResponse messages.
// The supplied defaultName is used as the default network instance name.
func RIBFromGetResponses(defaultName string, responses []*spb.GetResponse) (*RIB, error) {
func FromGetResponses(defaultName string, responses []*spb.GetResponse, opt ...RIBOpt) (*RIB, error) {
r := New(defaultName)
niAFTs := map[string]*aftpb.Afts{}

Expand Down Expand Up @@ -70,3 +72,89 @@

return r, nil
}

// fakeRIB is a RIB for use in testing which exposes methods that can be used to more easily
// construct a RIB's contents.
type fakeRIB struct {
r *RIB
}

// NewFake returns a new Fake RIB.
func NewFake(defaultName string, opt ...RIBOpt) *fakeRIB {
return &fakeRIB{
r: New(defaultName, opt...),
}
}

// RIB returns the constructed fake RIB to the caller.
func (f *fakeRIB) RIB() *RIB {
return f.r
}

// InjectIPv4 adds an IPv4 entry to network instance ni, with the specified
// prefix (pfx), and referencing the specified next-hop-group with index nhg.
// It returns an error if the entry cannot be injected.
func (f *fakeRIB) InjectIPv4(ni, pfx string, nhg uint64) error {
niR, ok := f.r.NetworkInstanceRIB(ni)
if !ok {
return fmt.Errorf("unknown NI, %s", ni)
}
if _, _, err := niR.AddIPv4(&aftpb.Afts_Ipv4EntryKey{
Prefix: pfx,
Ipv4Entry: &aftpb.Afts_Ipv4Entry{
NextHopGroup: &wpb.UintValue{Value: nhg},
},
}, false); err != nil {
return fmt.Errorf("cannot add IPv4 entry, err: %v", err)
}

return nil
}

// InjectNHG adds a next-hop-group entry to network instance ni, with the specified
// ID (nhgId). The next-hop-group contains the next hops specified in the nhs map,
// with the key of the map being the next-hop ID and the value being the weight within
// the group.
func (f *fakeRIB) InjectNHG(ni string, nhgId uint64, nhs map[uint64]uint64) error {

Check failure on line 118 in rib/helpers.go

View workflow job for this annotation

GitHub Actions / go / Static Analysis

method parameter nhgId should be nhgID
niR, ok := f.r.NetworkInstanceRIB(ni)
if !ok {
return fmt.Errorf("unknown NI, %s", ni)
}

nhg := &aftpb.Afts_NextHopGroupKey{
Id: nhgId,
NextHopGroup: &aftpb.Afts_NextHopGroup{},
}
for nh, weight := range nhs {
nhg.NextHopGroup.NextHop = append(nhg.NextHopGroup.NextHop, &aftpb.Afts_NextHopGroup_NextHopKey{
Index: nh,
NextHop: &aftpb.Afts_NextHopGroup_NextHop{
Weight: &wpb.UintValue{Value: weight},
},
})
}

if _, _, err := niR.AddNextHopGroup(nhg, false); err != nil {
return fmt.Errorf("cannot add NHG entry, err: %v", err)
}

return nil
}

// InjectNH adds a next-hop entry to network instance ni, with the specified
// index (nhIdx). An error is returned if it cannot be added.
func (f *fakeRIB) InjectNH(ni string, nhIdx uint64) error {
niR, ok := f.r.NetworkInstanceRIB(ni)
if !ok {
return fmt.Errorf("unknown NI, %s", ni)
}

if _, _, err := niR.AddNextHop(&aftpb.Afts_NextHopKey{
Index: nhIdx,
NextHop: &aftpb.Afts_NextHop{},
}, false); err != nil {
return fmt.Errorf("cannot add NH entry, err: %v", err)
}

return nil
}
155 changes: 151 additions & 4 deletions rib/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/openconfig/ygot/ygot"
)

func TestRIBFromGetResponses(t *testing.T) {
func TestFromGetResponses(t *testing.T) {
defaultName := "DEFAULT"
tests := []struct {
desc string
Expand Down Expand Up @@ -240,9 +240,9 @@ func TestRIBFromGetResponses(t *testing.T) {

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
got, err := RIBFromGetResponses(tt.inDefaultName, tt.inResponses)
got, err := FromGetResponses(tt.inDefaultName, tt.inResponses)
if (err != nil) != tt.wantErr {
t.Fatalf("RIBFromGetResponses(...): did not get expected error, got: %v, wantErr? %v", err, tt.wantErr)
t.Fatalf("FromGetResponses(...): did not get expected error, got: %v, wantErr? %v", err, tt.wantErr)
}

if diff := cmp.Diff(got, tt.wantRIB,
Expand All @@ -251,7 +251,154 @@ func TestRIBFromGetResponses(t *testing.T) {
cmp.AllowUnexported(RIBHolder{}),
cmpopts.IgnoreFields(RIBHolder{}, "mu", "refCounts", "checkFn"),
); diff != "" {
t.Fatalf("RIBFromGetResponses(...): did not get expected RIB, diff(-got,+want):\n%s", diff)
t.Fatalf("FromGetResponses(...): did not get expected RIB, diff(-got,+want):\n%s", diff)
}
})
}
}

func TestFakeRIB(t *testing.T) {
dn := "DEFAULT"
tests := []struct {
desc string
inBuild func() *fakeRIB
wantRIB *RIB
}{{
desc: "nh only",
inBuild: func() *fakeRIB {
f := NewFake(dn)
if err := f.InjectNH(dn, 1); err != nil {
t.Fatalf("cannot add NH, err: %v", err)
}
return f
},
wantRIB: &RIB{
defaultName: dn,
niRIB: map[string]*RIBHolder{
dn: {
name: dn,
r: &aft.RIB{
Afts: &aft.Afts{
NextHop: map[uint64]*aft.Afts_NextHop{
1: {Index: ygot.Uint64(1)},
},
},
},
},
},
},
}, {
desc: "ipv4 only",
inBuild: func() *fakeRIB {
f := NewFake(dn, DisableRIBCheckFn())
if err := f.InjectIPv4(dn, "1.0.0.0/24", 1); err != nil {
t.Fatalf("cannot add IPv4, err: %v", err)
}
return f
},
wantRIB: &RIB{
defaultName: dn,
niRIB: map[string]*RIBHolder{
dn: {
name: dn,
r: &aft.RIB{
Afts: &aft.Afts{
Ipv4Entry: map[string]*aft.Afts_Ipv4Entry{
"1.0.0.0/24": {Prefix: ygot.String("1.0.0.0/24"), NextHopGroup: ygot.Uint64(1)},
},
},
},
},
},
},
}, {
desc: "nhg only",
inBuild: func() *fakeRIB {
f := NewFake(dn, DisableRIBCheckFn())
if err := f.InjectNHG(dn, 1, map[uint64]uint64{1: 1}); err != nil {
t.Fatalf("cannot add NHG, err: %v", err)
}
return f
},
wantRIB: &RIB{
defaultName: dn,
niRIB: map[string]*RIBHolder{
dn: {
name: dn,
r: &aft.RIB{
Afts: &aft.Afts{
NextHopGroup: map[uint64]*aft.Afts_NextHopGroup{
1: {
Id: ygot.Uint64(1),
NextHop: map[uint64]*aft.Afts_NextHopGroup_NextHop{
1: {
Index: ygot.Uint64(1),
Weight: ygot.Uint64(1),
},
},
},
},
},
},
},
},
},
}, {
desc: "full ipv4 chain",
inBuild: func() *fakeRIB {
f := NewFake(dn)
// Discard the errors, since the test will check whether the entries are there.
f.InjectNH(dn, 1)
f.InjectNHG(dn, 1, map[uint64]uint64{1: 1})
f.InjectIPv4(dn, "192.0.2.1/32", 1)
return f
},
wantRIB: &RIB{
defaultName: dn,
niRIB: map[string]*RIBHolder{
dn: {
name: dn,
r: &aft.RIB{
Afts: &aft.Afts{
NextHop: map[uint64]*aft.Afts_NextHop{
1: {
Index: ygot.Uint64(1),
},
},
NextHopGroup: map[uint64]*aft.Afts_NextHopGroup{
1: {
Id: ygot.Uint64(1),
NextHop: map[uint64]*aft.Afts_NextHopGroup_NextHop{
1: {
Index: ygot.Uint64(1),
Weight: ygot.Uint64(1),
},
},
},
},
Ipv4Entry: map[string]*aft.Afts_Ipv4Entry{
"192.0.2.1/32": {
Prefix: ygot.String("192.0.2.1/32"),
NextHopGroup: ygot.Uint64(1),
},
},
},
},
},
},
},
}}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
got := tt.inBuild().RIB()
if diff := cmp.Diff(got, tt.wantRIB,
cmpopts.EquateEmpty(), cmp.AllowUnexported(RIB{}),
cmpopts.IgnoreFields(RIB{}, "nrMu", "pendMu", "ribCheck"),
cmp.AllowUnexported(RIBHolder{}),
cmpopts.IgnoreFields(RIBHolder{}, "mu", "refCounts", "checkFn"),
); diff != "" {
t.Fatalf("FakeRIB.RIB(...): did not get expected RIB, diff(-got,+want):\n%s", diff)
}
})
}
Expand Down
Loading
Loading