forked from openconfig/ygot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix protobuf generation issues; add getting started documentation (op…
…enconfig#106) * Working commit for protobuf generation. * Fix issues with protobuf generation toolchain use in Go. * Remove stale comments. * Add copyright to new files. * Initial verbiage for the getting started document. * Update test scripts, remove stale files. * Update test dependencies. * Fix makefile ordering for travis. * Add dependencies for vendored YANG modules. * Update makefile to avoid dependencies on generated files. * Update Makefile, add initial documentation. * Update demo code, documentation. * Fix missing comments. * Address review comments. * Fix gofmt.
- Loading branch information
Showing
55 changed files
with
5,134 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ribproto | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// Copyright 2017 Google Inc. All Rights Reserved. | ||
// | ||
// 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 main contains a demo of using a protobuf schema generated by ygot | ||
// to generate a Protobuf form of the OpenConfig RIB model. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
log "github.com/golang/glog" | ||
"github.com/golang/protobuf/proto" | ||
|
||
ocpb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig" | ||
ocenums "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/enums" | ||
ocrpb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp" | ||
ocrbpb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib" | ||
ocrbapb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/afi_safis" | ||
ocrbaapb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/afi_safis/afi_safi" | ||
ocrbaai4pb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/afi_safis/afi_safi/ipv4_unicast" | ||
ocrbaai4lpb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/afi_safis/afi_safi/ipv4_unicast/loc_rib" | ||
ocrbaai4lrpb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/afi_safis/afi_safi/ipv4_unicast/loc_rib/routes" | ||
ocrbaai4lrrpb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/afi_safis/afi_safi/ipv4_unicast/loc_rib/routes/route" | ||
ocrapb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/attr_sets" | ||
ocraapb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/openconfig_rib_bgp/bgp_rib/attr_sets/attr_set" | ||
ywpb "github.com/openconfig/ygot/proto/ywrapper" | ||
) | ||
|
||
func main() { | ||
rt := buildRouteProto(&ipv4Prefix{ | ||
atomicAggregate: true, | ||
localPref: 100, | ||
med: 10, | ||
nextHop: "10.0.1.1", | ||
origin: ocenums.OpenconfigRibBgpBgpOriginAttrType_OPENCONFIGRIBBGPBGPORIGINATTRTYPE_EGP, | ||
originatorID: "192.0.2.42", | ||
prefix: "192.0.2.0/24", | ||
protocolOrigin: ocenums.OpenconfigPolicyTypesINSTALLPROTOCOLTYPE_OPENCONFIGPOLICYTYPESINSTALLPROTOCOLTYPE_BGP, | ||
}) | ||
|
||
b, err := proto.Marshal(rt) | ||
if err != nil { | ||
log.Exitf("Error marshalling proto: %v", err) | ||
} | ||
|
||
fmt.Printf("%s\n", proto.MarshalTextString(rt)) | ||
fmt.Printf("Marshalled proto size in bytes: %d\n", len(b)) | ||
} | ||
|
||
// ipv4Prefix describes an IPv4 Prefix within the OpenConfig BGP RIB model. | ||
type ipv4Prefix struct { | ||
atomicAggregate bool // atomicAggregate is set when a downstream BGP speaker has aggregated the prefix. | ||
localPref uint64 // localPrefix is the value of the BGP LOCAL_PREFERENCE attribute. | ||
med uint64 // med is the value of the BGP multi-exit discriminator. | ||
nextHop string // nextHop is the IP next-hop used for the BGP route. | ||
origin ocenums.OpenconfigRibBgpBgpOriginAttrType // origin is the value of the ORIGIN attribute of the BGP prefix. | ||
originatorID string // originatorID specifies the address of the BGP originator of the prefix. | ||
prefix string // prefix is the IPv4 prefix for the route. | ||
protocolOrigin ocenums.OpenconfigPolicyTypesINSTALLPROTOCOLTYPE // protocolOrigin specifies the route on the device via which the prefix was learnt. | ||
} | ||
|
||
// buildRouteProto returns a Protobuf representation a route and associated | ||
// attribute set in the OpenConfig BGP RIB model. | ||
func buildRouteProto(pfx *ipv4Prefix) *ocpb.Device { | ||
return &ocpb.Device{ | ||
BgpRib: &ocrpb.BgpRib{ | ||
AttrSets: &ocrbpb.AttrSets{ | ||
AttrSet: []*ocrbpb.AttrSetKey{{ | ||
Index: 1, | ||
AttrSet: &ocrapb.AttrSet{ | ||
State: &ocraapb.State{ | ||
AtomicAggregate: &ywpb.BoolValue{pfx.atomicAggregate}, | ||
Index: &ywpb.UintValue{1}, | ||
LocalPref: &ywpb.UintValue{pfx.localPref}, | ||
Med: &ywpb.UintValue{pfx.med}, | ||
NextHop: &ywpb.StringValue{pfx.nextHop}, | ||
Origin: pfx.origin, | ||
OriginatorId: &ywpb.StringValue{pfx.originatorID}, | ||
}, | ||
}, | ||
}}, | ||
}, | ||
AfiSafis: &ocrbpb.AfiSafis{ | ||
AfiSafi: []*ocrbpb.AfiSafiKey{{ | ||
AfiSafiName: ocenums.OpenconfigBgpTypesAFISAFITYPE_OPENCONFIGBGPTYPESAFISAFITYPE_IPV4_UNICAST, | ||
AfiSafi: &ocrbapb.AfiSafi{ | ||
Ipv4Unicast: &ocrbaapb.Ipv4Unicast{ | ||
LocRib: &ocrbaai4pb.LocRib{ | ||
Routes: &ocrbaai4lpb.Routes{ | ||
Route: []*ocrbaai4lpb.RouteKey{{ | ||
Prefix: pfx.prefix, | ||
Origin: &ocrbaai4lpb.RouteKey_OriginOpenconfigpolicytypesinstallprotocoltype{ocenums.OpenconfigPolicyTypesINSTALLPROTOCOLTYPE_OPENCONFIGPOLICYTYPESINSTALLPROTOCOLTYPE_BGP}, | ||
PathId: 1, | ||
Route: &ocrbaai4lrpb.Route{ | ||
State: &ocrbaai4lrrpb.State{ | ||
PathId: &ywpb.UintValue{1}, | ||
Prefix: &ywpb.StringValue{pfx.prefix}, | ||
Origin: &ocrbaai4lrrpb.State_OriginOpenconfigpolicytypesinstallprotocoltype{pfx.protocolOrigin}, | ||
AttrIndex: &ywpb.UintValue{1}, | ||
}, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// Copyright 2017 Google Inc. All Rights Reserved. | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"io/ioutil" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/kylelemons/godebug/pretty" | ||
|
||
ocpb "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig" | ||
ocenums "github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto/openconfig/enums" | ||
) | ||
|
||
func TestProtoGenerate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inTestFunc func(*ipv4Prefix) *ocpb.Device | ||
inPrefix *ipv4Prefix | ||
wantTextProto string | ||
}{{ | ||
name: "simple route entry test", | ||
inTestFunc: buildRouteProto, | ||
inPrefix: &ipv4Prefix{ | ||
atomicAggregate: true, | ||
localPref: 100, | ||
med: 10, | ||
nextHop: "10.0.1.1", | ||
origin: ocenums.OpenconfigRibBgpBgpOriginAttrType_OPENCONFIGRIBBGPBGPORIGINATTRTYPE_EGP, | ||
originatorID: "192.0.2.42", | ||
prefix: "192.0.2.0/24", | ||
protocolOrigin: ocenums.OpenconfigPolicyTypesINSTALLPROTOCOLTYPE_OPENCONFIGPOLICYTYPESINSTALLPROTOCOLTYPE_BGP, | ||
}, | ||
wantTextProto: "route_entry.txtpb", | ||
}} | ||
|
||
for _, tt := range tests { | ||
got := tt.inTestFunc(tt.inPrefix) | ||
|
||
want := &ocpb.Device{} | ||
|
||
wantStr, err := ioutil.ReadFile(filepath.Join("testdata", tt.wantTextProto)) | ||
if err != nil { | ||
t.Errorf("%s: ioutil.ReadFile(testdata/%s): could not read file, got: %v, want: nil", tt.name, tt.wantTextProto, err) | ||
} | ||
|
||
if err := proto.UnmarshalText(string(wantStr), want); err != nil { | ||
t.Errorf("%s: proto.UnmarshalText(file: %s): could not unmarshal test proto, got: %v, want: nil", tt.name, tt.wantTextProto, err) | ||
} | ||
|
||
if !proto.Equal(got, want) { | ||
t.Errorf("%s: %T: did not get expected return proto, diff(-got,+want):\n%s", tt.name, tt.inTestFunc, pretty.Compare(got, want)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
bgp_rib: < | ||
afi_safis: < | ||
afi_safi: < | ||
afi_safi_name: OPENCONFIGBGPTYPESAFISAFITYPE_IPV4_UNICAST | ||
afi_safi: < | ||
ipv4_unicast: < | ||
loc_rib: < | ||
routes: < | ||
route: < | ||
prefix: "192.0.2.0/24" | ||
origin_openconfigpolicytypesinstallprotocoltype: OPENCONFIGPOLICYTYPESINSTALLPROTOCOLTYPE_BGP | ||
path_id: 1 | ||
route: < | ||
state: < | ||
attr_index: < | ||
value: 1 | ||
> | ||
origin_openconfigpolicytypesinstallprotocoltype: OPENCONFIGPOLICYTYPESINSTALLPROTOCOLTYPE_BGP | ||
path_id: < | ||
value: 1 | ||
> | ||
prefix: < | ||
value: "192.0.2.0/24" | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> | ||
attr_sets: < | ||
attr_set: < | ||
index: 1 | ||
attr_set: < | ||
state: < | ||
atomic_aggregate: < | ||
value: true | ||
> | ||
index: < | ||
value: 1 | ||
> | ||
local_pref: < | ||
value: 100 | ||
> | ||
med: < | ||
value: 10 | ||
> | ||
next_hop: < | ||
value: "10.0.1.1" | ||
> | ||
origin: OPENCONFIGRIBBGPBGPORIGINATTRTYPE_EGP | ||
originator_id: < | ||
value: "192.0.2.42" | ||
> | ||
> | ||
> | ||
> | ||
> | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
|
||
clean() { | ||
rm -rf public | ||
rm -rf deps | ||
} | ||
|
||
# Ensure that the .pb.go has been generated for the extensions | ||
# that are required. | ||
(cd ../../proto/yext && go generate) | ||
(cd ../../proto/ywrapper && go generate) | ||
|
||
clean | ||
rm -rf ribproto | ||
|
||
go run ../../proto_generator/protogenerator.go \ | ||
-generate_fakeroot \ | ||
-base_import_path="github.com/openconfig/ygot/demo/protobuf_getting_started/ribproto" \ | ||
-path=yang -output_dir=ribproto \ | ||
-enum_package_name=enums -package_name=openconfig \ | ||
-exclude_modules=ietf-interfaces \ | ||
yang/rib/openconfig-rib-bgp.yang | ||
|
||
go get -u github.com/google/protobuf | ||
proto_imports=".:${GOPATH}/src/github.com/google/protobuf/src:${GOPATH}/src" | ||
find ribproto -name "*.proto" | while read l; do | ||
protoc -I=$proto_imports --go_out=. $l | ||
done | ||
|
||
clean |
Oops, something went wrong.