--- /dev/null
+module test-import {
+
+ yang-version 1;
+ namespace "urn:test:simple:test:import";
+ prefix "test-import";
+
+ revision 2017-04-21;
+
+ container my-import-cont {
+ }
+}
--- /dev/null
+module test {
+
+ yang-version 1;
+ namespace "urn:test:simple:test";
+ prefix "test";
+
+ import test-import { prefix "imported-test"; revision-date 2017-04-21; }
+
+ revision 2017-02-06;
+
+ typedef my-type {
+ type int8;
+ }
+
+ container my-cont {
+ }
+}
import java.util.Set;
import java.util.concurrent.Callable;
import org.opendaylight.yangtools.concepts.Codec;
+import org.opendaylight.yangtools.sal.binding.yang.types.BaseYangTypes;
import org.opendaylight.yangtools.yang.binding.BindingMapping;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
final class UnionTypeCodec extends ReflectionBasedCodec {
private final ImmutableSet<UnionValueOptionContext> typeCodecs;
private UnionTypeCodec(final Class<?> unionCls,final Set<UnionValueOptionContext> codecs) {
super(unionCls);
- typeCodecs = ImmutableSet.copyOf(codecs);
+ this.typeCodecs = ImmutableSet.copyOf(codecs);
}
static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
final BindingCodecContext bindingCodecContext) {
return () -> {
final Set<UnionValueOptionContext> values = new LinkedHashSet<>();
- for (TypeDefinition<?> subtype : unionType.getTypes()) {
- Method valueGetter = unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
- Class<?> valueType = valueGetter.getReturnType();
- Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
- values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
+ for (final TypeDefinition<?> subtype : unionType.getTypes()) {
+ if (subtype instanceof LeafrefTypeDefinition) {
+ addLeafrefValueCodec(unionCls, unionType, bindingCodecContext, values, subtype);
+ } else {
+ final Method valueGetter =
+ unionCls.getMethod("get" + BindingMapping.getClassName(subtype.getQName()));
+ final Class<?> valueType = valueGetter.getReturnType();
+ final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(valueType, subtype);
+
+ values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, valueCodec));
+ }
}
return new UnionTypeCodec(unionCls, values);
};
}
+ /**
+ * Prepare codec for type from leaf's return type of leafref.
+ *
+ * @param unionCls
+ * - union class
+ * @param unionType
+ * - union type
+ * @param bindingCodecContext
+ * - binding codec context
+ * @param values
+ * - union values
+ * @param subtype
+ * - subtype of union
+ * @throws NoSuchMethodException
+ */
+ private static void addLeafrefValueCodec(final Class<?> unionCls, final UnionTypeDefinition unionType,
+ final BindingCodecContext bindingCodecContext, final Set<UnionValueOptionContext> values,
+ final TypeDefinition<?> subtype) throws NoSuchMethodException {
+ final SchemaContext schemaContext = bindingCodecContext.getRuntimeContext().getSchemaContext();
+ final Module module = schemaContext.findModuleByNamespaceAndRevision(
+ subtype.getQName().getNamespace(), subtype.getQName().getRevision());
+ final RevisionAwareXPath xpath = ((LeafrefTypeDefinition) subtype).getPathStatement();
+ // find schema node in schema context by xpath of leafref
+ final SchemaNode dataNode;
+ if (xpath.isAbsolute()) {
+ dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath);
+ } else {
+ dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module,
+ unionType, xpath);
+ }
+ final String className = BindingMapping.getClassName(unionCls.getSimpleName());
+ final LeafSchemaNode typeNode = (LeafSchemaNode) dataNode;
+
+ // prepare name of type form return type of referenced leaf
+ final String typeName = BindingMapping.getClassName(BaseYangTypes.BASE_YANG_TYPES_PROVIDER
+ .javaTypeForSchemaDefinitionType(typeNode.getType(), typeNode).getName());
+
+ // get method via reflection from generated code according to
+ // get_TypeName_Value method
+ final Method valueGetterParent = unionCls.getMethod(
+ new StringBuilder("get").append(typeName).append(className).append("Value").toString());
+ final Class<?> returnType = valueGetterParent.getReturnType();
+
+ // prepare codec of union subtype according to return type of referenced
+ // leaf
+ final Codec<Object, Object> valueCodec = bindingCodecContext.getCodec(returnType, subtype);
+ values.add(new UnionValueOptionContext(unionCls, returnType, valueGetterParent, valueCodec));
+ }
+
@Override
public Object deserialize(final Object input) {
- for (UnionValueOptionContext member : typeCodecs) {
+ for (final UnionValueOptionContext member : this.typeCodecs) {
final Object ret = member.deserializeUnion(input);
if (ret != null) {
return ret;
@Override
public Object serialize(final Object input) {
if (input != null) {
- for (UnionValueOptionContext valCtx : typeCodecs) {
+ for (final UnionValueOptionContext valCtx : this.typeCodecs) {
final Object domValue = valCtx.serialize(input);
if (domValue != null) {
return domValue;
--- /dev/null
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.binding.data.codec.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Map.Entry;
+import javassist.ClassPool;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.Cont;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.Cont.Ref;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContBuilder;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContInt32;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContInt32.RefUnionInt32;
+import org.opendaylight.yang.gen.v1.bug8449.rev180405.ContInt32Builder;
+import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
+import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public class LeafrefSerializeDeserializeTest extends AbstractBindingRuntimeTest {
+
+ private BindingNormalizedNodeCodecRegistry registry;
+
+ @Override
+ @Before
+ public void setup() {
+ super.setup();
+ final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
+ this.registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
+ this.registry.onBindingRuntimeContextUpdated(getRuntimeContext());
+ }
+
+ @Test
+ public void listReferenceTest() {
+ final YangInstanceIdentifier contYII = YangInstanceIdentifier.builder().node(Cont.QNAME).build();
+ final InstanceIdentifier<?> fromYangInstanceIdentifier = this.registry.fromYangInstanceIdentifier(contYII);
+ assertNotNull(fromYangInstanceIdentifier);
+
+ final InstanceIdentifier<Cont> BA_II_CONT = InstanceIdentifier.builder(Cont.class).build();
+ final Ref refVal = new Ref("myvalue");
+ final Cont data = new ContBuilder().setRef(refVal).build();
+ final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
+ this.registry.toNormalizedNode(BA_II_CONT, data);
+ assertNotNull(normalizedNode);
+
+ final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode =
+ this.registry.fromNormalizedNode(contYII, normalizedNode.getValue());
+ assertNotNull(fromNormalizedNode);
+ final Cont value = (Cont) fromNormalizedNode.getValue();
+ assertEquals(refVal, value.getRef());
+ }
+
+ @Test
+ public void uint32LeafrefTest() {
+ final YangInstanceIdentifier contYII = YangInstanceIdentifier.builder().node(ContInt32.QNAME).build();
+ final InstanceIdentifier<?> fromYangInstanceIdentifier = this.registry.fromYangInstanceIdentifier(contYII);
+ assertNotNull(fromYangInstanceIdentifier);
+
+ final InstanceIdentifier<ContInt32> BA_II_CONT = InstanceIdentifier.builder(ContInt32.class).build();
+ final RefUnionInt32 refVal = new RefUnionInt32(5l);
+ final ContInt32 data = new ContInt32Builder().setRefUnionInt32(refVal).build();
+ final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> normalizedNode =
+ this.registry.toNormalizedNode(BA_II_CONT, data);
+ assertNotNull(normalizedNode);
+
+ final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode =
+ this.registry.fromNormalizedNode(contYII, normalizedNode.getValue());
+ assertNotNull(fromNormalizedNode);
+ final ContInt32 value = (ContInt32) fromNormalizedNode.getValue();
+ assertEquals(refVal, value.getRefUnionInt32());
+ }
+}
public List<Type> generateTypes(final SchemaContext context) {
checkArgument(context != null, "Schema Context reference cannot be NULL.");
checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
- schemaContext = context;
- typeProvider = new TypeProviderImpl(context);
+ this.schemaContext = context;
+ this.typeProvider = new TypeProviderImpl(context);
final Set<Module> modules = context.getModules();
return generateTypes(context, modules);
}
checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
checkArgument(modules != null, "Set of Modules cannot be NULL.");
- schemaContext = context;
- typeProvider = new TypeProviderImpl(context);
+ this.schemaContext = context;
+ this.typeProvider = new TypeProviderImpl(context);
final Module[] modulesArray = new Module[context.getModules().size()];
context.getModules().toArray(modulesArray);
final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
- genTypeBuilders = new HashMap<>();
+ this.genTypeBuilders = new HashMap<>();
for (final Module contextModule : contextModules) {
moduleToGenTypes(contextModule, context);
final List<Type> filteredGenTypes = new ArrayList<>();
for (final Module m : modules) {
- final ModuleContext ctx = checkNotNull(genCtx.get(m), "Module context not found for module %s", m);
+ final ModuleContext ctx = checkNotNull(this.genCtx.get(m), "Module context not found for module %s", m);
filteredGenTypes.addAll(ctx.getGeneratedTypes());
- final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
+ final Set<Type> additionalTypes = ((TypeProviderImpl) this.typeProvider).getAdditionalTypes().get(m);
if (additionalTypes != null) {
filteredGenTypes.addAll(additionalTypes);
}
}
private void moduleToGenTypes(final Module m, final SchemaContext context) {
- genCtx.put(m, new ModuleContext());
+ this.genCtx.put(m, new ModuleContext());
allTypeDefinitionsToGenTypes(m);
groupingsToGenTypes(m, m.getGroupings());
rpcMethodsToGenType(m);
if (!m.getChildNodes().isEmpty()) {
final GeneratedTypeBuilder moduleType = moduleToDataType(m);
- genCtx.get(m).addModuleNode(moduleType);
+ this.genCtx.get(m).addModuleNode(moduleType);
final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
resolveDataSchemaNodes(m, basePackageName, moduleType, moduleType, m.getChildNodes());
}
for (final TypeDefinition<?> typedef : typeDefinitions) {
if (typedef != null) {
- final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
+ final Type type = ((TypeProviderImpl) this.typeProvider).generatedTypeForExtendedDefinitionType(typedef,
typedef);
if (type != null) {
- final ModuleContext ctx = genCtx.get(module);
+ final ModuleContext ctx = this.genCtx.get(module);
ctx.addTypedefType(typedef.getPath(), type);
ctx.addTypeToSchema(type,typedef);
}
genType.setReference(node.getReference());
genType.setSchemaPath(node.getPath().getPathFromRoot());
if (node instanceof DataNodeContainer) {
- genCtx.get(module).addChildNodeType(node, genType);
+ this.genCtx.get(module).addChildNodeType(node, genType);
groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
processUsesAugments((DataNodeContainer) node, module);
}
inType.addImplementsType(augmentable(inType));
annotateDeprecatedIfNecessary(rpc.getStatus(), inType);
resolveDataSchemaNodes(module, basePackageName, inType, inType, input.getChildNodes());
- genCtx.get(module).addChildNodeType(input, inType);
+ this.genCtx.get(module).addChildNodeType(input, inType);
final GeneratedType inTypeInstance = inType.toInstance();
method.addParameter(inTypeInstance, "input");
}
outType.addImplementsType(augmentable(outType));
annotateDeprecatedIfNecessary(rpc.getStatus(), outType);
resolveDataSchemaNodes(module, basePackageName, outType, outType, output.getChildNodes());
- genCtx.get(module).addChildNodeType(output, outType);
+ this.genCtx.get(module).addChildNodeType(output, outType);
outTypeInstance = outType.toInstance();
}
}
}
- genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
+ this.genCtx.get(module).addTopLevelNodeType(interfaceBuilder);
}
- private static boolean isExplicitStatement(ContainerSchemaNode node) {
+ private static boolean isExplicitStatement(final ContainerSchemaNode node) {
return node instanceof EffectiveStatement
&& ((EffectiveStatement) node).getDeclared().getStatementSource() == StatementSource.DECLARATION;
}
notification, null, module);
annotateDeprecatedIfNecessary(notification.getStatus(), notificationInterface);
notificationInterface.addImplementsType(NOTIFICATION);
- genCtx.get(module).addChildNodeType(notification, notificationInterface);
+ this.genCtx.get(module).addChildNodeType(notification, notificationInterface);
// Notification object
resolveDataSchemaNodes(module, basePackageName, notificationInterface, notificationInterface,
}
listenerInterface.setDescription(createDescription(notifications, module.getName()));
- genCtx.get(module).addTopLevelNodeType(listenerInterface);
+ this.genCtx.get(module).addTopLevelNodeType(listenerInterface);
}
/**
final QName qname = identity.getQName();
qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, qname);
- genCtx.get(module).addIdentityType(identity.getQName(), newType);
+ this.genCtx.get(module).addIdentityType(identity.getQName(), newType);
}
private static Constant qnameConstant(final GeneratedTypeBuilderBase<?> toBuilder, final String constantName,
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);
+ this.genCtx.get(module).addGroupingType(grouping.getPath(), genType);
resolveDataSchemaNodes(module, basePackageName, genType, genType, grouping.getChildNodes());
groupingsToGenTypes(module, grouping.getGroupings());
processUsesAugments(grouping, module);
final String enumTypedefDescription = encodeAngleBrackets(enumTypeDef.getDescription());
enumBuilder.setDescription(enumTypedefDescription);
enumBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDef);
- ModuleContext ctx = genCtx.get(module);
+ final ModuleContext ctx = this.genCtx.get(module);
ctx.addInnerTypedefType(enumTypeDef.getPath(), enumBuilder);
return enumBuilder;
}
final SchemaPath targetPath = augSchema.getTargetPath();
SchemaNode targetSchemaNode = null;
- targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
+ targetSchemaNode = findDataSchemaNode(this.schemaContext, targetPath);
if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
if (targetSchemaNode instanceof DerivableSchemaNode) {
targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull();
* @return node from its original location in grouping
*/
private DataSchemaNode findOriginalTargetFromGrouping(final SchemaPath targetPath, final UsesNode parentUsesNode) {
- final SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, parentUsesNode.getGroupingPath()
+ final SchemaNode targetGrouping = findNodeInSchemaContext(this.schemaContext, parentUsesNode.getGroupingPath()
.getPathFromRoot());
if (!(targetGrouping instanceof GroupingDefinition)) {
throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode);
}
if (result instanceof DerivableSchemaNode) {
- DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
- Optional<? extends SchemaNode> originalNode = castedResult
+ final DerivableSchemaNode castedResult = (DerivableSchemaNode) result;
+ final Optional<? extends SchemaNode> originalNode = castedResult
.getOriginal();
if (castedResult.isAddedByUses() && originalNode.isPresent()) {
result = originalNode.get();
}
if (result instanceof DataSchemaNode) {
- DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
+ final DataSchemaNode resultDataSchemaNode = (DataSchemaNode) result;
if (resultDataSchemaNode.isAddedByUses()) {
// The original node is required, but we have only the copy of
// the original node.
*/
private GeneratedTypeBuilder addRawAugmentGenTypeDefinition(final Module module, final String augmentPackageName,
final String basePackageName, final Type targetTypeRef, final AugmentationSchema augSchema) {
- Map<String, GeneratedTypeBuilder> augmentBuilders = genTypeBuilders.get(augmentPackageName);
+ Map<String, GeneratedTypeBuilder> augmentBuilders = this.genTypeBuilders.get(augmentPackageName);
if (augmentBuilders == null) {
augmentBuilders = new HashMap<>();
- genTypeBuilders.put(augmentPackageName, augmentBuilders);
+ this.genTypeBuilders.put(augmentPackageName, augmentBuilders);
}
final String augIdentifier = getAugmentIdentifier(augSchema.getUnknownSchemaNodes());
augmentBuilders.put(augTypeName, augTypeBuilder);
if (!augSchema.getChildNodes().isEmpty()) {
- genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
+ this.genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema);
}
- genCtx.get(module).addAugmentType(augTypeBuilder);
+ this.genCtx.get(module).addAugmentType(augTypeBuilder);
return augTypeBuilder;
}
choiceNode.getDescription(), choiceTypeBuilder, choiceNode.getStatus());
choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
annotateDeprecatedIfNecessary(choiceNode.getStatus(), choiceTypeBuilder);
- genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
+ this.genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
}
}
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);
+ this.genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
+ this.genCtx.get(module).addChoiceToCaseMapping(refChoiceType, caseTypeBuilder, caseNode);
final Iterable<DataSchemaNode> caseChildNodes = caseNode.getChildNodes();
if (caseChildNodes != null) {
final SchemaPath choiceNodeParentPath = choiceNode.getPath().getParent();
if (!Iterables.isEmpty(choiceNodeParentPath.getPathFromRoot())) {
- SchemaNode parent = findDataSchemaNode(schemaContext, choiceNodeParentPath);
+ SchemaNode parent = findDataSchemaNode(this.schemaContext, choiceNodeParentPath);
if (parent instanceof AugmentationSchema) {
final AugmentationSchema augSchema = (AugmentationSchema) parent;
final SchemaPath targetPath = augSchema.getTargetPath();
- SchemaNode targetSchemaNode = findDataSchemaNode(schemaContext, targetPath);
+ SchemaNode targetSchemaNode = findDataSchemaNode(this.schemaContext, targetPath);
if (targetSchemaNode instanceof DataSchemaNode
&& ((DataSchemaNode) targetSchemaNode).isAddedByUses()) {
if (targetSchemaNode instanceof DerivableSchemaNode) {
SchemaNode parent = null;
final SchemaPath nodeSp = targetNode.getPath();
- parent = findDataSchemaNode(schemaContext, nodeSp.getParent());
+ parent = findDataSchemaNode(this.schemaContext, nodeSp.getParent());
GeneratedTypeBuilder childOfType = null;
if (parent instanceof Module) {
- childOfType = genCtx.get(parent).getModuleNode();
+ childOfType = this.genCtx.get(parent).getModuleNode();
} else if (parent instanceof ChoiceCaseNode) {
childOfType = findCaseByPath(parent.getPath());
} else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) {
node = (ChoiceCaseNode) caseNode;
} else if (targetNode.getCaseNodeByName(caseLocalName) == null) {
final String targetNodeLocalName = targetNode.getQName().getLocalName();
- for (DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) {
+ for (final DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) {
if (dataSchemaNode instanceof ChoiceSchemaNode && targetNodeLocalName.equals(dataSchemaNode.getQName
().getLocalName())) {
node = ((ChoiceSchemaNode) dataSchemaNode).getCaseNodeByName(caseLocalName);
if (childNodes != null) {
resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, childNodes);
}
- genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
- genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
+ this.genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder);
+ this.genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node);
}
}
}
return null;
}
- final Module parentModule = findParentModule(schemaContext, leaf);
+ final Module parentModule = findParentModule(this.schemaContext, leaf);
Type returnType = null;
final TypeDefinition<?> typeDef = CompatUtils.compatLeafType(leaf);
if (isInnerType(leaf, typeDef)) {
if (typeDef instanceof EnumTypeDefinition) {
- returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
+ returnType = this.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);
}
- ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
+ ((TypeProviderImpl) this.typeProvider).putReferencedType(leaf.getPath(), returnType);
} else if (typeDef instanceof UnionTypeDefinition) {
- GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
+ final 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);
+ final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
if (genTOBuilder != null) {
returnType = genTOBuilder.toInstance();
}
// 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,
+ returnType = this.typeProvider.javaTypeForSchemaDefinitionType(getBaseOrDeclaredType(typeDef), leaf,
restrictions);
}
} else {
final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
- returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
+ returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
}
if (returnType == null) {
}
if (typeDef instanceof EnumTypeDefinition) {
- ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
+ ((TypeProviderImpl) this.typeProvider).putReferencedType(leaf.getPath(), returnType);
}
String leafDesc = leaf.getDescription();
private Module findModuleFromImports(final Set<ModuleImport> imports, final String prefix) {
for (final ModuleImport imp : imports) {
if (imp.getPrefix().equals(prefix)) {
- return schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
+ return this.schemaContext.findModuleByName(imp.getModuleName(), imp.getRevision());
}
}
return null;
// GeneratedType for this type definition should be already
// created
final QName qname = typeDef.getQName();
- final Module unionModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
+ final Module unionModule = this.schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
qname.getRevision());
- final ModuleContext mc = genCtx.get(unionModule);
+ final ModuleContext mc = this.genCtx.get(unionModule);
returnType = mc.getTypedefs().get(typeDef.getPath());
} 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();
- final Module enumModule = schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
+ final LeafSchemaNode originalLeaf = (LeafSchemaNode) SchemaNodeUtils.getRootOriginalIfPossible(leaf);
+ final QName qname = originalLeaf.getQName();
+ final Module enumModule = this.schemaContext.findModuleByNamespaceAndRevision(qname.getNamespace(),
qname.getRevision());
- returnType = genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
+ returnType = this.genCtx.get(enumModule).getInnerType(originalLeaf.getType().getPath());
} else {
- returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
+ returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
}
return resolveLeafSchemaNodeAsProperty(toBuilder, leaf, returnType, isReadOnly);
}
}
final TypeDefinition<?> typeDef = node.getType();
- final Module parentModule = findParentModule(schemaContext, node);
+ final Module parentModule = findParentModule(this.schemaContext, node);
Type returnType = null;
if (typeDef.getBaseType() == null) {
if (typeDef instanceof EnumTypeDefinition) {
- returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node);
+ returnType = this.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);
+ ((TypeProviderImpl) this.typeProvider).putReferencedType(node.getPath(), returnType);
} else if (typeDef instanceof UnionTypeDefinition) {
final GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, node, parentModule);
if (genTOBuilder != null) {
returnType = genTOBuilder.toInstance();
} else {
final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
- returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
+ returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
}
} else {
final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
- returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
+ returnType = this.typeProvider.javaTypeForSchemaDefinitionType(typeDef, node, restrictions);
}
final ParameterizedType listType = Types.listTypeFor(returnType);
method.setAccessModifier(AccessModifier.PUBLIC);
method.setStatic(true);
- final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
+ final Set<Type> types = ((TypeProviderImpl) this.typeProvider).getAdditionalTypes().get(parentModule);
if (types == null) {
- ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
+ ((TypeProviderImpl) this.typeProvider).getAdditionalTypes().put(parentModule,
Sets.<Type> newHashSet(unionBuilder.toInstance()));
} else {
types.add(unionBuilder.toInstance());
// FIXME: Validation of name conflict
final GeneratedTypeBuilderImpl newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
- final Module module = findParentModule(schemaContext, schemaNode);
+ final Module module = findParentModule(this.schemaContext, schemaNode);
qnameConstant(newType, BindingMapping.QNAME_STATIC_FIELD_NAME, schemaNode.getQName());
newType.addComment(schemaNode.getDescription());
newType.setDescription(createDescription(schemaNode, newType.getFullyQualifiedName()));
newType.setSchemaPath(schemaNode.getPath().getPathFromRoot());
newType.setModuleName(module.getName());
- if (!genTypeBuilders.containsKey(packageName)) {
+ if (!this.genTypeBuilders.containsKey(packageName)) {
final Map<String, GeneratedTypeBuilder> builders = new HashMap<>();
builders.put(genTypeName, newType);
- genTypeBuilders.put(packageName, builders);
+ this.genTypeBuilders.put(packageName, builders);
} else {
- final Map<String, GeneratedTypeBuilder> builders = genTypeBuilders.get(packageName);
+ final Map<String, GeneratedTypeBuilder> builders = this.genTypeBuilders.get(packageName);
if (!builders.containsKey(genTypeName)) {
builders.put(genTypeName, newType);
}
if (genTOBuilder != null) {
final GeneratedTransferObject genTO = genTOBuilder.toInstance();
constructGetter(typeBuilder, "key", "Returns Primary Key of Yang List Type", genTO, Status.CURRENT);
- genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
+ this.genCtx.get(module).addGeneratedTOBuilder(genTOBuilder);
}
}
final List<GeneratedTOBuilder> genTOBuilders = new ArrayList<>();
final String packageName = typeBuilder.getFullyQualifiedName();
if (typeDef instanceof UnionTypeDefinition) {
- final List<GeneratedTOBuilder> types = ((TypeProviderImpl) typeProvider)
+ final List<GeneratedTOBuilder> types = ((TypeProviderImpl) this.typeProvider)
.provideGeneratedTOBuildersForUnionTypeDef(packageName, ((UnionTypeDefinition) typeDef),
classNameFromLeaf, leaf);
genTOBuilders.addAll(types);
resultTOBuilder.addToStringProperty(genPropBuilder);
} else if (typeDef instanceof BitsTypeDefinition) {
- genTOBuilders.add((((TypeProviderImpl) typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
+ genTOBuilders.add((((TypeProviderImpl) this.typeProvider)).provideGeneratedTOBuilderForBitsTypeDefinition(
packageName, typeDef, classNameFromLeaf, parentModule.getName()));
}
if (!genTOBuilders.isEmpty()) {
}
sb.append(NEW_LINE);
- if (verboseClassComments) {
+ if (this.verboseClassComments) {
sb.append("<pre>");
sb.append(NEW_LINE);
sb.append(encodeAngleBrackets(YangTemplate.generateYangSnipet(schemaNodes)));
sb.append(NEW_LINE);
}
- if (verboseClassComments) {
- final Module module = findParentModule(schemaContext, schemaNode);
+ if (this.verboseClassComments) {
+ final Module module = findParentModule(this.schemaContext, schemaNode);
final StringBuilder linkToBuilderClass = new StringBuilder();
final String[] namespace = Iterables.toArray(BSDOT_SPLITTER.split(fullyQualifiedName), String.class);
final String className = namespace[namespace.length - 1];
sb.append(NEW_LINE);
}
- if (verboseClassComments) {
+ if (this.verboseClassComments) {
sb.append("<p>");
sb.append("This class represents the following YANG schema fragment defined in module <b>");
sb.append(module.getName());
}
private GeneratedTypeBuilder findChildNodeByPath(final SchemaPath path) {
- for (final ModuleContext ctx : genCtx.values()) {
+ for (final ModuleContext ctx : this.genCtx.values()) {
final GeneratedTypeBuilder result = ctx.getChildNode(path);
if (result != null) {
return result;
}
private GeneratedTypeBuilder findGroupingByPath(final SchemaPath path) {
- for (final ModuleContext ctx : genCtx.values()) {
+ for (final ModuleContext ctx : this.genCtx.values()) {
final GeneratedTypeBuilder result = ctx.getGrouping(path);
if (result != null) {
return result;
}
private GeneratedTypeBuilder findCaseByPath(final SchemaPath path) {
- for (final ModuleContext ctx : genCtx.values()) {
+ for (final ModuleContext ctx : this.genCtx.values()) {
final GeneratedTypeBuilder result = ctx.getCase(path);
if (result != null) {
return result;
}
public Map<Module, ModuleContext> getModuleContexts() {
- return genCtx;
+ return this.genCtx;
}
@VisibleForTesting
Preconditions.checkArgument(refTypePath != null,
"Path reference of Enumeration Type Definition cannot be NULL!");
Preconditions.checkArgument(refType != null, "Reference to Enumeration Type cannot be NULL!");
- referencedTypes.put(refTypePath, refType);
+ this.referencedTypes.put(refTypePath, refType);
}
public Map<Module, Set<Type>> getAdditionalTypes() {
- return additionalTypes;
+ return this.additionalTypes;
}
/**
Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
Preconditions.checkArgument(typeDefinition.getQName() != null,
"Type Definition cannot have non specified QName (QName cannot be NULL!)");
- String typedefName = typeDefinition.getQName().getLocalName();
+ final String typedefName = typeDefinition.getQName().getLocalName();
Preconditions.checkArgument(typedefName != null, "Type Definitions Local Name cannot be NULL!");
// Deal with base types
Type returnType = javaTypeForExtendedType(typeDefinition);
if (r != null && returnType instanceof GeneratedTransferObject) {
- GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
- Module module = findParentModule(schemaContext, parentNode);
- String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typeDefinition.getPath());
- String genTOName = BindingMapping.getClassName(typedefName);
- String name = packageName + "." + genTOName;
+ final GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
+ final Module module = findParentModule(this.schemaContext, parentNode);
+ final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
+ final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typeDefinition.getPath());
+ final String genTOName = BindingMapping.getClassName(typedefName);
+ final String name = packageName + "." + genTOName;
if (!(returnType.getFullyQualifiedName().equals(name))) {
returnType = shadedTOWithRestrictions(gto, r);
}
}
private static GeneratedTransferObject shadedTOWithRestrictions(final GeneratedTransferObject gto, final Restrictions r) {
- GeneratedTOBuilder gtob = new GeneratedTOBuilderImpl(gto.getPackageName(), gto.getName());
- GeneratedTransferObject parent = gto.getSuperType();
+ final GeneratedTOBuilder gtob = new GeneratedTOBuilderImpl(gto.getPackageName(), gto.getName());
+ final GeneratedTransferObject parent = gto.getSuperType();
if (parent != null) {
gtob.setExtendsType(parent);
}
gtob.setRestrictions(r);
- for (GeneratedProperty gp : gto.getProperties()) {
- GeneratedPropertyBuilder gpb = gtob.addProperty(gp.getName());
+ for (final GeneratedProperty gp : gto.getProperties()) {
+ final GeneratedPropertyBuilder gpb = gtob.addProperty(gp.getName());
gpb.setValue(gp.getValue());
gpb.setReadOnly(gp.isReadOnly());
gpb.setAccessModifier(gp.getAccessModifier());
while (iterator.hasNext() && !isAugmenting) {
final QName next = iterator.next();
if (current == null) {
- dataChildByName = schemaContext.getDataChildByName(next);
+ dataChildByName = this.schemaContext.getDataChildByName(next);
} else {
dataChildByName = current.getDataChildByName(next);
}
}
/////
- Module parentModule = getParentModule(parentNode);
+ final Module parentModule = getParentModule(parentNode);
if (!leafRefStrippedXPath.isAbsolute()) {
- leafRefValueNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule,
+ leafRefValueNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(this.schemaContext, parentModule,
parentNode, leafRefStrippedXPath);
} else {
- leafRefValueNode = SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, leafRefStrippedXPath);
+ leafRefValueNode = SchemaContextUtil.findDataSchemaNode(this.schemaContext, parentModule, leafRefStrippedXPath);
}
return (leafRefValueNode != null) ? leafRefValueNode.equals(parentNode) : false;
}
final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypeDef;
returnType = provideTypeForEnum(enumTypeDef, typedefName, typeDefinition);
} else {
- final Module module = findParentModule(schemaContext, typeDefinition);
- Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
+ final Module module = findParentModule(this.schemaContext, typeDefinition);
+ final Restrictions r = BindingGeneratorUtil.getRestrictions(typeDefinition);
if (module != null) {
- final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
+ final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(module.getName());
final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
if (genTOs != null) {
returnType = genTOs.get(typedefName);
* <code>idref</code>
*/
private Type provideTypeForIdentityref(final IdentityrefTypeDefinition idref) {
- QName baseIdQName = idref.getIdentity().getQName();
- Module module = schemaContext.findModuleByNamespaceAndRevision(baseIdQName.getNamespace(),
+ final QName baseIdQName = idref.getIdentity().getQName();
+ final Module module = this.schemaContext.findModuleByNamespaceAndRevision(baseIdQName.getNamespace(),
baseIdQName.getRevision());
IdentitySchemaNode identity = null;
- for (IdentitySchemaNode id : module.getIdentities()) {
+ for (final IdentitySchemaNode id : module.getIdentities()) {
if (id.getQName().equals(baseIdQName)) {
identity = id;
}
final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, identity.getPath());
final String genTypeName = BindingMapping.getClassName(identity.getQName());
- Type baseType = Types.typeForClass(Class.class);
- Type paramType = Types.wildcardTypeFor(packageName, genTypeName);
+ final Type baseType = Types.typeForClass(Class.class);
+ final Type paramType = Types.wildcardTypeFor(packageName, genTypeName);
return Types.parameterizedTypeFor(baseType, paramType);
}
final TypeDefinition<?> baseTypeDef = baseTypeDefForExtendedType(typeDefinition);
if (!(baseTypeDef instanceof LeafrefTypeDefinition) && !(baseTypeDef instanceof IdentityrefTypeDefinition)) {
- final Module module = findParentModule(schemaContext, parentNode);
+ final Module module = findParentModule(this.schemaContext, parentNode);
if (module != null) {
- final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(module.getName());
+ final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(module.getName());
final Map<String, Type> genTOs = modulesByDate.get(module.getRevision());
if (genTOs != null) {
return genTOs.get(typeDefinition.getQName().getLocalName());
if (strXPath != null) {
if (strXPath.indexOf('[') == -1) {
- final Module module = findParentModule(schemaContext, parentNode);
+ final Module module = findParentModule(this.schemaContext, parentNode);
Preconditions.checkArgument(module != null, "Failed to find module for parent %s", parentNode);
final SchemaNode dataNode;
if (xpath.isAbsolute()) {
- dataNode = findDataSchemaNode(schemaContext, module, xpath);
+ dataNode = findDataSchemaNode(this.schemaContext, module, xpath);
} else {
- dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+ dataNode = findDataSchemaNodeForRelativeXPath(this.schemaContext, module, parentNode, xpath);
}
Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
if (leafContainsEnumDefinition(dataNode)) {
- returnType = referencedTypes.get(dataNode.getPath());
+ returnType = this.referencedTypes.get(dataNode.getPath());
} else if (leafListContainsEnumDefinition(dataNode)) {
- returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
+ returnType = Types.listTypeFor(this.referencedTypes.get(dataNode.getPath()));
} else {
returnType = resolveTypeFromDataSchemaNode(dataNode);
}
final String enumerationName = BindingMapping.getClassName(enumName);
- Module module = findParentModule(schemaContext, parentNode);
+ final Module module = findParentModule(this.schemaContext, parentNode);
final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
final EnumerationBuilderImpl enumBuilder = new EnumerationBuilderImpl(basePackageName, enumerationName);
*
*/
private void resolveTypeDefsFromContext() {
- final Set<Module> modules = schemaContext.getModules();
+ final Set<Module> modules = this.schemaContext.getModules();
Preconditions.checkArgument(modules != null, "Set of Modules cannot be NULL!");
final Module[] modulesArray = new Module[modules.size()];
int i = 0;
- for (Module modul : modules) {
+ for (final Module modul : modules) {
modulesArray[i++] = modul;
}
final List<Module> modulesSortedByDependency = org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort
.sort(modulesArray);
for (final Module module : modulesSortedByDependency) {
- Map<Date, Map<String, Type>> dateTypeMap = genTypeDefsContextMap.get(module.getName());
+ Map<Date, Map<String, Type>> dateTypeMap = this.genTypeDefsContextMap.get(module.getName());
if (dateTypeMap == null) {
dateTypeMap = new HashMap<>();
}
dateTypeMap.put(module.getRevision(), Collections.<String, Type>emptyMap());
- genTypeDefsContextMap.put(module.getName(), dateTypeMap);
+ this.genTypeDefsContextMap.put(module.getName(), dateTypeMap);
}
for (final Module module : modulesSortedByDependency) {
* <code>modulName</code> or <code>typedef</code> or Q name of
* <code>typedef</code> equals <code>null</code>
*/
- private Type typedefToGeneratedType(final String basePackageName, final Module module, final TypeDefinition<?> typedef) {
+ private Type typedefToGeneratedType(final String basePackageName, final Module module,
+ final TypeDefinition<?> typedef) {
final String moduleName = module.getName();
final Date moduleRevision = module.getRevision();
if ((basePackageName != null) && (moduleName != null) && (typedef != null) && (typedef.getQName() != null)) {
makeSerializable((GeneratedTOBuilderImpl) genTOBuilder);
returnType = genTOBuilder.toInstance();
// union builder
- GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
+ final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
genTOBuilder.getName() + "Builder");
unionBuilder.setIsUnionBuilder(true);
- MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
+ final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
method.setReturnType(returnType);
method.addParameter(Types.STRING, "defaultValue");
method.setAccessModifier(AccessModifier.PUBLIC);
method.setStatic(true);
- Set<Type> types = additionalTypes.get(module);
+ Set<Type> types = this.additionalTypes.get(module);
if (types == null) {
types = Sets.<Type> newHashSet(unionBuilder.toInstance());
- additionalTypes.put(module, types);
+ this.additionalTypes.put(module, types);
} else {
types.add(unionBuilder.toInstance());
}
returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
}
if (returnType != null) {
- final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(moduleName);
+ final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(moduleName);
Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
if (typeMap != null) {
if (typeMap.isEmpty()) {
Preconditions.checkState(!builders.isEmpty(), "No GeneratedTOBuilder objects generated from union %s", typedef);
final GeneratedTOBuilder resultTOBuilder = builders.remove(0);
- for (GeneratedTOBuilder genTOBuilder : builders) {
+ for (final GeneratedTOBuilder genTOBuilder : builders) {
resultTOBuilder.addEnclosingTransferObject(genTOBuilder);
}
* <li>if Qname of <code>typedef</code> is null</li>
* </ul>
*/
- public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName, final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) {
+ public List<GeneratedTOBuilder> provideGeneratedTOBuildersForUnionTypeDef(final String basePackageName,
+ final UnionTypeDefinition typedef, final String typeDefName, final SchemaNode parentNode) {
Preconditions.checkNotNull(basePackageName, "Base Package Name cannot be NULL!");
Preconditions.checkNotNull(typedef, "Type Definition cannot be NULL!");
Preconditions.checkNotNull(typedef.getQName(), "Type definition QName cannot be NULL!");
final List<GeneratedTOBuilder> generatedTOBuilders = new ArrayList<>();
final List<TypeDefinition<?>> unionTypes = typedef.getTypes();
- final Module module = findParentModule(schemaContext, parentNode);
+ final Module module = findParentModule(this.schemaContext, parentNode);
final GeneratedTOBuilderImpl unionGenTOBuilder;
if (typeDefName != null && !typeDefName.isEmpty()) {
*
* In this case the new generated TO is created for union subtype (recursive
* call of method
- * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition,
- * String, SchemaNode)}
+ * {@link #provideGeneratedTOBuildersForUnionTypeDef(String, UnionTypeDefinition, String, SchemaNode)}
* provideGeneratedTOBuilderForUnionTypeDef} and in parent TO builder
* <code>parentUnionGenTOBuilder</code> is created property which type is
* equal to new generated TO.
* bigger one due to recursive call of
* <code>provideGeneratedTOBuildersForUnionTypeDef</code> method.
*/
- private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) {
+ private List<GeneratedTOBuilder> resolveUnionSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
+ final UnionTypeDefinition unionSubtype, final String basePackageName, final SchemaNode parentNode) {
final String newTOBuilderName = provideAvailableNameForGenTOBuilder(parentUnionGenTOBuilder.getName());
final List<GeneratedTOBuilder> subUnionGenTOBUilders = provideGeneratedTOBuildersForUnionTypeDef(
basePackageName, unionSubtype, newTOBuilderName, parentNode);
* parent Schema Node for Extended Subtype
*
*/
- private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder, final TypeDefinition<?> unionSubtype, final List<String> regularExpressions, final SchemaNode parentNode) {
+ private void resolveExtendedSubtypeAsUnion(final GeneratedTOBuilder parentUnionGenTOBuilder,
+ final TypeDefinition<?> unionSubtype, final List<String> regularExpressions, final SchemaNode parentNode) {
final String unionTypeName = unionSubtype.getQName().getLocalName();
final Type genTO = findGenTO(unionTypeName, unionSubtype);
if (genTO != null) {
} else {
final TypeDefinition<?> baseType = baseTypeDefForExtendedType(unionSubtype);
if (unionTypeName.equals(baseType.getQName().getLocalName())) {
- final Type javaType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType,
- parentNode);
+ final Type javaType =
+ BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(baseType, parentNode);
if (javaType != null) {
updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, unionTypeName);
}
+ } else if (baseType instanceof LeafrefTypeDefinition) {
+ final Type javaType = javaTypeForSchemaDefinitionType(baseType, parentNode);
+ boolean typeExist = false;
+ for (final GeneratedPropertyBuilder generatedPropertyBuilder : parentUnionGenTOBuilder
+ .getProperties()) {
+ final Type origType = ((GeneratedPropertyBuilderImpl) generatedPropertyBuilder).getReturnType();
+ if (origType != null && javaType != null && javaType == origType) {
+ typeExist = true;
+ break;
+ }
+ }
+ if (!typeExist && javaType != null) {
+ updateUnionTypeAsProperty(parentUnionGenTOBuilder, javaType, new StringBuilder(javaType.getName())
+ .append(parentUnionGenTOBuilder.getName()).append("Value").toString());
+ }
}
if (baseType instanceof StringTypeDefinition) {
regularExpressions.addAll(resolveRegExpressionsFromTypedef(unionSubtype));
* <code>null</code> it it doesn't exist
*/
private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) {
- final Module typeModule = findParentModule(schemaContext, parentNode);
+ final Module typeModule = findParentModule(this.schemaContext, parentNode);
if (typeModule != null && typeModule.getName() != null) {
- final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(typeModule.getName());
+ final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(typeModule.getName());
final Map<String, Type> genTOs = modulesByDate.get(typeModule.getRevision());
if (genTOs != null) {
return genTOs.get(searchedTypeName);
*/
private void storeGenTO(final TypeDefinition<?> newTypeDef, final GeneratedTOBuilder genTOBuilder, final SchemaNode parentNode) {
if (!(newTypeDef instanceof UnionTypeDefinition)) {
- final Module parentModule = findParentModule(schemaContext, parentNode);
+ final Module parentModule = findParentModule(this.schemaContext, parentNode);
if (parentModule != null && parentModule.getName() != null) {
- Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
- Map<String, Type> genTOsMap = modulesByDate.get(parentModule.getRevision());
+ final Map<Date, Map<String, Type>> modulesByDate = this.genTypeDefsContextMap.get(parentModule.getName());
+ final Map<String, Type> genTOsMap = modulesByDate.get(parentModule.getRevision());
genTOsMap.put(newTypeDef.getQName().getLocalName(), genTOBuilder.toInstance());
}
}
/**
* Adds a new property with the name <code>propertyName</code> and with type
- * <code>type</code> to <code>unonGenTransObject</code>.
+ * <code>type</code> to <code>unionGenTransObject</code>.
*
* @param unionGenTransObject
* generated TO to which should be property added
Preconditions.checkArgument(basePackageName != null, "Base Package Name cannot be NULL!");
if (typeDef instanceof BitsTypeDefinition) {
- BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) typeDef;
+ final BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) typeDef;
final String typeName = BindingMapping.getClassName(typeDefName);
final GeneratedTOBuilderImpl genTOBuilder = new GeneratedTOBuilderImpl(basePackageName, typeName);
final List<Bit> bitList = bitsTypeDefinition.getBits();
GeneratedPropertyBuilder genPropertyBuilder;
for (final Bit bit : bitList) {
- String name = bit.getName();
+ final String name = bit.getName();
genPropertyBuilder = genTOBuilder.addProperty(BindingMapping.getPropertyName(name));
genPropertyBuilder.setReadOnly(true);
genPropertyBuilder.setReturnType(BaseYangTypes.BOOLEAN_TYPE);
}
final List<String> regExps = new ArrayList<>(patternConstraints.size());
- for (PatternConstraint patternConstraint : patternConstraints) {
+ for (final PatternConstraint patternConstraint : patternConstraints) {
final String regEx = patternConstraint.getRegularExpression();
final String modifiedRegEx = StringEscapeUtils.escapeJava(regEx);
regExps.add(modifiedRegEx);
genTOBuilder.setSchemaPath(typedef.getPath().getPathFromRoot());
genTOBuilder.setModuleName(moduleName);
genTOBuilder.setTypedef(true);
- Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
+ final Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
genTOBuilder.setRestrictions(r);
if (typedef.getStatus() == Status.DEPRECATED) {
genTOBuilder.addAnnotation("", "Deprecated");
Map<Date, Map<String, Type>> modulesByDate = null;
Map<String, Type> typeMap = null;
- final Module parentModule = findParentModule(schemaContext, innerExtendedType);
+ final Module parentModule = findParentModule(this.schemaContext, innerExtendedType);
if (parentModule != null) {
- modulesByDate = genTypeDefsContextMap.get(parentModule.getName());
+ modulesByDate = this.genTypeDefsContextMap.get(parentModule.getName());
typeMap = modulesByDate.get(parentModule.getRevision());
}
if (typeMap != null) {
- Type type = typeMap.get(innerTypeDef);
+ final Type type = typeMap.get(innerTypeDef);
if (type instanceof GeneratedTransferObject) {
genTOBuilder.setExtendsType((GeneratedTransferObject) type);
}
*/
private static void makeSerializable(final GeneratedTOBuilderImpl gto) {
gto.addImplementsType(Types.typeForClass(Serializable.class));
- GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
+ final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("serialVersionUID");
prop.setValue(Long.toString(BindingGeneratorUtil.computeDefaultSUID(gto)));
gto.setSUID(prop);
}
*/
private static List<TypeDefinition<?>> sortTypeDefinitionAccordingDepth(
final Collection<TypeDefinition<?>> unsortedTypeDefinitions) {
- List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
+ final List<TypeDefinition<?>> sortedTypeDefinition = new ArrayList<>();
- Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
- for (TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
+ final Map<Integer, List<TypeDefinition<?>>> typeDefinitionsDepths = new TreeMap<>();
+ for (final TypeDefinition<?> unsortedTypeDefinition : unsortedTypeDefinitions) {
final int depth = getTypeDefinitionDepth(unsortedTypeDefinition);
List<TypeDefinition<?>> typeDefinitionsConcreteDepth = typeDefinitionsDepths.get(depth);
if (typeDefinitionsConcreteDepth == null) {
}
// SortedMap guarantees order corresponding to keys in ascending order
- for (List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
+ for (final List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
sortedTypeDefinition.addAll(v);
}
if (typeDefinition == null) {
return 1;
}
- TypeDefinition<?> baseType = typeDefinition.getBaseType();
+ final TypeDefinition<?> baseType = typeDefinition.getBaseType();
if (baseType == null) {
return 1;
}
if (baseType.getBaseType() != null) {
depth = depth + getTypeDefinitionDepth(baseType);
} else if (baseType instanceof UnionTypeDefinition) {
- List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
+ final List<TypeDefinition<?>> childTypeDefinitions = ((UnionTypeDefinition) baseType).getTypes();
int maxChildDepth = 0;
int childDepth = 1;
- for (TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
+ for (final TypeDefinition<?> childTypeDefinition : childTypeDefinitions) {
childDepth = childDepth + getTypeDefinitionDepth(childTypeDefinition);
if (childDepth > maxChildDepth) {
maxChildDepth = childDepth;
* @return string with the number suffix incremented by one (or 1 is added)
*/
private static String provideAvailableNameForGenTOBuilder(final String name) {
- Matcher mtch = NUMBERS_PATTERN.matcher(name);
+ final Matcher mtch = NUMBERS_PATTERN.matcher(name);
if (mtch.find()) {
final int newSuffix = Integer.valueOf(name.substring(mtch.start())) + 1;
return name.substring(0, mtch.start()) + newSuffix;
public static void addUnitsToGenTO(final GeneratedTOBuilder to, final String units) {
if (!Strings.isNullOrEmpty(units)) {
to.addConstant(Types.STRING, "_UNITS", "\"" + units + "\"");
- GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
+ final GeneratedPropertyBuilder prop = new GeneratedPropertyBuilderImpl("UNITS");
prop.setReturnType(Types.STRING);
to.addToStringProperty(prop);
}
}
public String getTypeDefaultConstruction(final LeafSchemaNode node, final String defaultValue) {
- TypeDefinition<?> type = CompatUtils.compatLeafType(node);
- QName typeQName = type.getQName();
- TypeDefinition<?> base = baseTypeDefForExtendedType(type);
+ final TypeDefinition<?> type = CompatUtils.compatLeafType(node);
+ final QName typeQName = type.getQName();
+ final TypeDefinition<?> base = baseTypeDefForExtendedType(type);
Preconditions.checkNotNull(type, "Cannot provide default construction for null type of %s", node);
Preconditions.checkNotNull(defaultValue, "Cannot provide default construction for null default statement of %s",
node);
- StringBuilder sb = new StringBuilder();
+ final StringBuilder sb = new StringBuilder();
String result = null;
if (base instanceof BinaryTypeDefinition) {
result = binaryToDef(defaultValue);
} else if (base instanceof BitsTypeDefinition) {
String parentName;
String className;
- Module parent = getParentModule(node);
- Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
+ final Module parent = getParentModule(node);
+ final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
path.next();
if (!(path.hasNext())) {
parentName = BindingMapping.getClassName(parent.getName()) + "Data";
- String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
+ final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
} else {
- String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
+ final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
+ final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
parentName = BindingMapping.getClassName(parent.getName());
className = packageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
}
} else if (base instanceof EmptyTypeDefinition) {
result = typeToDef(Boolean.class, defaultValue);
} else if (base instanceof EnumTypeDefinition) {
- char[] defValArray = defaultValue.toCharArray();
- char first = Character.toUpperCase(defaultValue.charAt(0));
+ final char[] defValArray = defaultValue.toCharArray();
+ final char first = Character.toUpperCase(defaultValue.charAt(0));
defValArray[0] = first;
- String newDefVal = new String(defValArray);
+ final String newDefVal = new String(defValArray);
String className;
if (type.getBaseType() != null) {
- Module m = getParentModule(type);
- String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
+ final Module m = getParentModule(type);
+ final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
+ final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
className = packageName + "." + BindingMapping.getClassName(typeQName);
} else {
- Module parentModule = getParentModule(node);
- String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, node.getPath());
+ final Module parentModule = getParentModule(node);
+ final String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
+ final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, node.getPath());
className = packageName + "." + BindingMapping.getClassName(node.getQName());
}
result = className + "." + newDefVal;
if (type.getBaseType() != null && !(base instanceof LeafrefTypeDefinition)
&& !(base instanceof EnumTypeDefinition) && !(base instanceof UnionTypeDefinition)) {
- Module m = getParentModule(type);
- String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
- String className = packageName + "." + BindingMapping.getClassName(typeQName);
+ final Module m = getParentModule(type);
+ final String basePackageName = BindingMapping.getRootPackageName(m.getQNameModule());
+ final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, type.getPath());
+ final String className = packageName + "." + BindingMapping.getClassName(typeQName);
sb.insert(0, "new " + className + "(");
sb.insert(sb.length(), ')');
}
}
private static String binaryToDef(final String defaultValue) {
- StringBuilder sb = new StringBuilder();
- BaseEncoding en = BaseEncoding.base64();
- byte[] encoded = en.decode(defaultValue);
+ final StringBuilder sb = new StringBuilder();
+ final BaseEncoding en = BaseEncoding.base64();
+ final byte[] encoded = en.decode(defaultValue);
sb.append("new byte[] {");
for (int i = 0; i < encoded.length; i++) {
sb.append(encoded[i]);
private static final Comparator<Bit> BIT_NAME_COMPARATOR = (o1, o2) -> o1.getName().compareTo(o2.getName());
private static String bitsToDef(final BitsTypeDefinition type, final String className, final String defaultValue, final boolean isExt) {
- List<Bit> bits = new ArrayList<>(type.getBits());
+ final List<Bit> bits = new ArrayList<>(type.getBits());
Collections.sort(bits, BIT_NAME_COMPARATOR);
- StringBuilder sb = new StringBuilder();
+ final StringBuilder sb = new StringBuilder();
if (!isExt) {
sb.append("new ");
sb.append(className);
}
private Module getParentModule(final SchemaNode node) {
- QName qname = node.getPath().getPathFromRoot().iterator().next();
- URI namespace = qname.getNamespace();
- Date revision = qname.getRevision();
- return schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+ final QName qname = node.getPath().getPathFromRoot().iterator().next();
+ final URI namespace = qname.getNamespace();
+ final Date revision = qname.getRevision();
+ return this.schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
}
private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType, final String defaultValue) {
if (strXPath != null) {
if (strXPath.indexOf('[') == -1) {
- final Module module = findParentModule(schemaContext, parentNode);
+ final Module module = findParentModule(this.schemaContext, parentNode);
if (module != null) {
final SchemaNode dataNode;
if (xpath.isAbsolute()) {
- dataNode = findDataSchemaNode(schemaContext, module, xpath);
+ dataNode = findDataSchemaNode(this.schemaContext, module, xpath);
} else {
- dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath);
+ dataNode = findDataSchemaNodeForRelativeXPath(this.schemaContext, module, parentNode, xpath);
}
- String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue);
+ final String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue);
return result;
}
} else {
String className;
if (type.getBaseType() != null) {
- QName typeQName = type.getQName();
+ final QName typeQName = type.getQName();
Module module = null;
- Set<Module> modules = schemaContext.findModuleByNamespace(typeQName.getNamespace());
+ final Set<Module> modules = this.schemaContext.findModuleByNamespace(typeQName.getNamespace());
if (modules.size() > 1) {
- for (Module m : modules) {
+ for (final Module m : modules) {
if (m.getRevision().equals(typeQName.getRevision())) {
module = m;
break;
}
}
if (module == null) {
- List<Module> modulesList = new ArrayList<>(modules);
+ final List<Module> modulesList = new ArrayList<>(modules);
Collections.sort(modulesList, (o1, o2) -> o1.getRevision().compareTo(o2.getRevision()));
module = modulesList.get(0);
}
module = modules.iterator().next();
}
- String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
+ final String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
className = basePackageName + "." + BindingMapping.getClassName(typeQName);
} else {
- Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
- QName first = path.next();
+ final Iterator<QName> path = node.getPath().getPathFromRoot().iterator();
+ final QName first = path.next();
if (!(path.hasNext())) {
- URI namespace = first.getNamespace();
- Date revision = first.getRevision();
- Module parent = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+ final URI namespace = first.getNamespace();
+ final Date revision = first.getRevision();
+ final Module parent = this.schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
parentName = BindingMapping.getClassName((parent).getName()) + "Data";
- String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
+ final String basePackageName = BindingMapping.getRootPackageName(parent.getQNameModule());
className = basePackageName + "." + parentName + "." + BindingMapping.getClassName(node.getQName());
} else {
- URI namespace = first.getNamespace();
- Date revision = first.getRevision();
- Module parentModule = schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
- String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
- String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, UNION_PATH);
+ final URI namespace = first.getNamespace();
+ final Date revision = first.getRevision();
+ final Module parentModule = this.schemaContext.findModuleByNamespaceAndRevision(namespace, revision);
+ final String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule());
+ final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, UNION_PATH);
className = packageName + "." + BindingMapping.getClassName(node.getQName());
}
}
}
private static String union(final String className, final String defaultValue, final LeafSchemaNode node) {
- StringBuilder sb = new StringBuilder();
+ final StringBuilder sb = new StringBuilder();
sb.append("new ");
sb.append(className);
sb.append("(\"");
*/
package org.opendaylight.yangtools.sal.binding.generator.impl;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.io.File;
import java.util.List;
import org.junit.Test;
+import org.opendaylight.yangtools.binding.generator.util.Types;
import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
import org.opendaylight.yangtools.sal.binding.model.api.Type;
+import org.opendaylight.yangtools.sal.binding.yang.types.TestUtils;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
public class UnionTypeDefTest {
@Test
public void unionTypeResolvingTest() throws Exception {
- File abstractTopology = new File(getClass().getResource("/union-test-models/abstract-topology.yang").toURI());
- File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
+ final File abstractTopology = new File(getClass().getResource("/union-test-models/abstract-topology.yang").toURI());
+ final File ietfInetTypes = new File(getClass().getResource("/ietf/ietf-inet-types.yang").toURI());
final SchemaContext context = TestUtils.parseYangSources(abstractTopology, ietfInetTypes);
assertNotNull("context is null", context);
// TODO: implement test
}
+
+ @Test
+ public void unionTypedefLeafrefTest() throws Exception {
+ final File yang = new File(getClass().getResource("/leafref_typedef_union/bug8449.yang").toURI());
+ final SchemaContext schemaContext = TestUtils.parseYangSources(yang);
+ assertNotNull(schemaContext);
+ final List<Type> generateTypes = new BindingGeneratorImpl(false).generateTypes(schemaContext);
+ assertNotNull(generateTypes);
+ for (final Type type : generateTypes) {
+ if (type.getName().equals("Cont")) {
+ final GeneratedType gt = ((GeneratedType) type);
+ assertNotNull(gt);
+ final GeneratedType refType = gt.getEnclosedTypes().iterator().next();
+ for (final GeneratedProperty generatedProperty : refType.getProperties()) {
+ switch (generatedProperty.getName()) {
+ case "stringRefValue":
+ assertEquals(Types.STRING, generatedProperty.getReturnType());
+ break;
+ case "value":
+ assertEquals(Types.CHAR_ARRAY, generatedProperty.getReturnType());
+ break;
+ }
+ }
+ }
+ }
+ }
}
--- /dev/null
+module bug8449 {
+ yang-version 1;
+ namespace "bug8449";
+ prefix "tst";
+
+ revision "2017-16-05" {
+ }
+
+ typedef name1-ref {
+ type leafref {
+ path "/tst:top/tst:name1";
+ }
+ }
+
+ typedef name2-ref {
+ type leafref {
+ path "/tst:top/tst:name2";
+ }
+ }
+
+ typedef name3-ref {
+ type string;
+ }
+
+ container top {
+ leaf name1 {
+ type string;
+ }
+ leaf name2 {
+ type string;
+ }
+ }
+
+ container test {
+ leaf ref {
+ type name3-ref;
+ }
+ }
+
+ container cont {
+ leaf ref {
+ type union {
+ type name1-ref;
+ type name2-ref;
+ }
+ mandatory true;
+ }
+ }
+}
\ No newline at end of file
return builder;
}
- protected Type getReturnType() {
+ public Type getReturnType() {
return returnType;
}
if (getClass() != obj.getClass()) {
return false;
}
- AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
+ final AbstractTypeMemberBuilder<?> other = (AbstractTypeMemberBuilder<?>) obj;
return Objects.equals(getName(), other.getName()) && Objects.equals(getReturnType(), other.getReturnType());
}
@Override
public String toString() {
- StringBuilder builder = new StringBuilder();
+ final StringBuilder builder = new StringBuilder();
builder.append("GeneratedPropertyImpl [name=");
builder.append(getName());
builder.append(", annotations=");
--- /dev/null
+module bug8449 {
+ yang-version 1;
+ namespace "bug8449";
+ prefix "tst";
+
+ revision "2017-16-05" {
+ }
+
+ typedef name1-ref {
+ type leafref {
+ path "/tst:top/tst:name1";
+ }
+ }
+
+ typedef name2-ref {
+ type leafref {
+ path "/tst:top/tst:name2";
+ }
+ }
+
+ typedef int32-ref {
+ type leafref {
+ path "/tst:top-int/tst:leaf-int32";
+ }
+ }
+
+ container top-int {
+ leaf leaf-int32 {
+ type uint32;
+ }
+ }
+
+ container top {
+ leaf name1 {
+ type string;
+ }
+ leaf name2 {
+ type string;
+ }
+ }
+
+ container cont-int32 {
+ leaf ref-union-int32 {
+ type union {
+ type int32-ref;
+ }
+ }
+ }
+
+ container cont {
+ leaf ref {
+ type union {
+ type name1-ref;
+ type name2-ref;
+ }
+ mandatory true;
+ }
+ }
+}