From: Robert Varga Date: Mon, 6 Jun 2016 17:29:59 +0000 (+0200) Subject: BUG-865: eliminate references to ExtendedType and base types X-Git-Tag: release/boron~88 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F97%2F39897%2F12;p=mdsal.git BUG-865: eliminate references to ExtendedType and base types These are no longer emitted from YANG parser, eliminate references obvious references to them. Change-Id: Ia89278098108da2356252dfc5bf47fddbe732198 Signed-off-by: Robert Varga --- diff --git a/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend b/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend index f9806f74e0..5462a14cb5 100644 --- a/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend +++ b/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/yangtools/yang/unified/doc/generator/GeneratorImpl.xtend @@ -50,7 +50,6 @@ import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition -import org.opendaylight.yangtools.yang.model.util.ExtendedType import org.slf4j.Logger import org.slf4j.LoggerFactory import org.sonatype.plexus.build.incremental.BuildContext @@ -1308,10 +1307,6 @@ class GeneratorImpl { «type.lengthConstraints.toLengthStmt» ''' - private def dispatch toLength(ExtendedType type) ''' - «type.lengthConstraints.toLengthStmt» - ''' - private def dispatch toRange(TypeDefinition type) { } @@ -1327,10 +1322,6 @@ class GeneratorImpl { «type.rangeConstraints.toRangeStmt» ''' - private def dispatch toRange(ExtendedType type) ''' - «type.rangeConstraints.toRangeStmt» - ''' - def toLengthStmt(Collection lengths) ''' «IF lengths != null && !lengths.empty» «listItem("Length restrictions:")» diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java index e202da9df5..a1b8a70c48 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java @@ -103,7 +103,6 @@ import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; import org.opendaylight.yangtools.yang.model.util.DataNodeIterator; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils; import org.opendaylight.yangtools.yang.model.util.type.CompatUtils; @@ -1371,11 +1370,6 @@ public class BindingGeneratorImpl implements BindingGenerator { } private static boolean isInnerType(final LeafSchemaNode leaf, final TypeDefinition type) { - // Deal with old parser, clearing out references to typedefs - if (type instanceof ExtendedType) { - return false; - } - // New parser with encapsulated type if (leaf.getPath().equals(type.getPath())) { return true; @@ -1472,10 +1466,6 @@ public class BindingGeneratorImpl implements BindingGenerator { } private static TypeDefinition getBaseOrDeclaredType(final TypeDefinition typeDef) { - if (typeDef instanceof ExtendedType) { - // Legacy behaviour returning ExtendedType is enough - return typeDef; - } // Returns DerivedType in case of new parser. final TypeDefinition baseType = typeDef.getBaseType(); return (baseType != null && baseType.getBaseType() != null) ? baseType : typeDef; @@ -2017,10 +2007,8 @@ public class BindingGeneratorImpl implements BindingGenerator { /** * Builds generated TO builders for typeDef of type - * {@link org.opendaylight.yangtools.yang.model.util.UnionType UnionType} or - * {@link org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition - * BitsTypeDefinition} which are also added to typeBuilder as - * enclosing transfer object. + * {@link UnionTypeDefinition} or {@link BitsTypeDefinition} which are + * also added to typeBuilder as enclosing transfer object. * * If more then one generated TO builder is created for enclosing then all * of the generated TO builders are added to typeBuilder as @@ -2283,4 +2271,4 @@ public class BindingGeneratorImpl implements BindingGenerator { builder.addAnnotation("", "Deprecated"); } } -} \ No newline at end of file +} diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java index 48da02b8ff..7fbdcd6149 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/util/BindingRuntimeContext.java @@ -53,7 +53,6 @@ import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -342,13 +341,8 @@ public class BindingRuntimeContext implements Immutable { private static BiMap getEnumMapping(final Entry typeWithSchema) { final TypeDefinition typeDef = (TypeDefinition) typeWithSchema.getValue(); - final EnumTypeDefinition enumType; - if(typeDef instanceof ExtendedType) { - enumType = (EnumTypeDefinition) ((ExtendedType) typeDef).getBaseType(); - } else { - Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition); - enumType = (EnumTypeDefinition) typeDef; - } + Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition); + final EnumTypeDefinition enumType = (EnumTypeDefinition) typeDef; final HashBiMap mappedEnums = HashBiMap.create(); diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java index e0771573a6..3975576115 100644 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java +++ b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java @@ -80,11 +80,8 @@ import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.Decimal64; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; -import org.opendaylight.yangtools.yang.model.util.UnionType; import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; import org.opendaylight.yangtools.yang.model.util.type.CompatUtils; import org.opendaylight.yangtools.yang.parser.util.YangValidationException; @@ -95,6 +92,11 @@ public final class TypeProviderImpl implements TypeProvider { private static final Logger LOG = LoggerFactory.getLogger(TypeProviderImpl.class); private static final Pattern NUMBERS_PATTERN = Pattern.compile("[0-9]+\\z"); + // Backwards compatibility: Union types used to be instantiated in YANG namespace, which is no longer + // the case, as unions are emitted to their correct schema path. + private static final SchemaPath UNION_PATH = SchemaPath.create(true, + org.opendaylight.yangtools.yang.model.util.BaseTypes.UNION_QNAME); + /** * Contains the schema data red from YANG files. */ @@ -199,8 +201,8 @@ public final class TypeProviderImpl implements TypeProvider { if (typeDefinition.getBaseType() == null) { // We have to deal with differing handling of decimal64. The old parser used a fixed Decimal64 type // and generated an enclosing ExtendedType to hold any range constraints. The new parser instantiates - // a base type which holds these constraints -- and the class is not a Decimal64. - if (typeDefinition instanceof DecimalTypeDefinition && !(typeDefinition instanceof Decimal64)) { + // a base type which holds these constraints. + if (typeDefinition instanceof DecimalTypeDefinition) { final Type ret = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForSchemaDefinitionType(typeDefinition, parentNode, r); if (ret != null) { return ret; @@ -1178,14 +1180,7 @@ public final class TypeProviderImpl implements TypeProvider { Preconditions.checkArgument(typedef != null, "typedef can't be null"); final List patternConstraints; - if (typedef instanceof ExtendedType) { - final TypeDefinition strTypeDef = baseTypeDefForExtendedType(typedef); - if (strTypeDef instanceof StringTypeDefinition) { - patternConstraints = ((ExtendedType)typedef).getPatternConstraints(); - } else { - patternConstraints = ImmutableList.of(); - } - } else if (typedef instanceof StringTypeDefinition) { + if (typedef instanceof StringTypeDefinition) { // FIXME: run diff against base patternConstraints = ((StringTypeDefinition) typedef).getPatternConstraints(); } else { @@ -1364,7 +1359,7 @@ public final class TypeProviderImpl implements TypeProvider { * definition to the base type */ private static int getTypeDefinitionDepth(final TypeDefinition typeDefinition) { - // FIXME: rewrite this in a non-recursive manner, without ExtendedType and UnionType + // FIXME: rewrite this in a non-recursive manner if (typeDefinition == null) { return 1; } @@ -1645,18 +1640,7 @@ public final class TypeProviderImpl implements TypeProvider { Date revision = first.getRevision(); Module parentModule = schemaContext.findModuleByNamespaceAndRevision(namespace, revision); String basePackageName = BindingMapping.getRootPackageName(parentModule.getQNameModule()); - - // Backwards compatibility: Union types used to be instantiated in YANG namespace, which is no longer - // the case, as unions are emitted to their correct schema path. Create a proxy instance to meet the - // codepath's expectations - final SchemaPath typePath; - if (type instanceof UnionType) { - typePath = type.getPath(); - } else { - typePath = UnionType.create(((UnionTypeDefinition)type).getTypes()).getPath(); - } - - String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, typePath); + String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, UNION_PATH); className = packageName + "." + BindingMapping.getClassName(node.getQName()); } } diff --git a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/UnionDependencySort.java b/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/UnionDependencySort.java deleted file mode 100644 index dfd4c8f0db..0000000000 --- a/binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/UnionDependencySort.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2013 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.sal.binding.yang.types; - -import com.google.common.base.Function; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.parser.util.TopologicalSort; -import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class UnionDependencySort { - private static final Logger LOGGER = LoggerFactory.getLogger(UnionDependencySort.class); - - /** - * Sorts union types by mutual dependencies. - * - * At the beginning the union types are selected from - * typeDefinitions and wrapped to nodes. The nodes are sorted - * and then the wrapped payload is extracted. - * - * @param typeDefinitions - * set of type definitions. - * @return list of extended type which are sorted by mutual dependencies - * @throws IllegalArgumentException - * if typeDefinitions equals null - */ - public List sort(final Set> typeDefinitions) { - if (typeDefinitions == null) { - LOGGER.error("Set of Type Definitions cannot be NULL!"); - throw new IllegalArgumentException("Set of Type Definitions " + "cannot be NULL!"); - } - - final Set extUnionTypes = unionsFromTypeDefinitions(typeDefinitions); - - final Set unsorted = unionTypesToNodes(extUnionTypes); - - final List sortedNodes = TopologicalSort.sort(unsorted); - return Lists.transform(sortedNodes, new Function() { - @Override - public ExtendedType apply(final Node input) { - return (ExtendedType) (((NodeWrappedType) input).getWrappedType()); - } - }); - } - - /** - * Extracts only union types from typeDefinitions set. - * - * @param typeDefinitions - * set of all type definitions - * @return set of extended type which are union type definition - */ - private static Set unionsFromTypeDefinitions(final Set> typeDefinitions) { - final Set unions = Sets.newHashSet(); - - for (final TypeDefinition typedef : typeDefinitions) { - if ((typedef != null) && (typedef.getBaseType() != null) && (typedef instanceof ExtendedType) - && (typedef.getBaseType() instanceof UnionTypeDefinition)) { - unions.add((ExtendedType) typedef); - } - } - return unions; - } - - /** - * Wraps every extended type which represents union to node type and adds to - * every node information about dependencies. - * - * The mapping from union type to node is created. For every created node - * (next nodeFrom) is for its wrapped union type passed the list of - * inner types through and only those inner types which represent union type - * are next considered. For every inner union type is found its wrapping - * node (next as nodeTo). This dependency relationship between - * nodeFrom and all found nodesTo is modeled with creating of one edge from - * nodeFrom to nodeTo. - * - * - * @param extUnionTypes - * set of extended types which represents union types - * @return set of nodes which contains wrapped union types set of node where - * each one contains wrapped one union type - */ - private static Set unionTypesToNodes(final Set extUnionTypes) { - final Map nodeMap = Maps.newHashMap(); - final Set resultNodes = Sets.newHashSet(); - - for (final ExtendedType unionType : extUnionTypes) { - final Node node = new NodeWrappedType(unionType); - nodeMap.put(unionType, node); - resultNodes.add(node); - } - - for (final Node node : resultNodes) { - final NodeWrappedType nodeFrom = (NodeWrappedType) node; - final ExtendedType extUnionType = (ExtendedType) nodeFrom.getWrappedType(); - - final UnionTypeDefinition unionType = (UnionTypeDefinition) extUnionType.getBaseType(); - - final List> innerTypes = unionType.getTypes(); - for (final TypeDefinition typedef : innerTypes) { - if (extUnionTypes.contains(typedef)) { - final Node toNode = nodeMap.get(typedef); - nodeFrom.addEdge(toNode); - } - } - } - - return resultNodes; - } - -} diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/yang/types/UnionDependencySortTest.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/yang/types/UnionDependencySortTest.java deleted file mode 100644 index 0c3c1e1b02..0000000000 --- a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/yangtools/sal/binding/yang/types/UnionDependencySortTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2014 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.sal.binding.yang.types; - -import static org.junit.Assert.assertNotNull; - -import com.google.common.base.Optional; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.model.api.SchemaPath; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; - -public class UnionDependencySortTest { - - @Rule - public ExpectedException expException = ExpectedException.none(); - - @Test - public void testSortMethod() { - - final UnionDependencySort unionDependencySort = new UnionDependencySort(); - final Set> typeDefs = new HashSet<>(); - - final TypeDefinition stringType = BaseTypes.stringType(); - final ExtendedType extendedType = ExtendedType.builder(QName.create("ExtendedType1"), stringType, Optional. absent(), Optional. absent(), SchemaPath.create(false, QName.create("Cont1"), QName.create("List1"))).build(); - - typeDefs.add(stringType); - typeDefs.add(extendedType); - - final List sortedExtendedTypes = unionDependencySort.sort(typeDefs); - assertNotNull(sortedExtendedTypes); - - expException.expect(IllegalArgumentException.class); - expException.expectMessage("Set of Type Definitions cannot be NULL!"); - unionDependencySort.sort(null); - } -} diff --git a/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java b/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java index 9507a00861..3c03294ccb 100644 --- a/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java +++ b/binding/mdsal-binding-generator-util/src/main/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtil.java @@ -43,8 +43,6 @@ import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.Decimal64; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; import org.opendaylight.yangtools.yang.model.util.type.DecimalTypeBuilder; @@ -498,8 +496,8 @@ public final class BindingGeneratorUtil { // instantiate a temporary unconstrained type and compare them. // // FIXME: looking at the generated code it looks as though we need to pass the restrictions without - // comparison and also even in the case of the old parser - if (type instanceof DecimalTypeDefinition && !(type instanceof Decimal64)) { + // comparison + if (type instanceof DecimalTypeDefinition) { final DecimalTypeDefinition decimal = (DecimalTypeDefinition) type; final DecimalTypeBuilder tmpBuilder = BaseTypes.decimalTypeBuilder(decimal.getPath()); tmpBuilder.setFractionDigits(decimal.getFractionDigits()); @@ -550,26 +548,7 @@ public final class BindingGeneratorUtil { * * FIXME: this probably not the best solution and needs further analysis. */ - if (type instanceof ExtendedType) { - final ExtendedType ext = (ExtendedType)type; - length = ext.getLengthConstraints(); - pattern = ext.getPatternConstraints(); - - // Interesting special-case... - List tmp = ext.getRangeConstraints(); - if (tmp.isEmpty()) { - final TypeDefinition base = ext.getBaseType(); - if (base instanceof IntegerTypeDefinition) { - tmp = ((IntegerTypeDefinition)base).getRangeConstraints(); - } else if (base instanceof UnsignedIntegerTypeDefinition) { - tmp = ((UnsignedIntegerTypeDefinition)base).getRangeConstraints(); - } else if (base instanceof DecimalTypeDefinition) { - tmp = ((DecimalTypeDefinition)base).getRangeConstraints(); - } - } - - range = tmp; - } else if (type instanceof BinaryTypeDefinition) { + if (type instanceof BinaryTypeDefinition) { final BinaryTypeDefinition binary = (BinaryTypeDefinition)type; final BinaryTypeDefinition base = binary.getBaseType(); if (base != null && base.getBaseType() != null) { diff --git a/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java b/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java index d48af8e83c..0e32ba431e 100644 --- a/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java +++ b/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/BindingGeneratorUtilTest.java @@ -16,12 +16,11 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; - import com.google.common.base.Optional; +import com.google.common.collect.ImmutableList; import java.io.File; import java.io.IOException; import java.io.Serializable; -import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -42,20 +41,18 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; -import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint; -import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; import org.opendaylight.yangtools.yang.model.util.BaseConstraints; import org.opendaylight.yangtools.yang.model.util.DataNodeIterator; -import org.opendaylight.yangtools.yang.model.util.Decimal64; -import org.opendaylight.yangtools.yang.model.util.ExtendedType; -import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder; import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; +import org.opendaylight.yangtools.yang.model.util.type.DerivedTypes; +import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes; +import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; public class BindingGeneratorUtilTest { + private static final SchemaPath ROOT_PATH = SchemaPath.create(true, QName.create("/root")); @Rule public ExpectedException expectedEx = ExpectedException.none(); @@ -316,90 +313,59 @@ public class BindingGeneratorUtilTest { @Test public void getRestrictionsTest() { + final Optional absent = Optional.absent(); + final StringTypeBuilder builder = + RestrictedTypes.newStringBuilder(BaseTypes.stringType(), ROOT_PATH); - Optional absent = Optional.absent(); - - Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"), - BaseTypes.int16Type(), absent, absent, SchemaPath.create(true, QName.create("/root"))); - - ArrayList lenght = new ArrayList(); - ArrayList range = new ArrayList(); - ArrayList pattern = new ArrayList(); + builder.addPatternConstraint(BaseConstraints.newPatternConstraint(".*", absent, absent)); + builder.setLengthAlternatives(ImmutableList.of( + BaseConstraints.newLengthConstraint(1, 2, absent, absent))); - lenght.add(BaseConstraints.newLengthConstraint(1, 2, absent, absent)); - range.add(BaseConstraints.newRangeConstraint(1, 2, absent, absent)); - pattern.add(BaseConstraints.newPatternConstraint(".*", absent, absent)); - - extTypeBuilder.lengths(lenght); - extTypeBuilder.ranges(range); - extTypeBuilder.patterns(pattern); - - Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extTypeBuilder.build()); + Restrictions restrictions = BindingGeneratorUtil.getRestrictions(builder.build()); assertNotNull(restrictions); assertEquals(1, restrictions.getLengthConstraints().size()); - assertEquals(1, restrictions.getRangeConstraints().size()); + assertEquals(0, restrictions.getRangeConstraints().size()); assertEquals(1, restrictions.getPatternConstraints().size()); assertFalse(restrictions.isEmpty()); assertTrue(restrictions.getLengthConstraints().contains( BaseConstraints.newLengthConstraint(1, 2, absent, absent))); - assertTrue(restrictions.getRangeConstraints() - .contains(BaseConstraints.newRangeConstraint(1, 2, absent, absent))); assertTrue(restrictions.getPatternConstraints().contains( BaseConstraints.newPatternConstraint(".*", absent, absent))); } @Test public void getEmptyRestrictionsTest() { - - Optional absent = Optional.absent(); - - Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"), - BaseTypes.stringType(), absent, absent, SchemaPath.create(true, QName.create("/root"))); - - Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extTypeBuilder.build()); + final TypeDefinition type = DerivedTypes.derivedTypeBuilder(BaseTypes.stringType(), ROOT_PATH).build(); + final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type); assertNotNull(restrictions); assertTrue(restrictions.isEmpty()); - } @Test public void getDefaultIntegerRestrictionsTest() { - - Optional absent = Optional.absent(); - - Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"), - BaseTypes.int16Type(), absent, absent, SchemaPath.create(true, QName.create("/root"))); - - ExtendedType extType = extTypeBuilder.build(); - Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType); + final TypeDefinition type = DerivedTypes.derivedTypeBuilder(BaseTypes.int16Type(), ROOT_PATH).build(); + final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type); assertNotNull(restrictions); assertFalse(restrictions.isEmpty()); - assertEquals(((IntegerTypeDefinition) extType.getBaseType()).getRangeConstraints(), + assertEquals(((IntegerTypeDefinition) type.getBaseType()).getRangeConstraints(), restrictions.getRangeConstraints()); assertTrue(restrictions.getLengthConstraints().isEmpty()); assertTrue(restrictions.getPatternConstraints().isEmpty()); - } @Test public void getDefaultUnsignedIntegerRestrictionsTest() { - - Optional absent = Optional.absent(); - - Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"), - BaseTypes.uint16Type(), absent, absent, SchemaPath.create(true, QName.create("/root"))); - - ExtendedType extType = extTypeBuilder.build(); - Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType); + final TypeDefinition type = DerivedTypes.derivedTypeBuilder(BaseTypes.uint16Type(), ROOT_PATH).build(); + final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type); assertNotNull(restrictions); assertFalse(restrictions.isEmpty()); - assertEquals(((UnsignedIntegerTypeDefinition) extType.getBaseType()).getRangeConstraints(), + assertEquals(((UnsignedIntegerTypeDefinition) type.getBaseType()).getRangeConstraints(), restrictions.getRangeConstraints()); assertTrue(restrictions.getLengthConstraints().isEmpty()); assertTrue(restrictions.getPatternConstraints().isEmpty()); @@ -407,23 +373,15 @@ public class BindingGeneratorUtilTest { @Test public void getDefaultDecimalRestrictionsTest() { + final DecimalTypeDefinition base = BaseTypes.decimalTypeBuilder(ROOT_PATH).setFractionDigits(10).build(); + final TypeDefinition type = DerivedTypes.derivedTypeBuilder(base, ROOT_PATH).build(); - Optional absent = Optional.absent(); - SchemaPath path = SchemaPath.create(true, QName.create("/root")); - - Builder extTypeBuilder = ExtendedType.builder(new QName(URI.create("namespace"), "localName"), - Decimal64.create(path, 10), absent, absent, path); - - ExtendedType extType = extTypeBuilder.build(); - Restrictions restrictions = BindingGeneratorUtil.getRestrictions(extType); + final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(type); assertNotNull(restrictions); assertFalse(restrictions.isEmpty()); - assertEquals(((DecimalTypeDefinition) extType.getBaseType()).getRangeConstraints(), - restrictions.getRangeConstraints()); + assertEquals(base.getRangeConstraints(), restrictions.getRangeConstraints()); assertTrue(restrictions.getLengthConstraints().isEmpty()); assertTrue(restrictions.getPatternConstraints().isEmpty()); - } - } diff --git a/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/generated/type/builder/EnumerationBuilderImplTest.java b/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/generated/type/builder/EnumerationBuilderImplTest.java index c2b4682103..00a4928f64 100644 --- a/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/generated/type/builder/EnumerationBuilderImplTest.java +++ b/binding/mdsal-binding-generator-util/src/test/java/org/opendaylight/yangtools/binding/generator/util/generated/type/builder/EnumerationBuilderImplTest.java @@ -12,9 +12,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; - -import com.google.common.base.Optional; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -24,9 +21,9 @@ import org.opendaylight.yangtools.sal.binding.model.api.Enumeration; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.Status; -import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; -import org.opendaylight.yangtools.yang.model.util.EnumerationType; +import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.type.BaseTypes; public class EnumerationBuilderImplTest { @@ -144,9 +141,8 @@ public class EnumerationBuilderImplTest { @Test public void testUpdateEnumPairsFromEnumTypeDef() { - EnumTypeDefinition.EnumPair enumPair = EnumPairImpl.create(qName); - EnumTypeDefinition enumTypeDefinition = EnumerationType.create(SchemaPath.SAME, - Arrays.asList(enumPair), Optional.of(enumPair)); + EnumTypeDefinition enumTypeDefinition = BaseTypes.enumerationTypeBuilder(SchemaPath.SAME) + .addEnum(EnumPairImpl.create(qName)).build(); enumerationBuilder.updateEnumPairsFromEnumTypeDef(enumTypeDefinition); } @@ -163,7 +159,7 @@ public class EnumerationBuilderImplTest { private final String name; private final Integer value; - private EnumPairImpl(QName qName) { + private EnumPairImpl(final QName qName) { qname = qName; path = SchemaPath.SAME; description = "Some Other Description"; @@ -173,7 +169,7 @@ public class EnumerationBuilderImplTest { value = 45; } - public static EnumPairImpl create(QName qName) { + public static EnumPairImpl create(final QName qName) { return new EnumPairImpl(qName); }