X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=code-generator%2Fbinding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fbinding%2Fgenerator%2Fimpl%2FBindingGeneratorImpl.java;h=befe3e88e02c494e48b287d8089cf2f05eb8fd08;hb=c13c841a2311ac4c0c507451624e0b3ddf26e775;hp=d657087326b61d962b025fb52fff82e1f0d883ca;hpb=0bcdd622447e3ab07ab34be96e25f1543020a5c3;p=yangtools.git diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java index d657087326..befe3e88e0 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java @@ -30,6 +30,7 @@ import static org.opendaylight.yangtools.yang.model.util.SchemaContextUtil.findP import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -38,6 +39,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; + import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil; import org.opendaylight.yangtools.binding.generator.util.BindingTypes; import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl; @@ -105,9 +107,28 @@ import org.slf4j.LoggerFactory; public class BindingGeneratorImpl implements BindingGenerator { private static final Logger LOG = LoggerFactory.getLogger(BindingGeneratorImpl.class); + private static final Splitter COLON_SPLITTER = Splitter.on(':'); + private static final Splitter BSDOT_SPLITTER = Splitter.on("\\."); + private static final char NEW_LINE = '\n'; + + /** + * Constant with the concrete name of identifier. + */ + private static final String AUGMENT_IDENTIFIER_NAME = "augment-identifier"; + + /** + * Constant with the concrete name of namespace. + */ + private static final String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext"; private final Map genCtx = new HashMap<>(); + /** + * When set to true, generated classes will include javadoc comments which + * are useful for users. + */ + private final boolean verboseClassComments; + /** * Outer key represents the package name. Outer value represents map of all * builders in the same package. Inner key represents the schema node name @@ -128,18 +149,23 @@ public class BindingGeneratorImpl implements BindingGenerator { private SchemaContext schemaContext; /** - * Constant with the concrete name of namespace. + * Create a new binding generator with verboe comments. + * + * @deprecated Use {@link #BindingGeneratorImpl(boolean)} instead. */ - private final static String YANG_EXT_NAMESPACE = "urn:opendaylight:yang:extension:yang-ext"; + @Deprecated + public BindingGeneratorImpl() { + this(true); + } /** - * Constant with the concrete name of identifier. + * Create a new binding generator. + * + * @param verboseClassComments generate verbose comments */ - private final static String AUGMENT_IDENTIFIER_NAME = "augment-identifier"; - - private final char NEW_LINE = '\n'; - - private final char TAB = '\t'; + public BindingGeneratorImpl(final boolean verboseClassComments) { + this.verboseClassComments = verboseClassComments; + } /** * Resolves generated types from context schema nodes of all @@ -617,7 +643,6 @@ public class BindingGeneratorImpl implements BindingGenerator { newType.setDescription(createDescription(identity, newType.getFullyQualifiedName())); newType.setReference(identity.getReference()); newType.setModuleName(module.getName()); - SchemaPath path = identity.getPath(); newType.setSchemaPath(identity.getPath().getPathFromRoot()); final QName qname = identity.getQName(); @@ -1345,7 +1370,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } else if (typeDef instanceof BitsTypeDefinition) { genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule); if (genTOBuilder != null) { - returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName()); + returnType = genTOBuilder.toInstance(); } } else { final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); @@ -1368,7 +1393,7 @@ public class BindingGeneratorImpl implements BindingGenerator { final String nodeParam = node.getNodeParameter(); IdentitySchemaNode identity = null; String basePackageName = null; - final Iterable splittedElement = Splitter.on(':').split(nodeParam); + final Iterable splittedElement = COLON_SPLITTER.split(nodeParam); final Iterator iterator = splittedElement.iterator(); final int length = Iterables.size(splittedElement); if (length == 1) { @@ -1436,13 +1461,8 @@ public class BindingGeneratorImpl implements BindingGenerator { // GeneratedType for this type definition should be already // created QName qname = typeDef.getQName(); - Module unionModule = null; - String prefix = qname.getPrefix(); - if (prefix == null || prefix.isEmpty() || prefix.equals(module.getPrefix())) { - unionModule = module; - } else { - unionModule = findModuleFromImports(module.getImports(), qname.getPrefix()); - } + Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(), + qname.getRevision()); final ModuleContext mc = genCtx.get(unionModule); returnType = mc.getTypedefs().get(typeDef.getPath()); } else { @@ -1532,7 +1552,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } } else if (typeDef instanceof BitsTypeDefinition) { final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule); - returnType = new ReferencedTypeImpl(genTOBuilder.getPackageName(), genTOBuilder.getName()); + returnType = genTOBuilder.toInstance(); } else { final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef); returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions); @@ -1970,15 +1990,13 @@ public class BindingGeneratorImpl implements BindingGenerator { private String createDescription(final Set schemaNodes, final String moduleName, final String moduleSourcePath) { final StringBuilder sb = new StringBuilder(); - final String yangSnipet = YangTemplate.generateYangSnipet(schemaNodes); if (!isNullOrEmpty(schemaNodes)) { final SchemaNode node = schemaNodes.iterator().next(); if (node instanceof RpcDefinition) { sb.append("Interface for implementing the following YANG RPCs defined in module " + moduleName + ""); - } - else if (node instanceof NotificationDefinition) { + } else if (node instanceof NotificationDefinition) { sb.append("Interface for receiving the following YANG notifications defined in module " + moduleName + ""); } } @@ -1987,72 +2005,77 @@ public class BindingGeneratorImpl implements BindingGenerator { sb.append(moduleSourcePath); sb.append("):"); sb.append(NEW_LINE); - sb.append("
");
-        sb.append(NEW_LINE);
-        sb.append(yangSnipet);
-        sb.append("
"); - sb.append(NEW_LINE); + + if (verboseClassComments) { + sb.append("
");
+            sb.append(NEW_LINE);
+            sb.append(YangTemplate.generateYangSnipet(schemaNodes));
+            sb.append("
"); + sb.append(NEW_LINE); + } return sb.toString(); } private String createDescription(final SchemaNode schemaNode, final String fullyQualifiedName) { final StringBuilder sb = new StringBuilder(); - final Module module = findParentModule(schemaContext, schemaNode); - final String yangSnipet = YangTemplate.generateYangSnipet(schemaNode); final String formattedDescription = YangTemplate.formatToParagraph(schemaNode.getDescription(), 0); - final StringBuilder linkToBuilderClass = new StringBuilder(); - final StringBuilder linkToKeyClass = new StringBuilder(); - final Splitter splitter = Splitter.on("\\."); - final String[] namespace = Iterables.toArray(splitter.split(fullyQualifiedName), String.class); - String className = namespace[namespace.length - 1]; - - if (hasBuilderClass(schemaNode)) { - linkToBuilderClass.append(className); - linkToBuilderClass.append("Builder"); - - if (schemaNode instanceof ListSchemaNode) { - linkToKeyClass.append(className); - linkToKeyClass.append("Key"); - } - } if (!isNullOrEmpty(formattedDescription)) { sb.append(formattedDescription); sb.append(NEW_LINE); } - sb.append("

"); - sb.append("This class represents the following YANG schema fragment defined in module "); - sb.append(module.getName()); - sb.append(""); - sb.append(NEW_LINE); - sb.append("
(Source path: "); - sb.append(module.getModuleSourcePath()); - sb.append("):"); - sb.append(NEW_LINE); - sb.append("

");
-        sb.append(NEW_LINE);
-        sb.append(yangSnipet);
-        sb.append("
"); - sb.append(NEW_LINE); - sb.append("The schema path to identify an instance is"); - sb.append(NEW_LINE); - sb.append(""); - sb.append(YangTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot())); - sb.append(""); - sb.append(NEW_LINE); - if (hasBuilderClass(schemaNode)) { + if (verboseClassComments) { + final Module module = findParentModule(schemaContext, schemaNode); + final StringBuilder linkToBuilderClass = new StringBuilder(); + final StringBuilder linkToKeyClass = new StringBuilder(); + final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class); + String className = namespace[namespace.length - 1]; + + if (hasBuilderClass(schemaNode)) { + linkToBuilderClass.append(className); + linkToBuilderClass.append("Builder"); + + if (schemaNode instanceof ListSchemaNode) { + linkToKeyClass.append(className); + linkToKeyClass.append("Key"); + } + } + + sb.append("

"); + sb.append("This class represents the following YANG schema fragment defined in module "); + sb.append(module.getName()); + sb.append(""); + sb.append(NEW_LINE); + sb.append("
(Source path: "); + sb.append(module.getModuleSourcePath()); + sb.append("):"); sb.append(NEW_LINE); - sb.append("

To create instances of this class use " + "{@link " + linkToBuilderClass + "}."); + sb.append("

");
             sb.append(NEW_LINE);
-            sb.append("@see ");
-            sb.append(linkToBuilderClass);
-            if (schemaNode instanceof ListSchemaNode) {
+            sb.append(YangTemplate.generateYangSnipet(schemaNode));
+            sb.append("
"); + sb.append(NEW_LINE); + sb.append("The schema path to identify an instance is"); + sb.append(NEW_LINE); + sb.append(""); + sb.append(YangTemplate.formatSchemaPath(module.getName(), schemaNode.getPath().getPathFromRoot())); + sb.append(""); + sb.append(NEW_LINE); + + if (hasBuilderClass(schemaNode)) { + sb.append(NEW_LINE); + sb.append("

To create instances of this class use " + "{@link " + linkToBuilderClass + "}."); + sb.append(NEW_LINE); sb.append("@see "); - sb.append(linkToKeyClass); + sb.append(linkToBuilderClass); + if (schemaNode instanceof ListSchemaNode) { + sb.append("@see "); + sb.append(linkToKeyClass); + } + sb.append(NEW_LINE); } - sb.append(NEW_LINE); } return sb.toString(); @@ -2072,26 +2095,28 @@ public class BindingGeneratorImpl implements BindingGenerator { private String createDescription(final Module module) { final StringBuilder sb = new StringBuilder(); - final String yangSnipet = YangTemplate.generateYangSnipet(module); final String formattedDescription = YangTemplate.formatToParagraph(module.getDescription(), 0); if (!isNullOrEmpty(formattedDescription)) { sb.append(formattedDescription); sb.append(NEW_LINE); } - sb.append("

"); - sb.append("This class represents the following YANG schema fragment defined in module "); - sb.append(module.getName()); - sb.append(""); - sb.append(NEW_LINE); - sb.append("
Source path: "); - sb.append(module.getModuleSourcePath()); - sb.append("):"); - sb.append(NEW_LINE); - sb.append("

");
-        sb.append(NEW_LINE);
-        sb.append(yangSnipet);
-        sb.append("
"); + + if (verboseClassComments) { + sb.append("

"); + sb.append("This class represents the following YANG schema fragment defined in module "); + sb.append(module.getName()); + sb.append(""); + sb.append(NEW_LINE); + sb.append("
Source path: "); + sb.append(module.getModuleSourcePath()); + sb.append("):"); + sb.append(NEW_LINE); + sb.append("

");
+            sb.append(NEW_LINE);
+            sb.append(YangTemplate.generateYangSnipet(module));
+            sb.append("
"); + } return sb.toString(); }