Skip to content

Commit

Permalink
Add Flag -trim_enum_openconfig_prefix to Trim Enumerated Names (#454)
Browse files Browse the repository at this point in the history
* Add Flag -trim_enum_openconfig_prefix to Trim Enumerated Names
  • Loading branch information
wenovus authored Oct 2, 2020
1 parent 14d9a77 commit 50b44fc
Show file tree
Hide file tree
Showing 28 changed files with 87,126 additions and 86,040 deletions.
2 changes: 1 addition & 1 deletion demo/bgp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func CreateDemoBGPInstance() (*oc.Bgp, error) {
// The returned struct represents the new peer that was created.
nPeer.PeerAs = ygot.Uint32(29636)
nPeer.Description = ygot.String("catalyst2 Services Ltd")
nPeer.PeerType = oc.OpenconfigBgpTypes_PeerType_INTERNAL
nPeer.PeerType = oc.BgpTypes_PeerType_INTERNAL

// Initialize all containers underneath the newly created peer. This allows us
// to now specify containers in the hierarchy that we didn't yet initialize.
Expand Down
4 changes: 2 additions & 2 deletions demo/device/devicedemo.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func CreateDemoDeviceInstance() (*oc.Device, error) {
if err != nil {
return nil, err
}
c.Type = oc.OpenconfigPlatformTypes_OPENCONFIG_SOFTWARE_COMPONENT_OPERATING_SYSTEM
c.Type = oc.PlatformTypes_OPENCONFIG_SOFTWARE_COMPONENT_OPERATING_SYSTEM

// Create a second device instance, and populate the OS component under
// it. This code demonstrates how ygot.MergeStructs can be used to combine
Expand Down Expand Up @@ -139,7 +139,7 @@ func addNetworkInstance(d *oc.Device) error {
return err
}

p, err := netinst.NewProtocol(oc.OpenconfigPolicyTypes_INSTALL_PROTOCOL_TYPE_BGP, "15169")
p, err := netinst.NewProtocol(oc.PolicyTypes_INSTALL_PROTOCOL_TYPE_BGP, "15169")
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions demo/gnmi_telemetry/gnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func CreateAFTInstance() (*oc.Device, error) {
if err != nil {
return nil, err
}
ni.Type = oc.OpenconfigNetworkInstanceTypes_NETWORK_INSTANCE_TYPE_DEFAULT_INSTANCE
ni.Type = oc.NetworkInstanceTypes_NETWORK_INSTANCE_TYPE_DEFAULT_INSTANCE

// Initialise the containers within the network instance model.
ygot.BuildEmptyTree(ni)
Expand Down Expand Up @@ -108,7 +108,7 @@ func CreateAFTInstance() (*oc.Device, error) {
nh.PushedMplsLabelStack = []oc.NetworkInstance_Afts_NextHop_PushedMplsLabelStack_Union{
oc.UnionUint32(42),
oc.UnionUint32(84),
oc.OpenconfigMplsTypes_MplsLabel_Enum_IPV4_EXPLICIT_NULL,
oc.MplsTypes_MplsLabel_Enum_IPV4_EXPLICIT_NULL,
}

return d, nil
Expand Down
85,447 changes: 42,654 additions & 42,793 deletions exampleoc/oc.go

Large diffs are not rendered by default.

244 changes: 122 additions & 122 deletions exampleoc/ocpath.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions exampleoc/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ go run ../generator/generator.go -path=public,deps -output_file=oc.go \
-generate_path_structs -path_structs_output_file=ocpath.go\
-package_name=exampleoc -generate_fakeroot -fakeroot_name=device -compress_paths=true \
-shorten_enum_leaf_names \
-trim_enum_openconfig_prefix \
-typedef_enum_with_defmod \
-exclude_modules=ietf-interfaces \
-generate_rename \
Expand Down
85,665 changes: 42,772 additions & 42,893 deletions exampleoc/wrapperunionoc/oc.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions exampleoc/wrapperunionoc/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cp ../../demo/getting_started/yang/{ietf,iana}* deps
go run ../../generator/generator.go -path=public,deps -output_file=oc.go \
-package_name=wrapperunionoc -generate_fakeroot -fakeroot_name=device -compress_paths=true \
-shorten_enum_leaf_names \
-trim_enum_openconfig_prefix \
-typedef_enum_with_defmod \
-exclude_modules=ietf-interfaces \
-generate_rename \
Expand Down
14 changes: 14 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ var (
shortenEnumLeafNames = flag.Bool("shorten_enum_leaf_names", false, "If also set to true when compress_paths=true, all leaves of type enumeration will by default not be prefixed with the name of its residing module.")
useDefiningModuleForTypedefEnumNames = flag.Bool("typedef_enum_with_defmod", false, "If set to true, all typedefs of type enumeration or identity will be prefixed with the name of its module of definition instead of its residing module.")
ygotImportPath = flag.String("ygot_path", genutil.GoDefaultYgotImportPath, "The import path to use for ygot.")
trimEnumOpenConfigPrefix = flag.Bool("trim_enum_openconfig_prefix", false, `If set to true when compressPaths=true, the organizational prefix "openconfig-" is trimmed from the module part of the name of enumerated names in the generated code`)
enumOrgPrefixesToTrim []string

// Flags used for GoStruct generation only.
generateFakeRoot = flag.Bool("generate_fakeroot", false, "If set to true, a fake element at the root of the data tree is generated. By default the fake root entity is named Device, its name can be controlled with the fakeroot_name flag.")
Expand Down Expand Up @@ -232,12 +234,22 @@ func writeFiles(dir string, out map[string]string) error {
return nil
}

// processFlags does some minimal processing of flags where otherwise
// inconvenient before they're passed to the code generators.
func processFlags() {
if *compressPaths && *trimEnumOpenConfigPrefix {
// No organization name is trimmed if compress paths is false.
enumOrgPrefixesToTrim = []string{"openconfig"}
}
}

// main parses command-line flags to determine the set of YANG modules for
// which code generation should be performed, and calls the codegen library
// to generate Go code corresponding to their schema. The output is written
// to the specified file.
func main() {
flag.Parse()
processFlags()
// Extract the set of modules that code is to be generated for,
// throwing an error if the set is empty.
generateModules := flag.Args()
Expand Down Expand Up @@ -310,6 +322,7 @@ func main() {
GenerateFakeRoot: *generateFakeRoot,
FakeRootName: *fakeRootName,
ShortenEnumLeafNames: *shortenEnumLeafNames,
EnumOrgPrefixesToTrim: enumOrgPrefixesToTrim,
UseDefiningModuleForTypedefEnumNames: *useDefiningModuleForTypedefEnumNames,
},
PackageName: *packageName,
Expand Down Expand Up @@ -390,6 +403,7 @@ func main() {
PreferOperationalState: *preferOperationalState,
SkipEnumDeduplication: *skipEnumDedup,
ShortenEnumLeafNames: *shortenEnumLeafNames,
EnumOrgPrefixesToTrim: enumOrgPrefixesToTrim,
UseDefiningModuleForTypedefEnumNames: *useDefiningModuleForTypedefEnumNames,
FakeRootName: *fakeRootName,
PathStructSuffix: *pathStructSuffix,
Expand Down
17 changes: 10 additions & 7 deletions ygen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ type TransformationOpts struct {
// ShortenEnumLeafNames removes the module name from the name of
// enumeration leaves.
ShortenEnumLeafNames bool
// EnumOrgPrefixesToTrim trims the organization name from the module
// part of the name of enumeration leaves if there is a match.
EnumOrgPrefixesToTrim []string
// UseDefiningModuleForTypedefEnumNames uses the defining module name
// to prefix typedef enumerated types instead of the module where the
// typedef enumerated value is used.
Expand Down Expand Up @@ -383,15 +386,15 @@ func (cg *YANGCodeGenerator) GenerateGoCode(yangFiles, includePaths []string) (*
return nil, errs
}

enumSet, goEnums, errs := findEnumSet(mdef.enumEntries, cg.Config.TransformationOptions.CompressBehaviour.CompressEnabled(), false, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames)
enumSet, goEnums, errs := findEnumSet(mdef.enumEntries, cg.Config.TransformationOptions.CompressBehaviour.CompressEnabled(), false, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.Config.TransformationOptions.EnumOrgPrefixesToTrim)
if errs != nil {
return nil, errs
}

// Store the returned schematree and enumSet within the state for this code generation.
gogen := newGoGenState(mdef.schematree, enumSet)

directoryMap, errs := gogen.buildDirectoryDefinitions(mdef.directoryEntries, cg.Config.TransformationOptions.CompressBehaviour, cg.Config.TransformationOptions.GenerateFakeRoot, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames)
directoryMap, errs := gogen.buildDirectoryDefinitions(mdef.directoryEntries, cg.Config.TransformationOptions.CompressBehaviour, cg.Config.TransformationOptions.GenerateFakeRoot, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.Config.TransformationOptions.EnumOrgPrefixesToTrim)
if errs != nil {
return nil, errs
}
Expand Down Expand Up @@ -427,7 +430,7 @@ func (cg *YANGCodeGenerator) GenerateGoCode(yangFiles, includePaths []string) (*
continue
}
structOut, errs := writeGoStruct(dirNameMap[directoryName], directoryMap, gogen,
cg.Config.TransformationOptions.CompressBehaviour.CompressEnabled(), cg.Config.GenerateJSONSchema, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.Config.GoOptions)
cg.Config.TransformationOptions.CompressBehaviour.CompressEnabled(), cg.Config.GenerateJSONSchema, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.Config.TransformationOptions.EnumOrgPrefixesToTrim, cg.Config.GoOptions)
if errs != nil {
codegenErr = util.AppendErrs(codegenErr, errs)
continue
Expand Down Expand Up @@ -512,14 +515,14 @@ func (dcg *DirectoryGenConfig) GetDirectoriesAndLeafTypes(yangFiles, includePath

dirsToProcess := map[string]*yang.Entry(mdef.directoryEntries)

enumSet, _, errs := findEnumSet(mdef.enumEntries, cg.TransformationOptions.CompressBehaviour.CompressEnabled(), false, cg.ParseOptions.SkipEnumDeduplication, cg.TransformationOptions.ShortenEnumLeafNames, cg.TransformationOptions.UseDefiningModuleForTypedefEnumNames)
enumSet, _, errs := findEnumSet(mdef.enumEntries, cg.TransformationOptions.CompressBehaviour.CompressEnabled(), false, cg.ParseOptions.SkipEnumDeduplication, cg.TransformationOptions.ShortenEnumLeafNames, cg.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.TransformationOptions.EnumOrgPrefixesToTrim)
if errs != nil {
return nil, nil, errs
}
// Store the returned schematree and enumSet within the state for this code generation.
gogen := newGoGenState(mdef.schematree, enumSet)

directoryMap, errs := gogen.buildDirectoryDefinitions(dirsToProcess, cg.TransformationOptions.CompressBehaviour, cg.TransformationOptions.GenerateFakeRoot, cg.ParseOptions.SkipEnumDeduplication, cg.TransformationOptions.ShortenEnumLeafNames, cg.TransformationOptions.UseDefiningModuleForTypedefEnumNames)
directoryMap, errs := gogen.buildDirectoryDefinitions(dirsToProcess, cg.TransformationOptions.CompressBehaviour, cg.TransformationOptions.GenerateFakeRoot, cg.ParseOptions.SkipEnumDeduplication, cg.TransformationOptions.ShortenEnumLeafNames, cg.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.TransformationOptions.EnumOrgPrefixesToTrim)
if errs != nil {
return nil, nil, errs
}
Expand All @@ -540,7 +543,7 @@ func (dcg *DirectoryGenConfig) GetDirectoriesAndLeafTypes(yangFiles, includePath
for _, fieldName := range GetOrderedFieldNames(dir) {
field := dir.Fields[fieldName]
if isLeaf := field.IsLeaf() || field.IsLeafList(); isLeaf {
mtype, err := gogen.yangTypeToGoType(resolveTypeArgs{yangType: field.Type, contextEntry: field}, dcg.TransformationOptions.CompressBehaviour.CompressEnabled(), cg.ParseOptions.SkipEnumDeduplication, cg.TransformationOptions.ShortenEnumLeafNames, cg.TransformationOptions.UseDefiningModuleForTypedefEnumNames)
mtype, err := gogen.yangTypeToGoType(resolveTypeArgs{yangType: field.Type, contextEntry: field}, dcg.TransformationOptions.CompressBehaviour.CompressEnabled(), cg.ParseOptions.SkipEnumDeduplication, cg.TransformationOptions.ShortenEnumLeafNames, cg.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.TransformationOptions.EnumOrgPrefixesToTrim)
if err != nil {
errs = util.AppendErr(errs, err)
continue
Expand Down Expand Up @@ -614,7 +617,7 @@ func (cg *YANGCodeGenerator) GenerateProto3(yangFiles, includePaths []string) (*
if errs != nil {
return nil, errs
}
enumSet, penums, errs := findEnumSet(mdef.enumEntries, cg.Config.TransformationOptions.CompressBehaviour.CompressEnabled(), true, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames)
enumSet, penums, errs := findEnumSet(mdef.enumEntries, cg.Config.TransformationOptions.CompressBehaviour.CompressEnabled(), true, cg.Config.ParseOptions.SkipEnumDeduplication, cg.Config.TransformationOptions.ShortenEnumLeafNames, cg.Config.TransformationOptions.UseDefiningModuleForTypedefEnumNames, cg.Config.TransformationOptions.EnumOrgPrefixesToTrim)
if errs != nil {
return nil, errs
}
Expand Down
46 changes: 45 additions & 1 deletion ygen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ type yangTestCase struct {
// invalid.
func TestSimpleStructs(t *testing.T) {
tests := []yangTestCase{{
name: "simple openconfig test, with compression",
name: "simple openconfig test, with compression, with (useless) enum org name trimming",
inFiles: []string{filepath.Join(datapath, "openconfig-simple.yang")},
inConfig: GeneratorConfig{
GoOptions: GoOpts{
Expand All @@ -437,6 +437,7 @@ func TestSimpleStructs(t *testing.T) {
TransformationOptions: TransformationOpts{
CompressBehaviour: genutil.PreferIntendedConfig,
ShortenEnumLeafNames: true,
EnumOrgPrefixesToTrim: []string{"openconfig"},
UseDefiningModuleForTypedefEnumNames: true,
},
},
Expand All @@ -454,6 +455,34 @@ func TestSimpleStructs(t *testing.T) {
},
},
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-simple-no-compress.formatted-txt"),
}, {
name: "simple openconfig test, with compression, without shortened enum leaf names, with enum org name trimming",
inFiles: []string{filepath.Join(datapath, "openconfig-simple.yang")},
inConfig: GeneratorConfig{
GoOptions: GoOpts{
GenerateSimpleUnions: true,
},
TransformationOptions: TransformationOpts{
CompressBehaviour: genutil.PreferIntendedConfig,
EnumOrgPrefixesToTrim: []string{"openconfig"},
UseDefiningModuleForTypedefEnumNames: true,
},
},
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-simple.long-enum-names.trimmed-enum.formatted-txt"),
}, {
name: "simple openconfig test, with no compression, with enum org name trimming",
inFiles: []string{filepath.Join(datapath, "openconfig-simple.yang")},
inConfig: GeneratorConfig{
GoOptions: GoOpts{
GenerateSimpleUnions: true,
},
TransformationOptions: TransformationOpts{
ShortenEnumLeafNames: true,
EnumOrgPrefixesToTrim: []string{"openconfig"},
UseDefiningModuleForTypedefEnumNames: true,
},
},
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-simple-no-compress.trimmed-enum.formatted-txt"),
}, {
name: "OpenConfig schema test - with annotations",
inFiles: []string{filepath.Join(datapath, "openconfig-simple.yang")},
Expand Down Expand Up @@ -513,6 +542,21 @@ func TestSimpleStructs(t *testing.T) {
},
},
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-list-enum-key.formatted-txt"),
}, {
name: "simple openconfig test, with a list that has an enumeration key, with enum org name trimming",
inFiles: []string{filepath.Join(datapath, "openconfig-list-enum-key.yang")},
inConfig: GeneratorConfig{
GoOptions: GoOpts{
GenerateSimpleUnions: true,
},
TransformationOptions: TransformationOpts{
CompressBehaviour: genutil.PreferIntendedConfig,
ShortenEnumLeafNames: true,
EnumOrgPrefixesToTrim: []string{"openconfig"},
UseDefiningModuleForTypedefEnumNames: true,
},
},
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-list-enum-key.trimmed-enum.formatted-txt"),
}, {
name: "openconfig test with a identityref union",
inFiles: []string{filepath.Join(datapath, "openconfig-unione.yang")},
Expand Down
Loading

0 comments on commit 50b44fc

Please sign in to comment.