Bug 5882: Wrong placement of deprecated annotation
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / BindingGeneratorImpl.java
index 3854be4c7c237f5c5f5364b10c0e32215f89fe43..f44afa2cb4a1a052c550e1bb0ea16a125ffe1aeb 100644 (file)
@@ -11,6 +11,8 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.computeDefaultSUID;
+import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
+import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForAugmentedGeneratedType;
 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.packageNameForGeneratedType;
 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_OBJECT;
 import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_ROOT;
@@ -25,7 +27,9 @@ import static org.opendaylight.yangtools.binding.generator.util.Types.typeForCla
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findDataSchemaNode;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findNodeInSchemaContext;
 import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findParentModule;
+
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
@@ -91,17 +95,19 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
-import org.opendaylight.yangtools.yang.model.util.BaseTypes;
 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
+import org.opendaylight.yangtools.yang.model.util.ExtendedType;
 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
 import org.opendaylight.yangtools.yang.model.util.UnionType;
+import org.opendaylight.yangtools.yang.model.util.type.CompatUtils;
 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
 import org.slf4j.Logger;
@@ -112,7 +118,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
     private static final Splitter COLON_SPLITTER = Splitter.on(':');
     private static final Splitter BSDOT_SPLITTER = Splitter.on("\\.");
     private static final char NEW_LINE = '\n';
-    private static final String QNAME_FQCN = QName.class.getName();
 
     /**
      * Constant with the concrete name of identifier.
@@ -153,16 +158,6 @@ public class BindingGeneratorImpl implements BindingGenerator {
      */
     private SchemaContext schemaContext;
 
-    /**
-     * Create a new binding generator with verboe comments.
-     *
-     * @deprecated Use {@link #BindingGeneratorImpl(boolean)} instead.
-     */
-    @Deprecated
-    public BindingGeneratorImpl() {
-        this(true);
-    }
-
     /**
      * Create a new binding generator.
      *
@@ -319,6 +314,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
         final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, node, childOf, module);
         genType.addComment(node.getDescription());
+        annotateDeprecatedIfNecessary(node.getStatus(), genType);
         genType.setDescription(createDescription(node, genType.getFullyQualifiedName()));
         genType.setModuleName(module.getName());
         genType.setReference(node.getReference());
@@ -335,7 +331,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
         final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
         if (genType != null) {
-            constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
+            constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType, node.getStatus());
             resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
         }
     }
@@ -344,7 +340,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
             final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
         final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
         if (genType != null) {
-            constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
+            constructGetter(parent, node.getQName().getLocalName(), node.getDescription(),
+                    Types.listTypeFor(genType), node.getStatus());
 
             final List<String> listKeys = listKeys(node);
             final String packageName = packageNameForGeneratedType(basePackageName, node.getPath());
@@ -491,7 +488,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             if (rpc != null) {
                 final String rpcName = BindingMapping.getClassName(rpc.getQName());
                 final String rpcMethodName = BindingMapping.getPropertyName(rpcName);
-                final String rpcComment = rpc.getDescription();
+                final String rpcComment = encodeAngleBrackets(rpc.getDescription());
                 final MethodSignatureBuilder method = interfaceBuilder.addMethod(rpcMethodName);
                 final ContainerSchemaNode input = rpc.getInput();
                 final ContainerSchemaNode output = rpc.getOutput();
@@ -501,6 +498,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     addImplementedInterfaceFromUses(input, inType);
                     inType.addImplementsType(DATA_OBJECT);
                     inType.addImplementsType(augmentable(inType));
+                    annotateDeprecatedIfNecessary(rpc.getStatus(), inType);
                     resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
                     genCtx.get(module).addChildNodeType(input, inType);
                     final GeneratedType inTypeInstance = inType.toInstance();
@@ -513,6 +511,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     addImplementedInterfaceFromUses(output, outType);
                     outType.addImplementsType(DATA_OBJECT);
                     outType.addImplementsType(augmentable(outType));
+                    annotateDeprecatedIfNecessary(rpc.getStatus(), outType);
                     resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
                     genCtx.get(module).addChildNodeType(output, outType);
                     outTypeInstance = outType.toInstance();
@@ -564,6 +563,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
                 final GeneratedTypeBuilder notificationInterface = addDefaultInterfaceDefinition(basePackageName,
                         notification, null, module);
+                annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
                 notificationInterface.addImplementsType(NOTIFICATION);
                 genCtx.get(module).addChildNodeType(notification, notificationInterface);
 
@@ -573,7 +573,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
                 listenerInterface.addMethod("on" + notificationInterface.getName())
                 .setAccessModifier(AccessModifier.PUBLIC).addParameter(notificationInterface, "notification")
-                .setComment(notification.getDescription()).setReturnType(Types.VOID);
+                .setComment(encodeAngleBrackets(notification.getDescription())).setReturnType(Types.VOID);
             }
         }
         listenerInterface.setDescription(createDescription(notifications, module.getName(), module.getModuleSourcePath()));
@@ -660,19 +660,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
     private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
             final QName name) {
-        final StringBuilder sb = new StringBuilder(QNAME_FQCN);
-        sb.append(".cachedReference(");
-        sb.append(QNAME_FQCN);
-        sb.append(".create(");
-        sb.append('"');
-        sb.append(name.getNamespace());
-        sb.append("\",\"");
-        sb.append(name.getFormattedRevision());
-        sb.append("\",\"");
-        sb.append(name.getLocalName());
-        sb.append("\"))");
-
-        return toBuilder.addConstant(typeForClass(QName.class), constantName, sb.toString());
+        return toBuilder.addConstant(typeForClass(QName.class), constantName, name);
     }
 
     /**
@@ -714,6 +702,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
     private void groupingToGenType(final String basePackageName, final GroupingDefinition grouping, final Module module) {
         final String packageName = packageNameForGeneratedType(basePackageName, grouping.getPath());
         final GeneratedTypeBuilder genType = addDefaultInterfaceDefinition(packageName, grouping, module);
+        annotateDeprecatedIfNecessary(grouping.getStatus(), genType);
         genCtx.get(module).addGroupingType(grouping.getPath(), genType);
         resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
         groupingsToGenTypes(module, grouping.getGroupings());
@@ -745,7 +734,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 && (enumTypeDef.getQName().getLocalName() != null)) {
             final String enumerationName = BindingMapping.getClassName(enumName);
             final EnumBuilder enumBuilder = typeBuilder.addEnumeration(enumerationName);
-            enumBuilder.setDescription(enumTypeDef.getDescription());
+            final String enumTypedefDescription = encodeAngleBrackets(enumTypeDef.getDescription());
+            enumBuilder.setDescription(enumTypedefDescription);
             enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
             ModuleContext ctx = genCtx.get(module);
             ctx.addInnerTypedefType(enumTypeDef.getPath(), enumBuilder);
@@ -846,7 +836,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         } else {
             generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
-                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
+                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), null);
         }
     }
 
@@ -875,14 +865,13 @@ public class BindingGeneratorImpl implements BindingGenerator {
         if (!(targetSchemaNode instanceof ChoiceSchemaNode)) {
             String packageName = augmentPackageName;
             if (usesNodeParent instanceof SchemaNode) {
-                packageName = packageNameForGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath(),
-                        true);
+                packageName = packageNameForAugmentedGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath());
             }
             addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetTypeBuilder.toInstance(),
                     augSchema);
         } else {
             generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
-                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
+                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), usesNodeParent);
         }
     }
 
@@ -917,21 +906,32 @@ public class BindingGeneratorImpl implements BindingGenerator {
             return null;
         }
 
-        boolean fromUses = ((DataSchemaNode) result).isAddedByUses();
-        final Iterator<UsesNode> groupingUses = grouping.getUses().iterator();
-        while (groupingUses.hasNext() && fromUses) {
-            result = findOriginalTargetFromGrouping(targetPath, groupingUses.next());
-            if (result != null) {
-                fromUses = ((DataSchemaNode) result).isAddedByUses();
+        if (result instanceof DerivableSchemaNode) {
+            DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
+            Optional<? extends SchemaNode> originalNode = castedResult
+                    .getOriginal();
+            if (castedResult.isAddedByUses() && originalNode.isPresent()) {
+                result = originalNode.get();
             }
         }
-        if (fromUses) {
-            // this indicates invalid yang and thus possible bug in code because
-            // invalid yang should be already spotted by parser
-            throw new IllegalStateException("Failed to generate code for augment in " + parentUsesNode);
-        }
 
-        return (DataSchemaNode) result;
+        if (result instanceof DataSchemaNode) {
+            DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
+            if (resultDataSchemaNode.isAddedByUses()) {
+                // The original node is required, but we have only the copy of
+                // the original node.
+                // Maybe this indicates a bug in Yang parser.
+                throw new IllegalStateException(
+                        "Failed to generate code for augment in "
+                                + parentUsesNode);
+            } else {
+                return resultDataSchemaNode;
+            }
+        } else {
+            throw new IllegalStateException(
+                    "Target node of uses-augment statement must be DataSchemaNode. Failed to generate code for augment in "
+                            + parentUsesNode);
+        }
     }
 
     /**
@@ -975,6 +975,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         augTypeBuilder.addImplementsType(DATA_OBJECT);
         augTypeBuilder.addImplementsType(Types.augmentationTypeFor(targetTypeRef));
+        annotateDeprecatedIfNecessary(augSchema.getStatus(), augTypeBuilder);
         addImplementedInterfaceFromUses(augSchema, augTypeBuilder);
 
         augSchemaNodeToMethods(module, basePackageName, augTypeBuilder, augTypeBuilder, augSchema.getChildNodes());
@@ -1168,9 +1169,10 @@ public class BindingGeneratorImpl implements BindingGenerator {
         if (!choiceNode.isAddedByUses()) {
             final String packageName = packageNameForGeneratedType(basePackageName, choiceNode.getPath());
             final GeneratedTypeBuilder choiceTypeBuilder = addRawInterfaceDefinition(packageName, choiceNode);
-            constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
-                    choiceTypeBuilder);
+            constructGetter(parent, choiceNode.getQName().getLocalName(),
+                    choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
             choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
+            annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
             genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
             generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
         }
@@ -1219,6 +1221,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath());
                 final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode, module);
                 caseTypeBuilder.addImplementsType(refChoiceType);
+                annotateDeprecatedIfNecessary(caseNode.getStatus(), caseTypeBuilder);
                 genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
                 genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
                 final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
@@ -1291,7 +1294,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *             </ul>
      */
     private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName,
-            final Type targetType, final ChoiceSchemaNode targetNode, final Iterable<DataSchemaNode> augmentedNodes) {
+            final Type targetType, final ChoiceSchemaNode targetNode, final Iterable<DataSchemaNode> augmentedNodes,
+            final DataNodeContainer usesNodeParent) {
         checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
         checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
         checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
@@ -1322,10 +1326,20 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 }
 
                 ChoiceCaseNode node = null;
+                final String caseLocalName = caseNode.getQName().getLocalName();
                 if (caseNode instanceof ChoiceCaseNode) {
                     node = (ChoiceCaseNode) caseNode;
+                } else if (targetNode.getCaseNodeByName(caseLocalName) == null) {
+                    final String targetNodeLocalName = targetNode.getQName().getLocalName();
+                    for (DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) {
+                        if (dataSchemaNode instanceof ChoiceSchemaNode && targetNodeLocalName.equals(dataSchemaNode.getQName
+                                ().getLocalName())) {
+                            node = ((ChoiceSchemaNode) dataSchemaNode).getCaseNodeByName(caseLocalName);
+                            break;
+                        }
+                    }
                 } else {
-                    node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
+                    node = targetNode.getCaseNodeByName(caseLocalName);
                 }
                 final Iterable<DataSchemaNode> childNodes = node.getChildNodes();
                 if (childNodes != null) {
@@ -1337,6 +1351,30 @@ public class BindingGeneratorImpl implements BindingGenerator {
         }
     }
 
+    private static boolean isInnerType(final LeafSchemaNode leaf, final TypeDefinition<?> type) {
+        // Deal with old parser, clearing out references to typedefs
+        if (type instanceof ExtendedType) {
+            return false;
+        }
+
+        // New parser with encapsulated type
+        if (leaf.getPath().equals(type.getPath())) {
+            return true;
+        }
+
+        // Embedded type definition with new parser. Also takes care of the old parser with bits
+        if (leaf.getPath().equals(type.getPath().getParent())) {
+            return true;
+        }
+
+        // Old parser uses broken Union type, which does not change its schema path
+        if (type instanceof UnionType) {
+            return true;
+        }
+
+        return false;
+    }
+
     /**
      * Converts <code>leaf</code> to the getter method which is added to
      * <code>typeBuilder</code>.
@@ -1357,52 +1395,78 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *         </ul>
      */
     private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf, final Module module) {
-        Type returnType = null;
-        if ((leaf != null) && (typeBuilder != null)) {
-            final String leafName = leaf.getQName().getLocalName();
-            String leafDesc = leaf.getDescription();
-            if (leafDesc == null) {
-                leafDesc = "";
-            }
+        if (leaf == null || typeBuilder == null || leaf.isAddedByUses()) {
+            return null;
+        }
 
-            final Module parentModule = findParentModule(schemaContext, leaf);
-            if (leafName != null && !leaf.isAddedByUses()) {
-                final TypeDefinition<?> typeDef = leaf.getType();
+        final String leafName = leaf.getQName().getLocalName();
+        if (leafName == null) {
+            return null;
+        }
 
-                GeneratedTOBuilder genTOBuilder;
-                if (typeDef instanceof EnumTypeDefinition) {
-                    returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
-                    final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
-                    final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
-                            typeBuilder,module);
+        final Module parentModule = findParentModule(schemaContext, leaf);
+        Type returnType = null;
 
-                    if (enumBuilder != null) {
-                        returnType = enumBuilder.toInstance(typeBuilder);
-                    }
-                    ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
-                } else if (typeDef instanceof UnionType) {
-                    genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
-                    if (genTOBuilder != null) {
-                        returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
-                    }
-                } else if (typeDef instanceof BitsTypeDefinition) {
-                    genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
-                    if (genTOBuilder != null) {
-                        returnType = genTOBuilder.toInstance();
-                    }
-                } else {
-                    final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
-                    returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
+        final TypeDefinition<?> typeDef = CompatUtils.compatLeafType(leaf);
+        if (isInnerType(leaf, typeDef)) {
+            if (typeDef instanceof EnumTypeDefinition) {
+                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
+                final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
+                final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
+                    typeBuilder, module);
+
+                if (enumBuilder != null) {
+                    returnType = enumBuilder.toInstance(typeBuilder);
                 }
-                if (returnType != null) {
-                    final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
-                    processContextRefExtension(leaf, getter, parentModule);
+                ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
+            } else if (typeDef instanceof UnionTypeDefinition) {
+                GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
+                if (genTOBuilder != null) {
+                    returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
                 }
+            } else if (typeDef instanceof BitsTypeDefinition) {
+                GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
+                if (genTOBuilder != null) {
+                    returnType = genTOBuilder.toInstance();
+                }
+            } else {
+                // It is constrained version of already declared type (inner declared type exists,
+                // onlyfor special cases (Enum, Union, Bits), which were already checked.
+                // In order to get proper class we need to look up closest derived type
+                // and apply restrictions from leaf type
+                final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
+                returnType = typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
+                        restrictions);
             }
+        } else {
+            final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
+            returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
         }
+
+        if (returnType == null) {
+            return null;
+        }
+
+        String leafDesc = leaf.getDescription();
+        if (leafDesc == null) {
+            leafDesc = "";
+        }
+
+        final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType, leaf.getStatus());
+        processContextRefExtension(leaf, getter, parentModule);
         return returnType;
     }
 
+    private static TypeDefinition<?> getBaseOrDeclaredType(TypeDefinition<?> typeDef) {
+        if (typeDef instanceof ExtendedType) {
+            // Legacy behaviour returning ExtendedType is enough
+            return typeDef;
+        }
+        // Returns DerivedType in case of new parser.
+        final TypeDefinition<?> baseType = typeDef.getBaseType();
+        return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef;
+    }
+
     private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter,
             final Module module) {
         for (final UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) {
@@ -1466,13 +1530,12 @@ public class BindingGeneratorImpl implements BindingGenerator {
     private boolean resolveLeafSchemaNodeAsProperty(final GeneratedTOBuilder toBuilder, final LeafSchemaNode leaf,
             final boolean isReadOnly, final Module module) {
         if ((leaf != null) && (toBuilder != null)) {
-            final String leafName = leaf.getQName().getLocalName();
             String leafDesc = leaf.getDescription();
             if (leafDesc == null) {
                 leafDesc = "";
             }
             Type returnType = null;
-            final TypeDefinition<?> typeDef = leaf.getType();
+            final TypeDefinition<?> typeDef = CompatUtils.compatLeafType(leaf);
             if (typeDef instanceof UnionTypeDefinition) {
                 // GeneratedType for this type definition should be already
                 // created
@@ -1481,7 +1544,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                         qname.getRevision());
                 final ModuleContext mc = genCtx.get(unionModule);
                 returnType = mc.getTypedefs().get(typeDef.getPath());
-            } else if (typeDef instanceof EnumTypeDefinition && BaseTypes.ENUMERATION_QNAME.equals(typeDef.getQName())) {
+            } else if (typeDef instanceof EnumTypeDefinition && typeDef.getBaseType() == null) {
                 // Annonymous enumeration (already generated, since it is inherited via uses).
                 LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
                 QName qname = originalLeaf.getQName();
@@ -1523,7 +1586,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             return false;
         }
         final String leafName = leaf.getQName().getLocalName();
-        final String leafDesc = leaf.getDescription();
+        final String leafDesc = encodeAngleBrackets(leaf.getDescription());
         final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(BindingMapping.getPropertyName(leafName));
         propBuilder.setReadOnly(isReadOnly);
         propBuilder.setReturnType(returnType);
@@ -1553,55 +1616,63 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *         </ul>
      */
     private boolean resolveLeafListSchemaNode(final GeneratedTypeBuilder typeBuilder, final LeafListSchemaNode node, final Module module) {
-        if ((node != null) && (typeBuilder != null)) {
-            final QName nodeName = node.getQName();
-
-            if (nodeName != null && !node.isAddedByUses()) {
-                final TypeDefinition<?> typeDef = node.getType();
-                final Module parentModule = findParentModule(schemaContext, node);
-
-                Type returnType = null;
-                if (typeDef instanceof EnumTypeDefinition) {
-                    returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
-                    final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
-                    final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
-                            typeBuilder,module);
-                    returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
-                    ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
-                } else if (typeDef instanceof UnionType) {
-                    final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
-                    if (genTOBuilder != null) {
-                        returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
-                    }
-                } else if (typeDef instanceof BitsTypeDefinition) {
-                    final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
-                    returnType = genTOBuilder.toInstance();
-                } else {
-                    final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
-                    returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
-                }
+        if (node == null || typeBuilder == null || node.isAddedByUses()) {
+            return false;
+        }
+
+        final QName nodeName = node.getQName();
+        if (nodeName == null) {
+            return false;
+        }
+
+        final TypeDefinition<?> typeDef = node.getType();
+        final Module parentModule = findParentModule(schemaContext, node);
 
-                final ParameterizedType listType = Types.listTypeFor(returnType);
-                constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType);
-                return true;
+        Type returnType = null;
+        if (typeDef.getBaseType() == null) {
+            if (typeDef instanceof EnumTypeDefinition) {
+                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
+                final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
+                final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, nodeName,
+                    typeBuilder,module);
+                returnType = new ReferencedTypeImpl(enumBuilder.getPackageName(), enumBuilder.getName());
+                ((TypeProviderImpl) typeProvider).putReferencedType(node.getPath(), returnType);
+            } else if (typeDef instanceof UnionTypeDefinition) {
+                final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
+                if (genTOBuilder != null) {
+                    returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
+                }
+            } else if (typeDef instanceof BitsTypeDefinition) {
+                final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
+                returnType = genTOBuilder.toInstance();
+            } else {
+                final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
+                returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
             }
+        } else {
+            final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
+            returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
         }
-        return false;
+
+        final ParameterizedType listType = Types.listTypeFor(returnType);
+        constructGetter(typeBuilder, nodeName.getLocalName(), node.getDescription(), listType, node.getStatus());
+        return true;
     }
 
     private Type createReturnTypeForUnion(final GeneratedTOBuilder genTOBuilder, final TypeDefinition<?> typeDef,
             final GeneratedTypeBuilder typeBuilder, final Module parentModule) {
         final GeneratedTOBuilderImpl returnType = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
                 genTOBuilder.getName());
+        final String typedefDescription = encodeAngleBrackets(typeDef.getDescription());
 
-        returnType.setDescription(typeDef.getDescription());
+        returnType.setDescription(typedefDescription);
         returnType.setReference(typeDef.getReference());
         returnType.setSchemaPath(typeDef.getPath().getPathFromRoot());
         returnType.setModuleName(parentModule.getName());
 
         genTOBuilder.setTypedef(true);
         genTOBuilder.setIsUnion(true);
-        ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
+        TypeProviderImpl.addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
 
 
 
@@ -1804,14 +1875,19 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *            string with comment for the getter method
      * @param returnType
      *            type which represents the return type of the getter method
+     * @param status
+     *            status from yang file, for deprecated annotation
      * @return method signature builder which represents the getter method of
      *         <code>interfaceBuilder</code>
      */
     private static MethodSignatureBuilder constructGetter(final GeneratedTypeBuilder interfaceBuilder,
-            final String schemaNodeName, final String comment, final Type returnType) {
+            final String schemaNodeName, final String comment, final Type returnType, final Status status) {
         final MethodSignatureBuilder getMethod = interfaceBuilder
                 .addMethod(getterMethodName(schemaNodeName, returnType));
-        getMethod.setComment(comment);
+        if (status == Status.DEPRECATED) {
+            getMethod.addAnnotation("", "Deprecated");
+        }
+        getMethod.setComment(encodeAngleBrackets(comment));
         getMethod.setReturnType(returnType);
         return getMethod;
     }
@@ -1876,7 +1952,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         if (genTOBuilder != null) {
             final GeneratedTransferObject genTO = genTOBuilder.toInstance();
-            constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO);
+            constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
             genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
         }
     }
@@ -1977,7 +2053,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
                     packageName, typeDef, classNameFromLeaf, parentModule.getName()));
         }
-        if (genTOBuilders != null && !genTOBuilders.isEmpty()) {
+        if (!genTOBuilders.isEmpty()) {
             for (final GeneratedTOBuilder genTOBuilder : genTOBuilders) {
                 typeBuilder.addEnclosingTransferObject(genTOBuilder);
             }
@@ -2051,7 +2127,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
         if (verboseClassComments) {
             sb.append("<pre>");
             sb.append(NEW_LINE);
-            sb.append(YangTemplate.generateYangSnipet(schemaNodes));
+            sb.append(encodeAngleBrackets(YangTemplate.generateYangSnipet(schemaNodes)));
             sb.append("</pre>");
             sb.append(NEW_LINE);
         }
@@ -2061,7 +2137,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
     private String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName) {
         final StringBuilder sb = new StringBuilder();
-        final String formattedDescription = YangTemplate.formatToParagraph(schemaNode.getDescription(), 0);
+        final String nodeDescription = encodeAngleBrackets(schemaNode.getDescription());
+        final String formattedDescription = YangTemplate.formatToParagraph(nodeDescription, 0);
 
         if (!Strings.isNullOrEmpty(formattedDescription)) {
             sb.append(formattedDescription);
@@ -2090,7 +2167,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             sb.append(NEW_LINE);
             sb.append("<pre>");
             sb.append(NEW_LINE);
-            sb.append(YangTemplate.generateYangSnipet(schemaNode));
+            sb.append(encodeAngleBrackets(YangTemplate.generateYangSnipet(schemaNode)));
             sb.append("</pre>");
             sb.append(NEW_LINE);
             sb.append("The schema path to identify an instance is");
@@ -2132,7 +2209,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
     private String createDescription(final Module module) {
         final StringBuilder sb = new StringBuilder();
-        final String formattedDescription = YangTemplate.formatToParagraph(module.getDescription(), 0);
+        final String moduleDescription = encodeAngleBrackets(module.getDescription());
+        final String formattedDescription = YangTemplate.formatToParagraph(moduleDescription, 0);
 
         if (!Strings.isNullOrEmpty(formattedDescription)) {
             sb.append(formattedDescription);
@@ -2151,7 +2229,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
             sb.append(NEW_LINE);
             sb.append("<pre>");
             sb.append(NEW_LINE);
-            sb.append(YangTemplate.generateYangSnipet(module));
+            sb.append(encodeAngleBrackets(YangTemplate.generateYangSnipet(module)));
             sb.append("</pre>");
         }
 
@@ -2194,7 +2272,13 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
     @VisibleForTesting
     static String replaceAllIllegalChars(final StringBuilder stringBuilder){
-        return UNICODE_CHAR_PATTERN.matcher(stringBuilder).replaceAll("\\\\\\\\u");
+        final String ret = UNICODE_CHAR_PATTERN.matcher(stringBuilder).replaceAll("\\\\\\\\u");
+        return ret.isEmpty() ? "" : ret;
     }
 
-}
+    private void annotateDeprecatedIfNecessary(Status status, GeneratedTypeBuilder builder) {
+        if (status == Status.DEPRECATED) {
+            builder.addAnnotation("", "Deprecated");
+        }
+    }
+}
\ No newline at end of file