Skip to content

Commit

Permalink
Support retrieving from a remote RIB.
Browse files Browse the repository at this point in the history
  • Loading branch information
robshakir committed Sep 22, 2023
1 parent 5dd69e4 commit 197d34f
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 9 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ require (
github.com/golang/glog v1.1.2
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/kr/pretty v0.3.1
github.com/openconfig/gnmi v0.10.0
github.com/openconfig/goyang v1.4.1
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.20230922074452-052ed885651c
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 All @@ -31,6 +32,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/k-sone/critbitgo v1.4.0 // indirect
github.com/kentik/patricia v1.2.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -42,6 +44,7 @@ require (
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -171,9 +172,11 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down Expand Up @@ -210,11 +213,14 @@ github.com/openconfig/ygot v0.10.4/go.mod h1:oCQNdXnv7dWc8scTDgoFkauv1wwplJn5Hsp
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.20230922074452-052ed885651c h1:0rE/tRnck61Guj+zkButSrb1VMWtDP0xX4cmanEDHL4=
github.com/openconfig/ygot v0.29.11-0.20230922074452-052ed885651c/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=
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
Expand All @@ -224,6 +230,7 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
Expand Down
2 changes: 1 addition & 1 deletion rib/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// FromGetResponses returns a RIB from a slice of gRIBI GetResponse messages.
// The supplied defaultName is used as the default network instance name.
func FromGetResponses(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
22 changes: 17 additions & 5 deletions rib/reconciler/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package reconciler

import (
"context"
"fmt"

"github.com/openconfig/gribigo/rib"
Expand All @@ -48,7 +49,10 @@ type R struct {
type RIBTarget interface {
// Get returns a RIB containing all network-instances and AFTs that are
// supported by the RIB.
Get() (*rib.RIB, error)
Get(context.Context) (*rib.RIB, error)
// CleanUp is called to indicate that the RIBTarget should remove any
// state or external connections as it is no longer required.
CleanUp()
}

// LocalRIB wraps a RIB that is locally available on the system as a gRIBIgo
Expand All @@ -58,10 +62,18 @@ type LocalRIB struct {
}

// Get returns the contents of the local RIB.
func (l *LocalRIB) Get() (*rib.RIB, error) {
func (l *LocalRIB) Get(_ context.Context) (*rib.RIB, error) {
return l.r, nil
}

// CleanUp implements the RIBTarget interface. No local cleanup is required.
func (l *LocalRIB) CleanUp() {}

var (
// Compile time check that LocalRIB implements the RIBTarget interface.
_ RIBTarget = &LocalRIB{}
)

// New returns a new reconciler with the specified intended and target RIBs.
func New(intended, target RIBTarget) *R {
return &R{
Expand All @@ -72,14 +84,14 @@ func New(intended, target RIBTarget) *R {

// Reconcile performs a reconciliation operation between the intended and specified
// remote RIB.
func (r *R) Reconcile() error {
func (r *R) Reconcile(ctx context.Context) error {
// Get the current contents of intended and target.
iRIB, err := r.intended.Get()
iRIB, err := r.intended.Get(ctx)
if err != nil {
return fmt.Errorf("cannot reconcile RIBs, cannot get contents of intended, %v", err)
}

tRIB, err := r.target.Get()
tRIB, err := r.target.Get(ctx)
if err != nil {
return fmt.Errorf("cannot reconcile RIBs, cannot get contents of target, %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion rib/reconciler/reconcile_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package reconciler

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -16,7 +17,7 @@ func TestLocalRIB(t *testing.T) {
}

want := rib.New("DEFAULT")
got, err := l.Get()
got, err := l.Get(context.Background())
if err != nil {
t.Fatalf("(*LocalRIB).Get(): did not get expected error, got: %v, want: nil", err)
}
Expand Down
70 changes: 70 additions & 0 deletions rib/reconciler/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package reconciler

import (
"context"
"fmt"

"github.com/openconfig/gribigo/client"
"github.com/openconfig/gribigo/rib"
"google.golang.org/protobuf/encoding/prototext"

spb "github.com/openconfig/gribi/v1/proto/service"
)

// RemoteRIB implements the RIBTarget interface and wraps a remote gRIBI RIB.
// The contents are accessed via the gRIBI gRPC API.
type RemoteRIB struct {
c *client.Client

defaultName string
}

// NewRemoteRIB returns a new remote gRIBI RIB. The context supplied is used to
// dial the remote gRIBI server at the address 'addr'. the 'defName' argument
// is used to identify the name of the default network instance on the server.
func NewRemoteRIB(ctx context.Context, defName, addr string) (*RemoteRIB, error) {
gc, err := client.New()
if err != nil {
return nil, fmt.Errorf("cannot create gRIBI client, %v", err)
}

r := &RemoteRIB{
c: gc,
defaultName: defName,
}

if err := r.c.Dial(ctx, addr); err != nil {
return nil, fmt.Errorf("cannot dial remote server, %v", err)
}
return r, nil
}

// CleanUp closes the remote connection to the gRIBI server.
func (r *RemoteRIB) CleanUp() {
r.c.Close()
}

// Get retrieves the contents of the remote gRIBI server's RIB and returns it as a
// gRIBIgo RIB struct. The context is used for a Get RPC call to the remote server.
func (r *RemoteRIB) Get(ctx context.Context) (*rib.RIB, error) {
resp, err := r.c.Get(ctx, &spb.GetRequest{
NetworkInstance: &spb.GetRequest_All{
All: &spb.Empty{},
},
Aft: spb.AFTType_ALL,
})
if err != nil {
return nil, fmt.Errorf("cannot get remote RIB, %v", err)
}

fmt.Printf("%s\n", prototext.Format(resp))

// We always disable the RIB checking function because we want to see entries that have
// not got valid references so that we can reconcile them.
remRIB, err := rib.FromGetResponses(r.defaultName, []*spb.GetResponse{resp}, rib.DisableRIBCheckFn())
if err != nil {
return nil, fmt.Errorf("cannot build remote RIB from responses, %v", err)
}

return remRIB, nil
}
Loading

0 comments on commit 197d34f

Please sign in to comment.