Binding generator v2 - namespace fix #1
[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 generated context
112      * @param verboseClassComments verbosity switch
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 verbosity switch
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         moduleBuilder.setBasePackageName(packageName);
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         augTypeBuilder.setBasePackageName(BindingMapping.getRootPackageName(module));
324         annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
325
326         //produces getters for augTypeBuilder eventually
327         for (AugmentationSchema aug : schemaPathAugmentListEntry) {
328             //apply all uses
329             addImplementedInterfaceFromUses(aug, augTypeBuilder, genCtx);
330             augSchemaNodeToMethods(module, BindingMapping.getRootPackageName(module), augTypeBuilder, augTypeBuilder, aug.getChildNodes(),
331                genCtx, schemaContext, verboseClassComments, typeProvider, genTypeBuilders);
332         }
333
334         augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder);
335
336         if(!augSchema.getChildNodes().isEmpty()) {
337             genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
338             genCtx.get(module).addTargetToAugmentation(augTypeBuilder, augSchema.getTargetPath());
339         }
340         genCtx.get(module).addAugmentType(augTypeBuilder);
341         return genCtx;
342     }
343
344     /**
345      * Adds the methods to <code>typeBuilder</code> what represents subnodes of
346      * node for which <code>typeBuilder</code> was created.
347      *
348      * @param module
349      *            current module
350      * @param basePackageName
351      *            string contains the module package name
352      * @param typeBuilder
353      *            generated type builder which represents any node. The subnodes
354      *            of this node are added to the <code>typeBuilder</code> as
355      *            methods. The subnode can be of type leaf, leaf-list, list,
356      *            container, choice.
357      * @param childOf
358      *            parent type
359      * @param schemaNodes
360      *            set of data schema nodes which are the children of the node
361      *            for which <code>typeBuilder</code> was created
362      * @return generated type builder which is the same object as the input
363      *         parameter <code>typeBuilder</code>. The getter method could be
364      *         added to it.
365      */
366     private static GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
367             final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes,
368             final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
369             verboseClassComments, final TypeProvider typeProvider, final Map<String, Map<String,
370             GeneratedTypeBuilder>> genTypeBuilders) {
371         if (schemaNodes != null && typeBuilder != null) {
372             for (final DataSchemaNode schemaNode : schemaNodes) {
373                 if (!schemaNode.isAugmenting()) {
374                     addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module, genCtx,
375                             schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
376                 }
377             }
378         }
379         return typeBuilder;
380     }
381
382     /**
383      * Instantiates generated type builder with <code>packageName</code> and
384      * <code>schemaNode</code>.
385      *
386      * The new builder always implements
387      * {@link TreeNode TreeNode}.<br>
388      * If <code>schemaNode</code> is instance of GroupingDefinition it also
389      * implements {@link Augmentable
390      * Augmentable}.<br>
391      * If <code>schemaNode</code> is instance of
392      * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
393      * DataNodeContainer} it can also implement nodes which are specified in
394      * <i>uses</i>.
395      *
396      * @param packageName
397      *            string with the name of the package to which
398      *            <code>schemaNode</code> belongs.
399      * @param schemaNode
400      *            schema node for which is created generated type builder
401      * @param parent
402      *            parent type (can be null)
403      * @param schemaContext schema context
404      * @return generated type builder <code>schemaNode</code>
405      */
406     private static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
407             schemaNode, final Type parent, final Module module, final Map<Module, ModuleContext> genCtx,
408             final SchemaContext schemaContext, final boolean verboseClassComments, final Map<String, Map<String,
409             GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
410
411         GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, schemaContext, "",
412                 verboseClassComments, genTypeBuilders);
413         if (parent == null) {
414             it.addImplementsType(BindingTypes.TREE_NODE);
415         } else {
416             if (parent instanceof ListSchemaNode) {
417                 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
418                         (BindingTypes.IDENTIFIABLE_ITEM, parent)));
419             } else {
420                 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
421                         (BindingTypes.ITEM, parent)));
422                 it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
423             }
424         }
425
426         if (!(schemaNode instanceof GroupingDefinition)) {
427             it.addImplementsType(BindingTypes.augmentable(it));
428         }
429
430         if (schemaNode instanceof DataNodeContainer) {
431             groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings(), genCtx, schemaContext,
432                     verboseClassComments, genTypeBuilders, typeProvider);
433             it = addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it, genCtx);
434         }
435
436         return it;
437     }
438
439     static GeneratedTypeBuilder resolveNotification(final GeneratedTypeBuilder listenerInterface, String
440             parentName, final String basePackageName, final NotificationDefinition notification, final Module module,
441             final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>>
442             genTypeBuilders, TypeProvider typeProvider, Map<Module, ModuleContext> genCtx) {
443
444         processUsesAugments(schemaContext, notification, module, genCtx, genTypeBuilders,
445                 verboseClassComments, typeProvider);
446
447         final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition
448                 (basePackageName, notification, null, module, genCtx, schemaContext,
449                         verboseClassComments, genTypeBuilders, typeProvider);
450         annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
451         notificationInterface.addImplementsType(NOTIFICATION);
452         genCtx.get(module).addChildNodeType(notification, notificationInterface);
453
454         // Notification object
455         resolveDataSchemaNodes(module, basePackageName, notificationInterface,
456                 notificationInterface, notification.getChildNodes(), genCtx, schemaContext,
457                 verboseClassComments, genTypeBuilders, typeProvider);
458
459         //in case of tied notification, incorporate parent's localName
460         final StringBuilder sb = new StringBuilder("on_");
461         if (parentName != null) {
462             sb.append(parentName).append('_');
463         }
464         sb.append(notificationInterface.getName());
465
466         listenerInterface.addMethod(JavaIdentifierNormalizer.normalizeSpecificIdentifier(sb.toString(), JavaIdentifier.METHOD))
467                 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
468                 .setComment(encodeAngleBrackets(notification.getDescription())).setReturnType(Types.VOID);
469         return listenerInterface;
470     }
471
472     /**
473      * Returns reference to generated type builder for specified
474      * <code>schemaNode</code> with <code>packageName</code>.
475      *
476      * Firstly the generated type builder is searched in
477      * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
478      * found it is created and added to <code>genTypeBuilders</code>.
479      *
480      * @param packageName
481      *            string with the package name to which returning generated type
482      *            builder belongs
483      * @param schemaNode
484      *            schema node which provide data about the schema node name
485      * @param schemaContext schema context
486      * @param prefix
487      *            return type name prefix
488      * @return generated type builder for <code>schemaNode</code>
489      * @throws IllegalArgumentException
490      *             <ul>
491      *             <li>if <code>schemaNode</code> is null</li>
492      *             <li>if <code>packageName</code> is null</li>
493      *             <li>if QName of schema node is null</li>
494      *             <li>if schemaNode name is null</li>
495      *             </ul>
496      *
497      */
498     static GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
499                        final SchemaContext schemaContext, final String prefix, final boolean verboseClassComments,
500                        final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
501
502         Preconditions.checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
503         Preconditions.checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
504         String schemaNodeName = schemaNode.getQName().getLocalName();
505         Preconditions.checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
506
507         if (prefix != null && !prefix.isEmpty()) {
508             // underscore used as separator for distinction of class name parts
509             schemaNodeName = new StringBuilder(prefix).append('_').append(schemaNodeName).toString();
510         }
511
512         final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, schemaNodeName);
513         final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
514         qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
515         newType.addComment(schemaNode.getDescription());
516         newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName(), schemaContext, verboseClassComments));
517         newType.setReference(schemaNode.getReference());
518         newType.setSchemaPath((List<QName>) schemaNode.getPath().getPathFromRoot());
519         newType.setModuleName(module.getName());
520         newType.setBasePackageName(BindingMapping.getRootPackageName(module));
521
522         if (!genTypeBuilders.containsKey(packageName)) {
523             final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
524             builders.put(newType.getName(), newType);
525             genTypeBuilders.put(packageName, builders);
526         } else {
527             final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
528             if (!builders.containsKey(newType.getName())) {
529                 builders.put(newType.getName(), newType);
530             }
531         }
532         return newType;
533
534     }
535
536     private static void addSchemaNodeToBuilderAsMethod(final String basePackageName, final DataSchemaNode node,
537         final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf, final Module module,
538         final Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
539         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
540
541         if (node != null && typeBuilder != null) {
542             if (node instanceof ContainerSchemaNode) {
543                 containerToGenType(module, basePackageName, typeBuilder, childOf, (ContainerSchemaNode) node,
544                         schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
545             } else if (node instanceof LeafListSchemaNode) {
546                 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) node, module,
547                             typeProvider, genCtx);
548             } else if (node instanceof LeafSchemaNode) {
549                 resolveLeafSchemaNodeAsMethod("", schemaContext, typeBuilder, genCtx, (LeafSchemaNode) node, module,
550                         typeProvider);
551             } else if (node instanceof ListSchemaNode) {
552                 listToGenType(module, basePackageName, typeBuilder, childOf, (ListSchemaNode) node, schemaContext,
553                         verboseClassComments, genCtx, genTypeBuilders, typeProvider);
554             } else if (node instanceof ChoiceSchemaNode) {
555                 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, childOf,
556                         (ChoiceSchemaNode) node, genTypeBuilders, genCtx, typeProvider);
557             } else if (node instanceof AnyXmlSchemaNode || node instanceof AnyDataSchemaNode) {
558                 resolveAnyNodeAsMethod(schemaContext, typeBuilder, genCtx, node, module, typeProvider);
559             }
560         }
561     }
562
563     /**
564      * Converts <code>choiceNode</code> to the list of generated types for
565      * choice and its cases.
566      *
567      * The package names for choice and for its cases are created as
568      * concatenation of the module package (<code>basePackageName</code>) and
569      * names of all parents node.
570      *
571      * @param module
572      *            current module
573      * @param basePackageName
574      *            string with the module package name
575      * @param parent
576      *            parent type
577      * @param choiceNode
578      *            choice node which is mapped to generated type. Also child
579      *            nodes - cases are mapped to generated types.
580      * @throws IllegalArgumentException
581      *             <ul>
582      *             <li>if <code>basePackageName</code> is null</li>
583      *             <li>if <code>choiceNode</code> is null</li>
584      *             </ul>
585      */
586     private static void choiceToGenType(final Module module, final SchemaContext schemaContext, final boolean
587             verboseClasssComments, final String basePackageName, final GeneratedTypeBuilder parent, final
588             ChoiceSchemaNode choiceNode, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
589             final Map<Module, ModuleContext> genCtx, final TypeProvider typeProvider) {
590         checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
591         checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL.");
592
593         if (!choiceNode.isAddedByUses()) {
594             final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath(),
595                     BindingNamespaceType.Data);
596             final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode,
597                     schemaContext, "", verboseClasssComments, genTypeBuilders);
598             constructGetter(parent, choiceNode.getQName().getLocalName(),
599                     choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
600             choiceTypeBuilder.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, choiceTypeBuilder));
601             annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
602             genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
603             generateTypesFromChoiceCases(module, schemaContext, genCtx, basePackageName, choiceTypeBuilder.toInstance(),
604                 choiceNode, verboseClasssComments, typeProvider, genTypeBuilders);
605         }
606     }
607
608     private static void containerToGenType(final Module module, final String basePackageName,
609         final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node,
610         final SchemaContext schemaContext, final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
611         final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
612
613         final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
614                 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
615         if (genType != null) {
616             constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType, node.getStatus());
617             resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes(), genCtx,
618                     schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
619         }
620     }
621
622     private static void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder
623             parent, final GeneratedTypeBuilder childOf, final ListSchemaNode node, final SchemaContext schemaContext,
624             final boolean verboseClassComments, final Map<Module, ModuleContext> genCtx,
625             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
626
627         final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node,
628                 schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
629         if (genType != null) {
630             final String nodeName = node.getQName().getLocalName();
631             constructGetter(parent, nodeName, node.getDescription(), Types.listTypeFor(genType), node.getStatus());
632             final List<QName> listKeys = node.getKeyDefinition();
633             final String packageName = new StringBuilder(packageNameForGeneratedType(basePackageName, node.getPath(),
634                     BindingNamespaceType.Key)).append('.').append(nodeName).toString();
635
636             final GeneratedTOBuilder genTOBuilder = resolveListKeyTOBuilder(packageName, node);
637
638             for (final DataSchemaNode schemaNode : node.getChildNodes()) {
639                 if (!schemaNode.isAugmenting()) {
640                     addSchemaNodeToListBuilders(nodeName, basePackageName, schemaNode, genType, genTOBuilder, listKeys,
641                             module, typeProvider, schemaContext, genCtx, genTypeBuilders, verboseClassComments);
642                 }
643             }
644
645             // serialVersionUID
646             if (genTOBuilder != null) {
647                 final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
648                 prop.setValue(Long.toString(computeDefaultSUID(genTOBuilder)));
649                 genTOBuilder.setSUID(prop);
650             }
651
652             typeBuildersToGenTypes(module, genType, genTOBuilder, genCtx);
653         }
654     }
655
656     private static void typeBuildersToGenTypes(final Module module, final GeneratedTypeBuilder typeBuilder,
657             final GeneratedTOBuilder genTOBuilder, final Map<Module, ModuleContext> genCtx) {
658         checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
659         if (genTOBuilder != null) {
660             final GeneratedTransferObject genTO = genTOBuilder.toInstance();
661             constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
662             genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
663         }
664     }
665
666     /**
667      * Converts <code>leaf</code> to the getter method which is added to
668      * <code>typeBuilder</code>.
669      *
670      * @param typeBuilder
671      *            generated type builder to which is added getter method as
672      *            <code>leaf</code> mapping
673      * @param leaf
674      *            leaf schema node which is mapped as getter method which is
675      *            added to <code>typeBuilder</code>
676      * @param module
677      *            Module in which type was defined
678      * @return boolean value
679      *         <ul>
680      *         <li>false - if <code>leaf</code> or <code>typeBuilder</code> are
681      *         null</li>
682      *         <li>true - in other cases</li>
683      *         </ul>
684      */
685     private static Type resolveLeafSchemaNodeAsMethod(final String nodeName, final SchemaContext schemaContext,
686             final GeneratedTypeBuilder typeBuilder, final Map<Module, ModuleContext> genCtx, final LeafSchemaNode leaf,
687             final Module module, final TypeProvider typeProvider) {
688         if (leaf == null || typeBuilder == null || leaf.isAddedByUses()) {
689             return null;
690         }
691
692         final String leafName = leaf.getQName().getLocalName();
693         if (leafName == null) {
694             return null;
695         }
696
697         final Module parentModule = findParentModule(schemaContext, leaf);
698         Type returnType = null;
699
700         final TypeDefinition<?> typeDef = leaf.getType();
701         if (isInnerType(leaf, typeDef)) {
702             if (typeDef instanceof EnumTypeDefinition) {
703                 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
704                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
705                 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
706                         genCtx, typeBuilder, module);
707                 if (enumBuilder != null) {
708                     returnType = enumBuilder.toInstance(typeBuilder);
709                 }
710                 ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
711             } else if (typeDef instanceof UnionTypeDefinition) {
712                 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
713                         typeProvider, schemaContext);
714                 if (genTOBuilder != null) {
715                     //TODO: https://bugs.opendaylight.org/show_bug.cgi?id=2289
716                     returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
717                 }
718             } else if (typeDef instanceof BitsTypeDefinition) {
719                 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule,
720                         typeProvider, schemaContext);
721                 if (genTOBuilder != null) {
722                     returnType = genTOBuilder.toInstance();
723                 }
724             } else {
725                 // It is constrained version of already declared type (inner declared type exists,
726                 // onlyfor special cases (Enum, Union, Bits), which were already checked.
727                 // In order to get proper class we need to look up closest derived type
728                 // and apply restrictions from leaf type
729                 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
730                 returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
731                         restrictions);
732             }
733         } else {
734             final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
735             returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
736         }
737
738         if (returnType == null) {
739             return null;
740         }
741
742         if (typeDef instanceof EnumTypeDefinition) {
743             ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
744         }
745
746         String leafDesc = leaf.getDescription();
747         if (leafDesc == null) {
748             leafDesc = "";
749         }
750
751         final String leafGetterName;
752         if (!"".equals(nodeName)) {
753             StringBuilder sb = new StringBuilder(nodeName)
754                 .append('_')
755                 .append(leafName);
756             leafGetterName = sb.toString();
757         } else {
758             leafGetterName = leafName;
759         }
760
761         constructGetter(typeBuilder, leafGetterName, leafDesc, returnType, leaf.getStatus());
762         return returnType;
763     }
764
765     /**
766      * Converts <code>node</code> leaf list schema node to getter method of
767      * <code>typeBuilder</code>.
768      *
769      * @param typeBuilder
770      *            generated type builder to which is <code>node</code> added as
771      *            getter method
772      * @param node
773      *            leaf list schema node which is added to
774      *            <code>typeBuilder</code> as getter method
775      * @param module module
776      * @param typeProvider type provider instance
777      * @param genCtx actual generated context
778      * @return boolean value
779      *         <ul>
780      *         <li>true - if <code>node</code>, <code>typeBuilder</code>,
781      *         nodeName equal null or <code>node</code> is added by <i>uses</i></li>
782      *         <li>false - other cases</li>
783      *         </ul>
784      */
785     private static boolean resolveLeafListSchemaNode(final SchemaContext schemaContext, final GeneratedTypeBuilder
786             typeBuilder, final LeafListSchemaNode node, final Module module, final TypeProvider typeProvider,
787             final Map<Module, ModuleContext> genCtx) {
788         if (node == null || typeBuilder == null || node.isAddedByUses()) {
789             return false;
790         }
791
792         final QName nodeName = node.getQName();
793
794         final TypeDefinition<?> typeDef = node.getType();
795         final Module parentModule = findParentModule(schemaContext, node);
796
797         Type returnType = null;
798         if (typeDef.getBaseType() == null) {
799             if (typeDef instanceof EnumTypeDefinition) {
800                 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
801                 final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
802                 final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
803                         genCtx, typeBuilder, module);
804                 returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
805                 ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
806             } else if (typeDef instanceof UnionTypeDefinition) {
807                 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
808                         typeProvider, schemaContext);
809                 if (genTOBuilder != null) {
810                     returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule, typeProvider);
811                 }
812             } else if (typeDef instanceof BitsTypeDefinition) {
813                 final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule,
814                         typeProvider, schemaContext);
815                 returnType = genTOBuilder.toInstance();
816             } else {
817                 final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
818                 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
819             }
820         } else {
821             final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
822             returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
823         }
824
825         final ParameterizedType listType = Types.listTypeFor(returnType);
826         constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType, node.getStatus());
827         return true;
828     }
829
830     /**
831      * Converts <code>caseNodes</code> set to list of corresponding generated
832      * types.
833      *
834      * For every <i>case</i> which isn't added through augment or <i>uses</i> is
835      * created generated type builder. The package names for the builder is
836      * created as concatenation of the module package (
837      * <code>basePackageName</code>) and names of all parents nodes of the
838      * concrete <i>case</i>. There is also relation "<i>implements type</i>"
839      * between every case builder and <i>choice</i> type
840      *
841      * @param module
842      *            current module
843      * @param schemaContext
844      *            current schema context
845      * @param genCtx
846      *            actual generated context
847      * @param basePackageName
848      *            string with the module package name
849      * @param refChoiceType
850      *            type which represents superior <i>case</i>
851      * @param choiceNode
852      *            choice case node which is mapped to generated type
853      * @param verboseClassComments
854      *            Javadoc verbosity switch
855      * @throws IllegalArgumentException
856      *             <ul>
857      *             <li>if <code>basePackageName</code> equals null</li>
858      *             <li>if <code>refChoiceType</code> equals null</li>
859      *             <li>if <code>caseNodes</code> equals null</li>
860      *             </ul>
861      */
862     private static void generateTypesFromChoiceCases(final Module module, final SchemaContext schemaContext,
863             final Map<Module, ModuleContext> genCtx, final String basePackageName, final Type refChoiceType,
864             final ChoiceSchemaNode choiceNode, final boolean verboseClassComments, final TypeProvider typeProvider,
865             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
866         checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
867         checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL.");
868         checkArgument(choiceNode != null, "ChoiceNode cannot be NULL.");
869
870         final Set<ChoiceCaseNode> caseNodes = choiceNode.getCases();
871         if (caseNodes == null) {
872             return;
873         }
874
875         for (final ChoiceCaseNode caseNode : caseNodes) {
876             if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) {
877                 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath(),
878                     BindingNamespaceType.Data);
879                 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode,
880                     module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
881                 caseTypeBuilder.addImplementsType(refChoiceType);
882                 caseTypeBuilder.setParentTypeForBuilder(refChoiceType);
883                 annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder);
884                 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
885                 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
886                 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
887                 if (caseChildNodes != null) {
888                     final SchemaPath choiceNodeParentPath = choiceNode.getPath().getParent();
889
890                     if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
891                         SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
892
893                         if (parent instanceof AugmentationSchema) {
894                             final AugmentationSchema augSchema = (AugmentationSchema) parent;
895                             final SchemaPath targetPath = augSchema.getTargetPath();
896                             SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
897                             if (targetSchemaNode instanceof DataSchemaNode
898                                     && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
899                                 if (targetSchemaNode instanceof DerivableSchemaNode) {
900                                     targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
901                                 }
902                                 if (targetSchemaNode == null) {
903                                     throw new IllegalStateException(
904                                             "Failed to find target node from grouping for augmentation " + augSchema
905                                                     + " in module " + module.getName());
906                                 }
907                             }
908                             parent = targetSchemaNode;
909                         }
910
911                         Preconditions.checkState(parent != null, "Could not find Choice node parent %s",
912                                 choiceNodeParentPath);
913                         GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath(), genCtx);
914                         if (childOfType == null) {
915                             childOfType = findGroupingByPath(parent.getPath(), genCtx);
916                         }
917                         resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes,
918                                 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
919                     } else {
920                         resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, moduleToDataType(module,
921                                 genCtx, verboseClassComments), caseChildNodes, genCtx, schemaContext,
922                                 verboseClassComments, genTypeBuilders, typeProvider);
923                     }
924                 }
925             }
926             processUsesAugments(schemaContext, caseNode, module, genCtx, genTypeBuilders, verboseClassComments,
927                     typeProvider);
928         }
929     }
930
931     private static Type resolveAnyNodeAsMethod(final SchemaContext schemaContext, final GeneratedTypeBuilder
932             typeBuilder, final Map<Module, ModuleContext> genCtx, final DataSchemaNode node, final Module module,
933             final TypeProvider typeProvider) {
934
935         final String anyName = node.getQName().getLocalName();
936         if (anyName == null) {
937             return null;
938         }
939
940         String anyDesc = node.getDescription();
941         if (anyDesc == null) {
942             anyDesc = "";
943         }
944
945         Type returnType = Types.DOCUMENT;
946
947         constructGetter(typeBuilder, anyName, anyDesc, returnType, node.getStatus());
948         return returnType;
949     }
950
951     /**
952      * Adds <code>schemaNode</code> to <code>typeBuilder</code> as getter method
953      * or to <code>genTOBuilder</code> as property.
954      *
955      * @param nodeName
956      *            string contains the name of list
957      * @param basePackageName
958      *            string contains the module package name
959      * @param schemaNode
960      *            data schema node which should be added as getter method to
961      *            <code>typeBuilder</code> or as a property to
962      *            <code>genTOBuilder</code> if is part of the list key
963      * @param typeBuilder
964      *            generated type builder for the list schema node
965      * @param genTOBuilder
966      *            generated TO builder for the list keys
967      * @param listKeys
968      *            list of string which contains QNames of the list keys
969      * @param module
970      *            current module
971      * @param typeProvider
972      *            provider that defines contract for generated types
973      * @param schemaContext
974      *            schema context
975      * @param genCtx
976      *            map of generated entities in context of YANG modules
977      * @param genTypeBuilders
978      *            map of generated type builders
979      * @param verboseClassComments
980      *            generate verbose comments
981      * @throws IllegalArgumentException
982      *             <ul>
983      *             <li>if <code>schemaNode</code> equals null</li>
984      *             <li>if <code>typeBuilder</code> equals null</li>
985      *             </ul>
986      */
987     private static void addSchemaNodeToListBuilders(final String nodeName, final String basePackageName,
988             final DataSchemaNode schemaNode, final GeneratedTypeBuilder typeBuilder,
989             final GeneratedTOBuilder genTOBuilder, final List<QName> listKeys, final Module module,
990             final TypeProvider typeProvider, final SchemaContext schemaContext, final Map<Module, ModuleContext> genCtx,
991             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final boolean verboseClassComments) {
992         checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
993         checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL.");
994
995         if (schemaNode instanceof LeafSchemaNode) {
996             final LeafSchemaNode leaf = (LeafSchemaNode) schemaNode;
997             final QName leafQName = leaf.getQName();
998
999             final Type type = resolveLeafSchemaNodeAsMethod(nodeName, schemaContext, typeBuilder, genCtx, leaf, module,
1000                     typeProvider);
1001             if (listKeys.contains(leafQName)) {
1002                 if (type == null) {
1003                     resolveLeafSchemaNodeAsProperty(schemaContext, typeProvider, genCtx, genTOBuilder, leaf, true,
1004                         module);
1005                 } else {
1006                     AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(genTOBuilder, leaf, type, true);
1007                 }
1008             }
1009         } else if (!schemaNode.isAddedByUses()) {
1010             if (schemaNode instanceof LeafListSchemaNode) {
1011                 resolveLeafListSchemaNode(schemaContext, typeBuilder, (LeafListSchemaNode) schemaNode, module,
1012                         typeProvider, genCtx);
1013             } else if (schemaNode instanceof ContainerSchemaNode) {
1014                 containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode,
1015                         schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1016             } else if (schemaNode instanceof ListSchemaNode) {
1017                 listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode,
1018                         schemaContext, verboseClassComments, genCtx, genTypeBuilders, typeProvider);
1019             } else if (schemaNode instanceof ChoiceSchemaNode) {
1020                 choiceToGenType(module, schemaContext, verboseClassComments, basePackageName, typeBuilder,
1021                         (ChoiceSchemaNode) schemaNode, genTypeBuilders, genCtx, typeProvider);
1022             }
1023         }
1024     }
1025
1026     private static boolean resolveLeafSchemaNodeAsProperty(final SchemaContext schemaContext, final TypeProvider
1027             typeProvider, final Map<Module, ModuleContext> genCtx, final GeneratedTOBuilder
1028             toBuilder, final LeafSchemaNode leaf, final boolean isReadOnly, final Module module) {
1029
1030         if (leaf != null && toBuilder != null) {
1031             Type returnType;
1032             final TypeDefinition<?> typeDef = leaf.getType();
1033             if (typeDef instanceof UnionTypeDefinition) {
1034                 // GeneratedType for this type definition should be already
1035                 // created
1036                 final QName qname = typeDef.getQName();
1037                 final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1038                         qname.getRevision());
1039                 final ModuleContext mc = genCtx.get(unionModule);
1040                 returnType = mc.getTypedefs().get(typeDef.getPath());
1041             } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
1042                 // Annonymous enumeration (already generated, since it is inherited via uses).
1043                 LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
1044                 QName qname = originalLeaf.getQName();
1045                 final Module enumModule =  schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
1046                         qname.getRevision());
1047                 returnType = genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
1048             } else {
1049                 returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
1050             }
1051             return AuxiliaryGenUtils.resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
1052         }
1053         return false;
1054     }
1055
1056     private static TypeDefinition<?> getBaseOrDeclaredType(final TypeDefinition<?> typeDef) {
1057         final TypeDefinition<?> baseType = typeDef.getBaseType();
1058         return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
1059     }
1060
1061     @SuppressWarnings({ "rawtypes", "unchecked" })
1062     private static GeneratedTypeBuilder processDataSchemaNode(final Module module, final String basePackageName,
1063         final GeneratedTypeBuilder childOf, final DataSchemaNode node, final SchemaContext schemaContext,
1064         final boolean verboseClassComments, Map<Module, ModuleContext> genCtx, final Map<String, Map<String,
1065         GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1066
1067         if (node.isAugmenting() || node.isAddedByUses()) {
1068             return null;
1069         }
1070         final String packageName = packageNameForGeneratedType(basePackageName, node.getPath(), BindingNamespaceType.Data);
1071         final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf, module,
1072                 genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1073         genType.addComment(node.getDescription());
1074         annotateDeprecatedIfNecessary(node.getStatus(), genType);
1075         genType.setDescription(createDescription(node, genType.getFullyQualifiedName(), schemaContext, verboseClassComments));
1076         genType.setModuleName(module.getName());
1077         genType.setReference(node.getReference());
1078         genType.setSchemaPath((List) node.getPath().getPathFromRoot());
1079         genType.setParentTypeForBuilder(childOf);
1080         if (node instanceof DataNodeContainer) {
1081             genCtx.get(module).addChildNodeType(node, genType);
1082             genCtx = groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings(), genCtx, schemaContext,
1083                     verboseClassComments, genTypeBuilders, typeProvider);
1084             processUsesAugments(schemaContext, (DataNodeContainer) node, module, genCtx, genTypeBuilders,
1085                     verboseClassComments, typeProvider);
1086         }
1087         return genType;
1088     }
1089
1090     /**
1091      * Converts all <b>groupings</b> of the module to the list of
1092      * <code>Type</code> objects. Firstly are groupings sorted according mutual
1093      * dependencies. At least dependent (independent) groupings are in the list
1094      * saved at first positions. For every grouping the record is added to map
1095      * {@link ModuleContext#groupings allGroupings}
1096      *
1097      * @param module
1098      *            current module
1099      * @param groupings
1100      *            collection of groupings from which types will be generated
1101      * @param typeProvider
1102      *            provider that defines contract for generated types
1103      * @param schemaContext
1104      *            schema context
1105      * @param genCtx
1106      *            map of generated entities in context of YANG modules
1107      * @param genTypeBuilders
1108      *            map of generated type builders
1109      * @param verboseClassComments
1110      *            generate verbose comments
1111      *
1112      */
1113     static Map<Module, ModuleContext> groupingsToGenTypes(final Module module, final Collection<GroupingDefinition>
1114             groupings, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1115             verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1116         final String basePackageName = BindingMapping.getRootPackageName(module);
1117         final List<GroupingDefinition> groupingsSortedByDependencies = new GroupingDefinitionDependencySort()
1118                 .sort(groupings);
1119         for (final GroupingDefinition grouping : groupingsSortedByDependencies) {
1120             genCtx = groupingToGenType(basePackageName, grouping, module, genCtx, schemaContext,
1121                     verboseClassComments, genTypeBuilders, typeProvider);
1122         }
1123         return genCtx;
1124     }
1125
1126     /**
1127      * Converts individual grouping to GeneratedType. Firstly generated type
1128      * builder is created and every child node of grouping is resolved to the
1129      * method.
1130      *
1131      * @param basePackageName
1132      *            string contains the module package name
1133      * @param grouping
1134      *            GroupingDefinition which contains data about grouping
1135      * @param module
1136      *            current module
1137      * @param typeProvider
1138      *            provider that defines contract for generated types
1139      * @param schemaContext
1140      *            schema context
1141      * @param genCtx
1142      *            map of generated entities in context of YANG modules
1143      * @param genTypeBuilders
1144      *            map of generated type builders
1145      * @param verboseClassComments
1146      *            generate verbose comments
1147      *
1148      * @return GeneratedType which is generated from grouping (object of type
1149      *         <code>GroupingDefinition</code>)
1150      */
1151     private static Map<Module, ModuleContext> groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module
1152             module, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext, final boolean
1153             verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
1154         final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath(), BindingNamespaceType.Grouping);
1155         final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping, module, genCtx,
1156                 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1157         annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
1158         genCtx.get(module).addGroupingType(grouping.getPath(), genType);
1159         resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes(), genCtx,
1160                 schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
1161         genCtx = groupingsToGenTypes(module, grouping.getGroupings(), genCtx, schemaContext, verboseClassComments,
1162                 genTypeBuilders, typeProvider);
1163         genCtx = processUsesAugments(schemaContext, grouping, module, genCtx, genTypeBuilders, verboseClassComments,
1164                 typeProvider);
1165         return genCtx;
1166     }
1167
1168     /**
1169      * //TODO: add information about multiple base identities in YANG 1.1
1170      * Converts the <b>identity</b> object to GeneratedType. Firstly it is
1171      * created transport object builder. If identity contains base identity then
1172      * reference to base identity is added to superior identity as its extend.
1173      * If identity doesn't contain base identity then only reference to abstract
1174      * class {@link org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode
1175      * BaseIdentity} is added
1176      *
1177      * @param module
1178      *            current module
1179      * @param basePackageName
1180      *            string contains the module package name
1181      * @param identity
1182      *            IdentitySchemaNode which contains data about identity
1183      * @param schemaContext
1184      *            SchemaContext which is used to get package and name
1185      *            information about base of identity
1186      * @param genCtx generated context
1187      * @return returns generated context
1188      */
1189     static Map<Module, ModuleContext> identityToGenType(final Module module, final String basePackageName,
1190             final IdentitySchemaNode identity, final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx,
1191             boolean verboseClassComments, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
1192             final TypeProvider typeProvider, Map<QName, GeneratedTOBuilderImpl> generatedIdentities) {
1193
1194         //check first if identity has been resolved as base identity of some other one
1195         GeneratedTOBuilderImpl newType = generatedIdentities.get(identity.getQName());
1196
1197         if (newType == null) {
1198             final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath(),
1199                     BindingNamespaceType.Identity);
1200             newType = new GeneratedTOBuilderImpl(packageName, identity.getQName().getLocalName(), true, false);
1201         }
1202
1203         final Set<IdentitySchemaNode> baseIdentities = identity.getBaseIdentities();
1204         if (baseIdentities.size() == 0) {
1205             //no base - abstract
1206             final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(BaseIdentity.class.getPackage().getName(),
1207                 BaseIdentity.class.getSimpleName());
1208             newType.setExtendsType(gto.toInstance());
1209             generatedIdentities.put(identity.getQName(), newType);
1210         } else {
1211             //one base - inheritance
1212             final IdentitySchemaNode baseIdentity = baseIdentities.iterator().next();
1213             final Module baseIdentityParentModule = SchemaContextUtil.findParentModule(schemaContext, baseIdentity);
1214             final String returnTypePkgName = new StringBuilder(BindingMapping.getRootPackageName
1215                     (baseIdentityParentModule))
1216                     .append('.')
1217                     .append(BindingNamespaceType.Identity.getPackagePrefix())
1218                     .toString();
1219
1220             final GeneratedTOBuilderImpl existingIdentityGto = generatedIdentities.get(baseIdentity.getQName());
1221             if (existingIdentityGto != null) {
1222                 newType.setExtendsType(existingIdentityGto.toInstance());
1223             } else {
1224                 final GeneratedTOBuilderImpl gto = new GeneratedTOBuilderImpl(returnTypePkgName,
1225                         baseIdentity.getQName().getLocalName());
1226                 newType.setExtendsType(gto.toInstance());
1227                 generatedIdentities.put(baseIdentity.getQName(), gto);
1228             }
1229
1230             //FIXME: more bases - possible composition, multiple inheritance not possible
1231         }
1232         generatedIdentities.put(identity.getQName(), newType);
1233
1234         newType.setAbstract(true);
1235         newType.addComment(identity.getDescription());
1236         newType.setDescription(createDescription(identity, newType.getFullyQualifiedName(), schemaContext,
1237                 verboseClassComments));
1238         newType.setReference(identity.getReference());
1239         newType.setModuleName(module.getName());
1240         newType.setSchemaPath((List) identity.getPath().getPathFromRoot());
1241
1242         qNameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, identity.getQName());
1243
1244         genCtx.get(module).addIdentityType(identity.getQName(), newType);
1245         return genCtx;
1246     }
1247 }