2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
10 import static com.google.common.base.Preconditions.*;
11 import static extension org.opendaylight.yangtools.binding.generator.util.Types.*;
12 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.*;
13 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.*;
14 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.*;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
22 import java.util.Iterator
23 import java.util.Collection
24 import org.opendaylight.yangtools.binding.generator.util.BindingTypes;
25 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
26 import org.opendaylight.yangtools.binding.generator.util.Types;
27 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
28 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
29 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
30 import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
31 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
32 import org.opendaylight.yangtools.sal.binding.model.api.Type;
33 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
34 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
35 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
36 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
37 import org.opendaylight.yangtools.sal.binding.yang.types.GroupingDefinitionDependencySort;
38 import org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl;
39 import org.opendaylight.yangtools.yang.binding.RpcService;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
42 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
43 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
44 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
46 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
48 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.Module;
53 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
56 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
57 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
58 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
60 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
61 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
62 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
63 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
64 import org.opendaylight.yangtools.yang.model.util.UnionType;
65 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort
66 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
67 import org.opendaylight.yangtools.yang.model.api.UsesNode
68 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext
69 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder
70 import org.opendaylight.yangtools.yang.model.api.ModuleImport
71 import org.opendaylight.yangtools.yang.binding.DataContainer
72 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget
73 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
74 import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil
75 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
76 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder
77 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl
78 import org.opendaylight.yangtools.yang.common.QName
79 import org.opendaylight.yangtools.yang.binding.BindingMapping
80 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase
82 import com.google.common.collect.Sets
86 public class BindingGeneratorImpl implements BindingGenerator {
88 private final Map<Module, ModuleContext> genCtx = new HashMap()
91 * Outer key represents the package name. Outer value represents map of
92 * all builders in the same package. Inner key represents the schema node
93 * name (in JAVA class/interface name format). Inner value represents
94 * instance of builder for schema node specified in key part.
96 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders;
99 * Provide methods for converting YANG types to JAVA types.
101 private var TypeProvider typeProvider;
104 * Holds reference to schema context to resolve data of augmented element
105 * when creating augmentation builder
107 private var SchemaContext schemaContext;
110 * Constant with the concrete name of namespace.
112 private val static String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
115 * Constant with the concrete name of identifier.
117 private val static String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
120 * Resolves generated types from <code>context</code> schema nodes of all
123 * Generated types are created for modules, groupings, types, containers,
124 * lists, choices, augments, rpcs, notification, identities.
127 * schema context which contains data about all schema nodes
129 * @return list of types (usually <code>GeneratedType</code>
130 * <code>GeneratedTransferObject</code>which are generated from
131 * <code>context</code> data.
132 * @throws IllegalArgumentException
133 * if param <code>context</code> is null
134 * @throws IllegalStateException
135 * if <code>context</code> contain no modules
137 override generateTypes(SchemaContext context) {
138 checkArgument(context !== null, "Schema Context reference cannot be NULL.");
139 checkState(context.modules !== null, "Schema Context does not contain defined modules.");
140 schemaContext = context;
141 typeProvider = new TypeProviderImpl(context);
142 val Set<Module> modules = context.modules;
143 return generateTypes(context, modules);
147 * Resolves generated types from <code>context</code> schema nodes only for
148 * modules specified in <code>modules</code>
150 * Generated types are created for modules, groupings, types, containers,
151 * lists, choices, augments, rpcs, notification, identities.
154 * schema context which contains data about all schema nodes
157 * set of modules for which schema nodes should be generated
159 * @return list of types (usually <code>GeneratedType</code> or
160 * <code>GeneratedTransferObject</code>) which:
162 * <li>are generated from <code>context</code> schema nodes and</li>
163 * <li>are also part of some of the module in <code>modules</code>
166 * @throws IllegalArgumentException
168 * <li>if param <code>context</code> is null or</li>
169 * <li>if param <code>modules</code> is null</li>
171 * @throws IllegalStateException
172 * if <code>context</code> contain no modules
174 override generateTypes(SchemaContext context, Set<Module> modules) {
175 checkArgument(context !== null, "Schema Context reference cannot be NULL.");
176 checkState(context.modules !== null, "Schema Context does not contain defined modules.");
177 checkArgument(modules !== null, "Set of Modules cannot be NULL.");
179 schemaContext = context;
180 typeProvider = new TypeProviderImpl(context);
181 val contextModules = ModuleDependencySort.sort(context.modules);
182 genTypeBuilders = new HashMap();
184 for (contextModule : contextModules) {
185 moduleToGenTypes(contextModule, context);
187 for (contextModule : contextModules) {
188 allAugmentsToGenTypes(contextModule);
191 val List<Type> filteredGenTypes = new ArrayList();
192 for (Module m : modules) {
193 val ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m)
194 filteredGenTypes.addAll(ctx.generatedTypes);
195 val Set<Type> additionalTypes = (typeProvider as TypeProviderImpl).additionalTypes.get(m)
196 if (additionalTypes != null) {
197 filteredGenTypes.addAll(additionalTypes)
201 return filteredGenTypes;
204 private def void moduleToGenTypes(Module m, SchemaContext context) {
205 genCtx.put(m, new ModuleContext)
206 allTypeDefinitionsToGenTypes(m)
207 groupingsToGenTypes(m, m.groupings)
208 rpcMethodsToGenType(m)
209 allIdentitiesToGenTypes(m, context)
210 notificationsToGenType(m)
212 if (!m.childNodes.isEmpty()) {
213 val moduleType = moduleToDataType(m)
214 genCtx.get(m).addModuleNode(moduleType)
215 val basePackageName = moduleNamespaceToPackageName(m);
216 resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.childNodes)
221 * Converts all extended type definitions of module to the list of
222 * <code>Type</code> objects.
225 * module from which is obtained set of type definitions
226 * @throws IllegalArgumentException
228 * <li>if module equals null</li>
229 * <li>if name of module equals null</li>
230 * <li>if type definitions of module equal null</li>
234 private def void allTypeDefinitionsToGenTypes(Module module) {
235 checkArgument(module !== null, "Module reference cannot be NULL.");
236 checkArgument(module.name !== null, "Module name cannot be NULL.");
237 val it = new DataNodeIterator(module);
238 val List<TypeDefinition<?>> typeDefinitions = it.allTypedefs;
239 checkState(typeDefinitions !== null, '''Type Definitions for module «module.name» cannot be NULL.''');
241 for (TypeDefinition<?> typedef : typeDefinitions) {
242 if (typedef !== null) {
243 val type = (typeProvider as TypeProviderImpl).generatedTypeForExtendedDefinitionType(typedef, typedef);
245 genCtx.get(module).addTypedefType(typedef.path, type)
251 private def GeneratedTypeBuilder processDataSchemaNode(Module module, String basePackageName,
252 GeneratedTypeBuilder parent, GeneratedTypeBuilder childOf, DataSchemaNode node) {
253 if (node.augmenting || node.addedByUses) {
256 val packageName = packageNameForGeneratedType(basePackageName, node.path)
257 val genType = addDefaultInterfaceDefinition(packageName, node, childOf)
258 if (node instanceof DataNodeContainer) {
259 genCtx.get(module).addChildNodeType(node.path, genType)
260 groupingsToGenTypes(module, (node as DataNodeContainer).groupings)
261 processUsesAugments(node as DataNodeContainer, module)
266 private def void containerToGenType(Module module, String basePackageName, GeneratedTypeBuilder parent,
267 GeneratedTypeBuilder childOf, ContainerSchemaNode node) {
268 val genType = processDataSchemaNode(module, basePackageName, parent, childOf, node)
269 if (genType != null) {
270 constructGetter(parent, node.QName.localName, node.description, genType)
271 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.childNodes)
275 private def void listToGenType(Module module, String basePackageName, GeneratedTypeBuilder parent,
276 GeneratedTypeBuilder childOf, ListSchemaNode node) {
277 val genType = processDataSchemaNode(module, basePackageName, parent, childOf, node)
278 if (genType != null) {
279 constructGetter(parent, node.QName.localName, node.description, Types.listTypeFor(genType))
281 val List<String> listKeys = listKeys(node);
282 val packageName = packageNameForGeneratedType(basePackageName, (node).path)
283 val genTOBuilder = resolveListKeyTOBuilder(packageName, node);
284 if (genTOBuilder !== null) {
285 val identifierMarker = IDENTIFIER.parameterizedTypeFor(genType);
286 val identifiableMarker = IDENTIFIABLE.parameterizedTypeFor(genTOBuilder);
287 genTOBuilder.addImplementsType(identifierMarker);
288 genType.addImplementsType(identifiableMarker);
291 for (schemaNode : node.childNodes) {
292 if (!schemaNode.augmenting) {
293 addSchemaNodeToListBuilders(basePackageName, schemaNode, genType, genTOBuilder, listKeys, module);
298 if (genTOBuilder !== null) {
299 val GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
300 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder as GeneratedTOBuilderImpl)));
301 genTOBuilder.setSUID(prop);
304 typeBuildersToGenTypes(module, genType, genTOBuilder);
308 private def void processUsesAugments(DataNodeContainer node, Module module) {
309 val basePackageName = moduleNamespaceToPackageName(module);
310 for (usesNode : node.uses) {
311 for (augment : usesNode.augmentations) {
312 usesAugmentationToGenTypes(basePackageName, augment, module, usesNode, node);
313 processUsesAugments(augment, module);
319 * Converts all <b>augmentation</b> of the module to the list
320 * <code>Type</code> objects.
323 * module from which is obtained list of all augmentation objects
324 * to iterate over them
325 * @throws IllegalArgumentException
327 * <li>if the module equals null</li>
328 * <li>if the name of module equals null</li>
329 * <li>if the set of child nodes equals null</li>
333 private def void allAugmentsToGenTypes(Module module) {
334 checkArgument(module !== null, "Module reference cannot be NULL.");
335 checkArgument(module.name !== null, "Module name cannot be NULL.");
336 if (module.childNodes === null) {
337 throw new IllegalArgumentException(
338 "Reference to Set of Augmentation Definitions in module " + module.name + " cannot be NULL.");
341 val basePackageName = moduleNamespaceToPackageName(module);
342 val List<AugmentationSchema> augmentations = resolveAugmentations(module);
343 for (augment : augmentations) {
344 augmentationToGenTypes(basePackageName, augment, module);
349 * Returns list of <code>AugmentationSchema</code> objects. The objects are
350 * sorted according to the length of their target path from the shortest to
354 * module from which is obtained list of all augmentation objects
355 * @return list of sorted <code>AugmentationSchema</code> objects obtained
356 * from <code>module</code>
357 * @throws IllegalArgumentException
359 * <li>if the module equals null</li>
360 * <li>if the set of augmentation equals null</li>
364 private def List<AugmentationSchema> resolveAugmentations(Module module) {
365 checkArgument(module !== null, "Module reference cannot be NULL.");
366 checkState(module.augmentations !== null, "Augmentations Set cannot be NULL.");
368 val Set<AugmentationSchema> augmentations = module.augmentations;
369 val List<AugmentationSchema> sortedAugmentations = new ArrayList(augmentations);
370 Collections.sort(sortedAugmentations,
371 [ augSchema1, augSchema2 |
372 if (augSchema1.targetPath.path.size() > augSchema2.targetPath.path.size()) {
374 } else if (augSchema1.targetPath.path.size() < augSchema2.targetPath.path.size()) {
379 return sortedAugmentations;
383 * Converts whole <b>module</b> to <code>GeneratedType</code> object.
384 * Firstly is created the module builder object from which is vally
385 * obtained reference to <code>GeneratedType</code> object.
388 * module from which are obtained the module name, child nodes,
389 * uses and is derived package name
390 * @return <code>GeneratedType</code> which is internal representation of
392 * @throws IllegalArgumentException
393 * if the module equals null
396 private def GeneratedTypeBuilder moduleToDataType(Module module) {
397 checkArgument(module !== null, "Module reference cannot be NULL.");
399 val moduleDataTypeBuilder = moduleTypeBuilder(module, "Data");
400 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder);
401 moduleDataTypeBuilder.addImplementsType(DATA_ROOT);
402 return moduleDataTypeBuilder;
406 * Converts all <b>rpcs</b> inputs and outputs substatements of the module
407 * to the list of <code>Type</code> objects. In addition are to containers
408 * and lists which belong to input or output also part of returning list.
411 * module from which is obtained set of all rpc objects to
413 * @throws IllegalArgumentException
415 * <li>if the module equals null</li>
416 * <li>if the name of module equals null</li>
417 * <li>if the set of child nodes equals null</li>
421 private def void rpcMethodsToGenType(Module module) {
422 checkArgument(module !== null, "Module reference cannot be NULL.");
423 checkArgument(module.name !== null, "Module name cannot be NULL.");
424 checkArgument(module.childNodes !== null,
425 "Reference to Set of RPC Method Definitions in module " + module.name + " cannot be NULL.");
427 val basePackageName = moduleNamespaceToPackageName(module);
428 val Set<RpcDefinition> rpcDefinitions = module.rpcs;
429 if (rpcDefinitions.isEmpty()) {
433 val interfaceBuilder = moduleTypeBuilder(module, "Service");
434 interfaceBuilder.addImplementsType(Types.typeForClass(RpcService));
435 for (rpc : rpcDefinitions) {
437 val rpcName = BindingMapping.getClassName(rpc.QName);
438 val rpcMethodName = parseToValidParamName(rpcName);
439 val method = interfaceBuilder.addMethod(rpcMethodName);
440 val input = rpc.input;
441 val output = rpc.output;
443 if (input !== null) {
444 val inType = addRawInterfaceDefinition(basePackageName, input, rpcName);
445 addImplementedInterfaceFromUses(input, inType);
446 inType.addImplementsType(DATA_OBJECT);
447 inType.addImplementsType(augmentable(inType));
448 resolveDataSchemaNodes(module, basePackageName, inType, inType, input.childNodes);
449 genCtx.get(module).addChildNodeType(input.path, inType)
450 val inTypeInstance = inType.toInstance();
451 method.addParameter(inTypeInstance, "input");
454 var Type outTypeInstance = VOID;
455 if (output !== null) {
456 val outType = addRawInterfaceDefinition(basePackageName, output, rpcName);
457 addImplementedInterfaceFromUses(output, outType);
458 outType.addImplementsType(DATA_OBJECT);
459 outType.addImplementsType(augmentable(outType));
460 resolveDataSchemaNodes(module, basePackageName, outType, outType, output.childNodes);
461 genCtx.get(module).addChildNodeType(output.path, outType)
462 outTypeInstance = outType.toInstance();
465 val rpcRes = Types.parameterizedTypeFor(Types.typeForClass(RpcResult), outTypeInstance);
466 method.setReturnType(Types.parameterizedTypeFor(FUTURE, rpcRes));
470 genCtx.get(module).addTopLevelNodeType(interfaceBuilder)
474 * Converts all <b>notifications</b> of the module to the list of
475 * <code>Type</code> objects. In addition are to this list added containers
476 * and lists which are part of this notification.
479 * module from which is obtained set of all notification objects
480 * to iterate over them
481 * @throws IllegalArgumentException
483 * <li>if the module equals null</li>
484 * <li>if the name of module equals null</li>
485 * <li>if the set of child nodes equals null</li>
489 private def void notificationsToGenType(Module module) {
490 checkArgument(module !== null, "Module reference cannot be NULL.");
491 checkArgument(module.name !== null, "Module name cannot be NULL.");
493 if (module.childNodes === null) {
494 throw new IllegalArgumentException(
495 "Reference to Set of Notification Definitions in module " + module.name + " cannot be NULL.");
497 val notifications = module.notifications;
498 if(notifications.empty) return;
500 val listenerInterface = moduleTypeBuilder(module, "Listener");
501 listenerInterface.addImplementsType(BindingTypes.NOTIFICATION_LISTENER);
502 val basePackageName = moduleNamespaceToPackageName(module);
504 for (notification : notifications) {
505 if (notification !== null) {
506 processUsesAugments(notification, module);
508 val notificationInterface = addDefaultInterfaceDefinition(basePackageName, notification,
509 BindingTypes.DATA_OBJECT);
510 notificationInterface.addImplementsType(NOTIFICATION);
511 genCtx.get(module).addChildNodeType(notification.path, notificationInterface)
513 // Notification object
514 resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
515 notification.childNodes);
517 listenerInterface.addMethod("on" + notificationInterface.name) //
518 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification").
519 setReturnType(Types.VOID);
523 genCtx.get(module).addTopLevelNodeType(listenerInterface)
527 * Converts all <b>identities</b> of the module to the list of
528 * <code>Type</code> objects.
531 * module from which is obtained set of all identity objects to
534 * schema context only used as input parameter for method
535 * {@link identityToGenType}
538 private def void allIdentitiesToGenTypes(Module module, SchemaContext context) {
539 val Set<IdentitySchemaNode> schemaIdentities = module.identities;
540 val basePackageName = moduleNamespaceToPackageName(module);
542 if (schemaIdentities !== null && !schemaIdentities.isEmpty()) {
543 for (identity : schemaIdentities) {
544 identityToGenType(module, basePackageName, identity, context);
550 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
551 * created transport object builder. If identity contains base identity then
552 * reference to base identity is added to superior identity as its extend.
553 * If identity doesn't contain base identity then only reference to abstract
554 * class {@link org.opendaylight.yangtools.yang.model.api.BaseIdentity
555 * BaseIdentity} is added
557 * @param module current module
558 * @param basePackageName
559 * string contains the module package name
561 * IdentitySchemaNode which contains data about identity
563 * SchemaContext which is used to get package and name
564 * information about base of identity
567 private def void identityToGenType(Module module, String basePackageName, IdentitySchemaNode identity,
568 SchemaContext context) {
569 if (identity === null) {
572 val packageName = packageNameForGeneratedType(basePackageName, identity.path);
573 val genTypeName = BindingMapping.getClassName(identity.QName);
574 val newType = new GeneratedTOBuilderImpl(packageName, genTypeName);
575 val baseIdentity = identity.baseIdentity;
576 if (baseIdentity === null) {
577 newType.setExtendsType(Types.baseIdentityTO);
579 val baseIdentityParentModule = SchemaContextUtil.findParentModule(context, baseIdentity);
580 val returnTypePkgName = moduleNamespaceToPackageName(baseIdentityParentModule);
581 val returnTypeName = BindingMapping.getClassName(baseIdentity.QName);
582 val gto = new GeneratedTOBuilderImpl(returnTypePkgName, returnTypeName).toInstance();
583 newType.setExtendsType(gto);
585 newType.setAbstract(true);
586 val qname = identity.QName;
588 newType.qnameConstant(BindingMapping.QNAME_STATIC_FIELD_NAME,qname);
590 genCtx.get(module).addIdentityType(identity.QName,newType)
593 private static def qnameConstant(GeneratedTypeBuilderBase<?> toBuilder, String constantName, QName name) {
594 toBuilder.addConstant(QName.typeForClass,constantName,'''
595 org.opendaylight.yangtools.yang.common.QName.create("«name.namespace»","«name.formattedRevision»","«name.localName»")
600 * Converts all <b>groupings</b> of the module to the list of
601 * <code>Type</code> objects. Firstly are groupings sorted according mutual
602 * dependencies. At least dependent (independent) groupings are in the list
603 * saved at first positions. For every grouping the record is added to map
604 * {@link BindingGeneratorImpl#allGroupings allGroupings}
608 * @param collection of groupings from which types will be generated
611 private def void groupingsToGenTypes(Module module, Collection<GroupingDefinition> groupings) {
612 val basePackageName = moduleNamespaceToPackageName(module);
613 val List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort().sort(
615 for (grouping : groupingsSortedByDependencies) {
616 groupingToGenType(basePackageName, grouping, module);
621 * Converts individual grouping to GeneratedType. Firstly generated type
622 * builder is created and every child node of grouping is resolved to the
625 * @param basePackageName
626 * string contains the module package name
628 * GroupingDefinition which contains data about grouping
629 * @param module current module
630 * @return GeneratedType which is generated from grouping (object of type
631 * <code>GroupingDefinition</code>)
633 private def void groupingToGenType(String basePackageName, GroupingDefinition grouping, Module module) {
634 val packageName = packageNameForGeneratedType(basePackageName, grouping.path);
635 val genType = addDefaultInterfaceDefinition(packageName, grouping);
636 genCtx.get(module).addGroupingType(grouping.path, genType)
637 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.childNodes);
638 groupingsToGenTypes(module, grouping.groupings);
639 processUsesAugments(grouping, module);
643 * Tries to find EnumTypeDefinition in <code>typeDefinition</code>. If base
644 * type of <code>typeDefinition</code> is of the type ExtendedType then this
645 * method is recursively called with this base type.
647 * @param typeDefinition
648 * TypeDefinition in which should be EnumTypeDefinition found as
650 * @return EnumTypeDefinition if it is found inside
651 * <code>typeDefinition</code> or <code>null</code> in other case
653 private def EnumTypeDefinition enumTypeDefFromExtendedType(TypeDefinition<?> typeDefinition) {
654 if (typeDefinition !== null) {
655 if (typeDefinition.baseType instanceof EnumTypeDefinition) {
656 return typeDefinition.baseType as EnumTypeDefinition;
657 } else if (typeDefinition.baseType instanceof ExtendedType) {
658 return enumTypeDefFromExtendedType(typeDefinition.baseType);
665 * Adds enumeration builder created from <code>enumTypeDef</code> to
666 * <code>typeBuilder</code>.
668 * Each <code>enumTypeDef</code> item is added to builder with its name and
672 * EnumTypeDefinition contains enum data
674 * string contains name which will be assigned to enumeration
677 * GeneratedTypeBuilder to which will be enum builder assigned
678 * @return enumeration builder which contains data from
679 * <code>enumTypeDef</code>
681 private def EnumBuilder resolveInnerEnumFromTypeDefinition(EnumTypeDefinition enumTypeDef, QName enumName,
682 GeneratedTypeBuilder typeBuilder) {
683 if ((enumTypeDef !== null) && (typeBuilder !== null) && (enumTypeDef.QName !== null) &&
684 (enumTypeDef.QName.localName !== null)) {
685 val enumerationName = BindingMapping.getClassName(enumName);
686 val enumBuilder = typeBuilder.addEnumeration(enumerationName);
687 enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
694 * Generates type builder for <code>module</code>.
697 * Module which is source of package name for generated type
700 * string which is added to the module class name representation
702 * @return instance of GeneratedTypeBuilder which represents
703 * <code>module</code>.
704 * @throws IllegalArgumentException
705 * if <code>module</code> equals null
707 private def GeneratedTypeBuilder moduleTypeBuilder(Module module, String postfix) {
708 checkArgument(module !== null, "Module reference cannot be NULL.");
709 val packageName = moduleNamespaceToPackageName(module);
710 val moduleName = BindingMapping.getClassName(module.name) + postfix;
711 return new GeneratedTypeBuilderImpl(packageName, moduleName);
715 * Converts <code>augSchema</code> to list of <code>Type</code> which
716 * contains generated type for augmentation. In addition there are also
717 * generated types for all containers, list and choices which are child of
718 * <code>augSchema</code> node or a generated types for cases are added if
719 * augmented node is choice.
721 * @param augmentPackageName
722 * string with the name of the package to which the augmentation
725 * AugmentationSchema which is contains data about augmentation
726 * (target path, childs...)
727 * @param module current module
728 * @param parentUsesNode parent uses node of this augment (can be null if this augment is not defined under uses statement)
729 * @throws IllegalArgumentException
731 * <li>if <code>augmentPackageName</code> equals null</li>
732 * <li>if <code>augSchema</code> equals null</li>
733 * <li>if target path of <code>augSchema</code> equals null</li>
736 private def void augmentationToGenTypes(String augmentPackageName, AugmentationSchema augSchema, Module module) {
737 checkArgument(augmentPackageName !== null, "Package Name cannot be NULL.");
738 checkArgument(augSchema !== null, "Augmentation Schema cannot be NULL.");
739 checkState(augSchema.targetPath !== null,
740 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
742 processUsesAugments(augSchema, module);
743 val targetPath = augSchema.targetPath;
744 var SchemaNode targetSchemaNode = null
746 targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
747 if (targetSchemaNode instanceof DataSchemaNode && (targetSchemaNode as DataSchemaNode).isAddedByUses()) {
748 targetSchemaNode = findOriginal(targetSchemaNode as DataSchemaNode);
749 if (targetSchemaNode == null) {
750 throw new NullPointerException(
751 "Failed to find target node from grouping in augmentation " + augSchema + " in module " +
755 if (targetSchemaNode == null) {
756 throw new IllegalArgumentException("augment target not found: " + targetPath)
759 var targetTypeBuilder = findChildNodeByPath(targetSchemaNode.path)
760 if (targetTypeBuilder === null) {
761 targetTypeBuilder = findCaseByPath(targetSchemaNode.path)
763 if (targetTypeBuilder === null) {
764 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
767 if (!(targetSchemaNode instanceof ChoiceNode)) {
768 var packageName = augmentPackageName;
769 val augTypeBuilder = addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName,
770 targetTypeBuilder.toInstance, augSchema);
771 genCtx.get(module).addAugmentType(augTypeBuilder)
772 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
774 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance,
775 targetSchemaNode as ChoiceNode, augSchema.childNodes);
779 private def void usesAugmentationToGenTypes(String augmentPackageName, AugmentationSchema augSchema, Module module,
780 UsesNode usesNode, DataNodeContainer usesNodeParent) {
781 checkArgument(augmentPackageName !== null, "Package Name cannot be NULL.");
782 checkArgument(augSchema !== null, "Augmentation Schema cannot be NULL.");
783 checkState(augSchema.targetPath !== null,
784 "Augmentation Schema does not contain Target Path (Target Path is NULL).");
786 processUsesAugments(augSchema, module);
787 val targetPath = augSchema.targetPath;
788 var SchemaNode targetSchemaNode = null
789 targetSchemaNode = findOriginalTargetFromGrouping(targetPath, usesNode);
790 if (targetSchemaNode == null) {
791 throw new IllegalArgumentException("augment target not found: " + targetPath)
794 var targetTypeBuilder = findChildNodeByPath(targetSchemaNode.path)
795 if (targetTypeBuilder === null) {
796 targetTypeBuilder = findCaseByPath(targetSchemaNode.path)
798 if (targetTypeBuilder === null) {
799 throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
802 if (!(targetSchemaNode instanceof ChoiceNode)) {
803 var packageName = augmentPackageName;
804 if (usesNodeParent instanceof SchemaNode) {
805 packageName = packageNameForGeneratedType(augmentPackageName, (usesNodeParent as SchemaNode).path, true)
807 val augTypeBuilder = addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName,
808 targetTypeBuilder.toInstance, augSchema);
809 genCtx.get(module).addAugmentType(augTypeBuilder)
810 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
812 generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance,
813 targetSchemaNode as ChoiceNode, augSchema.childNodes);
818 * Utility method which search for original node defined in grouping.
820 private def DataSchemaNode findOriginal(DataSchemaNode node) {
821 var DataSchemaNode result = findCorrectTargetFromGrouping(node);
822 if (result == null) {
823 result = findCorrectTargetFromAugment(node);
824 if (result != null) {
825 if (result.addedByUses) {
826 result = findOriginal(result);
833 private def DataSchemaNode findCorrectTargetFromAugment(DataSchemaNode node) {
834 if (!node.augmenting) {
838 var QName currentName = node.QName
839 var Object currentNode = node
840 var Object parent = node;
841 val tmpPath = new ArrayList<QName>()
842 val tmpTree = new ArrayList<SchemaNode>()
844 var AugmentationSchema augment = null;
846 val SchemaPath sp = (parent as SchemaNode).path
847 val List<QName> names = sp.path
848 val List<QName> newNames = new ArrayList(names)
849 newNames.remove(newNames.size - 1)
850 val SchemaPath newSp = new SchemaPath(newNames, sp.absolute)
851 parent = findDataSchemaNode(schemaContext, newSp)
852 if (parent instanceof AugmentationTarget) {
853 tmpPath.add(currentName);
854 tmpTree.add(currentNode as SchemaNode)
855 augment = findNodeInAugment((parent as AugmentationTarget).availableAugmentations, currentName);
856 if (augment == null) {
857 currentName = (parent as DataSchemaNode).QName
861 } while ((parent as DataSchemaNode).augmenting && augment == null);
863 if (augment == null) {
866 Collections.reverse(tmpPath);
867 Collections.reverse(tmpTree);
868 var Object actualParent = augment;
869 var DataSchemaNode result = null;
870 for (name : tmpPath) {
871 if (actualParent instanceof DataNodeContainer) {
872 result = (actualParent as DataNodeContainer).getDataChildByName(name.localName);
873 actualParent = (actualParent as DataNodeContainer).getDataChildByName(name.localName);
875 if (actualParent instanceof ChoiceNode) {
876 result = (actualParent as ChoiceNode).getCaseNodeByName(name.localName);
877 actualParent = (actualParent as ChoiceNode).getCaseNodeByName(name.localName);
882 if (result.addedByUses) {
883 result = findCorrectTargetFromAugmentGrouping(result, augment, tmpTree);
890 private def AugmentationSchema findNodeInAugment(Collection<AugmentationSchema> augments, QName name) {
891 for (augment : augments) {
892 val DataSchemaNode node = augment.getDataChildByName(name);
900 private def DataSchemaNode findCorrectTargetFromGrouping(DataSchemaNode node) {
901 if (node.path.path.size == 1) {
902 // uses is under module statement
903 val Module m = findParentModule(schemaContext, node);
904 var DataSchemaNode result = null;
906 var SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, u.groupingPath.path);
907 if (!(targetGrouping instanceof GroupingDefinition)) {
908 throw new IllegalArgumentException("Failed to generate code for augment in " + u);
910 var gr = targetGrouping as GroupingDefinition;
911 result = gr.getDataChildByName(node.QName.localName);
913 if (result == null) {
914 throw new IllegalArgumentException("Failed to generate code for augment")
918 var DataSchemaNode result = null;
919 var QName currentName = node.QName
920 var tmpPath = new ArrayList<QName>()
921 var Object parent = null
923 val SchemaPath sp = node.path
924 val List<QName> names = sp.path
925 val List<QName> newNames = new ArrayList(names)
926 newNames.remove(newNames.size - 1)
927 val SchemaPath newSp = new SchemaPath(newNames, sp.absolute)
928 parent = findDataSchemaNode(schemaContext, newSp)
931 tmpPath.add(currentName);
932 if (parent instanceof DataNodeContainer) {
933 val dataNodeParent = parent as DataNodeContainer;
934 for (u : dataNodeParent.uses) {
935 if (result == null) {
936 result = getResultFromUses(u, currentName.localName)
940 if (result == null) {
941 currentName = (parent as SchemaNode).QName
942 if (parent instanceof SchemaNode) {
943 val SchemaPath nodeSp = (parent as SchemaNode).path
944 val List<QName> nodeNames = nodeSp.path
945 val List<QName> nodeNewNames = new ArrayList(nodeNames)
946 nodeNewNames.remove(nodeNewNames.size - 1)
947 if (nodeNewNames.empty) {
948 parent = getParentModule(parent as SchemaNode)
950 val SchemaPath nodeNewSp = new SchemaPath(nodeNewNames, nodeSp.absolute)
951 parent = findDataSchemaNode(schemaContext, nodeNewSp)
954 throw new IllegalArgumentException("Failed to generate code for augment")
957 } while (result == null && !(parent instanceof Module));
959 if (result != null) {
960 result = getTargetNode(tmpPath, result)
966 private def DataSchemaNode findCorrectTargetFromAugmentGrouping(DataSchemaNode node, AugmentationSchema parentNode,
967 List<SchemaNode> dataTree) {
969 var DataSchemaNode result = null;
970 var QName currentName = node.QName
971 var tmpPath = new ArrayList<QName>()
972 tmpPath.add(currentName)
974 var Object parent = null
977 if (dataTree.size < 2 || dataTree.size == i) {
980 parent = dataTree.get(dataTree.size - (i+1))
981 tmpPath.add((parent as SchemaNode).QName)
984 val dataNodeParent = parent as DataNodeContainer;
985 for (u : dataNodeParent.uses) {
986 if (result == null) {
987 result = getResultFromUses(u, currentName.localName)
990 if (result == null) {
992 currentName = (parent as SchemaNode).QName
994 } while (result == null);
996 if (result != null) {
997 result = getTargetNode(tmpPath, result)
1002 private def getResultFromUses(UsesNode u, String currentName) {
1003 var SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, u.groupingPath.path)
1004 if (!(targetGrouping instanceof GroupingDefinition)) {
1005 throw new IllegalArgumentException("Failed to generate code for augment in " + u)
1007 var gr = targetGrouping as GroupingDefinition
1008 return gr.getDataChildByName(currentName)
1011 private def getTargetNode(List<QName> tmpPath, DataSchemaNode node) {
1012 var DataSchemaNode result = node
1013 if (tmpPath.size == 1) {
1014 if (result != null && result.addedByUses) {
1015 result = findOriginal(result);
1019 var DataSchemaNode newParent = result;
1020 Collections.reverse(tmpPath);
1023 for (name : tmpPath) {
1024 // searching by local name is must, because node has different namespace in its original location
1025 if (newParent instanceof DataNodeContainer) {
1026 newParent = (newParent as DataNodeContainer).getDataChildByName(name.localName);
1028 newParent = (newParent as ChoiceNode).getCaseNodeByName(name.localName);
1031 if (newParent != null && newParent.addedByUses) {
1032 newParent = findOriginal(newParent);
1040 * Convenient method to find node added by uses statement.
1042 private def DataSchemaNode findOriginalTargetFromGrouping(SchemaPath targetPath, UsesNode parentUsesNode) {
1043 var SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.groupingPath.path);
1044 if (!(targetGrouping instanceof GroupingDefinition)) {
1045 throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
1048 var grouping = targetGrouping as GroupingDefinition;
1049 var SchemaNode result = grouping;
1050 val List<QName> path = targetPath.path
1052 // finding by local name is valid, grouping cannot contain nodes with same name and different namespace
1053 if (result instanceof DataNodeContainer) {
1054 result = (result as DataNodeContainer).getDataChildByName(node.localName)
1055 } else if (result instanceof ChoiceNode) {
1056 result = (result as ChoiceNode).getCaseNodeByName(node.localName)
1059 if (result == null) {
1063 val String targetSchemaNodeName = result.QName.localName;
1064 var boolean fromUses = (result as DataSchemaNode).addedByUses
1065 var Iterator<UsesNode> groupingUses = grouping.uses.iterator;
1067 if (groupingUses.hasNext()) {
1068 grouping = findNodeInSchemaContext(schemaContext, groupingUses.next().groupingPath.path) as GroupingDefinition;
1069 result = grouping.getDataChildByName(targetSchemaNodeName);
1070 fromUses = (result as DataSchemaNode).addedByUses;
1072 throw new NullPointerException("Failed to generate code for augment in " + parentUsesNode);
1076 return result as DataSchemaNode
1080 * Returns a generated type builder for an augmentation.
1082 * The name of the type builder is equal to the name of augmented node with
1083 * serial number as suffix.
1085 * @param module current module
1086 * @param augmentPackageName
1087 * string with contains the package name to which the augment
1089 * @param basePackageName
1090 * string with the package name to which the augmented node
1092 * @param targetTypeRef
1095 * augmentation schema which contains data about the child nodes
1096 * and uses of augment
1097 * @return generated type builder for augment
1099 private def GeneratedTypeBuilder addRawAugmentGenTypeDefinition(Module module, String augmentPackageName,
1100 String basePackageName, Type targetTypeRef, AugmentationSchema augSchema) {
1101 var Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
1102 if (augmentBuilders === null) {
1103 augmentBuilders = new HashMap();
1104 genTypeBuilders.put(augmentPackageName, augmentBuilders);
1106 val augIdentifier = getAugmentIdentifier(augSchema.unknownSchemaNodes);
1108 val augTypeName = if (augIdentifier !== null) {
1109 BindingMapping.getClassName(augIdentifier)
1111 augGenTypeName(augmentBuilders, targetTypeRef.name);
1114 val augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
1116 augTypeBuilder.addImplementsType(DATA_OBJECT);
1117 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
1118 addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
1120 augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.childNodes);
1121 augmentBuilders.put(augTypeName, augTypeBuilder);
1122 return augTypeBuilder;
1127 * @param unknownSchemaNodes
1128 * @return nodeParameter of UnknownSchemaNode
1130 private def String getAugmentIdentifier(List<UnknownSchemaNode> unknownSchemaNodes) {
1131 for (unknownSchemaNode : unknownSchemaNodes) {
1132 val nodeType = unknownSchemaNode.nodeType;
1133 if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.localName) &&
1134 YANG_EXT_NAMESPACE.equals(nodeType.namespace.toString())) {
1135 return unknownSchemaNode.nodeParameter;
1142 * Returns first unique name for the augment generated type builder. The
1143 * generated type builder name for augment consists from name of augmented
1144 * node and serial number of its augmentation.
1147 * map of builders which were created in the package to which the
1148 * augmentation belongs
1149 * @param genTypeName
1150 * string with name of augmented node
1151 * @return string with unique name for augmentation builder
1153 private def String augGenTypeName(Map<String, GeneratedTypeBuilder> builders, String genTypeName) {
1155 while ((builders !== null) && builders.containsKey(genTypeName + index)) {
1158 return genTypeName + index;
1162 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
1163 * node for which <code>typeBuilder</code> was created.
1165 * The subnodes aren't mapped to the methods if they are part of grouping or
1166 * augment (in this case are already part of them).
1168 * @param module current module
1169 * @param basePackageName
1170 * string contains the module package name
1172 * generated type builder which represents any node. The subnodes
1173 * of this node are added to the <code>typeBuilder</code> as
1174 * methods. The subnode can be of type leaf, leaf-list, list,
1175 * container, choice.
1176 * @param childOf parent type
1177 * @param schemaNodes
1178 * set of data schema nodes which are the children of the node
1179 * for which <code>typeBuilder</code> was created
1180 * @return generated type builder which is the same builder as input
1181 * parameter. The getter methods (representing child nodes) could be
1184 private def GeneratedTypeBuilder resolveDataSchemaNodes(Module module, String basePackageName,
1185 GeneratedTypeBuilder parent, GeneratedTypeBuilder childOf, Set<DataSchemaNode> schemaNodes) {
1186 if ((schemaNodes !== null) && (parent !== null)) {
1187 for (schemaNode : schemaNodes) {
1188 if (!schemaNode.augmenting && !schemaNode.addedByUses) {
1189 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
1197 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
1198 * node for which <code>typeBuilder</code> was created.
1200 * @param module current module
1201 * @param basePackageName
1202 * string contains the module package name
1203 * @param typeBuilder
1204 * generated type builder which represents any node. The subnodes
1205 * of this node are added to the <code>typeBuilder</code> as
1206 * methods. The subnode can be of type leaf, leaf-list, list,
1207 * container, choice.
1208 * @param childOf parent type
1209 * @param schemaNodes
1210 * set of data schema nodes which are the children of the node
1211 * for which <code>typeBuilder</code> was created
1212 * @return generated type builder which is the same object as the input
1213 * parameter <code>typeBuilder</code>. The getter method could be
1216 private def GeneratedTypeBuilder augSchemaNodeToMethods(Module module, String basePackageName,
1217 GeneratedTypeBuilder typeBuilder, GeneratedTypeBuilder childOf, Set<DataSchemaNode> schemaNodes) {
1218 if ((schemaNodes !== null) && (typeBuilder !== null)) {
1219 for (schemaNode : schemaNodes) {
1220 if (!schemaNode.isAugmenting()) {
1221 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
1229 * Adds to <code>typeBuilder</code> a method which is derived from
1230 * <code>schemaNode</code>.
1232 * @param basePackageName
1233 * string with the module package name
1235 * data schema node which is added to <code>typeBuilder</code> as
1237 * @param typeBuilder
1238 * generated type builder to which is <code>schemaNode</code>
1239 * added as a method.
1240 * @param childOf parent type
1241 * @param module current module
1243 private def void addSchemaNodeToBuilderAsMethod(String basePackageName, DataSchemaNode node,
1244 GeneratedTypeBuilder typeBuilder, GeneratedTypeBuilder childOf, Module module) {
1245 if (node !== null && typeBuilder !== null) {
1247 case node instanceof LeafSchemaNode:
1248 resolveLeafSchemaNodeAsMethod(typeBuilder, node as LeafSchemaNode)
1249 case node instanceof LeafListSchemaNode:
1250 resolveLeafListSchemaNode(typeBuilder, node as LeafListSchemaNode)
1251 case node instanceof ContainerSchemaNode:
1252 containerToGenType(module, basePackageName, typeBuilder, childOf, node as ContainerSchemaNode)
1253 case node instanceof ListSchemaNode:
1254 listToGenType(module, basePackageName, typeBuilder, childOf, node as ListSchemaNode)
1255 case node instanceof ChoiceNode:
1256 choiceToGeneratedType(module, basePackageName, typeBuilder, node as ChoiceNode)
1262 * Converts <code>choiceNode</code> to the list of generated types for
1263 * choice and its cases.
1265 * The package names for choice and for its cases are created as
1266 * concatenation of the module package (<code>basePackageName</code>) and
1267 * names of all parents node.
1269 * @param module current module
1270 * @param basePackageName
1271 * string with the module package name
1272 * @param parent parent type
1273 * @param childOf concrete parent for case child nodes
1275 * choice node which is mapped to generated type. Also child
1276 * nodes - cases are mapped to generated types.
1277 * @throws IllegalArgumentException
1279 * <li>if <code>basePackageName</code> equals null</li>
1280 * <li>if <code>choiceNode</code> equals null</li>
1284 private def void choiceToGeneratedType(Module module, String basePackageName, GeneratedTypeBuilder parent,
1285 ChoiceNode choiceNode) {
1286 checkArgument(basePackageName !== null, "Base Package Name cannot be NULL.");
1287 checkArgument(choiceNode !== null, "Choice Schema Node cannot be NULL.");
1289 if (!choiceNode.addedByUses) {
1290 val packageName = packageNameForGeneratedType(basePackageName, choiceNode.path);
1291 val choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
1292 constructGetter(parent, choiceNode.QName.localName, choiceNode.description, choiceTypeBuilder);
1293 choiceTypeBuilder.addImplementsType(DataContainer.typeForClass);
1294 genCtx.get(module).addChildNodeType(choiceNode.path, choiceTypeBuilder)
1295 generateTypesFromChoiceCases(module, basePackageName, parent, choiceTypeBuilder.toInstance, choiceNode);
1300 * Converts <code>caseNodes</code> set to list of corresponding generated
1303 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
1304 * created generated type builder. The package names for the builder is
1305 * created as concatenation of the module package (
1306 * <code>basePackageName</code>) and names of all parents nodes of the
1307 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
1308 * between every case builder and <i>choice</i> type
1310 * @param basePackageName
1311 * string with the module package name
1312 * @param refChoiceType
1313 * type which represents superior <i>case</i>
1315 * set of choice case nodes which are mapped to generated types
1316 * @return list of generated types for <code>caseNodes</code>.
1317 * @throws IllegalArgumentException
1319 * <li>if <code>basePackageName</code> equals null</li>
1320 * <li>if <code>refChoiceType</code> equals null</li>
1321 * <li>if <code>caseNodes</code> equals null</li>
1325 private def void generateTypesFromChoiceCases(Module module, String basePackageName,
1326 GeneratedTypeBuilder choiceParent, Type refChoiceType, ChoiceNode choiceNode) {
1327 checkArgument(basePackageName !== null, "Base Package Name cannot be NULL.");
1328 checkArgument(refChoiceType !== null, "Referenced Choice Type cannot be NULL.");
1329 checkArgument(choiceNode !== null, "ChoiceNode cannot be NULL.");
1331 val Set<ChoiceCaseNode> caseNodes = choiceNode.cases;
1332 if (caseNodes == null) {
1336 for (caseNode : caseNodes) {
1337 if (caseNode !== null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
1338 val packageName = packageNameForGeneratedType(basePackageName, caseNode.path)
1339 val caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode)
1340 caseTypeBuilder.addImplementsType(refChoiceType)
1341 genCtx.get(module).addCaseType(caseNode.path, caseTypeBuilder)
1342 val Set<DataSchemaNode> caseChildNodes = caseNode.childNodes
1343 if (caseChildNodes !== null) {
1344 var Object parentNode = null
1345 val SchemaPath nodeSp = choiceNode.path
1346 val List<QName> nodeNames = nodeSp.path
1347 val List<QName> nodeNewNames = new ArrayList(nodeNames)
1348 nodeNewNames.remove(nodeNewNames.size - 1)
1349 val SchemaPath nodeNewSp = new SchemaPath(nodeNewNames, nodeSp.absolute)
1350 parentNode = findDataSchemaNode(schemaContext, nodeNewSp)
1352 var SchemaNode parent
1353 if (parentNode instanceof AugmentationSchema) {
1354 val augSchema = parentNode as AugmentationSchema;
1355 val targetPath = augSchema.targetPath;
1356 var targetSchemaNode = findDataSchemaNode(schemaContext, targetPath)
1357 if (targetSchemaNode instanceof DataSchemaNode &&
1358 (targetSchemaNode as DataSchemaNode).isAddedByUses()) {
1359 targetSchemaNode = findOriginal(targetSchemaNode as DataSchemaNode);
1360 if (targetSchemaNode == null) {
1361 throw new NullPointerException(
1362 "Failed to find target node from grouping for augmentation " + augSchema +
1363 " in module " + module.name);
1366 parent = targetSchemaNode
1368 val SchemaPath sp = choiceNode.path
1369 val List<QName> names = sp.path
1370 val List<QName> newNames = new ArrayList(names)
1371 newNames.remove(newNames.size - 1)
1372 val SchemaPath newSp = new SchemaPath(newNames, sp.absolute)
1373 parent = findDataSchemaNode(schemaContext, newSp)
1375 var GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.path)
1376 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes)
1380 processUsesAugments(caseNode, module);
1385 * Generates list of generated types for all the cases of a choice which are
1386 * added to the choice through the augment.
1389 * @param basePackageName
1390 * string contains name of package to which augment belongs. If
1391 * an augmented choice is from an other package (pcg1) than an
1392 * augmenting choice (pcg2) then case's of the augmenting choice
1393 * will belong to pcg2.
1394 * @param refChoiceType
1395 * Type which represents the choice to which case belongs. Every
1396 * case has to contain its choice in extend part.
1398 * set of choice case nodes for which is checked if are/aren't
1399 * added to choice through augmentation
1400 * @return list of generated types which represents augmented cases of
1401 * choice <code>refChoiceType</code>
1402 * @throws IllegalArgumentException
1404 * <li>if <code>basePackageName</code> equals null</li>
1405 * <li>if <code>refChoiceType</code> equals null</li>
1406 * <li>if <code>caseNodes</code> equals null</li>
1409 private def void generateTypesFromAugmentedChoiceCases(Module module, String basePackageName, Type targetType,
1410 ChoiceNode targetNode, Set<DataSchemaNode> augmentedNodes) {
1411 checkArgument(basePackageName !== null, "Base Package Name cannot be NULL.");
1412 checkArgument(targetType !== null, "Referenced Choice Type cannot be NULL.");
1413 checkArgument(augmentedNodes !== null, "Set of Choice Case Nodes cannot be NULL.");
1415 for (caseNode : augmentedNodes) {
1416 if (caseNode !== null) {
1417 val packageName = packageNameForGeneratedType(basePackageName, caseNode.path);
1418 val caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode);
1419 caseTypeBuilder.addImplementsType(targetType);
1421 var SchemaNode parent = null
1422 val SchemaPath nodeSp = targetNode.path
1423 val List<QName> nodeNames = nodeSp.path
1424 val List<QName> nodeNewNames = new ArrayList(nodeNames)
1425 nodeNewNames.remove(nodeNewNames.size - 1)
1426 val SchemaPath nodeNewSp = new SchemaPath(nodeNewNames, nodeSp.absolute)
1427 parent = findDataSchemaNode(schemaContext, nodeNewSp)
1429 var GeneratedTypeBuilder childOfType = null;
1430 if (parent instanceof Module) {
1431 childOfType = genCtx.get(parent as Module).moduleNode
1432 } else if (parent instanceof ChoiceCaseNode) {
1433 childOfType = findCaseByPath(parent.path)
1434 } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
1435 childOfType = findChildNodeByPath(parent.path)
1436 } else if (parent instanceof GroupingDefinition) {
1437 childOfType = findGroupingByPath(parent.path);
1440 if (childOfType == null) {
1441 throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode);
1444 if (caseNode instanceof DataNodeContainer) {
1445 val DataNodeContainer dataNodeCase = caseNode as DataNodeContainer;
1446 val Set<DataSchemaNode> childNodes = dataNodeCase.childNodes;
1447 if (childNodes !== null) {
1448 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1451 val ChoiceCaseNode node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
1452 val Set<DataSchemaNode> childNodes = node.childNodes;
1453 if (childNodes !== null) {
1454 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
1458 genCtx.get(module).addCaseType(caseNode.path, caseTypeBuilder)
1465 * Converts <code>leaf</code> to the getter method which is added to
1466 * <code>typeBuilder</code>.
1468 * @param typeBuilder
1469 * generated type builder to which is added getter method as
1470 * <code>leaf</code> mapping
1472 * leaf schema node which is mapped as getter method which is
1473 * added to <code>typeBuilder</code>
1474 * @return boolean value
1476 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
1478 * <li>true - in other cases</li>
1481 private def boolean resolveLeafSchemaNodeAsMethod(GeneratedTypeBuilder typeBuilder, LeafSchemaNode leaf) {
1482 if ((leaf !== null) && (typeBuilder !== null)) {
1483 val leafName = leaf.QName.localName;
1484 var String leafDesc = leaf.description;
1485 if (leafDesc === null) {
1489 val parentModule = findParentModule(schemaContext, leaf);
1490 if (leafName !== null && !leaf.isAddedByUses()) {
1491 val TypeDefinition<?> typeDef = leaf.type;
1493 var Type returnType = null;
1494 var GeneratedTOBuilder genTOBuilder;
1495 if (typeDef instanceof EnumTypeDefinition) {
1496 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1497 val enumTypeDef = typeDef as EnumTypeDefinition;
1498 val enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.QName, typeBuilder);
1500 if (enumBuilder !== null) {
1501 returnType = new ReferencedTypeImpl(enumBuilder.packageName, enumBuilder.name);
1503 (typeProvider as TypeProviderImpl).putReferencedType(leaf.path, returnType);
1504 } else if (typeDef instanceof UnionType) {
1505 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1506 if (genTOBuilder !== null) {
1507 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule)
1509 } else if (typeDef instanceof BitsTypeDefinition) {
1510 genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
1511 if (genTOBuilder !== null) {
1512 returnType = new ReferencedTypeImpl(genTOBuilder.packageName, genTOBuilder.name);
1515 val Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1516 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
1518 if (returnType !== null) {
1519 val MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
1520 processContextRefExtension(leaf, getter, parentModule);
1528 private def void processContextRefExtension(LeafSchemaNode leaf, MethodSignatureBuilder getter, Module module) {
1529 for (node : leaf.unknownSchemaNodes) {
1530 val nodeType = node.nodeType;
1531 if ("context-reference".equals(nodeType.localName)) {
1532 val nodeParam = node.nodeParameter;
1533 var IdentitySchemaNode identity = null;
1534 var String basePackageName = null;
1535 val String[] splittedElement = nodeParam.split(":");
1536 if (splittedElement.length == 1) {
1537 identity = findIdentityByName(module.identities, splittedElement.get(0));
1538 basePackageName = moduleNamespaceToPackageName(module);
1539 } else if (splittedElement.length == 2) {
1540 var prefix = splittedElement.get(0);
1541 val Module dependentModule = findModuleFromImports(module.imports, prefix)
1542 if (dependentModule == null) {
1543 throw new IllegalArgumentException(
1544 "Failed to process context-reference: unknown prefix " + prefix);
1546 identity = findIdentityByName(dependentModule.identities, splittedElement.get(1));
1547 basePackageName = moduleNamespaceToPackageName(dependentModule);
1549 throw new IllegalArgumentException(
1550 "Failed to process context-reference: unknown identity " + nodeParam);
1552 if (identity == null) {
1553 throw new IllegalArgumentException(
1554 "Failed to process context-reference: unknown identity " + nodeParam);
1557 val Class<RoutingContext> clazz = typeof(RoutingContext);
1558 val AnnotationTypeBuilder rc = getter.addAnnotation(clazz.package.name, clazz.simpleName);
1559 val packageName = packageNameForGeneratedType(basePackageName, identity.path);
1560 val genTypeName = BindingMapping.getClassName(identity.QName.localName);
1561 rc.addParameter("value", packageName + "." + genTypeName + ".class");
1566 private def IdentitySchemaNode findIdentityByName(Set<IdentitySchemaNode> identities, String name) {
1567 for (id : identities) {
1568 if (id.QName.localName.equals(name)) {
1575 private def Module findModuleFromImports(Set<ModuleImport> imports, String prefix) {
1576 for (imp : imports) {
1577 if (imp.prefix.equals(prefix)) {
1578 return schemaContext.findModuleByName(imp.moduleName, imp.revision);
1585 * Converts <code>leaf</code> schema node to property of generated TO
1589 * generated TO builder to which is <code>leaf</code> added as
1592 * leaf schema node which is added to <code>toBuilder</code> as
1595 * boolean value which says if leaf property is|isn't read only
1596 * @return boolean value
1598 * <li>false - if <code>leaf</code>, <code>toBuilder</code> or leaf
1599 * name equals null or if leaf is added by <i>uses</i>.</li>
1600 * <li>true - other cases</li>
1603 private def boolean resolveLeafSchemaNodeAsProperty(GeneratedTOBuilder toBuilder, LeafSchemaNode leaf,
1604 boolean isReadOnly, Module module) {
1605 if ((leaf !== null) && (toBuilder !== null)) {
1606 val leafName = leaf.QName.localName;
1607 var String leafDesc = leaf.description;
1608 if (leafDesc === null) {
1612 if (leafName !== null) {
1613 var Type returnType = null;
1614 val TypeDefinition<?> typeDef = leaf.type;
1615 if (typeDef instanceof UnionTypeDefinition) {
1616 // GeneratedType for this type definition should be already created
1617 var qname = typeDef.QName
1618 var Module unionModule = null
1619 if (qname.prefix == null || qname.prefix.empty) {
1620 unionModule = module
1622 unionModule = findModuleFromImports(module.imports, qname.prefix)
1624 val ModuleContext mc = genCtx.get(unionModule)
1625 returnType = mc.typedefs.get(typeDef.path)
1627 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1630 if (returnType !== null) {
1631 val propBuilder = toBuilder.addProperty(parseToValidParamName(leafName));
1632 propBuilder.setReadOnly(isReadOnly);
1633 propBuilder.setReturnType(returnType);
1634 propBuilder.setComment(leafDesc);
1635 toBuilder.addEqualsIdentity(propBuilder);
1636 toBuilder.addHashIdentity(propBuilder);
1637 toBuilder.addToStringProperty(propBuilder);
1646 * Converts <code>node</code> leaf list schema node to getter method of
1647 * <code>typeBuilder</code>.
1649 * @param typeBuilder
1650 * generated type builder to which is <code>node</code> added as
1653 * leaf list schema node which is added to
1654 * <code>typeBuilder</code> as getter method
1655 * @return boolean value
1657 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
1658 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
1659 * <li>false - other cases</li>
1662 private def boolean resolveLeafListSchemaNode(GeneratedTypeBuilder typeBuilder, LeafListSchemaNode node) {
1663 if ((node !== null) && (typeBuilder !== null)) {
1664 val nodeName = node.QName;
1665 var String nodeDesc = node.description;
1666 if (nodeDesc === null) {
1669 if (nodeName !== null && !node.isAddedByUses()) {
1670 val TypeDefinition<?> typeDef = node.type;
1671 val parentModule = findParentModule(schemaContext, node);
1673 var Type returnType = null;
1674 if (typeDef instanceof EnumTypeDefinition) {
1675 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
1676 val enumTypeDef = typeDef as EnumTypeDefinition;
1677 val enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName, typeBuilder);
1678 returnType = new ReferencedTypeImpl(enumBuilder.packageName, enumBuilder.name);
1679 (typeProvider as TypeProviderImpl).putReferencedType(node.path, returnType);
1680 } else if (typeDef instanceof UnionType) {
1681 val genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1682 if (genTOBuilder !== null) {
1683 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule)
1685 } else if (typeDef instanceof BitsTypeDefinition) {
1686 val genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
1687 returnType = new ReferencedTypeImpl(genTOBuilder.packageName, genTOBuilder.name);
1689 val Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
1690 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
1693 val listType = Types.listTypeFor(returnType);
1694 constructGetter(typeBuilder, nodeName.localName, nodeDesc, listType);
1701 private def Type createReturnTypeForUnion(GeneratedTOBuilder genTOBuilder, TypeDefinition<?> typeDef,
1702 GeneratedTypeBuilder typeBuilder, Module parentModule) {
1703 val Type returnType = new ReferencedTypeImpl(genTOBuilder.packageName, genTOBuilder.name);
1704 genTOBuilder.setTypedef(true);
1705 genTOBuilder.setIsUnion(true);
1706 (typeProvider as TypeProviderImpl).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
1709 val GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
1710 genTOBuilder.getName() + "Builder");
1711 unionBuilder.setIsUnionBuilder(true);
1712 val MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
1713 method.setReturnType(returnType);
1714 method.addParameter(Types.STRING, "defaultValue");
1715 method.setAccessModifier(AccessModifier.PUBLIC);
1716 method.setStatic(true);
1718 val Set<Type> types = (typeProvider as TypeProviderImpl).additionalTypes.get(parentModule);
1719 if (types == null) {
1720 (typeProvider as TypeProviderImpl).additionalTypes.put(parentModule,
1721 Sets.newHashSet(unionBuilder.toInstance))
1723 types.add(unionBuilder.toInstance)
1728 private def GeneratedTypeBuilder addDefaultInterfaceDefinition(String packageName, SchemaNode schemaNode) {
1729 return addDefaultInterfaceDefinition(packageName, schemaNode, null);
1733 * Instantiates generated type builder with <code>packageName</code> and
1734 * <code>schemaNode</code>.
1736 * The new builder always implements
1737 * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.<br />
1738 * If <code>schemaNode</code> is instance of GroupingDefinition it also
1739 * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable
1740 * Augmentable}.<br />
1741 * If <code>schemaNode</code> is instance of
1742 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
1743 * DataNodeContainer} it can also implement nodes which are specified in
1746 * @param packageName
1747 * string with the name of the package to which
1748 * <code>schemaNode</code> belongs.
1750 * schema node for which is created generated type builder
1751 * @param parent parent type (can be null)
1752 * @return generated type builder <code>schemaNode</code>
1754 private def GeneratedTypeBuilder addDefaultInterfaceDefinition(String packageName, SchemaNode schemaNode,
1756 val it = addRawInterfaceDefinition(packageName, schemaNode, "");
1757 qnameConstant(BindingMapping.QNAME_STATIC_FIELD_NAME,schemaNode.QName);
1758 if (parent === null) {
1759 addImplementsType(DATA_OBJECT);
1761 addImplementsType(BindingTypes.childOf(parent));
1763 if (!(schemaNode instanceof GroupingDefinition)) {
1764 addImplementsType(augmentable(it));
1767 if (schemaNode instanceof DataNodeContainer) {
1768 addImplementedInterfaceFromUses(schemaNode as DataNodeContainer, it);
1775 * Wraps the calling of the same overloaded method.
1777 * @param packageName
1778 * string with the package name to which returning generated type
1781 * schema node which provide data about the schema node name
1782 * @return generated type builder for <code>schemaNode</code>
1784 private def GeneratedTypeBuilder addRawInterfaceDefinition(String packageName, SchemaNode schemaNode) {
1785 return addRawInterfaceDefinition(packageName, schemaNode, "");
1789 * Returns reference to generated type builder for specified
1790 * <code>schemaNode</code> with <code>packageName</code>.
1792 * Firstly the generated type builder is searched in
1793 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
1794 * found it is created and added to <code>genTypeBuilders</code>.
1796 * @param packageName
1797 * string with the package name to which returning generated type
1800 * schema node which provide data about the schema node name
1801 * @param prefix return type name prefix
1802 * @return generated type builder for <code>schemaNode</code>
1803 * @throws IllegalArgumentException
1805 * <li>if <code>schemaNode</code> equals null</li>
1806 * <li>if <code>packageName</code> equals null</li>
1807 * <li>if Q name of schema node is null</li>
1808 * <li>if schema node name is null</li>
1812 private def GeneratedTypeBuilder addRawInterfaceDefinition(String packageName, SchemaNode schemaNode,
1814 checkArgument(schemaNode !== null, "Data Schema Node cannot be NULL.");
1815 checkArgument(packageName !== null, "Package Name for Generated Type cannot be NULL.");
1816 checkArgument(schemaNode.QName !== null, "QName for Data Schema Node cannot be NULL.");
1817 val schemaNodeName = schemaNode.QName.localName;
1818 checkArgument(schemaNodeName !== null, "Local Name of QName for Data Schema Node cannot be NULL.");
1820 var String genTypeName;
1821 if (prefix === null) {
1822 genTypeName = BindingMapping.getClassName(schemaNodeName);
1824 genTypeName = prefix + BindingMapping.getClassName(schemaNodeName);
1827 //FIXME: Validation of name conflict
1828 val newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
1829 if (!genTypeBuilders.containsKey(packageName)) {
1830 val Map<String, GeneratedTypeBuilder> builders = new HashMap();
1831 builders.put(genTypeName, newType);
1832 genTypeBuilders.put(packageName, builders);
1834 val Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
1835 if (!builders.containsKey(genTypeName)) {
1836 builders.put(genTypeName, newType);
1843 * Creates the name of the getter method from <code>methodName</code>.
1846 * string with the name of the getter method
1847 * @param returnType return type
1848 * @return string with the name of the getter method for
1849 * <code>methodName</code> in JAVA method format
1851 public static def String getterMethodName(String localName, Type returnType) {
1852 val method = new StringBuilder();
1853 if (BOOLEAN.equals(returnType)) {
1854 method.append("is");
1856 method.append("get");
1858 method.append(BindingMapping.getPropertyName(localName).toFirstUpper);
1859 return method.toString();
1863 * Created a method signature builder as part of
1864 * <code>interfaceBuilder</code>.
1866 * The method signature builder is created for the getter method of
1867 * <code>schemaNodeName</code>. Also <code>comment</code> and
1868 * <code>returnType</code> information are added to the builder.
1870 * @param interfaceBuilder
1871 * generated type builder for which the getter method should be
1873 * @param schemaNodeName
1874 * string with schema node name. The name will be the part of the
1875 * getter method name.
1877 * string with comment for the getter method
1879 * type which represents the return type of the getter method
1880 * @return method signature builder which represents the getter method of
1881 * <code>interfaceBuilder</code>
1883 private def MethodSignatureBuilder constructGetter(GeneratedTypeBuilder interfaceBuilder, String schemaNodeName,
1884 String comment, Type returnType) {
1885 val getMethod = interfaceBuilder.addMethod(getterMethodName(schemaNodeName, returnType));
1886 getMethod.setComment(comment);
1887 getMethod.setReturnType(returnType);
1892 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
1893 * or to <code>genTOBuilder</code> as property.
1895 * @param basePackageName
1896 * string contains the module package name
1898 * data schema node which should be added as getter method to
1899 * <code>typeBuilder</code> or as a property to
1900 * <code>genTOBuilder</code> if is part of the list key
1901 * @param typeBuilder
1902 * generated type builder for the list schema node
1903 * @param genTOBuilder
1904 * generated TO builder for the list keys
1906 * list of string which contains names of the list keys
1907 * @param module current module
1908 * @throws IllegalArgumentException
1910 * <li>if <code>schemaNode</code> equals null</li>
1911 * <li>if <code>typeBuilder</code> equals null</li>
1914 private def void addSchemaNodeToListBuilders(String basePackageName, DataSchemaNode schemaNode,
1915 GeneratedTypeBuilder typeBuilder, GeneratedTOBuilder genTOBuilder, List<String> listKeys, Module module) {
1916 checkArgument(schemaNode !== null, "Data Schema Node cannot be NULL.");
1917 checkArgument(typeBuilder !== null, "Generated Type Builder cannot be NULL.");
1919 if (schemaNode instanceof LeafSchemaNode) {
1920 val leaf = schemaNode as LeafSchemaNode;
1921 val leafName = leaf.QName.localName;
1922 resolveLeafSchemaNodeAsMethod(typeBuilder, leaf);
1923 if (listKeys.contains(leafName)) {
1924 resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, true, module)
1926 } else if (!schemaNode.addedByUses) {
1927 if (schemaNode instanceof LeafListSchemaNode) {
1928 resolveLeafListSchemaNode(typeBuilder, schemaNode as LeafListSchemaNode);
1929 } else if (schemaNode instanceof ContainerSchemaNode) {
1930 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, schemaNode as ContainerSchemaNode);
1931 } else if (schemaNode instanceof ChoiceNode) {
1932 choiceToGeneratedType(module, basePackageName, typeBuilder, schemaNode as ChoiceNode);
1933 } else if (schemaNode instanceof ListSchemaNode) {
1934 listToGenType(module, basePackageName, typeBuilder, typeBuilder, schemaNode as ListSchemaNode);
1939 private def typeBuildersToGenTypes(Module module, GeneratedTypeBuilder typeBuilder, GeneratedTOBuilder genTOBuilder) {
1940 checkArgument(typeBuilder !== null, "Generated Type Builder cannot be NULL.");
1942 if (genTOBuilder !== null) {
1943 val genTO = genTOBuilder.toInstance();
1944 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
1945 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder)
1950 * Selects the names of the list keys from <code>list</code> and returns
1951 * them as the list of the strings
1954 * of string with names of the list keys
1955 * @return list of string which represents names of the list keys. If the
1956 * <code>list</code> contains no keys then the empty list is
1959 private def listKeys(ListSchemaNode list) {
1960 val List<String> listKeys = new ArrayList();
1962 if (list.keyDefinition !== null) {
1963 val keyDefinitions = list.keyDefinition;
1964 for (keyDefinition : keyDefinitions) {
1965 listKeys.add(keyDefinition.localName);
1972 * Generates for the <code>list</code> which contains any list keys special
1973 * generated TO builder.
1975 * @param packageName
1976 * string with package name to which the list belongs
1978 * list schema node which is source of data about the list name
1979 * @return generated TO builder which represents the keys of the
1980 * <code>list</code> or null if <code>list</code> is null or list of
1981 * key definitions is null or empty.
1983 private def GeneratedTOBuilder resolveListKeyTOBuilder(String packageName, ListSchemaNode list) {
1984 var GeneratedTOBuilder genTOBuilder = null;
1985 if ((list.keyDefinition !== null) && (!list.keyDefinition.isEmpty())) {
1986 val listName = list.QName.localName + "Key";
1987 val String genTOName = BindingMapping.getClassName(listName);
1988 genTOBuilder = new GeneratedTOBuilderImpl(packageName, genTOName);
1990 return genTOBuilder;
1994 * Builds generated TO builders for <code>typeDef</code> of type
1995 * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or
1996 * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
1997 * BitsTypeDefinition} which are also added to <code>typeBuilder</code> as
1998 * enclosing transfer object.
2000 * If more then one generated TO builder is created for enclosing then all
2001 * of the generated TO builders are added to <code>typeBuilder</code> as
2002 * enclosing transfer objects.
2005 * type definition which can be of type <code>UnionType</code> or
2006 * <code>BitsTypeDefinition</code>
2007 * @param typeBuilder
2008 * generated type builder to which is added generated TO created
2009 * from <code>typeDef</code>
2011 * string with name for generated TO builder
2013 * @param parentModule
2014 * @return generated TO builder for <code>typeDef</code>
2016 private def GeneratedTOBuilder addTOToTypeBuilder(TypeDefinition<?> typeDef, GeneratedTypeBuilder typeBuilder,
2017 DataSchemaNode leaf, Module parentModule) {
2018 val classNameFromLeaf = BindingMapping.getClassName(leaf.QName);
2019 val List<GeneratedTOBuilder> genTOBuilders = new ArrayList();
2020 val packageName = typeBuilder.fullyQualifiedName;
2021 if (typeDef instanceof UnionTypeDefinition) {
2022 val List<GeneratedTOBuilder> types = (typeProvider as TypeProviderImpl).
2023 provideGeneratedTOBuildersForUnionTypeDef(packageName, (typeDef as UnionTypeDefinition),
2024 classNameFromLeaf, leaf);
2025 genTOBuilders.addAll(types);
2028 var GeneratedTOBuilder resultTOBuilder = null;
2029 if (!types.isEmpty()) {
2030 resultTOBuilder = types.remove(0);
2031 for (GeneratedTOBuilder genTOBuilder : types) {
2032 resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
2036 val GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value");
2037 genPropBuilder.setReturnType(Types.primitiveType("char[]", null));
2038 resultTOBuilder.addEqualsIdentity(genPropBuilder);
2039 resultTOBuilder.addHashIdentity(genPropBuilder);
2040 resultTOBuilder.addToStringProperty(genPropBuilder);
2042 } else if (typeDef instanceof BitsTypeDefinition) {
2044 ((typeProvider as TypeProviderImpl) ).
2045 provideGeneratedTOBuilderForBitsTypeDefinition(packageName, typeDef, classNameFromLeaf));
2047 if (genTOBuilders !== null && !genTOBuilders.isEmpty()) {
2048 for (genTOBuilder : genTOBuilders) {
2049 typeBuilder.addEnclosingTransferObject(genTOBuilder);
2051 return genTOBuilders.get(0);
2058 * Adds the implemented types to type builder.
2060 * The method passes through the list of <i>uses</i> in
2061 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
2062 * generated type from {@link BindingGeneratorImpl#allGroupings
2063 * allGroupings} which is added as <i>implements type</i> to
2064 * <code>builder</code>
2066 * @param dataNodeContainer
2067 * element which contains the list of used YANG groupings
2069 * builder to which are added implemented types according to
2070 * <code>dataNodeContainer</code>
2071 * @return generated type builder with all implemented types
2073 private def addImplementedInterfaceFromUses(DataNodeContainer dataNodeContainer, GeneratedTypeBuilder builder) {
2074 for (usesNode : dataNodeContainer.uses) {
2075 if (usesNode.groupingPath !== null) {
2076 val genType = findGroupingByPath(usesNode.groupingPath).toInstance
2077 if (genType === null) {
2078 throw new IllegalStateException(
2079 "Grouping " + usesNode.groupingPath + "is not resolved for " + builder.name);
2081 builder.addImplementsType(genType);
2087 private def GeneratedTypeBuilder findChildNodeByPath(SchemaPath path) {
2088 for (ctx : genCtx.values) {
2089 var result = ctx.getChildNode(path)
2090 if (result !== null) {
2097 private def GeneratedTypeBuilder findGroupingByPath(SchemaPath path) {
2098 for (ctx : genCtx.values) {
2099 var result = ctx.getGrouping(path)
2100 if (result !== null) {
2107 private def GeneratedTypeBuilder findCaseByPath(SchemaPath path) {
2108 for (ctx : genCtx.values) {
2109 var result = ctx.getCase(path)
2110 if (result !== null) {
2117 private def Module getParentModule(SchemaNode node) {
2118 val QName qname = node.getPath().getPath().get(0);
2119 val URI namespace = qname.getNamespace();
2120 val Date revision = qname.getRevision();
2121 return schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
2124 public def getModuleContexts() {