pathgen

package
v0.0.0-...-45eeedb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package pathgen contains a library to generate gNMI paths from a YANG model. The ygen library is used to parse YANG and obtain intermediate and some final information. The output always assumes the OpenConfig-specific conventions for a compressed schema.

TODO(wenbli): This package is written with only compressed schemas in mind. If needed, can write tests, verify, and enhance it to support uncompressed ygen structs.

Index

Constants

View Source
const (

	// WildcardSuffix is the suffix given to the wildcard versions of each
	// node as well as a list's wildcard child constructor methods that
	// distinguishes each from its non-wildcard counterpart.
	WildcardSuffix = "Any"
	// BuilderCtorSuffix is the suffix applied to the list builder
	// constructor method's name in order to indicate itself to the user.
	BuilderCtorSuffix = "Any"
	// BuilderKeyPrefix is the prefix applied to the key-modifying builder
	// method for a list PathStruct that uses the builder API.
	// NOTE: This cannot be "", as the builder method name would conflict
	// with the child constructor method for the keys.
	BuilderKeyPrefix = "With"
)

Static default configuration values that differ from the zero value for their types.

Variables

This section is empty.

Functions

func GNMIGenerator

func GNMIGenerator(pathStructName string, dir *ygen.ParsedDirectory, node *NodeData) (string, error)

GNMIGenerator is a plugin generator for generating ygnmi query objects. Note: GNMIGenerator requires that PreferOperationalState be true when generating PathStructs. TODO(DanG100): pass schema from parent to child.

func GetOrderedNodeDataNames

func GetOrderedNodeDataNames(nodeDataMap NodeDataMap) []string

GetOrderedNodeDataNames returns the alphabetically-sorted slice of keys (path struct names) for a given NodeDataMap.

Types

type GenConfig

type GenConfig struct {
	// PackageName is the name that should be used for the generating package.
	PackageName string
	// GoImports contains package import options.
	GoImports GoImports
	// PreferOperationalState generates path-build methods for only the
	// "state" version of a field when it exists under both "config" and
	// "state" containers of its parent YANG model. If it is false, then
	// the reverse is true. There are no omissions if a conflict does not
	// exist, e.g. if a leaf exists only under a "state" container, then
	// its path-building method will always be generated, and use "state".
	PreferOperationalState bool
	// ExcludeState determines whether derived state leaves are excluded
	// from the path-building methods.
	ExcludeState bool
	// FakeRootName specifies the name of the struct that should be generated
	// representing the root.
	FakeRootName string
	// PathStructSuffix is the suffix to be appended to generated
	// PathStructs to distinguish them from the generated GoStructs, which
	// assume a similar name.
	PathStructSuffix string
	// SkipEnumDeduplication specifies whether leaves of type 'enumeration' that
	// are used in multiple places in the schema should share a common type within
	// the generated code that is output by ygen. By default (false), a common type
	// is used.
	// This is the same flag used by ygen: they must match for pathgen's
	// generated code to be compatible with it.
	SkipEnumDeduplication bool
	// ShortenEnumLeafNames removes the module name from the name of
	// enumeration leaves.
	// This is the same flag used by ygen: they must match for pathgen's
	// generated code to be compatible with it.
	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.
	// This is the same flag used by ygen: they must match for pathgen's
	// generated code to be compatible with it.
	UseDefiningModuleForTypedefEnumNames bool
	// AppendEnumSuffixForSimpleUnionEnums appends an "Enum" suffix to the
	// enumeration name for simple (i.e. non-typedef) leaves which are
	// unions with an enumeration inside. This makes all inlined
	// enumerations within unions, whether typedef or not, have this
	// suffix, achieving consistency.  Since this flag is planned to be a
	// v1 compatibility flag along with
	// UseDefiningModuleForTypedefEnumNames, and will be removed in v1, it
	// only applies when useDefiningModuleForTypedefEnumNames is also set
	// to true.
	AppendEnumSuffixForSimpleUnionEnums bool
	// ExcludeModules specifies any modules that are included within the set of
	// modules that should have code generated for them that should be ignored during
	// code generation. This is due to the fact that some schemas (e.g., OpenConfig
	// interfaces) currently result in overlapping entities (e.g., /interfaces).
	ExcludeModules []string
	// YANGParseOptions provides the options that should be handed to the
	// github.com/openconfig/goyang/pkg/yang library. These specify how the
	// input YANG files should be parsed.
	YANGParseOptions yang.Options
	// GeneratingBinary is the name of the binary calling the generator library, it is
	// included in the header of output files for debugging purposes. If a
	// string is not specified, the location of the library is utilised.
	GeneratingBinary string
	// GenerateWildcardPaths means to generate wildcard nodes and paths.
	GenerateWildcardPaths bool
	// SplitByModule controls whether to generate a go package for each yang module.
	SplitByModule bool
	// TrimPackageModulePrefix is the prefix to trim from generated go package names.
	TrimPackageModulePrefix string
	// BasePackagePath is used to create to full import path of the generated go packages.
	BasePackagePath string
	// PackageString is the string to apppend to the generated Go package names.
	PackageSuffix string
	// UnifyPathStructs controls whether to generate both config and states in the same package.
	UnifyPathStructs bool
	// ExtraGenerators are custom funcs that are used to extend the path struct generation.
	ExtraGenerators []Generator
}

GenConfig stores code generation configuration.

func NewDefaultConfig

func NewDefaultConfig(schemaStructPkgPath string) *GenConfig

NewDefaultConfig creates a GenConfig with default configuration. schemaStructPkgPath is a required configuration parameter. It should be set to "" when the generated PathStruct package is to be the same package as the GoStructs package.

func (*GenConfig) GeneratePathCode

func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (map[string]*GeneratedPathCode, NodeDataMap, util.Errors)

GeneratePathCode takes a slice of strings containing the path to a set of YANG files which contain YANG modules, and a second slice of strings which specifies the set of paths that are to be searched for associated models (e.g., modules that are included by the specified set of modules, or submodules of those modules). It extracts the set of modules that are to be generated, and returns a map of package names to GeneratedPathCode structs. Each struct contains all the generated code of that package needed support the path-creation API. The important components of the generated code are listed below:

  1. Struct definitions for each container, list, or leaf schema node, as well as the fakeroot.
  2. Next-level methods for the fakeroot and each non-leaf schema node, which instantiate and return the next-level structs corresponding to its child schema nodes.

With these components, the generated API is able to support absolute path creation of any node of the input schema. Also returned is the NodeDataMap of the schema, i.e. information about each node in the generated code, which may help callers add customized augmentations to the basic generated path code. If errors are encountered during code generation, they are returned.

type GeneratedPathCode

type GeneratedPathCode struct {
	Structs      []GoPathStructCodeSnippet // Structs is the generated set of structs representing containers or lists in the input YANG models.
	CommonHeader string                    // CommonHeader is the header that should be used for all output Go files.
	Deps         map[string]bool           // Deps is the list of packages that this package depends on.
}

GeneratedPathCode contains generated code snippets that can be processed by the calling application. The generated code is divided into two types of objects - both represented as a slice of strings: Structs contains a set of Go structures that have been generated, and Enums contains the code for generated enumerated types (corresponding to identities, or enumerated values within the YANG models for which code is being generated). Additionally the header with package comment of the generated code is returned in Header, along with the a slice of strings containing the packages that are required for the generated Go code to be compiled is returned.

For schemas that contain enumerated types (identities, or enumerations), a code snippet is returned as the EnumMap field that allows the string values from the YANG schema to be resolved. The keys of the map are strings corresponding to the name of the generated type, with the map values being maps of the int64 identifier for each value of the enumeration to the name of the element, as used in the YANG schema.

func (GeneratedPathCode) SplitFiles

func (genCode GeneratedPathCode) SplitFiles(fileN int) ([]string, error)

SplitFiles returns a slice of strings, each representing a file that together contains the entire generated code. fileN specifies the number of files to split the code into, and has to be between 1 and the total number of directory entries in the input schema. By splitting, the size of the output files can be roughly controlled.

func (GeneratedPathCode) String

func (genCode GeneratedPathCode) String() string

String method for GeneratedPathCode, which can be used to write all the generated code into a single file.

type Generator

type Generator func(string, *ygen.ParsedDirectory, *NodeData) (string, error)

Generator is func to returns extra generated code for a given node.

type GoImports

type GoImports struct {
	// SchemaStructPkgPath specifies the path to the ygen-generated structs, which
	// is used to get the enum and union type names used as the list key
	// for calling a list path accessor.
	SchemaStructPkgPath string
	// YgotImportPath specifies the path to the ygot library that should be used
	// in the generated code.
	YgotImportPath string
	// YtypesImportPath specifies the path to the ytypes library that should be used
	// in the generated code.
	YtypesImportPath string
	// YgnmiImportPath is the import path to the ygnmi library that should be used
	// in the generated code.
	YgnmiImportPath string
}

GoImports contains package import options.

type GoPathStructCodeSnippet

type GoPathStructCodeSnippet struct {
	// PathStructName is the name of the struct that is contained within the snippet.
	// It is stored such that callers can identify the struct to control where it
	// is output.
	PathStructName string
	// StructBase stores the basic code snippet that represents the struct that is
	// the input when code generation is performed, which includes its definition.
	StructBase string
	// ChildConstructors contains the method code snippets with the input struct as a
	// receiver, that is used to get the child path struct.
	ChildConstructors string
	// Package is the name of the package that this snippet belongs to.
	Package string
	// Deps are any packages that this snippet depends on.
	Deps []string
	// ExtraGeneration is the output of any extra generators passed into GenConfig.
	ExtraGeneration string
}

GoPathStructCodeSnippet is used to store the generated code snippets associated with a particular Go struct entity (corresponding to a container, list, or leaf in the schema).

func (GoPathStructCodeSnippet) String

func (g GoPathStructCodeSnippet) String() string

String returns the contents of a GoPathStructCodeSnippet as a string by simply writing out all of its generated code.

type NodeData

type NodeData struct {
	// GoTypeName is the generated Go type name of a schema node. It is
	// qualified by the SchemaStructPkgAlias if necessary. It could be a
	// GoStruct or a leaf type.
	GoTypeName string
	// LocalGoTypeName is the generated Go type name of a schema node, but
	// always with the SchemaStructPkgAlias stripped. It could be a
	// GoStruct or a leaf type.
	LocalGoTypeName string
	// GoFieldName is the field name of the node under its parent struct.
	GoFieldName string
	// SubsumingGoStructName is the GoStruct type name corresponding to the node. If
	// the node is a leaf, then it is the parent GoStruct's name.
	SubsumingGoStructName string
	// IsLeaf indicates whether this child is a leaf node.
	IsLeaf bool
	// IsScalarField indicates a leaf that is stored as a pointer in its
	// parent struct.
	IsScalarField bool
	// HasDefault indicates whether this node has a default value
	// associated with it. This is only relevant to leaf or leaf-list
	// nodes.
	HasDefault bool
	// YANGTypeName is the type of the leaf given in the YANG file (without
	// the module prefix, if any, per goyang behaviour). If the node is not
	// a leaf this will be empty. Note that the current purpose for this is
	// to allow callers to handle certain types as special cases, but since
	// the name of the node is a very basic piece of information which
	// excludes the defining module, this is somewhat hacky, so it may be
	// removed or modified in the future.
	YANGTypeName string
	// YANGPath is the schema path of the YANG node.
	YANGPath string
	// GoPathPackageName is the Go package name containing the generated PathStruct for the schema node.
	GoPathPackageName string
	// YANGFieldName is the name of the field entry for this node, only set for leaves.
	YANGFieldName string
	// Directory is the name of the directory used to create this node. For leaves, it's the parent directory.
	DirectoryName string
}

NodeData contains information about the ygen-generated code of a YANG schema node.

type NodeDataMap

type NodeDataMap map[string]*NodeData

NodeDataMap is a map from the path struct type name of a schema node to its NodeData.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL