Skip to content

Commit

Permalink
Add reference to ygnmi in README and go:generate for gogen's integr…
Browse files Browse the repository at this point in the history
…ation test (#963)

* Add reference to ygnmi in README

* add reference to example code

* add another test to clarify that module prefix not needed during unmarshal of identities
  • Loading branch information
wenovus authored Apr 4, 2024
1 parent 486959d commit a59697e
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 9 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ if err := oc.Unmarshal([]byte(json), loadd); err != nil {

Currently, only the `RFC7951` format of JSON is supported for unmarshalling, the `Internal` format supported by ygot is not yet supported.

### Interacting with gNMI Server(s) via ygot GoStructs

While ygot provides Go types for structuring YANG data, the process of
interacting with a gNMI server is often cumbersome:

1. Marshalling a GoStruct to JSON.
2. Creating a gNMI `SetRequest` or `SubscribeRequest` object.
3. Processing the `SubscribeResponse` stream and accumulating per-path errors.
4. Unmarshalling the response from scalar protobufs or JSON to a GoStruct.

If this applies to you, refer to [ygnmi](https://github.com/openconfig/ygnmi)
for a ygot-based tool that generates not only the same ygot GoStructs, but also
helpers for each GoStruct and leaf [example
code](https://github.com/openconfig/ygnmi/blob/3863fe4d5757372579da234636b064f933205ab8/ygnmi/example_test.go#L266).
ygnmi is used by [Ondatra](https://github.com/openconfig/ondatra) to create its
[gNMI test library](https://github.com/openconfig/ondatra/tree/main/gnmi) used
in [featureprofiles](This too://github.com/openconfig/featureprofiles).

## For Developers
* [Contributing](CONTRIBUTING.md) - how to contribute to ygot.
* [Contributors](docs/CONTRIBUTORS.md) - Folks who have contributed to ygot, thanks very much!
Expand Down
19 changes: 14 additions & 5 deletions gogen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (

var updateGolden = flag.Bool("update_golden", false, "Update golden files")

//go:generate go test -run TestSimpleStructs -args -update_golden

// yangTestCase describs a test case for which code generation is performed
// through Goyang's API, it provides the input set of parameters in a way that
// can be reused across tests.
Expand Down Expand Up @@ -1183,6 +1185,12 @@ func TestSimpleStructs(t *testing.T) {
}

if tt.wantSchemaFile != "" {
if *updateGolden {
if err := os.WriteFile(tt.wantSchemaFile, []byte(string(gotGeneratedCode.RawJSONSchema)), 0644); err != nil {
panic(err)
}
}

wantSchema, rferr := os.ReadFile(tt.wantSchemaFile)
if rferr != nil {
t.Fatalf("%s: os.ReadFile(%q) error: %v", tt.name, tt.wantSchemaFile, rferr)
Expand All @@ -1199,6 +1207,12 @@ func TestSimpleStructs(t *testing.T) {
}
}

if *updateGolden {
if err := os.WriteFile(tt.wantStructsCodeFile, []byte(gotCode), 0644); err != nil {
t.Fatal(err)
}
}

wantCodeBytes, rferr := os.ReadFile(tt.wantStructsCodeFile)
if rferr != nil {
t.Fatalf("%s: os.ReadFile(%q) error: %v", tt.name, tt.wantStructsCodeFile, rferr)
Expand All @@ -1207,11 +1221,6 @@ func TestSimpleStructs(t *testing.T) {
wantCode := string(wantCodeBytes)

if gotCode != wantCode {
if *updateGolden {
if err := os.WriteFile(tt.wantStructsCodeFile, []byte(gotCode), 0644); err != nil {
t.Fatal(err)
}
}
// Use difflib to generate a unified diff between the
// two code snippets such that this is simpler to debug
// in the test output.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,4 @@
"schemapath": "/",
"structname": "Device"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,4 @@
"isCompressedSchema": true,
"isFakeRoot": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,4 @@
"schemapath": "/",
"structname": "Device"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,4 @@
"Annotation": {
"isFakeRoot": true
}
}
}
78 changes: 78 additions & 0 deletions ytypes/schema_tests/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,84 @@ func TestSet(t *testing.T) {
},
Data: []exampleoc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetCommunity_Inline_Communities_Union{exampleoc.BgpTypes_BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE},
},
}, {
desc: "set-on-union-with-invalid-string-but-valid-enum-no-module",
inSchema: mustSchema(exampleoc.Schema),
inPath: &gpb.Path{
Elem: []*gpb.PathElem{{
Name: "routing-policy",
}, {
Name: "policy-definitions",
}, {
Name: "policy-definition",
Key: map[string]string{
"name": "test",
},
}, {
Name: "statements",
}, {
Name: "statement",
Key: map[string]string{
"name": "test-stmt",
},
}, {
Name: "actions",
}, {
Name: "bgp-actions",
}, {
Name: "set-community",
}, {
Name: "inline",
}, {
Name: "config",
}, {
Name: "communities",
}},
},
inValue: &gpb.TypedValue{
Value: &gpb.TypedValue_LeaflistVal{
LeaflistVal: &gpb.ScalarArray{
Element: []*gpb.TypedValue{{
Value: &gpb.TypedValue_StringVal{StringVal: "NO_ADVERTISE"},
}},
},
},
},
inOpts: []ytypes.SetNodeOpt{&ytypes.InitMissingElements{}},
wantNode: &ytypes.TreeNode{
Path: &gpb.Path{
Elem: []*gpb.PathElem{{
Name: "routing-policy",
}, {
Name: "policy-definitions",
}, {
Name: "policy-definition",
Key: map[string]string{
"name": "test",
},
}, {
Name: "statements",
}, {
Name: "statement",
Key: map[string]string{
"name": "test-stmt",
},
}, {
Name: "actions",
}, {
Name: "bgp-actions",
}, {
Name: "set-community",
}, {
Name: "inline",
}, {
Name: "config",
}, {
Name: "communities",
}},
},
Data: []exampleoc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetCommunity_Inline_Communities_Union{exampleoc.BgpTypes_BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE},
},
}, {
desc: "set-on-union-with-invalid-string-but-valid-enum-json-container",
inSchema: mustSchema(opstateoc.Schema),
Expand Down

0 comments on commit a59697e

Please sign in to comment.