ygen

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2022 License: Apache-2.0 Imports: 24 Imported by: 24

Documentation

Overview

Package ygen contains a library to generate Go structs from a YANG model. The Goyang parsing library is used to parse YANG. The output can consider OpenConfig-specific conventions such that the schema is compressed.

Index

Constants

View Source
const (
	// DefaultBasePackageName defines the default base package that is
	// generated when generating proto3 code.
	DefaultBasePackageName = "openconfig"
	// DefaultEnumPackageName defines the default package name that is
	// used for the package that defines enumerated types that are
	// used throughout the schema.
	DefaultEnumPackageName = "enums"
	// DefaultYwrapperPath defines the default import path for the ywrapper.proto file,
	// excluding the filename.
	DefaultYwrapperPath = "github.com/openconfig/ygot/proto/ywrapper"
	// DefaultYextPath defines the default import path for the yext.proto file, excluding
	// the filename.
	DefaultYextPath = "github.com/openconfig/ygot/proto/yext"
)

Constants defining the defaults for Protobuf package generation. These constants can be referred to by calling applications as defaults that are presented to a user.

View Source
const (

	// DefaultAnnotationPrefix is the default string that is used to prefix the name
	// of metadata fields in the output Go structs.
	DefaultAnnotationPrefix string = "Λ"
)

Variables

This section is empty.

Functions

func BytesToGoByteSlice

func BytesToGoByteSlice(b []byte) []string

BytesToGoByteSlice takes an input slice of bytes and outputs it as a slice of strings corresponding to lines of Go source code that define the byte slice. Each string within the slice contains up to 16 bytes of the output byte array, with each byte represented as a two digit hex character.

func FindSchemaPath added in v0.7.0

func FindSchemaPath(parent *Directory, fieldName string, absolutePaths bool) ([]string, error)

FindSchemaPath finds the relative or absolute schema path of a given field of a Directory. The Field is specified as a name in order to guarantee its existence before processing.

func FindShadowSchemaPath added in v0.18.0

func FindShadowSchemaPath(parent *Directory, fieldName string, absolutePaths bool) ([]string, error)

FindShadowSchemaPath finds the relative or absolute schema path of a given field of a Directory with preference to the shadow path. The Field is specified as a name in order to guarantee its existence before processing. NOTE: No error is returned if fieldName is not found.

func GetOrderedDirectories added in v0.7.0

func GetOrderedDirectories(directory map[string]*Directory) ([]string, map[string]*Directory, error)

GetOrderedDirectories returns an alphabetically-ordered slice of Directory names and a map of Directories keyed by their names instead of their paths, so that each directory can be processed in alphabetical order. This helps produce deterministic generated code, and minimize diffs when compared with expected output (i.e., diffs don't appear simply due to reordering of the Directory maps). If the names of the directories are not unique, which is unexpected, an error is returned. TODO(wenbli): Deprecate this after ygot uses the IR for code generation. This function's purpose is to check for name conflicts. This functionality doesn't belong during IR processing but rather with downstream language-specific processing since it's possible that conflicts are allowed (e.g. nested struct definitions).

func GetOrderedFieldNames added in v0.7.0

func GetOrderedFieldNames(directory *Directory) []string

GetOrderedFieldNames returns the field names of a Directory in alphabetical order.

func GetOrderedPathDirectories added in v0.18.1

func GetOrderedPathDirectories(directory map[string]*Directory) []string

GetOrderedPathDirectories returns an alphabetically-ordered slice of Directory names and a map of Directories keyed by their paths, so that each directory can be processed in path-alphabetical order. This helps produce deterministic generated code, and minimize diffs when compared with expected output (i.e., diffs don't appear simply due to reordering of the Directory maps).

func GoFieldNameMap added in v0.7.0

func GoFieldNameMap(directory *ParsedDirectory) map[string]string

GoFieldNameMap returns a map containing the Go name for a field (key is the field schema name). Name Uniquification is done to ensure compilation -- this is done deterministically.

func IsFakeRoot added in v0.7.0

func IsFakeRoot(e *yang.Entry) bool

IsFakeRoot checks whether a given entry is the generated fake root.

func IsScalarField added in v0.7.0

func IsScalarField(field *NodeDetails) bool

IsScalarField determines which fields should be converted to pointers when outputting structs; this is done to allow checks against nil.

func IsYgenDefinedGoType added in v0.7.0

func IsYgenDefinedGoType(t *MappedType) bool

IsYgenDefinedGoType returns true if the native type of a MappedType is a Go type that's defined by ygen's generated code.

func MakeFakeRoot added in v0.7.0

func MakeFakeRoot(rootName string) *yang.Entry

MakeFakeRoot creates and returns a fakeroot *yang.Entry with rootName as its name. It has an empty, but initialized Dir.

func WriteGzippedByteSlice

func WriteGzippedByteSlice(b []byte) ([]byte, error)

WriteGzippedByteSlice takes an input slice of bytes, gzips it and returns the resulting compressed output as a byte slice.

Types

type DirType added in v0.8.4

type DirType int64

DirType describes the different types of Directory that can be output within the IR such that 'list' directories can have special handling applied.

const (

	// Container represents a YANG 'container'.
	Container DirType
	// List represents a YANG 'list'.
	List
)

type Directory added in v0.7.0

type Directory struct {
	Name           string                 // Name is the name of the struct to be generated.
	Entry          *yang.Entry            // Entry is the yang.Entry that corresponds to the schema element being converted to a struct.
	Fields         map[string]*yang.Entry // Fields is a map, keyed by the YANG node identifier, of the entries that are the struct fields.
	ShadowedFields map[string]*yang.Entry // ShadowedFields is a map, keyed by the YANG node identifier, of the field entries duplicated via compression.
	Path           []string               // Path is a slice of strings indicating the element's path.
	ListAttr       *YangListAttr          // ListAttr is used to store characteristics of structs that represent YANG lists.
	IsFakeRoot     bool                   // IsFakeRoot indicates that the struct is a fake root struct, so specific mapping rules should be implemented.
}

Directory stores information needed for outputting a data node of the generated code. When viewed as a collection of entries that is generated from an entire YANG schema, they serve the purpose of mapping the YANG schema tree to a directory tree (connected through implicit yang.Entry edges) where each directory corresponds to a data node of the Go version of the schema, and where digested data is stored that is friendly to the code generation algorithm.

type DirectoryGenConfig added in v0.7.0

type DirectoryGenConfig struct {
	// ParseOptions contains parsing options for a given set of schema files.
	ParseOptions ParseOpts
	// TransformationOptions contains options for how the generated code
	// may be transformed from a simple 1:1 mapping with respect to the
	// given YANG schema.
	TransformationOptions TransformationOpts
	// GoOptions stores a struct which stores Go code generation specific
	// options for the code generaton.
	GoOptions GoOpts
}

DirectoryGenConfig contains the configuration necessary to generate a set of Directory objects for a given schema. The set of Directory objects is the intermediate representation generated by ygen, which can be useful for external code generation libraries which can make use of it.

func (*DirectoryGenConfig) GetDirectoriesAndLeafTypes added in v0.7.0

func (dcg *DirectoryGenConfig) GetDirectoriesAndLeafTypes(yangFiles, includePaths []string) (map[string]*Directory, map[string]map[string]*MappedType, util.Errors)

GetDirectoriesAndLeafTypes parses YANG files and returns two path-keyed maps. The first contains Directory entries that is the intermediate representation used by ygen for subsequent code generation, and the second contains the *MappedType for each field of the same corresponding Directory entries, with non-leafs having a nil value. This representation may be useful to external code generation libraries that make use of such information, e.g. if their output code depends on a ygen-generated type. yangFiles is a slice of strings containing the path to a set of YANG files which contain YANG modules, includePaths is 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). Any errors encountered during code generation are returned.

type EnumeratedValueType added in v0.8.4

type EnumeratedValueType int64

EnumeratedValueType is used to indicate the source YANG type that an enumeration was generated based on.

const (
	UnknownEnumerationType EnumeratedValueType = iota
	// SimpleEnumerationType represents 'enumeration' leaves within
	// the YANG schema that are defined inline.
	SimpleEnumerationType
	// DerivedEnumerationType represents enumerations that are defined
	// within a YANG 'typedef'.
	DerivedEnumerationType
	// UnionEnumerationType represents a 'type enumeration' defined within
	// a union.
	UnionEnumerationType
	// DerivedUnionEnumerationType represents a 'enumeration' defined
	// within a union that is itself within a typedef.
	DerivedUnionEnumerationType
	// IdentityType represents an enumeration that is an 'identity'
	// within the YANG schema.
	IdentityType
)

func (EnumeratedValueType) String added in v0.21.0

func (n EnumeratedValueType) String() string

type EnumeratedYANGType added in v0.8.4

type EnumeratedYANGType struct {
	// Name is the name of the generated enumeration to be
	// used in the generated code.
	Name string
	// Kind indicates the type of enumerated value that the
	// EnumeratedYANGType represents - allowing for a code
	// generation mechanism to select how different enumerated
	// value types are output.
	Kind EnumeratedValueType

	// TypeName stores the original YANG type name for the enumeration.
	TypeName string
	// TypeDefaultValue stores the default value of the enum type's default
	// statement (note: this is different from the default statement of the
	// leaf type).
	TypeDefaultValue string
	// ValToYANGDetails stores the YANG-ordered set of enumeration value
	// and its YANG-specific details (as defined by the
	// ygot.EnumDefinition).
	ValToYANGDetails []ygot.EnumDefinition
	// contains filtered or unexported fields
}

EnumeratedYANGType is an abstract representation of an enumerated type to be produced in the output code.

type GeneratedGoCode

type GeneratedGoCode struct {
	Structs      []GoStructCodeSnippet // Structs is the generated set of structs representing containers or lists in the input YANG models.
	Enums        []string              // Enums is the generated set of enum definitions corresponding to identities and enumerations in the input YANG models.
	CommonHeader string                // CommonHeader is the header that should be used for all output Go files.
	OneOffHeader string                // OneOffHeader defines the header that should be included in only one output Go file - such as package init statements.
	EnumMap      string                // EnumMap is a Go map that allows the YANG string values of enumerated types to be resolved.
	// JSONSchemaCode contains code defining a variable storing a serialised JSON schema for the
	// generated Go structs. When deserialised it consists of a map[string]*yang.Entry. The
	// entries are the root level yang.Entry definitions along with their corresponding
	// hierarchy (i.e., the yang.Entry for /foo contains /foo/... - all of foo's descendents).
	// Each yang.Entry which corresponds to a generated Go struct has two extra fields defined:
	//  - schemapath - the path to this entry within the schema. This is provided since the Path() method of
	//                 the deserialised yang.Entry does not return the path since the Parent pointer is not
	//                 populated.
	//  - structname - the name of the struct that was generated for the schema element.
	JSONSchemaCode string
	// RawJSONSchema stores the JSON document which is serialised and stored in JSONSchemaCode.
	// It is populated only if the StoreRawSchema YANGCodeGenerator boolean is set to true.
	RawJSONSchema []byte
	// EnumTypeMap is a Go map that allows YANG schemapaths to be mapped to reflect.Type values.
	EnumTypeMap string
}

GeneratedGoCode 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.

type GeneratedProto3

type GeneratedProto3 struct {
	// Packages stores a map, keyed by the Protobuf package name, and containing the contents of the protobuf3
	// messages defined within the package. The calling application can write out the defined packages to the
	// files expected by the protoc tool.
	Packages map[string]Proto3Package
}

GeneratedProto3 stores a set of generated Protobuf packages.

type GeneratorConfig

type GeneratorConfig struct {
	// PackageName is the name that should be used for the generating package.
	PackageName string
	// Caller 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.
	Caller string
	// GenerateJSONSchema stores a boolean which defines whether to generate
	// the JSON corresponding to the YANG schema parsed to generate the
	// output code.
	GenerateJSONSchema bool
	// StoreRawSchema the raw JSON schema should be returned by the code
	// generation function, such that it can be handled by an external
	// library.
	StoreRawSchema bool
	// ParseOptions contains parsing options for a given set of schema files.
	ParseOptions ParseOpts
	// TransformationOptions contains options for how the generated code
	// may be transformed from a simple 1:1 mapping with respect to the
	// given YANG schema.
	TransformationOptions TransformationOpts
	// GoOptions stores a struct which stores Go code generation specific
	// options for the code generaton.
	GoOptions GoOpts
	// ProtoOptions stores a struct which contains Protobuf specific options.
	ProtoOptions ProtoOpts
	// IncludeDescriptions specifies that YANG entry descriptions are added
	// to the JSON schema. Is false by default, to reduce the size of generated schema
	IncludeDescriptions bool
}

GeneratorConfig stores the configuration options used for code generation.

type GoLangMapper added in v0.18.1

type GoLangMapper struct {
	// contains filtered or unexported fields
}

GoLangMapper contains the functionality and state for generating Go names for the generated code.

func NewGoLangMapper added in v0.19.0

func NewGoLangMapper(simpleUnions bool) *GoLangMapper

NewGoLangMapper creates a new GoLangMapper instance, initialised with the default state required for code generation.

func (*GoLangMapper) DirectoryName added in v0.18.1

func (s *GoLangMapper) DirectoryName(e *yang.Entry, compressBehaviour genutil.CompressBehaviour) (string, error)

DirectoryName generates the final name to be used for a particular YANG schema element in the generated Go code. If path compressing is active, schemapaths are compressed, otherwise the name is returned simply as camel case. Although name conversion is lossy, name uniquification occurs at this stage since all generated struct names reside in the package namespace.

func (*GoLangMapper) FieldName added in v0.18.1

func (s *GoLangMapper) FieldName(e *yang.Entry) (string, error)

FieldName maps the input entry's name to what the Go name of the field would be. Since this conversion is lossy, a later step should resolve any naming conflicts between different fields.

func (*GoLangMapper) KeyLeafType added in v0.18.1

func (s *GoLangMapper) KeyLeafType(e *yang.Entry, opts IROptions) (*MappedType, error)

LeafType maps the input list key entry to a MappedType object containing the type information about the key field.

func (*GoLangMapper) LeafType added in v0.18.1

func (s *GoLangMapper) LeafType(e *yang.Entry, opts IROptions) (*MappedType, error)

LeafType maps the input leaf entry to a MappedType object containing the type information about the field.

func (*GoLangMapper) PackageName added in v0.18.1

PackageName is not used by Go generation.

func (*GoLangMapper) SetEnumSet added in v0.18.1

func (s *GoLangMapper) SetEnumSet(e *enumSet)

SetEnumSet is used to supply a set of enumerated values to the mapper such that leaves that have enumerated types can be looked up.

func (*GoLangMapper) SetSchemaTree added in v0.18.1

func (s *GoLangMapper) SetSchemaTree(st *schemaTree)

SetSchemaTree is used to supply a copy of the YANG schema tree to the mapped such that leaves of type leafref can be resolved to their target leaves.

type GoOpts

type GoOpts struct {
	// SchemaVarName is the name for the variable which stores the compressed
	// JSON schema in the generated Go code. JSON schema output is only
	// produced if the GenerateJSONSchema YANGCodeGenerator field is set to
	// true.
	SchemaVarName string
	// GoyangImportPath specifies the path that should be used in the generated
	// code for importing the goyang/pkg/yang package.
	GoyangImportPath string
	// YgotImportPath specifies the path to the ygot library that should be used
	// in the generated code.
	YgotImportPath string
	// YtypesImportPath specifies the path to ytypes library that should be used
	// in the generated code.
	YtypesImportPath string
	// GenerateRenameMethod specifies whether methods for renaming list entries
	// should be generated in the output Go code.
	GenerateRenameMethod bool
	// AddAnnotationFields specifies whether annotation fields should be added to
	// the generated structs. When set to true, a metadata field is added for each
	// struct, and for each field of each struct. Metadata field's names are
	// prefixed by the string specified in the AnnotationPrefix argument.
	AddAnnotationFields bool
	// AnnotationPrefix specifies the string which is prefixed to the name of
	// annotation fields. It defaults to Λ.
	AnnotationPrefix string
	// AddYangPresence specifies whether tags should be added to the generated
	// fields of a struct. When set to true, a struct tag will be added to the field
	// when a YANG container is a presence container
	// https://datatracker.ietf.org/doc/html/rfc6020#section-7.5.1
	// a field tag of `yangPresence="true"` will only be added if the container is
	// a YANG presence container, and will be omitted if this is not the case.
	AddYangPresence bool
	// GenerateGetters specifies whether GetOrCreate* methods should be created
	// for struct pointer (YANG container) and map (YANG list) fields of generated
	// structs.
	GenerateGetters bool
	// GenerateDeleteMethod specifies whether Delete* methods should be created for
	// map (YANG list) fields of generated structs.
	GenerateDeleteMethod bool
	// GenerateAppendList specifies whether Append* methods should be created for
	// list fields of a struct. These methods take an input list member type, extract
	// the key and append the supplied value to the list.
	GenerateAppendMethod bool
	// GenerateSimpleUnions specifies whether simple typedefs are used to
	// represent union subtypes in the generated code instead of using
	// wrapper types.
	GenerateSimpleUnions bool
	// GenerateLeafGetters specifies whether Get* methods should be created for
	// leaf fields of a struct. Care should be taken with this option since a Get
	// method returns the *Go* zero value for a particular entity if the field is
	// unset. This means that it is not possible for a caller of method to know
	// whether a field has been explicitly set to the zero value (i.e., an integer
	// field is set to 0), or whether the field was actually unset.
	GenerateLeafGetters bool
	// GeneratePopulateDefault specifies whether a PopulateDefaults method
	// should be generated for every GoStruct that recursively populates
	// default values within the subtree.
	GeneratePopulateDefault bool
	// GNMIProtoPath specifies the path to the generated gNMI protobuf, which
	// is used to store the catalogue entries for generated modules.
	GNMIProtoPath string
	// ValidateFunctionName specifies the name of a function that proxies ΛValidate.
	ValidateFunctionName string
	// IncludeModelData specifies whether gNMI ModelData messages should be generated
	// in the output code.
	IncludeModelData 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
}

GoOpts stores Go specific options for the code generation library.

type GoStructCodeSnippet added in v0.6.0

type GoStructCodeSnippet struct {
	// StructName 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.
	StructName string
	// StructDef stores the code snippet that represents the struct that is
	// the input when code generation is performed.
	StructDef string
	// ListKeys stores code snippets that are associated with structs that are
	// generated to represent the keys of multi-key lists. In the case that the
	// Go struct for which the code is being generated does not contain a list
	// with multiple keys, this string is empty.
	ListKeys string
	// Methods contains code snippsets that represent functions that have the
	// input struct as a receiver, that help the user create new entries within
	// lists, without needing to populate the keys of the list.
	Methods string
	// Interfaces contains code snippets that represent interfaces that are
	// used within the generated struct. Used when there are interfaces that
	// represent multi-type unions generated.
	Interfaces string
}

GoStructCodeSnippet is used to store the generated code snippets associated with a particular Go struct entity (generated from a container or list).

func (GoStructCodeSnippet) String added in v0.6.0

func (g GoStructCodeSnippet) String() string

String returns the contents of the receiver GoStructCodeSnippet as a string.

type IR added in v0.8.4

type IR struct {
	// Directories is the set of 'directory', or non-leaf entries that are
	// to be produced in the generated code. They are keyed by the absolute
	// YANG path of their locations.
	Directories map[string]*ParsedDirectory

	// Enums is the set of enumerated entries that are to be output in the
	// generated language code. They are each keyed by a name that
	// uniquely identifies the enumeration. Note that this name may not be
	// the same type name that would be used in the generated code due to
	// inner definitions.
	Enums map[string]*EnumeratedYANGType

	// ModelData stores the metadata extracted from the input YANG modules.
	ModelData []*gpb.ModelData
	// contains filtered or unexported fields
}

IR represents the returned intermediate representation produced by ygen to be consumed by language-specific passes prior to code generation.

func GenerateIR added in v0.8.4

func GenerateIR(yangFiles, includePaths []string, langMapper LangMapper, opts IROptions) (*IR, error)

GenerateIR creates the ygen intermediate representation for a set of YANG modules. The YANG files to be parsed are read from the yangFiles argument, with any includes that they use searched for in the string slice of paths specified by includePaths. The supplier LangMapper interface is used to perform mapping of language-specific naming whilst creating the IR -- the full details of the implementation of LangMapper can be found in ygen/ir.go and docs/code-generation-design.md.

The supplied IROptions controls the parsing and transformations that are applied to the schema whilst generating the IR.

GenerateIR returns the complete ygen intermediate representation.

func (*IR) OrderedDirectoryPaths added in v0.19.0

func (ir *IR) OrderedDirectoryPaths() []string

OrderedDirectoryPaths returns the absolute YANG paths of all ParsedDirectory entries in the IR in lexicographical order.

func (*IR) OrderedDirectoryPathsByName added in v0.19.0

func (ir *IR) OrderedDirectoryPathsByName() []string

OrderedDirectoryPathsByName returns the absolute YANG paths of all ParsedDirectory entries in the IR in the lexicographical order of their candidate generated names. Where there are duplicate names the path is used to tie-break.

func (*IR) SchemaTree added in v0.19.0

func (ir *IR) SchemaTree(inclDescriptions bool) ([]byte, error)

SchemaTree returns a JSON serialised tree of the schema for the set of modules used to generate the IR. The JSON document that is returned is always rooted on a yang.Entry which corresponds to the root item, and stores all root-level enties (and their subtrees) within the input module set. All YANG directories are annotated in the output JSON with the name of the type they correspond to in the generated code, and the absolute schema path that the entry corresponds to. In the case that there is not a fake root struct, a synthetic root entry is used to store the schema tree.

type IROptions added in v0.8.4

type IROptions struct {
	// ParseOptions specifies the options for how the YANG schema is
	// produced.
	ParseOptions ParseOpts

	// Transformation options specifies any transformations that should
	// be applied to the input YANG schema when producing the IR.
	TransformationOptions TransformationOpts

	// NestedDirectories specifies whether the generated directories should
	// be nested in the IR.
	NestedDirectories bool

	// AbsoluteMapPaths specifies whether the path annotation provided for
	// each field should be relative paths or absolute paths.
	AbsoluteMapPaths 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.
	// NOTE: This flag will be removed by v1 release.
	AppendEnumSuffixForSimpleUnionEnums bool
}

IROptions contains options used to customize IR generation.

type LangMapper added in v0.8.4

type LangMapper interface {
	// FieldName maps an input yang.Entry to the name that should be used
	// in the intermediate representation. It is called for each field of
	// a defined directory.
	FieldName(e *yang.Entry) (string, error)

	// DirectoryName maps an input yang.Entry to the name that should be used in the
	// intermediate representation (IR). It is called for any directory entity that
	// is to be output in the generated code.
	DirectoryName(*yang.Entry, genutil.CompressBehaviour) (string, error)

	// KeyLeafType maps an input yang.Entry which must represent a leaf to the
	// type that should be used when the leaf is used in the context of a
	// list key within the output IR.
	KeyLeafType(*yang.Entry, IROptions) (*MappedType, error)

	// LeafType maps an input yang.Entry which must represent a leaf to the
	// type that should be used when the leaf is used in the context of a
	// field within a directory within the output IR.
	LeafType(*yang.Entry, IROptions) (*MappedType, error)

	// PackageName maps an input yang.Entry, which must correspond to a
	// directory type (container or list), to the package name to which it
	// belongs. The bool parameter specifies whether the generated
	// directories will be nested or not since some languages allow nested
	// structs.
	PackageName(*yang.Entry, genutil.CompressBehaviour, bool) (string, error)

	// SetEnumSet is used to supply a set of enumerated values to the
	// mapper such that leaves that have enumerated types can be looked up.
	// An enumSet provides lookup methods that allow:
	//  - simple enumerated types
	//  - identityrefs
	//  - enumerations within typedefs
	//  - identityrefs within typedefs
	// to be resolved to the corresponding type that is to be used in
	// the IR.
	SetEnumSet(*enumSet)

	// SetSchemaTree is used to supply a copy of the YANG schema tree to
	// the mapped such that leaves of type leafref can be resolved to
	// their target leaves.
	SetSchemaTree(*schemaTree)
}

LangMapper is the interface to be implemented by a language-specific library and provided as an input to the IR production phase of ygen.

Note: though the output names are meant to be usable within the output language, it may not be the final name used in the generated code, for example due to naming conflicts, which are better resolved in a later pass prior to code generation (see note below).

NB: LangMapper's methods should be idempotent, such that the order in which they're called and the number of times each is called per input parameter does not affect the output. Do not depend on the same order of method calls on langMapper by GenerateIR.

type ListKey added in v0.18.1

type ListKey struct {
	// Name is the candidate language-specific name of the list key leaf.
	Name string
	// LangType describes the type that the node should be given in
	// the output code, using the output of the language-specific
	// type mapping provided by calling the LangMapper interface.
	LangType *MappedType
}

type MappedType added in v0.7.0

type MappedType struct {
	// NativeType is the candidate language-specific type name which is to
	// be used for the mapped entity.
	NativeType string
	// UnionTypes is a map, keyed by the generated type name, of the types
	// specified as valid for a union. The value of the map indicates the
	// order of the type, since order is important for unions in YANG.
	// Where two types are mapped to the same generated language type
	// (e.g., string) then only the order of the first is maintained. Since
	// the generated code from the structs maintains only type validation,
	// this is not currently a limitation.
	UnionTypes map[string]int
	// UnionTypeInfos stores other information about each union subtype.
	// It uses the same key as UnionTypes (NativeType of the subtype).
	UnionTypeInfos map[string]MappedUnionSubtype
	// IsEnumeratedValue specifies whether the NativeType that is returned
	// is a generated enumerated value. Such entities are reflected as
	// derived types with constant values, and are hence not represented
	// as pointers in the output code.
	IsEnumeratedValue bool
	// EnumeratedYANGTypeKey stores a globally-unique key that can be
	// used to key into IR's EnumeratedYANGTypes map containing all of the
	// enumeration definitions. This value should only be populated when
	// IsEnumeratedValue is true.
	EnumeratedYANGTypeKey string
	// ZeroValue stores the value that should be used for the type if
	// it is unset. This is used only in contexts where the nil pointer
	// cannot be used, such as leaf getters.
	ZeroValue string
	// DefaultValue stores the default value for the type if is specified.
	// It is represented as a string pointer to ensure that default values
	// of the empty string can be distinguished from unset defaults.
	DefaultValue *string
}

MappedType is used to store the generated language type that a leaf entity in YANG is mapped to. The NativeType is always populated for any leaf. UnionTypes is populated when the type may have subtypes (i.e., is a union). enumValues is populated when the type is an enumerated type.

The code generation explicitly maps YANG types to corresponding generated language types. In the case that an explicit mapping is not specified, a type will be mapped to an empty interface (interface{}). For an explicit list of types that are supported, see the yangTypeTo*Type functions in this package.

func (*MappedType) OrderedUnionTypes added in v0.8.11

func (t *MappedType) OrderedUnionTypes() []string

OrderedUnionTypes returns a slice of union type names of the given MappedType in YANG order. If the type is not a union (i.e. UnionTypes is empty), then a nil slice is returned.

type MappedUnionSubtype added in v0.21.0

type MappedUnionSubtype struct {
	// EnumeratedYANGTypeKey stores a globally-unique key that can be
	// used to key into IR's EnumeratedYANGTypes map containing all of the
	// enumeration definitions. This value should only be populated when
	// the union subtype is an enumerated type.
	EnumeratedYANGTypeKey string
}

MappedUnionSubtype stores information associated with a union subtype within a MappedType.

type NodeDetails added in v0.8.4

type NodeDetails struct {
	// Name is the language-specific name that should be used for
	// the node.
	Name string
	// YANGDetails stores details of the node from the original
	// YANG schema, such that some characteristics can be accessed
	// by the code generation process. Only details that are
	// directly required are provided.
	YANGDetails YANGNodeDetails
	// Type describes the type of node that the leaf represents,
	// allowing for container, list, leaf and leaf-list entries
	// to be distinguished.
	// In the future it can be used to store other node types that
	// form a direct child of a subtree node.
	Type NodeType
	// LangType describes the type that the node should be given in
	// the output code, using the output of the language-specific
	// type mapping provided by calling the LangMapper interface.
	LangType *MappedType
	// MappedPaths describes the paths that the output node should
	// be mapped to in the output code - these annotations can be
	// used to annotation the output code with the field(s) that it
	// corresponds to in the YANG schema.
	MappedPaths [][]string
	// MappedPathModules describes the path elements' belonging modules that
	// the output node should be mapped to in the output code - these
	// annotations can be used to annotation the output code with the
	// field(s) that it corresponds to in the YANG schema.
	MappedPathModules [][]string
	// ShadowMappedPaths describes the shadow paths (if any) that the output
	// node should be mapped to in the output code - these annotations can
	// be used to annotation the output code with the field(s) that it
	// corresponds to in the YANG schema.
	// Shadow paths are paths that have sibling config/state values
	// that have been compressed out due to path compression.
	ShadowMappedPaths [][]string
	// ShadowMappedPathModules describes the shadow path elements' belonging
	// modules (if any) that the output node should be mapped to in the
	// output code - these annotations can be used to annotation the output
	// code with the field(s) that it corresponds to in the YANG schema.
	// Shadow paths are paths that have sibling config/state values
	// that have been compressed out due to path compression.
	ShadowMappedPathModules [][]string
}

NodeDetails describes an individual field of the generated code tree. The Node may correspond to another Directory entry in the output code, or a individual leaf node.

type NodeType added in v0.8.4

type NodeType int64

NodeType describes the different types of node that can be output within the IR.

const (
	// InvalidNode represents a node that has not been correctly
	// set up.
	InvalidNode NodeType = iota
	// ContainerNode indicates a YANG 'container'.
	ContainerNode
	// ListNode indicates a YANG 'list'.
	ListNode
	// LeafNode represents a YANG 'leaf'.
	LeafNode
	// LeafListNode represents a YANG 'leaf-list'.
	LeafListNode
	// AnyDataNode represents a YANG 'anydata'.
	AnyDataNode
)

func (NodeType) String added in v0.19.0

func (n NodeType) String() string

type ParseOpts added in v0.7.0

type ParseOpts struct {
	// 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
	// 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 behaviour is useful in scenarios where there is no difference between
	// two types, and the leaf is mirrored in a logical way (e.g., the OpenConfig
	// config/state split). For example:
	//
	// grouping foo-config {
	//	leaf enabled {
	//		type enumeration {
	//			enum A;
	//			enum B;
	//			enum C;
	//		}
	//	 }
	// }
	//
	//  container config { uses foo-config; }
	//  container state { uses foo-config; }
	//
	// will result in a single enumeration type (ModuleName_Config_Enabled) being
	// output when de-duplication is enabled.
	//
	// When it is disabled, two different enumerations (ModuleName_(State|Config)_Enabled)
	// will be output in the generated code.
	SkipEnumDeduplication bool
}

ParseOpts contains parsing configuration for a given schema.

type ParsedDirectory added in v0.8.4

type ParsedDirectory struct {
	// Name is the candidate language-specific name of the directory.
	Name string
	// Type describes the type of directory that is being produced -
	// such that YANG 'list' entries can have special handling.
	Type DirType
	// Path specifies the absolute YANG schema path of the node.
	Path string
	// Fields is the set of direct children of the node that are to be
	// output. It is keyed by the YANG node identifier of the child field
	// since there could be name conflicts at this processing stage.
	Fields map[string]*NodeDetails
	// ListKeys describes the leaves of a YANG list that
	// are required in the output code (e.g., the characteristics
	// of the list's keys). It is keyed by the YANG name of the list key.
	ListKeys map[string]*ListKey
	// ListKeyYANGNames is the ordered list of YANG names specified in the
	// YANG list per Section 7.8.2 of RFC6020. The consumer of the IR can
	// rely on this ordering for deterministic ordering in output code and
	// rendering.
	ListKeyYANGNames []string
	// PackageName is the package in which this directory node's generated
	// code should reside.
	PackageName string
	// IsFakeRoot indicates whether the directory being described
	// is the root entity and has been synthetically generated by
	// ygen.
	IsFakeRoot bool
	// BelongingModule is the name of the module having the same XML
	// namespace as this directory node.
	// For more information on YANG's XML namespaces see
	// https://datatracker.ietf.org/doc/html/rfc7950#section-5.3
	BelongingModule string
	// RootElementModule is the module in which the root of the YANG tree that the
	// node is attached to was instantiated (rather than the module that
	// has the same namespace as the node).
	//
	// In this example, container 'con' has
	// RootElementModule: "openconfig-simple"
	// BelongingModule:   "openconfig-augment"
	// DefiningModule:    "openconfig-grouping"
	//
	//   module openconfig-augment {
	//     import openconfig-simple { prefix "s"; }
	//     import openconfig-grouping { prefix "g"; }
	//
	//     augment "/s:parent/child/state" {
	//       uses g:group;
	//     }
	//   }
	//
	//   module openconfig-grouping {
	//     grouping group {
	//       container con {
	//         leaf zero { type string; }
	//       }
	//     }
	//   }
	RootElementModule string
	// DefiningModule is the module that contains the text definition of
	// the field.
	DefiningModule string
	// ConfigFalse represents whether the node is state data as opposed to
	// configuration data.
	// The meaning of "config" is exactly the same as the "config"
	// statement in YANG:
	// https://datatracker.ietf.org/doc/html/rfc7950#section-7.21.1
	ConfigFalse bool
}

ParsedDirectory describes an internal node within the generated code. Such a 'directory' may represent a struct, or a message, in the generated code. It represents a YANG 'container' or 'list'.

func (*ParsedDirectory) ChildDirectories added in v0.21.0

func (d *ParsedDirectory) ChildDirectories(ir *IR) ([]*ParsedDirectory, error)

OrderedFieldNames returns the YANG name of all child directories for ParsedDirectory in lexicographical order. It returns an error if any child directory field doesn't exist in the input IR.

func (*ParsedDirectory) OrderedFieldNames added in v0.19.0

func (d *ParsedDirectory) OrderedFieldNames() []string

OrderedFieldNames returns the YANG name of all fields belonging to the ParsedDirectory in lexicographical order.

func (*ParsedDirectory) OrderedListKeyNames added in v0.21.0

func (d *ParsedDirectory) OrderedListKeyNames() []string

OrderedFieldNames returns the YANG name of all key fields belonging to the ParsedDirectory in lexicographical order.

type Proto3Package

type Proto3Package struct {
	FilePath           []string // FilePath is the path to the file that this package should be written to.
	Header             string   // Header is the header text to be used in the package.
	Messages           []string // Messages is a slice of strings containing the set of messages that are within the generated package.
	Enums              []string // Enums is a slice of string containing the generated set of enumerations within the package.
	UsesYwrapperImport bool     // UsesYwrapperImport indicates whether the ywrapper proto package is used within the generated package.
	UsesYextImport     bool     // UsesYextImport indicates whether the yext proto package is used within the generated package.
}

Proto3Package stores the code for a generated protobuf3 package.

type ProtoLangMapper added in v0.21.0

type ProtoLangMapper struct {
	// contains filtered or unexported fields
}

ProtoLangMapper contains the functionality and state for generating proto names for the generated code.

func NewProtoLangMapper added in v0.21.0

func NewProtoLangMapper(basePackageName, enumPackageName string) *ProtoLangMapper

NewProtoLangMapper creates a new ProtoLangMapper instance, initialised with the default state required for code generation.

func (*ProtoLangMapper) DirectoryName added in v0.21.0

func (s *ProtoLangMapper) DirectoryName(e *yang.Entry, cb genutil.CompressBehaviour) (string, error)

DirectoryName generates the proto message name to be used for a particular YANG schema element in the generated code. Since this conversion is lossy, a later step should resolve any naming conflicts between different fields.

func (*ProtoLangMapper) FieldName added in v0.21.0

func (s *ProtoLangMapper) FieldName(e *yang.Entry) (string, error)

FieldName maps the input entry's name to what the proto name of the field would be. Since this conversion is lossy, a later step should resolve any naming conflicts between different fields.

func (*ProtoLangMapper) KeyLeafType added in v0.21.0

func (s *ProtoLangMapper) KeyLeafType(e *yang.Entry, opts IROptions) (*MappedType, error)

LeafType maps the input list key entry to a MappedType object containing the type information about the key field.

func (*ProtoLangMapper) LeafType added in v0.21.0

func (s *ProtoLangMapper) LeafType(e *yang.Entry, opts IROptions) (*MappedType, error)

LeafType maps the input leaf entry to a MappedType object containing the type information about the field.

func (*ProtoLangMapper) PackageName added in v0.21.0

func (s *ProtoLangMapper) PackageName(e *yang.Entry, compressBehaviour genutil.CompressBehaviour, nestedMessages bool) (string, error)

PackageName determines the package that the particular output protobuf should reside in. In the case that nested messages are being output, the package name is derived based on the top-level module that the message is within.

func (*ProtoLangMapper) SetEnumSet added in v0.21.0

func (s *ProtoLangMapper) SetEnumSet(e *enumSet)

SetEnumSet is used to supply a set of enumerated values to the mapper such that leaves that have enumerated types can be looked up.

func (*ProtoLangMapper) SetSchemaTree added in v0.21.0

func (s *ProtoLangMapper) SetSchemaTree(st *schemaTree)

SetSchemaTree is used to supply a copy of the YANG schema tree to the mapped such that leaves of type leafref can be resolved to their target leaves.

type ProtoOpts

type ProtoOpts struct {
	// BaseImportPath stores the root URL or path for imports that are
	// relative within the imported protobufs.
	BaseImportPath string
	// EnumPackageName stores the package name that should be used
	// for the package that defines enumerated types that are used
	// in multiple parts of the schema (identityrefs, and enumerations)
	// that fall within type definitions.
	EnumPackageName string
	// YwrapperPath is the path to the ywrapper.proto file that stores
	// the definition of the wrapper messages used to ensure that unset
	// fields can be distinguished from those that are set to their
	// default value. The path excluds the filename.
	YwrapperPath string
	// YextPath is the path to the yext.proto file that stores the
	// definition of the extension messages that are used to annotat the
	// generated protobuf messages.
	YextPath string
	// AnnotateSchemaPaths specifies whether the extensions defined in
	// yext.proto should be used to annotate schema paths into the output
	// protobuf file. See
	// https://github.com/openconfig/ygot/blob/master/docs/yang-to-protobuf-transformations-spec.md#annotation-of-schema-paths
	AnnotateSchemaPaths bool
	// AnnotateEnumNames specifies whether the extensions defined in
	// yext.proto should be used to annotate enum values with their
	// original YANG names in the output protobuf file.
	// See https://github.com/openconfig/ygot/blob/master/docs/yang-to-protobuf-transformations-spec.md#annotation-of-enums
	AnnotateEnumNames bool
	// NestedMessages indicates whether nested messages should be
	// output for the protobuf schema. If false, a separate package
	// is generated per package.
	NestedMessages bool
	// GoPackageBase specifies the base of the names that are used in
	// the go_package file option for generated protobufs. Additional
	// package identifiers are appended to the go_package - such that
	// the format <base>/<path>/<to>/<package> is used.
	GoPackageBase string
}

ProtoOpts stores Protobuf specific options for the code generation library.

type TransformationOpts added in v0.7.0

type TransformationOpts struct {
	// CompressBehaviour specifies how the set of direct children of any
	// entry should determined. Specifically, whether compression is
	// enabled, and whether state fields in the schema should be excluded.
	CompressBehaviour genutil.CompressBehaviour
	// IgnoreShadowSchemaPaths indicates whether when OpenConfig path
	// compression is enabled, that the shadowed paths are to be ignored
	// while while unmarshalling.
	IgnoreShadowSchemaPaths bool
	// GenerateFakeRoot specifies whether an entity that represents the
	// root of the YANG schema tree should be generated in the generated
	// code.
	GenerateFakeRoot bool
	// FakeRootName specifies the name of the struct that should be generated
	// representing the root.
	FakeRootName string
	// ExcludeState specifies whether config false values should be
	// included in the generated code output. When set, all values that are
	// not writeable (i.e., config false) within the YANG schema and their
	// children are excluded from the generated code.
	ExcludeState bool
	// 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.
	UseDefiningModuleForTypedefEnumNames bool
	// EnumerationsUseUnderscores specifies whether enumeration names
	// should use underscores between path segments.
	EnumerationsUseUnderscores bool
}

TransformationOpts specifies transformations to the generated code with respect to the input schema.

type YANGCodeGenerator

type YANGCodeGenerator struct {
	// Config stores the configuration parameters used for code generation.
	Config GeneratorConfig
}

YANGCodeGenerator is a structure that is used to pass arguments as to how the output Go code should be generated.

func NewYANGCodeGenerator

func NewYANGCodeGenerator(c *GeneratorConfig) *YANGCodeGenerator

NewYANGCodeGenerator returns a new instance of the YANGCodeGenerator struct to the calling function.

func (*YANGCodeGenerator) GenerateGoCode

func (cg *YANGCodeGenerator) GenerateGoCode(yangFiles, includePaths []string) (*GeneratedGoCode, util.Errors)

GenerateGoCode 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 GeneratedGoCode struct which contains:

  1. A struct definition for each container or list that is within the specified set of models.
  2. Enumerated values which correspond to the set of enumerated entities (leaves of type enumeration, identities, typedefs that reference an enumeration) within the specified models.

If errors are encountered during code generation, an error is returned.

func (*YANGCodeGenerator) GenerateProto3

func (cg *YANGCodeGenerator) GenerateProto3(yangFiles, includePaths []string) (*GeneratedProto3, util.Errors)

GenerateProto3 generates Protobuf 3 code for the input set of YANG files. The YANG schemas for which protobufs are to be created is supplied as the yangFiles argument, with included modules being searched for in includePaths. It returns a GeneratedProto3 struct containing the messages that are to be output, along with any associated values (e.g., enumerations).

type YANGNodeDetails added in v0.8.4

type YANGNodeDetails struct {
	// Name is the name of the node from the YANG schema.
	Name string
	// Defaults represents the 'default' value(s) directly
	// specified in the YANG schema.
	Defaults []string
	// BelongingModule is the name of the module having the same XML
	// namespace as this node.
	// For more information on YANG's XML namespaces see
	// https://datatracker.ietf.org/doc/html/rfc7950#section-5.3
	BelongingModule string
	// RootElementModule is the module in which the root of the YANG tree that the
	// node is attached to was instantiated (rather than the module that
	// has the same namespace as the node).
	//
	// In this example, leaf 'zero' has
	// RootElementModule: "openconfig-simple"
	// BelongingModule:   "openconfig-augment"
	// DefiningModule:    "openconfig-grouping"
	//
	//   module openconfig-augment {
	//     import openconfig-simple { prefix "s"; }
	//     import openconfig-grouping { prefix "g"; }
	//
	//     augment "/s:parent/child/state" {
	//       uses g:group;
	//     }
	//   }
	//
	//   module openconfig-grouping {
	//     grouping group {
	//       leaf zero { type string; }
	//     }
	//   }
	RootElementModule string
	// DefiningModule is the module that contains the text definition of
	// the field.
	DefiningModule string
	// Path specifies the absolute YANG schema node path that can be used
	// to index into the ParsedDirectory map in the IR. It includes the
	// module name as well as choice/case elements.
	Path string
	// SchemaPath specifies the absolute YANG schema node path. It does not
	// include the module name nor choice/case elements in the YANG file.
	SchemaPath string
	// ShadowSchemaPath, which specifies the absolute YANG schema node path
	// of the "shadowed" sibling node, is included when a leaf exists in
	// both 'intended' and 'applied' state of an OpenConfig schema (see
	// https://datatracker.ietf.org/doc/html/draft-openconfig-netmod-opstate-01)
	// and hence is within the 'config' and 'state' containers of the
	// schema. ShadowSchemaPath is populated only when the -compress
	// generator flag is used, and indicates the path of the node not
	// represented in the generated IR based on the preference to prefer
	// intended or applied leaves.
	// Similar to SchemaPath, it does not include the module name nor
	// choice/case elements.
	ShadowSchemaPath string
	// LeafrefTargetPath is the absolute YANG schema node path of the
	// target node to which the leafref points via its path statement. Note
	// that this is *not* the recursively-resolved path.
	// This is populated only if the YANG node was a leafref.
	LeafrefTargetPath string
	// PresenceStatement, if non-nil, indicates that this directory is a
	// presence container. It contains the value of the presence statement.
	PresenceStatement *string
	// Description contains the description of the node.
	Description string
	// Type is the YANG type which represents the node. It is only
	// applicable for leaf or leaf-list nodes because only these nodes can
	// have type statements.
	// TODO(wenbli): This needs to be replaced using a plugin mechanism.
	Type *YANGType
}

YANGNodeDetails stores the YANG-specific details of a node within the schema. TODO(wenbli): Split this out so that parts can be re-used by ParsedDirectory.

type YANGType added in v0.19.0

type YANGType struct {
	// Name is the YANG type name of the type.
	Name string
}

YANGType represents a YANG type.

type YangListAttr added in v0.7.0

type YangListAttr struct {
	// keys is a map, keyed by the name of the key leaf, with values of the type
	// of the key of a YANG list.
	Keys map[string]*ListKey
	// keyElems is a slice containing the pointers to yang.Entry structs that
	// make up the list key.
	KeyElems []*yang.Entry
	// ListKeyYANGNames is the ordered list of YANG names specified in the
	// YANG list per Section 7.8.2 of RFC6020. ygot relies on this ordering
	// for deterministic ordering in output code and rendering.
	ListKeyYANGNames []string
}

YangListAttr is used to store the additional elements for a Go struct that are required if the struct represents a YANG list. It stores the name of the key elements, and their associated types, along with pointers to those elements.

Directories

Path Synopsis
Package schematest is used for testing with the default OpenConfig generated structs.
Package schematest is used for testing with the default OpenConfig generated structs.

Jump to

Keyboard shortcuts

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