Bug 1411-4: MDSAL Binding2 Generator Impl
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding2 / generator / impl / GenHelperUtil.java
1 /*
2  * Copyright (c) 2016 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.binding2.generator.impl;
10
11 import static com.google.common.base.Preconditions.checkArgument;
12 import static org.opendaylight.mdsal.binding2.generator.impl.AugmentToGenType.usesAugmentationToGenTypes;
13 import static org.opendaylight.mdsal.binding2.generator.util.BindingTypes.TREE_ROOT;
14 import static org.opendaylight.mdsal.binding2.generator.util.BindingTypes.augmentable;
15 import static org.opendaylight.mdsal.binding2.generator.util.Types.typeForClass;
16
17 import com.google.common.annotations.Beta;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.base.Splitter;
20 import com.google.common.base.Strings;
21 import com.google.common.collect.Iterables;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Pattern;
26 import org.opendaylight.mdsal.binding2.generator.impl.util.YangTextTemplate;
27 import org.opendaylight.mdsal.binding2.generator.util.Binding2GeneratorUtil;
28 import org.opendaylight.mdsal.binding2.generator.util.Binding2Mapping;
29 import org.opendaylight.mdsal.binding2.generator.util.BindingTypes;
30 import org.opendaylight.mdsal.binding2.generator.util.Types;
31 import org.opendaylight.mdsal.binding2.generator.util.generated.type.builder.GeneratedTypeBuilderImpl;
32 import org.opendaylight.mdsal.binding2.model.api.Constant;
33 import org.opendaylight.mdsal.binding2.model.api.GeneratedType;
34 import org.opendaylight.mdsal.binding2.model.api.Type;
35 import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilder;
36 import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilderBase;
37 import org.opendaylight.mdsal.binding2.txt.yangTemplateForModule;
38 import org.opendaylight.mdsal.binding2.txt.yangTemplateForNode;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
41 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
43 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
45 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
48 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
52 import org.opendaylight.yangtools.yang.model.api.Status;
53 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.UsesNode;
55 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
56
57
58 /**
59  * Helper util class used for generation of types in binding spec v2.
60  */
61 @Beta
62 final class GenHelperUtil {
63
64     private GenHelperUtil() {
65         throw new UnsupportedOperationException("Util class");
66     }
67
68     private static final Pattern UNICODE_CHAR_PATTERN = Pattern.compile("\\\\+u");
69     private static final Splitter BSDOT_SPLITTER = Splitter.on("\\.");
70     private static final char NEW_LINE = '\n';
71
72     /**
73      * Constant with the concrete name of identifier.
74      */
75     private static final String AUGMENT_IDENTIFIER_NAME = "augment-identifier";
76
77     /**
78      * Constant with the concrete name of namespace.
79      */
80     private static final String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext";
81
82
83     /**
84      * Create GeneratedTypeBuilder object from module argument.
85      *
86      * @param module
87      *            Module object from which builder will be created
88      * @param genCtx
89      * @param verboseClassComments
90      *
91      * @return <code>GeneratedTypeBuilder</code> which is internal
92      *         representation of the module
93      * @throws IllegalArgumentException
94      *             if module is null
95      */
96     static GeneratedTypeBuilder moduleToDataType(final Module module, Map<Module, ModuleContext> genCtx, final boolean verboseClassComments) {
97         checkArgument(module != null, "Module reference cannot be NULL.");
98
99         final GeneratedTypeBuilder moduleDataTypeBuilder = moduleTypeBuilder(module, "Data", verboseClassComments);
100         addImplementedInterfaceFromUses(module, moduleDataTypeBuilder, genCtx);
101         moduleDataTypeBuilder.addImplementsType(TREE_ROOT);
102         moduleDataTypeBuilder.addComment(module.getDescription());
103         moduleDataTypeBuilder.setDescription(createDescription(module, verboseClassComments));
104         moduleDataTypeBuilder.setReference(module.getReference());
105         return moduleDataTypeBuilder;
106     }
107
108     /**
109      * Generates type builder for <code>module</code>.
110      *
111      * @param module
112      *            Module which is source of package name for generated type
113      *            builder
114      * @param postfix
115      *            string which is added to the module class name representation
116      *            as suffix
117      * @param verboseClassComments
118      * @return instance of GeneratedTypeBuilder which represents
119      *         <code>module</code>.
120      * @throws IllegalArgumentException
121      *             if <code>module</code> is null
122      */
123     static GeneratedTypeBuilder moduleTypeBuilder(final Module module, final String postfix, final boolean verboseClassComments) {
124         checkArgument(module != null, "Module reference cannot be NULL.");
125         final String packageName = Binding2Mapping.getRootPackageName(module);
126         final String moduleName = Binding2Mapping.getClassName(module.getName()) + postfix;
127
128         final GeneratedTypeBuilderImpl moduleBuilder = new GeneratedTypeBuilderImpl(packageName, moduleName);
129         moduleBuilder.setDescription(createDescription(module, verboseClassComments));
130         moduleBuilder.setReference(module.getReference());
131         moduleBuilder.setModuleName(moduleName);
132
133         return moduleBuilder;
134     }
135
136     /**
137      * Adds the implemented types to type builder.
138      *
139      * The method passes through the list of <i>uses</i> in
140      * {@code dataNodeContainer}. For every <i>use</i> is obtained corresponding
141      * generated type from all groupings
142      * allGroupings} which is added as <i>implements type</i> to
143      * <code>builder</code>
144      *
145      * @param dataNodeContainer
146      *            element which contains the list of used YANG groupings
147      * @param builder
148      *            builder to which are added implemented types according to
149      *            <code>dataNodeContainer</code>
150      * @param genCtx
151      * @return generated type builder with all implemented types
152      */
153     private static GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer,
154                           final GeneratedTypeBuilder builder, Map<Module, ModuleContext> genCtx) {
155         for (final UsesNode usesNode : dataNodeContainer.getUses()) {
156             if (usesNode.getGroupingPath() != null) {
157                 final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath(), genCtx).toInstance();
158                 if (genType == null) {
159                     throw new IllegalStateException("Grouping " + usesNode.getGroupingPath() + "is not resolved for "
160                             + builder.getName());
161                 }
162
163                 builder.addImplementsType(genType);
164             }
165         }
166         return builder;
167     }
168
169      static GeneratedTypeBuilder findGroupingByPath(final SchemaPath path, Map<Module, ModuleContext> genCtx) {
170         for (final ModuleContext ctx : genCtx.values()) {
171             final GeneratedTypeBuilder result = ctx.getGrouping(path);
172             if (result != null) {
173                 return result;
174             }
175         }
176         return null;
177      }
178
179     private static String createDescription(final Module module, final boolean verboseClassComments) {
180         final StringBuilder sb = new StringBuilder();
181         final String moduleDescription = Binding2GeneratorUtil.encodeAngleBrackets(module.getDescription());
182         final String formattedDescription = YangTextTemplate.formatToParagraph(moduleDescription, 0);
183
184         if (!Strings.isNullOrEmpty(formattedDescription)) {
185             sb.append(formattedDescription);
186             sb.append(NEW_LINE);
187         }
188
189         if (verboseClassComments) {
190             sb.append("<p>");
191             sb.append("This class represents the following YANG schema fragment defined in module <b>");
192             sb.append(module.getName());
193             sb.append("</b>");
194             sb.append(NEW_LINE);
195             sb.append("<pre>");
196             sb.append(NEW_LINE);
197             sb.append(Binding2GeneratorUtil.encodeAngleBrackets(yangTemplateForModule.render(module).body()));
198             sb.append("</pre>");
199         }
200
201         return replaceAllIllegalChars(sb);
202     }
203
204     @VisibleForTesting
205     public static String replaceAllIllegalChars(final StringBuilder stringBuilder){
206         final String ret = UNICODE_CHAR_PATTERN.matcher(stringBuilder).replaceAll("\\\\\\\\u");
207         return ret.isEmpty() ? "" : ret;
208     }
209
210     /**
211      * Adds the methods to <code>typeBuilder</code> which represent subnodes of
212      * node for which <code>typeBuilder</code> was created.
213      *
214      * The subnodes aren't mapped to the methods if they are part of grouping or
215      * augment (in this case are already part of them).
216      *
217      * @param module
218      *            current module
219      * @param basePackageName
220      *            string contains the module package name
221      * @param parent
222      *            generated type builder which represents any node. The subnodes
223      *            of this node are added to the <code>typeBuilder</code> as
224      *            methods. The subnode can be of type leaf, leaf-list, list,
225      *            container, choice.
226      * @param childOf
227      *            parent type
228      * @param schemaNodes
229      *            set of data schema nodes which are the children of the node
230      *            for which <code>typeBuilder</code> was created
231      * @return generated type builder which is the same builder as input
232      *         parameter. The getter methods (representing child nodes) could be
233      *         added to it.
234      */
235     static GeneratedTypeBuilder resolveDataSchemaNodes(final Module module, final String basePackageName,
236                           final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final Iterable<DataSchemaNode> schemaNodes) {
237         if (schemaNodes != null && parent != null) {
238             for (final DataSchemaNode schemaNode : schemaNodes) {
239                 if (!schemaNode.isAugmenting() && !schemaNode.isAddedByUses()) {
240                     //TODO: design decomposition and implement it
241                     //addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, parent, childOf, module);
242                 }
243             }
244         }
245         return parent;
246     }
247
248     static Map<Module, ModuleContext> processUsesAugments(final SchemaContext schemaContext, final
249                         DataNodeContainer node, final Module module, Map<Module, ModuleContext> genCtx,  Map<String,
250                         Map<String, GeneratedTypeBuilder>> genTypeBuilders, final boolean verboseClassComments) {
251         final String basePackageName = Binding2Mapping.getRootPackageName(module);
252         for (final UsesNode usesNode : node.getUses()) {
253             for (final AugmentationSchema augment : usesNode.getAugmentations()) {
254                 genCtx = usesAugmentationToGenTypes(schemaContext, basePackageName, augment, module, usesNode,
255                         node, genCtx, genTypeBuilders, verboseClassComments);
256                 genCtx = processUsesAugments(schemaContext, augment, module, genCtx, genTypeBuilders, verboseClassComments);
257             }
258         }
259         return genCtx;
260     }
261
262     static GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path, Map<Module, ModuleContext> genCtx) {
263         for (final ModuleContext ctx : genCtx.values()) {
264             final GeneratedTypeBuilder result = ctx.getChildNode(path);
265             if (result != null) {
266                 return result;
267             }
268         }
269         return null;
270     }
271
272     static GeneratedTypeBuilder findCaseByPath(final SchemaPath path, Map<Module, ModuleContext> genCtx) {
273         for (final ModuleContext ctx : genCtx.values()) {
274             final GeneratedTypeBuilder result = ctx.getCase(path);
275             if (result != null) {
276                 return result;
277             }
278         }
279         return null;
280     }
281
282     /**
283      * Returns a generated type builder for an augmentation.
284      *
285      * The name of the type builder is equal to the name of augmented node with
286      * serial number as suffix.
287      *
288      * @param module
289      *            current module
290      * @param augmentPackageName
291      *            string with contains the package name to which the augment
292      *            belongs
293      * @param basePackageName
294      *            string with the package name to which the augmented node
295      *            belongs
296      * @param targetTypeRef
297      *            target type
298      * @param augSchema
299      *            augmentation schema which contains data about the child nodes
300      *            and uses of augment
301      * @return generated type builder for augment in genCtx
302      */
303     static Map<Module, ModuleContext> addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
304                 final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema,
305                 Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, Map<Module, ModuleContext> genCtx) {
306
307         Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
308         if (augmentBuilders == null) {
309             augmentBuilders = new HashMap<>();
310             genTypeBuilders.put(augmentPackageName, augmentBuilders);
311         }
312         final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
313
314         String augTypeName;
315         if (augIdentifier != null) {
316             augTypeName = Binding2Mapping.getClassName(augIdentifier);
317         } else {
318             augTypeName = augGenTypeName(augmentBuilders, targetTypeRef.getName());
319         }
320
321         GeneratedTypeBuilder augTypeBuilder = new GeneratedTypeBuilderImpl(augmentPackageName, augTypeName);
322
323         augTypeBuilder.addImplementsType(BindingTypes.TREE_NODE);
324         augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
325         annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
326         augTypeBuilder = addImplementedInterfaceFromUses(augSchema, augTypeBuilder, genCtx);
327
328         augTypeBuilder = augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema
329                 .getChildNodes());
330         augmentBuilders.put(augTypeName, augTypeBuilder);
331
332         if(!augSchema.getChildNodes().isEmpty()) {
333             genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
334
335         }
336         genCtx.get(module).addAugmentType(augTypeBuilder);
337         return genCtx;
338     }
339
340     /**
341      * Adds the methods to <code>typeBuilder</code> what represents subnodes of
342      * node for which <code>typeBuilder</code> was created.
343      *
344      * @param module
345      *            current module
346      * @param basePackageName
347      *            string contains the module package name
348      * @param typeBuilder
349      *            generated type builder which represents any node. The subnodes
350      *            of this node are added to the <code>typeBuilder</code> as
351      *            methods. The subnode can be of type leaf, leaf-list, list,
352      *            container, choice.
353      * @param childOf
354      *            parent type
355      * @param schemaNodes
356      *            set of data schema nodes which are the children of the node
357      *            for which <code>typeBuilder</code> was created
358      * @return generated type builder which is the same object as the input
359      *         parameter <code>typeBuilder</code>. The getter method could be
360      *         added to it.
361      */
362     private static GeneratedTypeBuilder augSchemaNodeToMethods(final Module module, final String basePackageName,
363                                                         final GeneratedTypeBuilder typeBuilder, final GeneratedTypeBuilder childOf,
364                                                         final Iterable<DataSchemaNode> schemaNodes) {
365         if ((schemaNodes != null) && (typeBuilder != null)) {
366             for (final DataSchemaNode schemaNode : schemaNodes) {
367                 if (!schemaNode.isAugmenting()) {
368                     //TODO: design decomposition and implement it
369                     //addSchemaNodeToBuilderAsMethod(basePackageName, schemaNode, typeBuilder, childOf, module);
370                 }
371             }
372         }
373         return typeBuilder;
374     }
375
376     /**
377      * @param unknownSchemaNodes
378      * @return nodeParameter of UnknownSchemaNode
379      */
380     private static String getAugmentIdentifier(final List<UnknownSchemaNode> unknownSchemaNodes) {
381         for (final UnknownSchemaNode unknownSchemaNode : unknownSchemaNodes) {
382             final QName nodeType = unknownSchemaNode.getNodeType();
383             if (AUGMENT_IDENTIFIER_NAME.equals(nodeType.getLocalName())
384                     && YANG_EXT_NAMESPACE.equals(nodeType.getNamespace().toString())) {
385                 return unknownSchemaNode.getNodeParameter();
386             }
387         }
388         return null;
389     }
390
391     /**
392      * Returns first unique name for the augment generated type builder. The
393      * generated type builder name for augment consists from name of augmented
394      * node and serial number of its augmentation.
395      *
396      * @param builders
397      *            map of builders which were created in the package to which the
398      *            augmentation belongs
399      * @param genTypeName
400      *            string with name of augmented node
401      * @return string with unique name for augmentation builder
402      */
403     private static String augGenTypeName(final Map<String, GeneratedTypeBuilder> builders, final String genTypeName) {
404         int index = 1;
405         if (builders != null) {
406             while (builders.containsKey(genTypeName + index)) {
407                 index = index + 1;
408             }
409         }
410         return genTypeName + index;
411     }
412
413     static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
414             schemaNode, final Module module, Map<Module, ModuleContext> genCtx, final SchemaContext schemaContext,
415             final boolean verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
416         return addDefaultInterfaceDefinition(packageName, schemaNode, null, module, genCtx, schemaContext,
417                 verboseClassComments, genTypeBuilders);
418     }
419
420     /**
421      * Instantiates generated type builder with <code>packageName</code> and
422      * <code>schemaNode</code>.
423      *
424      * The new builder always implements
425      * {@link org.opendaylight.mdsal.binding2.spec.TreeNode TreeNode}.<br>
426      * If <code>schemaNode</code> is instance of GroupingDefinition it also
427      * implements {@link org.opendaylight.mdsal.binding2.spec.Augmentable
428      * Augmentable}.<br>
429      * If <code>schemaNode</code> is instance of
430      * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer
431      * DataNodeContainer} it can also implement nodes which are specified in
432      * <i>uses</i>.
433      *
434      * @param packageName
435      *            string with the name of the package to which
436      *            <code>schemaNode</code> belongs.
437      * @param schemaNode
438      *            schema node for which is created generated type builder
439      * @param parent
440      *            parent type (can be null)
441      * @param schemaContext
442      * @return generated type builder <code>schemaNode</code>
443      */
444     private static GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode
445             schemaNode, final Type parent, final Module module, Map<Module, ModuleContext> genCtx,
446             final SchemaContext schemaContext, final boolean verboseClassComments, Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
447         GeneratedTypeBuilder it = addRawInterfaceDefinition(packageName, schemaNode, schemaContext, "",
448                 verboseClassComments, genTypeBuilders);
449         if (parent == null) {
450             it.addImplementsType(BindingTypes.TREE_NODE);
451         } else {
452             it.addImplementsType(BindingTypes.treeChildNode(parent));
453         }
454         if (!(schemaNode instanceof GroupingDefinition)) {
455             it.addImplementsType(augmentable(it));
456         }
457
458         if (schemaNode instanceof DataNodeContainer) {
459             //TODO: design decomposition and implement it
460             //groupingsToGenTypes(module, ((DataNodeContainer) schemaNode).getGroupings());
461             it = addImplementedInterfaceFromUses((DataNodeContainer) schemaNode, it, genCtx);
462         }
463
464         return it;
465     }
466
467     /**
468      * Returns reference to generated type builder for specified
469      * <code>schemaNode</code> with <code>packageName</code>.
470      *
471      * Firstly the generated type builder is searched in
472      * {@link BindingGeneratorImpl#genTypeBuilders genTypeBuilders}. If it isn't
473      * found it is created and added to <code>genTypeBuilders</code>.
474      *
475      * @param packageName
476      *            string with the package name to which returning generated type
477      *            builder belongs
478      * @param schemaNode
479      *            schema node which provide data about the schema node name
480      * @param schemaContext
481      * @param prefix
482      *            return type name prefix
483      * @return generated type builder for <code>schemaNode</code>
484      * @throws IllegalArgumentException
485      *             <ul>
486      *             <li>if <code>schemaNode</code> is null</li>
487      *             <li>if <code>packageName</code> is null</li>
488      *             <li>if QName of schema node is null</li>
489      *             <li>if schemaNode name is null</li>
490      *             </ul>
491      *
492      */
493     private static GeneratedTypeBuilder addRawInterfaceDefinition(final String packageName, final SchemaNode schemaNode,
494                        final SchemaContext schemaContext, final String prefix, final boolean verboseClassComments,
495                        Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders) {
496         checkArgument(schemaNode != null, "Data Schema Node cannot be NULL.");
497         checkArgument(packageName != null, "Package Name for Generated Type cannot be NULL.");
498         checkArgument(schemaNode.getQName() != null, "QName for Data Schema Node cannot be NULL.");
499         final String schemaNodeName = schemaNode.getQName().getLocalName();
500         checkArgument(schemaNodeName != null, "Local Name of QName for Data Schema Node cannot be NULL.");
501
502         String genTypeName;
503         if (prefix == null) {
504             genTypeName = Binding2Mapping.getClassName(schemaNodeName);
505         } else {
506             genTypeName = prefix + Binding2Mapping.getClassName(schemaNodeName);
507         }
508
509         final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
510         final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
511         qNameConstant(newType, Binding2Mapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
512         newType.addComment(schemaNode.getDescription());
513         newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName(), schemaContext, verboseClassComments));
514         newType.setReference(schemaNode.getReference());
515         newType.setSchemaPath((List<QName>) schemaNode.getPath().getPathFromRoot());
516         newType.setModuleName(module.getName());
517
518         //FIXME: update genTypeBuilders for callers
519         if (!genTypeBuilders.containsKey(packageName)) {
520             final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
521             builders.put(genTypeName, newType);
522             genTypeBuilders.put(packageName, builders);
523         } else {
524             final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
525             if (!builders.containsKey(genTypeName)) {
526                 builders.put(genTypeName, newType);
527             }
528         }
529         return newType;
530
531     }
532
533     private static Constant qNameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
534                                           final QName name) {
535         return toBuilder.addConstant(typeForClass(QName.class), constantName, name);
536     }
537
538     private static String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName,
539                              final SchemaContext schemaContext, final boolean verboseClassComments) {
540         final StringBuilder sb = new StringBuilder();
541         final String nodeDescription = Binding2GeneratorUtil.encodeAngleBrackets(schemaNode.getDescription());
542         final String formattedDescription = YangTextTemplate.formatToParagraph(nodeDescription, 0);
543
544         if (!Strings.isNullOrEmpty(formattedDescription)) {
545             sb.append(formattedDescription);
546             sb.append(NEW_LINE);
547         }
548
549         if (verboseClassComments) {
550             final Module module = SchemaContextUtil.findParentModule(schemaContext, schemaNode);
551             final StringBuilder linkToBuilderClass = new StringBuilder();
552             final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
553             final String className = namespace[namespace.length - 1];
554
555             if (hasBuilderClass(schemaNode)) {
556                 linkToBuilderClass.append(className);
557                 linkToBuilderClass.append("Builder");
558             }
559
560             sb.append("<p>");
561             sb.append("This class represents the following YANG schema fragment defined in module <b>");
562             sb.append(module.getName());
563             sb.append("</b>");
564             sb.append(NEW_LINE);
565             sb.append("<pre>");
566             sb.append(NEW_LINE);
567             sb.append(Binding2GeneratorUtil.encodeAngleBrackets(yangTemplateForNode.render(schemaNode).body()));
568             sb.append("</pre>");
569             sb.append(NEW_LINE);
570             sb.append("The schema path to identify an instance is");
571             sb.append(NEW_LINE);
572             sb.append("<i>");
573             sb.append(YangTextTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot()));
574             sb.append("</i>");
575             sb.append(NEW_LINE);
576
577             if (hasBuilderClass(schemaNode)) {
578                 sb.append(NEW_LINE);
579                 sb.append("<p>To create instances of this class use " + "{@link " + linkToBuilderClass + "}.");
580                 sb.append(NEW_LINE);
581                 sb.append("@see ");
582                 sb.append(linkToBuilderClass);
583                 sb.append(NEW_LINE);
584                 if (schemaNode instanceof ListSchemaNode) {
585                     final List<QName> keyDef = ((ListSchemaNode)schemaNode).getKeyDefinition();
586                     if (keyDef != null && !keyDef.isEmpty()) {
587                         sb.append("@see ");
588                         sb.append(className);
589                         sb.append("Key");
590                     }
591                     sb.append(NEW_LINE);
592                 }
593             }
594         }
595
596         return replaceAllIllegalChars(sb);
597     }
598
599     private static void annotateDeprecatedIfNecessary(final Status status, final GeneratedTypeBuilder builder) {
600         if (status == Status.DEPRECATED) {
601             builder.addAnnotation("", "Deprecated");
602         }
603     }
604
605     private static boolean hasBuilderClass(final SchemaNode schemaNode) {
606         if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode ||
607                 schemaNode instanceof RpcDefinition || schemaNode instanceof NotificationDefinition) {
608             return true;
609         }
610         return false;
611     }
612
613 }