Skip to content

Commit

Permalink
Generate ΛEnumTypes for shadow paths (#917)
Browse files Browse the repository at this point in the history
* Generate `ΛEnumTypes` for shadow paths

* Fixed unmarshalling union leafs for shadow paths.
* Use correct path for keying `ΛEnumTypes`.
* Re-generate exampleoc for schema test.

* Remove stale comment
  • Loading branch information
wenovus authored Sep 28, 2023
1 parent 2224a32 commit 23c9aac
Show file tree
Hide file tree
Showing 11 changed files with 143,866 additions and 130,505 deletions.
93,551 changes: 49,129 additions & 44,422 deletions exampleoc/oc.go

Large diffs are not rendered by default.

4,895 changes: 4,307 additions & 588 deletions exampleoc/ocpath.go

Large diffs are not rendered by default.

92,494 changes: 48,488 additions & 44,006 deletions exampleoc/opstateoc/oc.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions exampleoc/opstateoc/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ go run ../../generator/generator.go -path=public,deps -output_file=oc.go \
public/release/models/optical-transport/openconfig-terminal-device.yang \
public/release/models/optical-transport/openconfig-transport-line-protection.yang \
public/release/models/platform/openconfig-platform.yang \
public/release/models/bgp/openconfig-bgp-policy.yang \
public/release/models/policy/openconfig-routing-policy.yang \
public/release/models/lacp/openconfig-lacp.yang \
public/release/models/system/openconfig-system.yang \
Expand Down
1 change: 1 addition & 0 deletions exampleoc/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go run ../generator/generator.go -path=public,deps -output_file=oc.go \
public/release/models/optical-transport/openconfig-terminal-device.yang \
public/release/models/optical-transport/openconfig-transport-line-protection.yang \
public/release/models/platform/openconfig-platform.yang \
public/release/models/bgp/openconfig-bgp-policy.yang \
public/release/models/policy/openconfig-routing-policy.yang \
public/release/models/lacp/openconfig-lacp.yang \
public/release/models/system/openconfig-system.yang \
Expand Down
5 changes: 5 additions & 0 deletions exampleoc/wrapperunionoc/oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ It should not be used within your application, as it WILL change,
without warning. Rather, you should generate structs directly from
OpenConfig models using the ygot package.

NOTE WELL: This is an example code file that is distributed with ygot.
It should not be used within your application, as it WILL change,
without warning. Rather, you should generate structs directly from
OpenConfig models using the ygot package.

This package was generated by github.com/openconfig/ygot
using the following YANG input files:
- public/release/models/network-instance/openconfig-network-instance.yang
Expand Down
9 changes: 6 additions & 3 deletions gogen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"fmt"
"sort"
"strings"

"github.com/openconfig/ygot/internal/igenutil"
"github.com/openconfig/ygot/util"
Expand Down Expand Up @@ -274,8 +273,7 @@ func (cg *CodeGenerator) Generate(yangFiles, includePaths []string) (*GeneratedC
for _, fn := range dir.OrderedFieldNames() {
field := dir.Fields[fn]

// Strip the module name from the path.
schemaPath := util.SlicePathToString(append([]string{""}, strings.Split(field.YANGDetails.Path, "/")[2:]...))
schemaPath := field.YANGDetails.SchemaPath
switch {
case field.LangType == nil:
// This is a directory, so we continue.
Expand Down Expand Up @@ -304,6 +302,11 @@ func (cg *CodeGenerator) Generate(yangFiles, includePaths []string) (*GeneratedC
return field.LangType.UnionTypes[enumTypeMap[schemaPath][i]].Index < field.LangType.UnionTypes[enumTypeMap[schemaPath][j]].Index
})
}
if v, ok := enumTypeMap[schemaPath]; ok {
if shadowPath := field.YANGDetails.ShadowSchemaPath; shadowPath != "" {
enumTypeMap[shadowPath] = v
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,8 @@ func initΛEnumTypes(){
"/parent/child/config/three": []reflect.Type{
reflect.TypeOf((E_Child_Three)(0)),
},
"/parent/child/state/three": []reflect.Type{
reflect.TypeOf((E_Child_Three)(0)),
},
}
}
83,205 changes: 41,720 additions & 41,485 deletions uexampleoc/oc.go

Large diffs are not rendered by default.

192 changes: 191 additions & 1 deletion ytypes/schema_tests/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,199 @@ func TestSet(t *testing.T) {
inPath *gpb.Path
inValue *gpb.TypedValue
inOpts []ytypes.SetNodeOpt
inGetOpts []ytypes.GetNodeOpt
wantErrSubstring string
wantNode *ytypes.TreeNode
}{{
desc: "set-on-union-with-invalid-string-but-valid-enum",
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: "openconfig-bgp-types: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),
inPath: &gpb.Path{
Elem: []*gpb.PathElem{{
Name: "routing-policy",
}, {
Name: "policy-definitions",
}, {
Name: "policy-definition",
Key: map[string]string{
"name": "foo",
},
}},
},
inValue: &gpb.TypedValue{
Value: &gpb.TypedValue_JsonIetfVal{
JsonIetfVal: []byte("{\n \"openconfig-routing-policy:state\": {\n \"name\": \"foo\"\n },\n \"openconfig-routing-policy:name\": \"foo\",\n \"openconfig-routing-policy:statements\": {\n \"statement\": [\n {\n \"actions\": {\n \"openconfig-bgp-policy:bgp-actions\": {\n \"set-community\": {\n \"state\": {\n \"method\": \"INLINE\"\n },\n \"inline\": {\n \"state\": {\n \"communities\": [\n \"openconfig-bgp-types:NO_ADVERTISE\"\n ]\n }\n }\n }\n }\n },\n \"state\": {\n \"name\": \"foo-stmt\"\n },\n \"name\": \"foo-stmt\"\n }\n ]\n }\n}"),
},
},
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": "foo",
},
}, {
Name: "statements",
}, {
Name: "statement",
Key: map[string]string{
"name": "foo-stmt",
},
}, {
Name: "actions",
}, {
Name: "bgp-actions",
}, {
Name: "set-community",
}, {
Name: "inline",
}, {
Name: "state",
}, {
Name: "communities",
}},
},
Data: []opstateoc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetCommunity_Inline_Communities_Union{opstateoc.OpenconfigBgpTypes_BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE},
},
}, {
desc: "set-on-union-with-invalid-string-but-valid-enum-json-container-shadow-path",
inSchema: mustSchema(opstateoc.Schema),
inPath: &gpb.Path{
Elem: []*gpb.PathElem{{
Name: "routing-policy",
}, {
Name: "policy-definitions",
}, {
Name: "policy-definition",
Key: map[string]string{
"name": "foo",
},
}},
},
inValue: &gpb.TypedValue{
Value: &gpb.TypedValue_JsonIetfVal{
JsonIetfVal: []byte("{\n \"openconfig-routing-policy:config\": {\n \"name\": \"foo\"\n },\n \"openconfig-routing-policy:name\": \"foo\",\n \"openconfig-routing-policy:statements\": {\n \"statement\": [\n {\n \"actions\": {\n \"openconfig-bgp-policy:bgp-actions\": {\n \"set-community\": {\n \"config\": {\n \"method\": \"INLINE\"\n },\n \"inline\": {\n \"config\": {\n \"communities\": [\n \"openconfig-bgp-types:NO_ADVERTISE\"\n ]\n }\n }\n }\n }\n },\n \"config\": {\n \"name\": \"foo-stmt\"\n },\n \"name\": \"foo-stmt\"\n }\n ]\n }\n}"),
},
},
inOpts: []ytypes.SetNodeOpt{&ytypes.InitMissingElements{}, &ytypes.PreferShadowPath{}},
inGetOpts: []ytypes.GetNodeOpt{&ytypes.PreferShadowPath{}},
wantNode: &ytypes.TreeNode{
Path: &gpb.Path{
Elem: []*gpb.PathElem{{
Name: "routing-policy",
}, {
Name: "policy-definitions",
}, {
Name: "policy-definition",
Key: map[string]string{
"name": "foo",
},
}, {
Name: "statements",
}, {
Name: "statement",
Key: map[string]string{
"name": "foo-stmt",
},
}, {
Name: "actions",
}, {
Name: "bgp-actions",
}, {
Name: "set-community",
}, {
Name: "inline",
}, {
Name: "config",
}, {
Name: "communities",
}},
},
Data: []opstateoc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetCommunity_Inline_Communities_Union{opstateoc.OpenconfigBgpTypes_BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE},
},
}, {
desc: "set leafref with mismatched name - compressed schema",
inSchema: mustSchema(exampleoc.Schema),
inPath: &gpb.Path{
Expand Down Expand Up @@ -390,7 +580,7 @@ func TestSet(t *testing.T) {
return
}

got, err := ytypes.GetNode(tt.inSchema.RootSchema(), tt.inSchema.Root, tt.wantNode.Path)
got, err := ytypes.GetNode(tt.inSchema.RootSchema(), tt.inSchema.Root, tt.wantNode.Path, tt.inGetOpts...)
if err != nil {
t.Fatalf("cannot perform get, %v", err)
}
Expand Down
15 changes: 15 additions & 0 deletions ytypes/schema_tests/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,3 +846,18 @@ func diffJSON(a, b []byte) (string, error) {

return testutil.GenerateUnifiedDiff(strings.Join(asv, "\n"), strings.Join(bsv, "\n"))
}

func TestValidateRestrictionsWithinUnion(t *testing.T) {
b4outPolicy := &oc.RoutingPolicy_PolicyDefinition_Statement_OrderedMap{}

stmt, err := b4outPolicy.AppendNew("stmt1")
if err != nil {
t.Fatal(err)
}
stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().GetOrCreateInline().Communities = []oc.RoutingPolicy_PolicyDefinition_Statement_Actions_BgpActions_SetCommunity_Inline_Communities_Union{oc.BgpTypes_BGP_WELL_KNOWN_STD_COMMUNITY_NO_ADVERTISE}
stmt.GetOrCreateActions().GetOrCreateBgpActions().GetOrCreateSetCommunity().Method = oc.SetCommunity_Method_INLINE

if err := stmt.ΛValidate(); err != nil {
t.Fatal(err)
}
}

0 comments on commit 23c9aac

Please sign in to comment.