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=8403a44a82689460f58d7bee6fb9f8cd973c2830;hb=51d05b20379d95850c65aaec46f872526e6364ec;hp=b98c48c9844f0b5067aeae72cd1894b9efd43b9c;hpb=f5fc08eb3884321ba42c6f2932e86057dbc12b6b;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 b98c48c984..8403a44a82 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 @@ -12,7 +12,6 @@ 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.packageNameForGeneratedType; -import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.parseToValidParamName; import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_OBJECT; import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.DATA_ROOT; import static org.opendaylight.yangtools.binding.generator.util.BindingTypes.IDENTIFIABLE; @@ -26,7 +25,7 @@ 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.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; @@ -72,7 +71,7 @@ import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceNode; +import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; @@ -108,6 +107,7 @@ 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. @@ -211,7 +211,7 @@ public class BindingGeneratorImpl implements BindingGenerator { * * @throws IllegalArgumentException * */ private void choiceToGeneratedType(final Module module, final String basePackageName, - final GeneratedTypeBuilder parent, final ChoiceNode choiceNode) { + final GeneratedTypeBuilder parent, final ChoiceSchemaNode choiceNode) { checkArgument(basePackageName != null, "Base Package Name cannot be NULL."); checkArgument(choiceNode != null, "Choice Schema Node cannot be NULL."); @@ -1195,7 +1160,6 @@ public class BindingGeneratorImpl implements BindingGenerator { constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(), choiceTypeBuilder); choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class)); - choiceTypeBuilder.setParentType(parent); genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder); generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode); } @@ -1229,7 +1193,7 @@ public class BindingGeneratorImpl implements BindingGenerator { * */ private void generateTypesFromChoiceCases(final Module module, final String basePackageName, - final Type refChoiceType, final ChoiceNode choiceNode) { + final Type refChoiceType, final ChoiceSchemaNode choiceNode) { checkArgument(basePackageName != null, "Base Package Name cannot be NULL."); checkArgument(refChoiceType != null, "Referenced Choice Type cannot be NULL."); checkArgument(choiceNode != null, "ChoiceNode cannot be NULL."); @@ -1239,7 +1203,7 @@ public class BindingGeneratorImpl implements BindingGenerator { return; } - for (ChoiceCaseNode caseNode : caseNodes) { + for (final ChoiceCaseNode caseNode : caseNodes) { if (caseNode != null && !caseNode.isAddedByUses() && !caseNode.isAugmenting()) { final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath()); final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode, module); @@ -1248,9 +1212,8 @@ public class BindingGeneratorImpl implements BindingGenerator { genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode); final Iterable caseChildNodes = caseNode.getChildNodes(); if (caseChildNodes != null) { - Object parentNode = null; final SchemaPath nodeSp = choiceNode.getPath(); - parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent()); + final Object parentNode = findDataSchemaNode(schemaContext, nodeSp.getParent()); SchemaNode parent; if (parentNode instanceof AugmentationSchema) { @@ -1273,6 +1236,7 @@ public class BindingGeneratorImpl implements BindingGenerator { final SchemaPath sp = choiceNode.getPath(); parent = findDataSchemaNode(schemaContext, sp.getParent()); } + Preconditions.checkState(parent != null, "Could not find Choice node parent "+choiceNode.getPath().getParent()); GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.getPath()); if (childOfType == null) { childOfType = findGroupingByPath(parent.getPath()); @@ -1280,7 +1244,6 @@ public class BindingGeneratorImpl implements BindingGenerator { resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, caseChildNodes); } } - processUsesAugments(caseNode, module); } } @@ -1313,12 +1276,12 @@ public class BindingGeneratorImpl implements BindingGenerator { * */ private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName, - final Type targetType, final ChoiceNode targetNode, final Iterable augmentedNodes) { + final Type targetType, final ChoiceSchemaNode targetNode, final Iterable augmentedNodes) { 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."); - for (DataSchemaNode caseNode : augmentedNodes) { + for (final DataSchemaNode caseNode : augmentedNodes) { if (caseNode != null) { final String packageName = packageNameForGeneratedType(basePackageName, caseNode.getPath()); final GeneratedTypeBuilder caseTypeBuilder = addDefaultInterfaceDefinition(packageName, caseNode, module); @@ -1425,7 +1388,7 @@ public class BindingGeneratorImpl implements BindingGenerator { private void processContextRefExtension(final LeafSchemaNode leaf, final MethodSignatureBuilder getter, final Module module) { - for (UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) { + for (final UnknownSchemaNode node : leaf.getUnknownSchemaNodes()) { final QName nodeType = node.getNodeType(); if ("context-reference".equals(nodeType.getLocalName())) { final String nodeParam = node.getNodeParameter(); @@ -1438,7 +1401,7 @@ public class BindingGeneratorImpl implements BindingGenerator { identity = findIdentityByName(module.getIdentities(), iterator.next()); basePackageName = BindingMapping.getRootPackageName(module.getQNameModule()); } else if (length == 2) { - String prefix = iterator.next(); + final String prefix = iterator.next(); final Module dependentModule = findModuleFromImports(module.getImports(), prefix); if (dependentModule == null) { throw new IllegalArgumentException("Failed to process context-reference: unknown prefix " @@ -1466,7 +1429,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } private IdentitySchemaNode findIdentityByName(final Set identities, final String name) { - for (IdentitySchemaNode id : identities) { + for (final IdentitySchemaNode id : identities) { if (id.getQName().getLocalName().equals(name)) { return id; } @@ -1475,7 +1438,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } private Module findModuleFromImports(final Set imports, final String prefix) { - for (ModuleImport imp : imports) { + for (final ModuleImport imp : imports) { if (imp.getPrefix().equals(prefix)) { return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision()); } @@ -1498,8 +1461,8 @@ public class BindingGeneratorImpl implements BindingGenerator { if (typeDef instanceof UnionTypeDefinition) { // GeneratedType for this type definition should be already // created - QName qname = typeDef.getQName(); - Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(), + final QName qname = typeDef.getQName(); + final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision()); final ModuleContext mc = genCtx.get(unionModule); returnType = mc.getTypedefs().get(typeDef.getPath()); @@ -1540,7 +1503,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } final String leafName = leaf.getQName().getLocalName(); final String leafDesc = leaf.getDescription(); - final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(parseToValidParamName(leafName)); + final GeneratedPropertyBuilder propBuilder = toBuilder.addProperty(BindingMapping.getPropertyName(leafName)); propBuilder.setReadOnly(isReadOnly); propBuilder.setReturnType(returnType); propBuilder.setComment(leafDesc); @@ -1618,10 +1581,11 @@ public class BindingGeneratorImpl implements BindingGenerator { genTOBuilder.setIsUnion(true); ((TypeProviderImpl) typeProvider).addUnitsToGenTO(genTOBuilder, typeDef.getUnits()); - // union builder - final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(), - genTOBuilder.getName() + "Builder"); - unionBuilder.setIsUnionBuilder(true); + + + final GeneratedTOBuilder unionBuilder = createUnionBuilder(genTOBuilder,typeBuilder); + + final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance"); method.setReturnType(returnType); method.addParameter(Types.STRING, "defaultValue"); @@ -1638,6 +1602,22 @@ public class BindingGeneratorImpl implements BindingGenerator { return returnType.toInstance(); } + private GeneratedTOBuilder createUnionBuilder(final GeneratedTOBuilder genTOBuilder, final GeneratedTypeBuilder typeBuilder) { + + final String outerCls = Types.getOuterClassName(genTOBuilder); + final StringBuilder name; + if(outerCls != null) { + name = new StringBuilder(outerCls); + } else { + name = new StringBuilder(); + } + name.append(genTOBuilder.getName()); + name.append("Builder"); + final GeneratedTOBuilderImpl unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),name.toString()); + unionBuilder.setIsUnionBuilder(true); + return unionBuilder; + } + private GeneratedTypeBuilder addDefaultInterfaceDefinition(final String packageName, final SchemaNode schemaNode, final Module module) { return addDefaultInterfaceDefinition(packageName, schemaNode, null, module); @@ -1648,10 +1628,10 @@ public class BindingGeneratorImpl implements BindingGenerator { * schemaNode. * * The new builder always implements - * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.
+ * {@link org.opendaylight.yangtools.yang.binding.DataObject DataObject}.
* If schemaNode is instance of GroupingDefinition it also * implements {@link org.opendaylight.yangtools.yang.binding.Augmentable - * Augmentable}.
+ * Augmentable}.
* If schemaNode is instance of * {@link org.opendaylight.yangtools.yang.model.api.DataNodeContainer * DataNodeContainer} it can also implement nodes which are specified in @@ -1780,7 +1760,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } else { method.append("get"); } - String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName)); + final String name = BindingMapping.toFirstUpper(BindingMapping.getPropertyName(localName)); method.append(name); return method.toString(); } @@ -1861,8 +1841,8 @@ public class BindingGeneratorImpl implements BindingGenerator { resolveLeafListSchemaNode(typeBuilder, (LeafListSchemaNode) schemaNode); } else if (schemaNode instanceof ContainerSchemaNode) { containerToGenType(module, basePackageName, typeBuilder, typeBuilder, (ContainerSchemaNode) schemaNode); - } else if (schemaNode instanceof ChoiceNode) { - choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceNode) schemaNode); + } else if (schemaNode instanceof ChoiceSchemaNode) { + choiceToGeneratedType(module, basePackageName, typeBuilder, (ChoiceSchemaNode) schemaNode); } else if (schemaNode instanceof ListSchemaNode) { listToGenType(module, basePackageName, typeBuilder, typeBuilder, (ListSchemaNode) schemaNode); } @@ -1874,7 +1854,7 @@ public class BindingGeneratorImpl implements BindingGenerator { checkArgument(typeBuilder != null, "Generated Type Builder cannot be NULL."); if (genTOBuilder != null) { - GeneratedTransferObject genTO = genTOBuilder.toInstance(); + final GeneratedTransferObject genTO = genTOBuilder.toInstance(); constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO); genCtx.get(module).addGeneratedTOBuilder(genTOBuilder); } @@ -1893,9 +1873,9 @@ public class BindingGeneratorImpl implements BindingGenerator { private List listKeys(final ListSchemaNode list) { final List listKeys = new ArrayList<>(); - List keyDefinition = list.getKeyDefinition(); + final List keyDefinition = list.getKeyDefinition(); if (keyDefinition != null) { - for (QName keyDef : keyDefinition) { + for (final QName keyDef : keyDefinition) { listKeys.add(keyDef.getLocalName()); } } @@ -1962,12 +1942,12 @@ public class BindingGeneratorImpl implements BindingGenerator { throw new IllegalStateException("No GeneratedTOBuilder objects generated from union " + typeDef); } resultTOBuilder = types.remove(0); - for (GeneratedTOBuilder genTOBuilder : types) { + for (final GeneratedTOBuilder genTOBuilder : types) { resultTOBuilder.addEnclosingTransferObject(genTOBuilder); } final GeneratedPropertyBuilder genPropBuilder = resultTOBuilder.addProperty("value"); - genPropBuilder.setReturnType(Types.primitiveType("char[]", null)); + genPropBuilder.setReturnType(Types.CHAR_ARRAY); resultTOBuilder.addEqualsIdentity(genPropBuilder); resultTOBuilder.addHashIdentity(genPropBuilder); resultTOBuilder.addToStringProperty(genPropBuilder); @@ -1977,7 +1957,7 @@ public class BindingGeneratorImpl implements BindingGenerator { packageName, typeDef, classNameFromLeaf, parentModule.getName())); } if (genTOBuilders != null && !genTOBuilders.isEmpty()) { - for (GeneratedTOBuilder genTOBuilder : genTOBuilders) { + for (final GeneratedTOBuilder genTOBuilder : genTOBuilders) { typeBuilder.addEnclosingTransferObject(genTOBuilder); } return genTOBuilders.get(0); @@ -2004,7 +1984,7 @@ public class BindingGeneratorImpl implements BindingGenerator { */ private GeneratedTypeBuilder addImplementedInterfaceFromUses(final DataNodeContainer dataNodeContainer, final GeneratedTypeBuilder builder) { - for (UsesNode usesNode : dataNodeContainer.getUses()) { + for (final UsesNode usesNode : dataNodeContainer.getUses()) { if (usesNode.getGroupingPath() != null) { final GeneratedType genType = findGroupingByPath(usesNode.getGroupingPath()).toInstance(); if (genType == null) { @@ -2042,7 +2022,7 @@ public class BindingGeneratorImpl implements BindingGenerator { } } sb.append(NEW_LINE); - sb.append("
(Source path: "); + sb.append("
(Source path: "); sb.append(moduleSourcePath); sb.append("):"); sb.append(NEW_LINE); @@ -2070,18 +2050,12 @@ public class BindingGeneratorImpl implements BindingGenerator { 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]; + final 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("

"); @@ -2089,7 +2063,7 @@ public class BindingGeneratorImpl implements BindingGenerator { sb.append(module.getName()); sb.append(""); sb.append(NEW_LINE); - sb.append("
(Source path: "); + sb.append("
(Source path: "); sb.append(module.getModuleSourcePath()); sb.append("):"); sb.append(NEW_LINE); @@ -2111,11 +2085,16 @@ public class BindingGeneratorImpl implements BindingGenerator { sb.append(NEW_LINE); sb.append("@see "); sb.append(linkToBuilderClass); + sb.append(NEW_LINE); if (schemaNode instanceof ListSchemaNode) { - sb.append("@see "); - sb.append(linkToKeyClass); + final List keyDef = ((ListSchemaNode)schemaNode).getKeyDefinition(); + if (keyDef != null && !keyDef.isEmpty()) { + sb.append("@see "); + sb.append(className); + sb.append("Key"); + } + sb.append(NEW_LINE); } - sb.append(NEW_LINE); } } @@ -2149,7 +2128,7 @@ public class BindingGeneratorImpl implements BindingGenerator { sb.append(module.getName()); sb.append(""); sb.append(NEW_LINE); - sb.append("
Source path: "); + sb.append("
Source path: "); sb.append(module.getModuleSourcePath()); sb.append("):"); sb.append(NEW_LINE); @@ -2163,8 +2142,8 @@ public class BindingGeneratorImpl implements BindingGenerator { } private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) { - for (ModuleContext ctx : genCtx.values()) { - GeneratedTypeBuilder result = ctx.getChildNode(path); + for (final ModuleContext ctx : genCtx.values()) { + final GeneratedTypeBuilder result = ctx.getChildNode(path); if (result != null) { return result; } @@ -2173,8 +2152,8 @@ public class BindingGeneratorImpl implements BindingGenerator { } private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) { - for (ModuleContext ctx : genCtx.values()) { - GeneratedTypeBuilder result = ctx.getGrouping(path); + for (final ModuleContext ctx : genCtx.values()) { + final GeneratedTypeBuilder result = ctx.getGrouping(path); if (result != null) { return result; } @@ -2183,8 +2162,8 @@ public class BindingGeneratorImpl implements BindingGenerator { } private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) { - for (ModuleContext ctx : genCtx.values()) { - GeneratedTypeBuilder result = ctx.getCase(path); + for (final ModuleContext ctx : genCtx.values()) { + final GeneratedTypeBuilder result = ctx.getCase(path); if (result != null) { return result; }