Binding generator v2 - Augments fix
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / GenHelperUtil.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.binding.javav2.generator.impl;
10
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;
30
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;
37 import java.util.Map;
38 import java.util.Set;
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;
95
96 /**
97  * Helper util class used for generation of types in Binding spec v2.
98  */
99 @Beta
100 final class GenHelperUtil {
101
102     private GenHelperUtil() {
103         throw new UnsupportedOperationException("Util class");
104     }
105
106     /**
107      * Create GeneratedTypeBuilder object from module argument.
108      *
109      * @param module
110      *            Module object from which builder will be created
111      * @param genCtx
112      * @param verboseClassComments
113      *
114      * @return <code>GeneratedTypeBuilder</code> which is internal
115      *         representation of the module
116      * @throws IllegalArgumentException
117      *             if module is null
118      */
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.");
121
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;
129     }
130
131     /**
132      * Generates type builder for <code>module</code>.
133      *
134      * @param module
135      *            Module which is source of package name for generated type
136      *            builder
137      * @param postfix
138      *            string which is added to the module class name representation
139      *            as suffix
140      * @param verboseClassComments
141      * @return instance of GeneratedTypeBuilder which represents
142      *         <code>module</code>.
143      * @throws IllegalArgumentException
144      *             if <code>module</code> is null
145      */
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();
152
153         final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
154         moduleBuilder.setDescription(createDescription(module, verboseClassComments));
155         moduleBuilder.setReference(module.getReference());
156         moduleBuilder.setModuleName(moduleName);
157
158         return moduleBuilder;
159     }
160
161     /**
162      * Adds the implemented types to type builder.
163      *
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>
169      *
170      * @param dataNodeContainer
171      *            element which contains the list of used YANG groupings
172      * @param builder
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
177      */
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());
185             }
186             builder.addImplementsType(genType);
187         }
188         return builder;
189     }
190
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) {
195                 return result;
196             }
197         }
198         return null;
199      }
200
201     /**
202      * Adds the methods to <code>typeBuilder</code> which represent subnodes of
203      * node for which <code>typeBuilder</code> was created.
204      *
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).
207      *
208      * @param module
209      *            current module
210      * @param basePackageName
211      *            string contains the module package name
212      * @param parent
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,
216      *            container, choice.
217      * @param childOf
218      *            parent type
219      * @param schemaNodes
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
224      *         added to it.
225      */
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) {
232
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);
238                 }
239             }
240         }
241         return parent;
242     }
243
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);
250     }
251
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);
267                 }
268             }
269         }
270         return genCtx;
271     }
272
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) {
277                 return result;
278             }
279         }
280         return null;
281     }
282
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) {
287                 return result;
288             }
289         }
290         return null;
291     }
292
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) {
297
298         //pick augmentation grouped by augmentation target, there is always at least one
299         final AugmentationSchema augSchema = schemaPathAugmentListEntry.get(0);
300
301         Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.computeIfAbsent(
302                 augmentPackageName, k -> new HashMap<>());
303
304         //this requires valid semantics in YANG model
305         String augIdentifier = null;
306         for (AugmentationSchema aug : schemaPathAugmentListEntry) {
307             augIdentifier = getAugmentIdentifier(aug.getUnknownSchemaNodes());
308             break;
309         }
310
311         boolean isTypeNormalized = false;
312         if (augIdentifier == null) {
313             augIdentifier = augGenTypeName(augmentBuilders, targetTypeRef.getName());
314             isTypeNormalized = true;
315         }
316
317         GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augIdentifier,
318                 false, isTypeNormalized);
319
320         augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
321         augTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, augTypeBuilder));
322         augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
323         annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
324
325         //produces getters for augTypeBuilder eventually
326         for (AugmentationSchema aug : schemaPathAugmentListEntry) {
327             //apply all uses
328             addImplementedInterfaceFromUses(aug, augTypeBuilder, genCtx);
329             augSchemaNodeToMethods(module, BindingMapping.getRootPackageName(module), augTypeBuilder, augTypeBuilder, aug.getChildNodes(),
330                genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders);
331         }
332
333         augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
334
335         if(!augSchema.getChildNodes().isEmpty()) {
336             genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
337             genCtx.get(module).addTargetToAugmentation(augTypeBuilder, augSchema.getTargetPath());
338         }
339         genCtx.get(module).addAugmentType(augTypeBuilder);
340         return genCtx;
341     }
342
343     /**
344      * Adds the methods to <code>typeBuilder</code> what represents subnodes of
345      * node for which <code>typeBuilder</code> was created.
346      *
347      * @param module
348      *            current module
349      * @param basePackageName
350      *            string contains the module package name
351      * @param typeBuilder
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,
355      *            container, choice.
356      * @param childOf
357      *            parent type
358      * @param schemaNodes
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
363      *         added to it.
364      */
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);
375                 }
376             }
377         }
378         return typeBuilder;
379     }
380
381     /**
382      * Instantiates generated type builder with <code>packageName</code> and
383      * <code>schemaNode</code>.
384      *
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
389      * Augmentable}.<br>
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
393      * <i>uses</i>.
394      *
395      * @param packageName
396      *            string with the name of the package to which
397      *            <code>schemaNode</code> belongs.
398      * @param schemaNode
399      *            schema node for which is created generated type builder
400      * @param parent
401      *            parent type (can be null)
402      * @param schemaContext schema context
403      * @return generated type builder <code>schemaNode</code>
404      */
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) {
409
410         GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, schemaContext, "",
411                 verboseClassComments, genTypeBuilders);
412         if (parent == null) {
413             it.addImplementsType(BindingTypes.TREE_NODE);
414         } else {
415             if (parent instanceof ListSchemaNode) {
416                 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
417                         (BindingTypes.IDENTIFIABLE_ITEM, parent)));
418             } else {
419                 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
420                         (BindingTypes.ITEM, parent)));
421                 it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
422             }
423         }
424
425         if (!(schemaNode instanceof GroupingDefinition)) {
426             it.addImplementsType(BindingTypes.augmentable(it));
427         }
428
429         if (schemaNode instanceof DataNodeContainer) {
430             groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings(), genCtx, schemaContext,
431                     verboseClassComments, genTypeBuilders, typeProvider);
432             it = addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it, genCtx);
433         }
434
435         return it;
436     }
437
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) {
442
443         processUsesAugments(schemaContext, notification, module, genCtx, genTypeBuilders,
444                 verboseClassComments, typeProvider);
445
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);
452
453         // Notification object
454         resolveDataSchemaNodes(module, basePackageName, notificationInterface,
455                 notificationInterface, notification.getChildNodes(), genCtx, schemaContext,
456                 verboseClassComments, genTypeBuilders, typeProvider);
457
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('_');
462         }
463         sb.append(notificationInterface.getName());
464
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;
469     }
470
471     /**
472      * Returns reference to generated type builder for specified
473      * <code>schemaNode</code> with <code>packageName</code>.
474      *
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>.
478      *
479      * @param packageName
480      *            string with the package name to which returning generated type
481      *            builder belongs
482      * @param schemaNode
483      *            schema node which provide data about the schema node name
484      * @param schemaContext schema context
485      * @param prefix
486      *            return type name prefix
487      * @return generated type builder for <code>schemaNode</code>
488      * @throws IllegalArgumentException
489      *             <ul>
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>
494      *             </ul>
495      *
496      */
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) {
500
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.");
505
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();
509         }
510
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());
519
520         if (!genTypeBuilders.containsKey(packageName)) {
521             final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
522             builders.put(newType.getName(), newType);
523             genTypeBuilders.put(packageName, builders);
524         } else {
525             final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
526             if (!builders.containsKey(newType.getName())) {
527                 builders.put(newType.getName(), newType);
528             }
529         }
530         return newType;
531
532     }
533
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) {
538
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,
548                         typeProvider);
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);
557             }
558         }
559     }
560
561     /**
562      * Converts <code>choiceNode</code> to the list of generated types for
563      * choice and its cases.
564      *
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.
568      *
569      * @param module
570      *            current module
571      * @param basePackageName
572      *            string with the module package name
573      * @param parent
574      *            parent type
575      * @param choiceNode
576      *            choice node which is mapped to generated type. Also child
577      *            nodes - cases are mapped to generated types.
578      * @throws IllegalArgumentException
579      *             <ul>
580      *             <li>if <code>basePackageName</code> is null</li>
581      *             <li>if <code>choiceNode</code> is null</li>
582      *             </ul>
583      */
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.");
590
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);
603         }
604     }
605
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) {
610
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);
617         }
618     }
619
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) {
624
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();
633
634             final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
635
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);
640                 }
641             }
642
643             // serialVersionUID
644             if (genTOBuilder != null) {
645                 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
646                 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
647                 genTOBuilder.setSUID(prop);
648             }
649
650             typeBuildersToGenTypes(module, genType, genTOBuilder, genCtx);
651         }
652     }
653
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);
661         }
662     }
663
664     /**
665      * Converts <code>leaf</code> to the getter method which is added to
666      * <code>typeBuilder</code>.
667      *
668      * @param typeBuilder
669      *            generated type builder to which is added getter method as
670      *            <code>leaf</code> mapping
671      * @param leaf
672      *            leaf schema node which is mapped as getter method which is
673      *            added to <code>typeBuilder</code>
674      * @param module
675      *            Module in which type was defined
676      * @return boolean value
677      *         <ul>
678      *         <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
679      *         null</li>
680      *         <li>true - in other cases</li>
681      *         </ul>
682      */
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()) {
687             return null;
688         }
689
690         final String leafName = leaf.getQName().getLocalName();
691         if (leafName == null) {
692             return null;
693         }
694
695         final Module parentModule = findParentModule(schemaContext, leaf);
696         Type returnType = null;
697
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);
707                 }
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);
715                 }
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();
721                 }
722             } else {
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,
729                         restrictions);
730             }
731         } else {
732             final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
733             returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
734         }
735
736         if (returnType == null) {
737             return null;
738         }
739
740         if (typeDef instanceof EnumTypeDefinition) {
741             ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
742         }
743
744         String leafDesc = leaf.getDescription();
745         if (leafDesc == null) {
746             leafDesc = "";
747         }
748
749         final String leafGetterName;
750         if (!"".equals(nodeName)) {
751             StringBuilder sb = new StringBuilder(nodeName)
752                 .append('_')
753                 .append(leafName);
754             leafGetterName = sb.toString();
755         } else {
756             leafGetterName = leafName;
757         }
758
759         constructGetter(typeBuilder, leafGetterName, leafDesc, returnType, leaf.getStatus());
760         return returnType;
761     }
762
763     /**
764      * Converts <code>node</code> leaf list schema node to getter method of
765      * <code>typeBuilder</code>.
766      *
767      * @param typeBuilder
768      *            generated type builder to which is <code>node</code> added as
769      *            getter method
770      * @param node
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
778      *         <ul>
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>
782      *         </ul>
783      */
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()) {
788             return false;
789         }
790
791         final QName nodeName = node.getQName();
792
793         final TypeDefinition<?> typeDef = node.getType();
794         final Module parentModule = findParentModule(schemaContext, node);
795
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);
810                 }
811             } else if (typeDef instanceof BitsTypeDefinition) {
812                 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
813                         typeProvider, schemaContext);
814                 returnType = genTOBuilder.toInstance();
815             } else {
816                 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
817                 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
818             }
819         } else {
820             final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
821             returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
822         }
823
824         final ParameterizedType listType = Types.listTypeFor(returnType);
825         constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType, node.getStatus());
826         return true;
827     }
828
829     /**
830      * Converts <code>caseNodes</code> set to list of corresponding generated
831      * types.
832      *
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
839      *
840      * @param module
841      *            current module
842      * @param schemaContext
843      *            current schema context
844      * @param genCtx
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>
850      * @param choiceNode
851      *            choice case node which is mapped to generated type
852      * @param verboseClassComments
853      *            Javadoc verbosity switch
854      * @throws IllegalArgumentException
855      *             <ul>
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>
859      *             </ul>
860      */
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.");
868
869         final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
870         if (caseNodes == null) {
871             return;
872         }
873
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();
888
889                     if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
890                         SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
891
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();
900                                 }
901                                 if (targetSchemaNode == null) {
902                                     throw new IllegalStateException(
903                                             "Failed to find target node from grouping for augmentation " + augSchema
904                                                     + " in module " + module.getName());
905                                 }
906                             }
907                             parent = targetSchemaNode;
908                         }
909
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);
915                         }
916                         resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes,
917                                 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
918                     } else {
919                         resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, moduleToDataType(module,
920                                 genCtx, verboseClassComments), caseChildNodes, genCtx, schemaContext,
921                                 verboseClassComments, genTypeBuilders, typeProvider);
922                     }
923                 }
924             }
925             processUsesAugments(schemaContext, caseNode, module, genCtx, genTypeBuilders, verboseClassComments,
926                     typeProvider);
927         }
928     }
929
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) {
933
934         final String anyName = node.getQName().getLocalName();
935         if (anyName == null) {
936             return null;
937         }
938
939         String anyDesc = node.getDescription();
940         if (anyDesc == null) {
941             anyDesc = "";
942         }
943
944         Type returnType = Types.DOCUMENT;
945
946         constructGetter(typeBuilder, anyName, anyDesc, returnType, node.getStatus());
947         return returnType;
948     }
949
950     /**
951      * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
952      * or to <code>genTOBuilder</code> as property.
953      *
954      * @param nodeName
955      *            string contains the name of list
956      * @param basePackageName
957      *            string contains the module package name
958      * @param schemaNode
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
962      * @param typeBuilder
963      *            generated type builder for the list schema node
964      * @param genTOBuilder
965      *            generated TO builder for the list keys
966      * @param listKeys
967      *            list of string which contains QNames of the list keys
968      * @param module
969      *            current module
970      * @param typeProvider
971      *            provider that defines contract for generated types
972      * @param schemaContext
973      *            schema context
974      * @param genCtx
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
981      *             <ul>
982      *             <li>if <code>schemaNode</code> equals null</li>
983      *             <li>if <code>typeBuilder</code> equals null</li>
984      *             </ul>
985      */
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.");
993
994         if (schemaNode instanceof LeafSchemaNode) {
995             final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
996             final QName leafQName = leaf.getQName();
997
998             final Type type = resolveLeafSchemaNodeAsMethod(nodeName, schemaContext, typeBuilder, genCtx, leaf, module,
999                     typeProvider);
1000             if (listKeys.contains(leafQName)) {
1001                 if (type == null) {
1002                     resolveLeafSchemaNodeAsProperty(schemaContext, typeProvider, genCtx, genTOBuilder, leaf, true,
1003                         module);
1004                 } else {
1005                     AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1006                 }
1007             }
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);
1021             }
1022         }
1023     }
1024
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) {
1028
1029         if (leaf != null && toBuilder != null) {
1030             Type returnType;
1031             final TypeDefinition<?> typeDef = leaf.getType();
1032             if (typeDef instanceof UnionTypeDefinition) {
1033                 // GeneratedType for this type definition should be already
1034                 // created
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());
1047             } else {
1048                 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1049             }
1050             return AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1051         }
1052         return false;
1053     }
1054
1055     private static TypeDefinition<?> getBaseOrDeclaredType(final TypeDefinition<?> typeDef) {
1056         final TypeDefinition<?> baseType = typeDef.getBaseType();
1057         return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
1058     }
1059
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) {
1065
1066         if (node.isAugmenting() || node.isAddedByUses()) {
1067             return null;
1068         }
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);
1085         }
1086         return genType;
1087     }
1088
1089     /**
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}
1095      *
1096      * @param module
1097      *            current module
1098      * @param groupings
1099      *            collection of groupings from which types will be generated
1100      * @param typeProvider
1101      *            provider that defines contract for generated types
1102      * @param schemaContext
1103      *            schema context
1104      * @param genCtx
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
1110      *
1111      */
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()
1117                 .sort(groupings);
1118         for (final GroupingDefinition grouping : groupingsSortedByDependencies) {
1119             genCtx = groupingToGenType(basePackageName, grouping, module, genCtx, schemaContext,
1120                     verboseClassComments, genTypeBuilders, typeProvider);
1121         }
1122         return genCtx;
1123     }
1124
1125     /**
1126      * Converts individual grouping to GeneratedType. Firstly generated type
1127      * builder is created and every child node of grouping is resolved to the
1128      * method.
1129      *
1130      * @param basePackageName
1131      *            string contains the module package name
1132      * @param grouping
1133      *            GroupingDefinition which contains data about grouping
1134      * @param module
1135      *            current module
1136      * @param typeProvider
1137      *            provider that defines contract for generated types
1138      * @param schemaContext
1139      *            schema context
1140      * @param genCtx
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
1146      *
1147      * @return GeneratedType which is generated from grouping (object of type
1148      *         <code>GroupingDefinition</code>)
1149      */
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,
1163                 typeProvider);
1164         return genCtx;
1165     }
1166
1167     /**
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
1175      *
1176      * @param module
1177      *            current module
1178      * @param basePackageName
1179      *            string contains the module package name
1180      * @param identity
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
1187      */
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) {
1192
1193         final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
1194             BindingNamespaceType.Identity);
1195         final GeneratedTOBuilderImpl newType = new GeneratedTOBuilderImpl(packageName,
1196                 identity.getQName().getLocalName());
1197
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);
1205         } else {
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))
1211                     .append('.')
1212                     .append(BindingNamespaceType.Identity.getPackagePrefix())
1213                     .toString();
1214
1215             final GeneratedTOBuilderImpl existingIdentityGto = generatedIdentities.get(baseIdentity.getQName());
1216             if (existingIdentityGto != null) {
1217                 newType.setExtendsType(existingIdentityGto.toInstance());
1218             } else {
1219                 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(returnTypePkgName,
1220                         baseIdentity.getQName().getLocalName());
1221                 newType.setExtendsType(gto.toInstance());
1222                 generatedIdentities.put(baseIdentity.getQName(), gto);
1223             }
1224
1225             //FIXME: more bases - possible composition, multiple inheritance not possible
1226         }
1227         generatedIdentities.put(identity.getQName(), newType);
1228
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());
1236
1237         qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
1238
1239         genCtx.get(module).addIdentityType(identity.getQName(), newType);
1240         return genCtx;
1241     }
1242 }