2 * Copyright (c) 2017 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
9 package org.opendaylight.mdsal.binding.javav2.generator.impl;
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.addTOToTypeBuilder;
13 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.annotateDeprecatedIfNecessary;
14 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.augGenTypeName;
15 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.constructGetter;
16 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.createDescription;
17 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.createReturnTypeForUnion;
18 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.getAugmentIdentifier;
19 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.isInnerType;
20 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.qNameConstant;
21 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.resolveInnerEnumFromTypeDefinition;
22 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.resolveListKeyTOBuilder;
23 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.computeDefaultSUID;
24 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
25 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.packageNameForGeneratedType;
26 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.NOTIFICATION;
27 import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.parameterizedTypeFor;
28 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
29 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
31 import com.google.common.annotations.Beta;
32 import com.google.common.base.Preconditions;
33 import com.google.common.collect.Iterables;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.List;
39 import java.util.stream.Collectors;
40 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
41 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil;
42 import org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes;
43 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifier;
44 import org.opendaylight.mdsal.binding.javav2.generator.util.JavaIdentifierNormalizer;
45 import org.opendaylight.mdsal.binding.javav2.generator.util.ReferencedTypeImpl;
46 import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
47 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedPropertyBuilderImpl;
48 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
49 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
50 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.GroupingDefinitionDependencySort;
51 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
52 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
53 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
54 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
55 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
56 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
57 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
58 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
59 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
60 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
61 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
62 import org.opendaylight.mdsal.binding.javav2.spec.base.BaseIdentity;
63 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
64 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
65 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
66 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
67 import org.opendaylight.yangtools.yang.common.QName;
68 import org.opendaylight.yangtools.yang.model.api.AnyDataSchemaNode;
69 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
70 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
71 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
72 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
73 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
74 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
75 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
76 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
77 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
78 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
79 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
80 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
81 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
82 import org.opendaylight.yangtools.yang.model.api.Module;
83 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
84 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
85 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
86 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
87 import org.opendaylight.yangtools.yang.model.api.Status;
88 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
89 import org.opendaylight.yangtools.yang.model.api.UsesNode;
90 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
91 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
92 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
93 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
94 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
97 * Helper util class used for generation of types in Binding spec v2.
100 final class GenHelperUtil {
102 private GenHelperUtil() {
103 throw new UnsupportedOperationException("Util class");
107 * Create GeneratedTypeBuilder object from module argument.
110 * Module object from which builder will be created
112 * @param verboseClassComments
114 * @return <code>GeneratedTypeBuilder</code> which is internal
115 * representation of the module
116 * @throws IllegalArgumentException
119 static GeneratedTypeBuilder moduleToDataType(final Module module, final Map<Module, ModuleContext> genCtx, final boolean verboseClassComments) {
120 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
122 final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data", verboseClassComments);
123 addImplementedInterfaceFromUses(module, moduleDataTypeBuilder, genCtx);
124 moduleDataTypeBuilder.addImplementsType(BindingTypes.TREE_ROOT);
125 moduleDataTypeBuilder.addComment(module.getDescription());
126 moduleDataTypeBuilder.setDescription(createDescription(module, verboseClassComments));
127 moduleDataTypeBuilder.setReference(module.getReference());
128 return moduleDataTypeBuilder;
132 * Generates type builder for <code>module</code>.
135 * Module which is source of package name for generated type
138 * string which is added to the module class name representation
140 * @param verboseClassComments
141 * @return instance of GeneratedTypeBuilder which represents
142 * <code>module</code>.
143 * @throws IllegalArgumentException
144 * if <code>module</code> is null
146 static GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix, final boolean
147 verboseClassComments) {
148 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
149 final String packageName = BindingMapping.getRootPackageName(module);
150 // underscore used as separator for distinction of module name parts
151 final String moduleName = new StringBuilder(module.getName()).append('_').append(postfix).toString();
153 final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
154 moduleBuilder.setDescription(createDescription(module, verboseClassComments));
155 moduleBuilder.setReference(module.getReference());
156 moduleBuilder.setModuleName(moduleName);
158 return moduleBuilder;
162 * Adds the implemented types to type builder.
164 * The method passes through the list of <i>uses</i> in
165 * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
166 * generated type from all groupings
167 * allGroupings} which is added as <i>implements type</i> to
168 * <code>builder</code>
170 * @param dataNodeContainer
171 * element which contains the list of used YANG groupings
173 * builder to which are added implemented types according to
174 * <code>dataNodeContainer</code>
175 * @param genCtx generated context
176 * @return generated type builder with all implemented types
178 static GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
179 final GeneratedTypeBuilder builder, final Map<Module, ModuleContext> genCtx) {
180 for (final UsesNode usesNode : dataNodeContainer.getUses()) {
181 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath(), genCtx).toInstance();
182 if (genType == null) {
183 throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
184 + builder.getName());
186 builder.addImplementsType(genType);
191 static GeneratedTypeBuilder findGroupingByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
192 for (final ModuleContext ctx : genCtx.values()) {
193 final GeneratedTypeBuilder result = ctx.getGrouping(path);
194 if (result != null) {
202 * Adds the methods to <code>typeBuilder</code> which represent subnodes of
203 * node for which <code>typeBuilder</code> was created.
205 * The subnodes aren't mapped to the methods if they are part of grouping or
206 * augment (in this case are already part of them).
210 * @param basePackageName
211 * string contains the module package name
213 * generated type builder which represents any node. The subnodes
214 * of this node are added to the <code>typeBuilder</code> as
215 * methods. The subnode can be of type leaf, leaf-list, list,
220 * set of data schema nodes which are the children of the node
221 * for which <code>typeBuilder</code> was created
222 * @return generated type builder which is the same builder as input
223 * parameter. The getter methods (representing child nodes) could be
226 static GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
227 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf,
228 final Iterable<DataSchemaNode> schemaNodes, final Map<Module, ModuleContext> genCtx,
229 final SchemaContext schemaContext, final boolean verboseClassComments,
230 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
231 final TypeProvider typeProvider) {
233 if (schemaNodes != null && parent != null) {
234 for (final DataSchemaNode schemaNode : schemaNodes) {
235 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
236 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module, genCtx,
237 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
244 static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
245 schemaNode, final Module module, final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext,
246 final boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
247 final TypeProvider typeProvider) {
248 return addDefaultInterfaceDefinition(packageName, schemaNode, null, module, genCtx, schemaContext,
249 verboseClassComments, genTypeBuilders, typeProvider);
252 static Map<Module, ModuleContext> processUsesAugments(final SchemaContext schemaContext, final
253 DataNodeContainer node, final Module module, Map<Module, ModuleContext> genCtx,
254 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
255 final boolean verboseClassComments, final TypeProvider typeProvider) {
256 final String basePackageName = BindingMapping.getRootPackageName(module);
257 for (final UsesNode usesNode : node.getUses()) {
258 Map<SchemaPath, List<AugmentationSchema>> augmentationsGrouped =
259 usesNode.getAugmentations().stream().collect(Collectors.groupingBy(AugmentationSchema::getTargetPath));
260 for (Map.Entry<SchemaPath, List<AugmentationSchema>> schemaPathAugmentListEntry : augmentationsGrouped.entrySet()) {
261 genCtx = AugmentToGenType.usesAugmentationToGenTypes(schemaContext, basePackageName,
262 schemaPathAugmentListEntry.getValue(), module,
263 usesNode, node, genCtx, genTypeBuilders, verboseClassComments, typeProvider);
264 for (AugmentationSchema augSchema : schemaPathAugmentListEntry.getValue()) {
265 genCtx = processUsesAugments(schemaContext, augSchema, module, genCtx, genTypeBuilders,
266 verboseClassComments, typeProvider);
273 static GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
274 for (final ModuleContext ctx : genCtx.values()) {
275 final GeneratedTypeBuilder result = ctx.getChildNode(path);
276 if (result != null) {
283 static GeneratedTypeBuilder findCaseByPath(final SchemaPath path, final Map<Module, ModuleContext> genCtx) {
284 for (final ModuleContext ctx : genCtx.values()) {
285 final GeneratedTypeBuilder result = ctx.getCase(path);
286 if (result != null) {
293 static Map<Module, ModuleContext> addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
294 final Type targetTypeRef, final List<AugmentationSchema> schemaPathAugmentListEntry,
295 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final Map<Module, ModuleContext> genCtx,
296 final SchemaContext schemaContext, final boolean verboseClassComments, final TypeProvider typeProvider) {
298 //pick augmentation grouped by augmentation target, there is always at least one
299 final AugmentationSchema augSchema = schemaPathAugmentListEntry.get(0);
301 Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.computeIfAbsent(
302 augmentPackageName, k -> new HashMap<>());
304 //this requires valid semantics in YANG model
305 String augIdentifier = null;
306 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
307 augIdentifier = getAugmentIdentifier(aug.getUnknownSchemaNodes());
311 boolean isTypeNormalized = false;
312 if (augIdentifier == null) {
313 augIdentifier = augGenTypeName(augmentBuilders, targetTypeRef.getName());
314 isTypeNormalized = true;
317 GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augIdentifier,
318 false, isTypeNormalized);
320 augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
321 augTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, augTypeBuilder));
322 augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
323 annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
325 //produces getters for augTypeBuilder eventually
326 for (AugmentationSchema aug : schemaPathAugmentListEntry) {
328 addImplementedInterfaceFromUses(aug, augTypeBuilder, genCtx);
329 augSchemaNodeToMethods(module, BindingMapping.getRootPackageName(module), augTypeBuilder, augTypeBuilder, aug.getChildNodes(),
330 genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders);
333 augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
335 if(!augSchema.getChildNodes().isEmpty()) {
336 genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
337 genCtx.get(module).addTargetToAugmentation(augTypeBuilder, augSchema.getTargetPath());
339 genCtx.get(module).addAugmentType(augTypeBuilder);
344 * Adds the methods to <code>typeBuilder</code> what represents subnodes of
345 * node for which <code>typeBuilder</code> was created.
349 * @param basePackageName
350 * string contains the module package name
352 * generated type builder which represents any node. The subnodes
353 * of this node are added to the <code>typeBuilder</code> as
354 * methods. The subnode can be of type leaf, leaf-list, list,
359 * set of data schema nodes which are the children of the node
360 * for which <code>typeBuilder</code> was created
361 * @return generated type builder which is the same object as the input
362 * parameter <code>typeBuilder</code>. The getter method could be
365 private static GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
366 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes,
367 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
368 verboseClassComments, final TypeProvider typeProvider, final Map<String, Map<String,
369 GeneratedTypeBuilder>> genTypeBuilders) {
370 if (schemaNodes != null && typeBuilder != null) {
371 for (final DataSchemaNode schemaNode : schemaNodes) {
372 if (!schemaNode.isAugmenting()) {
373 addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module, genCtx,
374 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
382 * Instantiates generated type builder with <code>packageName</code> and
383 * <code>schemaNode</code>.
385 * The new builder always implements
386 * {@link TreeNode TreeNode}.<br>
387 * If <code>schemaNode</code> is instance of GroupingDefinition it also
388 * implements {@link Augmentable
390 * If <code>schemaNode</code> is instance of
391 * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
392 * DataNodeContainer} it can also implement nodes which are specified in
396 * string with the name of the package to which
397 * <code>schemaNode</code> belongs.
399 * schema node for which is created generated type builder
401 * parent type (can be null)
402 * @param schemaContext schema context
403 * @return generated type builder <code>schemaNode</code>
405 private static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
406 schemaNode, final Type parent, final Module module, final Map<Module, ModuleContext> genCtx,
407 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<String, Map<String,
408 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
410 GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, schemaContext, "",
411 verboseClassComments, genTypeBuilders);
412 if (parent == null) {
413 it.addImplementsType(BindingTypes.TREE_NODE);
415 if (parent instanceof ListSchemaNode) {
416 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
417 (BindingTypes.IDENTIFIABLE_ITEM, parent)));
419 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
420 (BindingTypes.ITEM, parent)));
421 it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
425 if (!(schemaNode instanceof GroupingDefinition)) {
426 it.addImplementsType(BindingTypes.augmentable(it));
429 if (schemaNode instanceof DataNodeContainer) {
430 groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings(), genCtx, schemaContext,
431 verboseClassComments, genTypeBuilders, typeProvider);
432 it = addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it, genCtx);
438 static GeneratedTypeBuilder resolveNotification(final GeneratedTypeBuilder listenerInterface, String
439 parentName, final String basePackageName, final NotificationDefinition notification, final Module module,
440 final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>>
441 genTypeBuilders, TypeProvider typeProvider, Map<Module, ModuleContext> genCtx) {
443 processUsesAugments(schemaContext, notification, module, genCtx, genTypeBuilders,
444 verboseClassComments, typeProvider);
446 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition
447 (basePackageName, notification, null, module, genCtx, schemaContext,
448 verboseClassComments, genTypeBuilders, typeProvider);
449 annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
450 notificationInterface.addImplementsType(NOTIFICATION);
451 genCtx.get(module).addChildNodeType(notification, notificationInterface);
453 // Notification object
454 resolveDataSchemaNodes(module, basePackageName, notificationInterface,
455 notificationInterface, notification.getChildNodes(), genCtx, schemaContext,
456 verboseClassComments, genTypeBuilders, typeProvider);
458 //in case of tied notification, incorporate parent's localName
459 final StringBuilder sb = new StringBuilder("on_");
460 if (parentName != null) {
461 sb.append(parentName).append('_');
463 sb.append(notificationInterface.getName());
465 listenerInterface.addMethod(JavaIdentifierNormalizer.normalizeSpecificIdentifier(sb.toString(), JavaIdentifier.METHOD))
466 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
467 .setComment(encodeAngleBrackets(notification.getDescription())).setReturnType(Types.VOID);
468 return listenerInterface;
472 * Returns reference to generated type builder for specified
473 * <code>schemaNode</code> with <code>packageName</code>.
475 * Firstly the generated type builder is searched in
476 * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
477 * found it is created and added to <code>genTypeBuilders</code>.
480 * string with the package name to which returning generated type
483 * schema node which provide data about the schema node name
484 * @param schemaContext schema context
486 * return type name prefix
487 * @return generated type builder for <code>schemaNode</code>
488 * @throws IllegalArgumentException
490 * <li>if <code>schemaNode</code> is null</li>
491 * <li>if <code>packageName</code> is null</li>
492 * <li>if QName of schema node is null</li>
493 * <li>if schemaNode name is null</li>
497 static GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
498 final SchemaContext schemaContext, final String prefix, final boolean verboseClassComments,
499 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
501 Preconditions.checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
502 Preconditions.checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
503 String schemaNodeName = schemaNode.getQName().getLocalName();
504 Preconditions.checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
506 if (prefix != null && !prefix.isEmpty()) {
507 // underscore used as separator for distinction of class name parts
508 schemaNodeName = new StringBuilder(prefix).append('_').append(schemaNodeName).toString();
511 final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, schemaNodeName);
512 final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
513 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
514 newType.addComment(schemaNode.getDescription());
515 newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName(), schemaContext, verboseClassComments));
516 newType.setReference(schemaNode.getReference());
517 newType.setSchemaPath((List<QName>) schemaNode.getPath().getPathFromRoot());
518 newType.setModuleName(module.getName());
520 if (!genTypeBuilders.containsKey(packageName)) {
521 final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
522 builders.put(newType.getName(), newType);
523 genTypeBuilders.put(packageName, builders);
525 final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
526 if (!builders.containsKey(newType.getName())) {
527 builders.put(newType.getName(), newType);
534 private static void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
535 final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module,
536 final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
537 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
539 if (node != null && typeBuilder != null) {
540 if (node instanceof ContainerSchemaNode) {
541 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node,
542 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
543 } else if (node instanceof LeafListSchemaNode) {
544 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) node, module,
545 typeProvider, genCtx);
546 } else if (node instanceof LeafSchemaNode) {
547 resolveLeafSchemaNodeAsMethod("", schemaContext, typeBuilder, genCtx, (LeafSchemaNode) node, module,
549 } else if (node instanceof ListSchemaNode) {
550 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node, schemaContext,
551 verboseClassComments, genCtx, genTypeBuilders, typeProvider);
552 } else if (node instanceof ChoiceSchemaNode) {
553 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, childOf,
554 (ChoiceSchemaNode) node, genTypeBuilders, genCtx, typeProvider);
555 } else if (node instanceof AnyXmlSchemaNode || node instanceof AnyDataSchemaNode) {
556 resolveAnyNodeAsMethod(schemaContext, typeBuilder, genCtx, node, module, typeProvider);
562 * Converts <code>choiceNode</code> to the list of generated types for
563 * choice and its cases.
565 * The package names for choice and for its cases are created as
566 * concatenation of the module package (<code>basePackageName</code>) and
567 * names of all parents node.
571 * @param basePackageName
572 * string with the module package name
576 * choice node which is mapped to generated type. Also child
577 * nodes - cases are mapped to generated types.
578 * @throws IllegalArgumentException
580 * <li>if <code>basePackageName</code> is null</li>
581 * <li>if <code>choiceNode</code> is null</li>
584 private static void choiceToGenType(final Module module, final SchemaContext schemaContext, final boolean
585 verboseClasssComments, final String basePackageName, final GeneratedTypeBuilder parent, final
586 ChoiceSchemaNode choiceNode, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
587 final Map<Module, ModuleContext> genCtx, final TypeProvider typeProvider) {
588 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
589 checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
591 if (!choiceNode.isAddedByUses()) {
592 final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath(),
593 BindingNamespaceType.Data);
594 final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode,
595 schemaContext, "", verboseClasssComments, genTypeBuilders);
596 constructGetter(parent, choiceNode.getQName().getLocalName(),
597 choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
598 choiceTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, choiceTypeBuilder));
599 annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
600 genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
601 generateTypesFromChoiceCases(module, schemaContext, genCtx, basePackageName, choiceTypeBuilder.toInstance(),
602 choiceNode, verboseClasssComments, typeProvider, genTypeBuilders);
606 private static void containerToGenType(final Module module, final String basePackageName,
607 final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node,
608 final SchemaContext schemaContext, final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
609 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
611 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
612 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
613 if (genType != null) {
614 constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType, node.getStatus());
615 resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes(), genCtx,
616 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
620 private static void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder
621 parent, final GeneratedTypeBuilder childOf, final ListSchemaNode node, final SchemaContext schemaContext,
622 final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
623 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
625 final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
626 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
627 if (genType != null) {
628 final String nodeName = node.getQName().getLocalName();
629 constructGetter(parent, nodeName, node.getDescription(), Types.listTypeFor(genType), node.getStatus());
630 final List<QName> listKeys = node.getKeyDefinition();
631 final String packageName = new StringBuilder(packageNameForGeneratedType(basePackageName, node.getPath(),
632 BindingNamespaceType.Key)).append('.').append(nodeName).toString();
634 final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
636 for (final DataSchemaNode schemaNode : node.getChildNodes()) {
637 if (!schemaNode.isAugmenting()) {
638 addSchemaNodeToListBuilders(nodeName, basePackageName, schemaNode, genType, genTOBuilder, listKeys,
639 module, typeProvider, schemaContext, genCtx, genTypeBuilders, verboseClassComments);
644 if (genTOBuilder != null) {
645 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
646 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
647 genTOBuilder.setSUID(prop);
650 typeBuildersToGenTypes(module, genType, genTOBuilder, genCtx);
654 private static void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
655 final GeneratedTOBuilder genTOBuilder, final Map<Module, ModuleContext> genCtx) {
656 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
657 if (genTOBuilder != null) {
658 final GeneratedTransferObject genTO = genTOBuilder.toInstance();
659 constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
660 genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
665 * Converts <code>leaf</code> to the getter method which is added to
666 * <code>typeBuilder</code>.
669 * generated type builder to which is added getter method as
670 * <code>leaf</code> mapping
672 * leaf schema node which is mapped as getter method which is
673 * added to <code>typeBuilder</code>
675 * Module in which type was defined
676 * @return boolean value
678 * <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
680 * <li>true - in other cases</li>
683 private static Type resolveLeafSchemaNodeAsMethod(final String nodeName, final SchemaContext schemaContext,
684 final GeneratedTypeBuilder typeBuilder, final Map<Module, ModuleContext> genCtx, final LeafSchemaNode leaf,
685 final Module module, final TypeProvider typeProvider) {
686 if (leaf == null || typeBuilder == null || leaf.isAddedByUses()) {
690 final String leafName = leaf.getQName().getLocalName();
691 if (leafName == null) {
695 final Module parentModule = findParentModule(schemaContext, leaf);
696 Type returnType = null;
698 final TypeDefinition<?> typeDef = leaf.getType();
699 if (isInnerType(leaf, typeDef)) {
700 if (typeDef instanceof EnumTypeDefinition) {
701 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
702 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
703 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
704 genCtx, typeBuilder, module);
705 if (enumBuilder != null) {
706 returnType = enumBuilder.toInstance(typeBuilder);
708 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
709 } else if (typeDef instanceof UnionTypeDefinition) {
710 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
711 typeProvider, schemaContext);
712 if (genTOBuilder != null) {
713 //TODO: https://bugs.opendaylight.org/show_bug.cgi?id=2289
714 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
716 } else if (typeDef instanceof BitsTypeDefinition) {
717 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
718 typeProvider, schemaContext);
719 if (genTOBuilder != null) {
720 returnType = genTOBuilder.toInstance();
723 // It is constrained version of already declared type (inner declared type exists,
724 // onlyfor special cases (Enum, Union, Bits), which were already checked.
725 // In order to get proper class we need to look up closest derived type
726 // and apply restrictions from leaf type
727 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
728 returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
732 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
733 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
736 if (returnType == null) {
740 if (typeDef instanceof EnumTypeDefinition) {
741 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
744 String leafDesc = leaf.getDescription();
745 if (leafDesc == null) {
749 final String leafGetterName;
750 if (!"".equals(nodeName)) {
751 StringBuilder sb = new StringBuilder(nodeName)
754 leafGetterName = sb.toString();
756 leafGetterName = leafName;
759 constructGetter(typeBuilder, leafGetterName, leafDesc, returnType, leaf.getStatus());
764 * Converts <code>node</code> leaf list schema node to getter method of
765 * <code>typeBuilder</code>.
768 * generated type builder to which is <code>node</code> added as
771 * leaf list schema node which is added to
772 * <code>typeBuilder</code> as getter method
773 * @param module module
774 * @param typeProvider type provider instance
775 * @param genCtx actual generated context
776 * @param genCtx actual generated context
777 * @return boolean value
779 * <li>true - if <code>node</code>, <code>typeBuilder</code>,
780 * nodeName equal null or <code>node</code> is added by <i>uses</i></li>
781 * <li>false - other cases</li>
784 private static boolean resolveLeafListSchemaNode(final SchemaContext schemaContext, final GeneratedTypeBuilder
785 typeBuilder, final LeafListSchemaNode node, final Module module, final TypeProvider typeProvider,
786 final Map<Module, ModuleContext> genCtx) {
787 if (node == null || typeBuilder == null || node.isAddedByUses()) {
791 final QName nodeName = node.getQName();
793 final TypeDefinition<?> typeDef = node.getType();
794 final Module parentModule = findParentModule(schemaContext, node);
796 Type returnType = null;
797 if (typeDef.getBaseType() == null) {
798 if (typeDef instanceof EnumTypeDefinition) {
799 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
800 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
801 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
802 genCtx, typeBuilder, module);
803 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
804 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
805 } else if (typeDef instanceof UnionTypeDefinition) {
806 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
807 typeProvider, schemaContext);
808 if (genTOBuilder != null) {
809 returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
811 } else if (typeDef instanceof BitsTypeDefinition) {
812 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
813 typeProvider, schemaContext);
814 returnType = genTOBuilder.toInstance();
816 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
817 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
820 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
821 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
824 final ParameterizedType listType = Types.listTypeFor(returnType);
825 constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType, node.getStatus());
830 * Converts <code>caseNodes</code> set to list of corresponding generated
833 * For every <i>case</i> which isn't added through augment or <i>uses</i> is
834 * created generated type builder. The package names for the builder is
835 * created as concatenation of the module package (
836 * <code>basePackageName</code>) and names of all parents nodes of the
837 * concrete <i>case</i>. There is also relation "<i>implements type</i>"
838 * between every case builder and <i>choice</i> type
842 * @param schemaContext
843 * current schema context
845 * actual generated context
846 * @param basePackageName
847 * string with the module package name
848 * @param refChoiceType
849 * type which represents superior <i>case</i>
851 * choice case node which is mapped to generated type
852 * @param verboseClassComments
853 * Javadoc verbosity switch
854 * @throws IllegalArgumentException
856 * <li>if <code>basePackageName</code> equals null</li>
857 * <li>if <code>refChoiceType</code> equals null</li>
858 * <li>if <code>caseNodes</code> equals null</li>
861 private static void generateTypesFromChoiceCases(final Module module, final SchemaContext schemaContext,
862 final Map<Module, ModuleContext> genCtx, final String basePackageName, final Type refChoiceType,
863 final ChoiceSchemaNode choiceNode, final boolean verboseClassComments, final TypeProvider typeProvider,
864 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
865 checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
866 checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
867 checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
869 final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
870 if (caseNodes == null) {
874 for (final ChoiceCaseNode caseNode : caseNodes) {
875 if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
876 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath(),
877 BindingNamespaceType.Data);
878 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode,
879 module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
880 caseTypeBuilder.addImplementsType(refChoiceType);
881 caseTypeBuilder.setParentTypeForBuilder(refChoiceType);
882 annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder);
883 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
884 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
885 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
886 if (caseChildNodes != null) {
887 final SchemaPath choiceNodeParentPath = choiceNode.getPath().getParent();
889 if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
890 SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
892 if (parent instanceof AugmentationSchema) {
893 final AugmentationSchema augSchema = (AugmentationSchema) parent;
894 final SchemaPath targetPath = augSchema.getTargetPath();
895 SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
896 if (targetSchemaNode instanceof DataSchemaNode
897 && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
898 if (targetSchemaNode instanceof DerivableSchemaNode) {
899 targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
901 if (targetSchemaNode == null) {
902 throw new IllegalStateException(
903 "Failed to find target node from grouping for augmentation " + augSchema
904 + " in module " + module.getName());
907 parent = targetSchemaNode;
910 Preconditions.checkState(parent != null, "Could not find Choice node parent %s",
911 choiceNodeParentPath);
912 GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath(), genCtx);
913 if (childOfType == null) {
914 childOfType = findGroupingByPath(parent.getPath(), genCtx);
916 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes,
917 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
919 resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, moduleToDataType(module,
920 genCtx, verboseClassComments), caseChildNodes, genCtx, schemaContext,
921 verboseClassComments, genTypeBuilders, typeProvider);
925 processUsesAugments(schemaContext, caseNode, module, genCtx, genTypeBuilders, verboseClassComments,
930 private static Type resolveAnyNodeAsMethod(final SchemaContext schemaContext, final GeneratedTypeBuilder
931 typeBuilder, final Map<Module, ModuleContext> genCtx, final DataSchemaNode node, final Module module,
932 final TypeProvider typeProvider) {
934 final String anyName = node.getQName().getLocalName();
935 if (anyName == null) {
939 String anyDesc = node.getDescription();
940 if (anyDesc == null) {
944 Type returnType = Types.DOCUMENT;
946 constructGetter(typeBuilder, anyName, anyDesc, returnType, node.getStatus());
951 * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
952 * or to <code>genTOBuilder</code> as property.
955 * string contains the name of list
956 * @param basePackageName
957 * string contains the module package name
959 * data schema node which should be added as getter method to
960 * <code>typeBuilder</code> or as a property to
961 * <code>genTOBuilder</code> if is part of the list key
963 * generated type builder for the list schema node
964 * @param genTOBuilder
965 * generated TO builder for the list keys
967 * list of string which contains QNames of the list keys
970 * @param typeProvider
971 * provider that defines contract for generated types
972 * @param schemaContext
975 * map of generated entities in context of YANG modules
976 * @param genTypeBuilders
977 * map of generated type builders
978 * @param verboseClassComments
979 * generate verbose comments
980 * @throws IllegalArgumentException
982 * <li>if <code>schemaNode</code> equals null</li>
983 * <li>if <code>typeBuilder</code> equals null</li>
986 private static void addSchemaNodeToListBuilders(final String nodeName, final String basePackageName,
987 final DataSchemaNode schemaNode, final GeneratedTypeBuilder typeBuilder,
988 final GeneratedTOBuilder genTOBuilder, final List<QName> listKeys, final Module module,
989 final TypeProvider typeProvider, final SchemaContext schemaContext, final Map<Module, ModuleContext> genCtx,
990 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final boolean verboseClassComments) {
991 checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
992 checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
994 if (schemaNode instanceof LeafSchemaNode) {
995 final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
996 final QName leafQName = leaf.getQName();
998 final Type type = resolveLeafSchemaNodeAsMethod(nodeName, schemaContext, typeBuilder, genCtx, leaf, module,
1000 if (listKeys.contains(leafQName)) {
1002 resolveLeafSchemaNodeAsProperty(schemaContext, typeProvider, genCtx, genTOBuilder, leaf, true,
1005 AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1008 } else if (!schemaNode.isAddedByUses()) {
1009 if (schemaNode instanceof LeafListSchemaNode) {
1010 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) schemaNode, module,
1011 typeProvider, genCtx);
1012 } else if (schemaNode instanceof ContainerSchemaNode) {
1013 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode,
1014 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1015 } else if (schemaNode instanceof ListSchemaNode) {
1016 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode,
1017 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1018 } else if (schemaNode instanceof ChoiceSchemaNode) {
1019 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, typeBuilder,
1020 (ChoiceSchemaNode) schemaNode, genTypeBuilders, genCtx, typeProvider);
1025 private static boolean resolveLeafSchemaNodeAsProperty(final SchemaContext schemaContext, final TypeProvider
1026 typeProvider, final Map<Module, ModuleContext> genCtx, final GeneratedTOBuilder
1027 toBuilder, final LeafSchemaNode leaf, final boolean isReadOnly, final Module module) {
1029 if (leaf != null && toBuilder != null) {
1031 final TypeDefinition<?> typeDef = leaf.getType();
1032 if (typeDef instanceof UnionTypeDefinition) {
1033 // GeneratedType for this type definition should be already
1035 final QName qname = typeDef.getQName();
1036 final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1037 qname.getRevision());
1038 final ModuleContext mc = genCtx.get(unionModule);
1039 returnType = mc.getTypedefs().get(typeDef.getPath());
1040 } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
1041 // Annonymous enumeration (already generated, since it is inherited via uses).
1042 LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
1043 QName qname = originalLeaf.getQName();
1044 final Module enumModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1045 qname.getRevision());
1046 returnType = genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
1048 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1050 return AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1055 private static TypeDefinition<?> getBaseOrDeclaredType(final TypeDefinition<?> typeDef) {
1056 final TypeDefinition<?> baseType = typeDef.getBaseType();
1057 return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
1060 @SuppressWarnings({ "rawtypes", "unchecked" })
1061 private static GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
1062 final GeneratedTypeBuilder childOf, final DataSchemaNode node, final SchemaContext schemaContext,
1063 final boolean verboseClassComments, Map<Module, ModuleContext> genCtx, final Map<String, Map<String,
1064 GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1066 if (node.isAugmenting() || node.isAddedByUses()) {
1069 final String packageName = packageNameForGeneratedType(basePackageName, node.getPath(), BindingNamespaceType.Data);
1070 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf, module,
1071 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1072 genType.addComment(node.getDescription());
1073 annotateDeprecatedIfNecessary(node.getStatus(), genType);
1074 genType.setDescription(createDescription(node, genType.getFullyQualifiedName(), schemaContext, verboseClassComments));
1075 genType.setModuleName(module.getName());
1076 genType.setReference(node.getReference());
1077 genType.setSchemaPath((List) node.getPath().getPathFromRoot());
1078 genType.setParentTypeForBuilder(childOf);
1079 if (node instanceof DataNodeContainer) {
1080 genCtx.get(module).addChildNodeType(node, genType);
1081 genCtx = groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings(), genCtx, schemaContext,
1082 verboseClassComments, genTypeBuilders, typeProvider);
1083 processUsesAugments(schemaContext, (DataNodeContainer) node, module, genCtx, genTypeBuilders,
1084 verboseClassComments, typeProvider);
1090 * Converts all <b>groupings</b> of the module to the list of
1091 * <code>Type</code> objects. Firstly are groupings sorted according mutual
1092 * dependencies. At least dependent (independent) groupings are in the list
1093 * saved at first positions. For every grouping the record is added to map
1094 * {@link ModuleContext#groupings allGroupings}
1099 * collection of groupings from which types will be generated
1100 * @param typeProvider
1101 * provider that defines contract for generated types
1102 * @param schemaContext
1105 * map of generated entities in context of YANG modules
1106 * @param genTypeBuilders
1107 * map of generated type builders
1108 * @param verboseClassComments
1109 * generate verbose comments
1112 static Map<Module, ModuleContext> groupingsToGenTypes(final Module module, final Collection<GroupingDefinition>
1113 groupings, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1114 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1115 final String basePackageName = BindingMapping.getRootPackageName(module);
1116 final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
1118 for (final GroupingDefinition grouping : groupingsSortedByDependencies) {
1119 genCtx = groupingToGenType(basePackageName, grouping, module, genCtx, schemaContext,
1120 verboseClassComments, genTypeBuilders, typeProvider);
1126 * Converts individual grouping to GeneratedType. Firstly generated type
1127 * builder is created and every child node of grouping is resolved to the
1130 * @param basePackageName
1131 * string contains the module package name
1133 * GroupingDefinition which contains data about grouping
1136 * @param typeProvider
1137 * provider that defines contract for generated types
1138 * @param schemaContext
1141 * map of generated entities in context of YANG modules
1142 * @param genTypeBuilders
1143 * map of generated type builders
1144 * @param verboseClassComments
1145 * generate verbose comments
1147 * @return GeneratedType which is generated from grouping (object of type
1148 * <code>GroupingDefinition</code>)
1150 private static Map<Module, ModuleContext> groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module
1151 module, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1152 verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1153 final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath(), BindingNamespaceType.Grouping);
1154 final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping, module, genCtx,
1155 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1156 annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
1157 genCtx.get(module).addGroupingType(grouping.getPath(), genType);
1158 resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes(), genCtx,
1159 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1160 genCtx = groupingsToGenTypes(module, grouping.getGroupings(), genCtx, schemaContext, verboseClassComments,
1161 genTypeBuilders, typeProvider);
1162 genCtx = processUsesAugments(schemaContext, grouping, module, genCtx, genTypeBuilders, verboseClassComments,
1168 * //TODO: add information about multiple base identities in YANG 1.1
1169 * Converts the <b>identity</b> object to GeneratedType. Firstly it is
1170 * created transport object builder. If identity contains base identity then
1171 * reference to base identity is added to superior identity as its extend.
1172 * If identity doesn't contain base identity then only reference to abstract
1173 * class {@link org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode
1174 * BaseIdentity} is added
1178 * @param basePackageName
1179 * string contains the module package name
1181 * IdentitySchemaNode which contains data about identity
1182 * @param schemaContext
1183 * SchemaContext which is used to get package and name
1184 * information about base of identity
1185 * @param genCtx generated context
1186 * @return returns generated context
1188 static Map<Module, ModuleContext> identityToGenType(final Module module, final String basePackageName,
1189 final IdentitySchemaNode identity, final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx,
1190 boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
1191 final TypeProvider typeProvider, Map<QName, GeneratedTOBuilderImpl> generatedIdentities) {
1193 final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
1194 BindingNamespaceType.Identity);
1195 final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName,
1196 identity.getQName().getLocalName());
1198 final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
1199 if (baseIdentities.size() == 0) {
1200 //no base - abstract
1201 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
1202 BaseIdentity.class.getSimpleName());
1203 newType.setExtendsType(gto.toInstance());
1204 generatedIdentities.put(identity.getQName(), newType);
1206 //one base - inheritance
1207 final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
1208 final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(schemaContext, baseIdentity);
1209 final String returnTypePkgName = new StringBuilder(BindingMapping.getRootPackageName
1210 (baseIdentityParentModule))
1212 .append(BindingNamespaceType.Identity.getPackagePrefix())
1215 final GeneratedTOBuilderImpl existingIdentityGto = generatedIdentities.get(baseIdentity.getQName());
1216 if (existingIdentityGto != null) {
1217 newType.setExtendsType(existingIdentityGto.toInstance());
1219 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(returnTypePkgName,
1220 baseIdentity.getQName().getLocalName());
1221 newType.setExtendsType(gto.toInstance());
1222 generatedIdentities.put(baseIdentity.getQName(), gto);
1225 //FIXME: more bases - possible composition, multiple inheritance not possible
1227 generatedIdentities.put(identity.getQName(), newType);
1229 newType.setAbstract(true);
1230 newType.addComment(identity.getDescription());
1231 newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
1232 verboseClassComments));
1233 newType.setReference(identity.getReference());
1234 newType.setModuleName(module.getName());
1235 newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
1237 qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
1239 genCtx.get(module).addIdentityType(identity.getQName(), newType);