genCtx.get(module).addChildNodeType(node, genType);
groupingsToGenTypes(module, ((DataNodeContainer) node).getGroupings());
processUsesAugments((DataNodeContainer) node, module);
+ if (node.isAddedByUses() || node.isAugmenting())
+ genType.setSuitableForBoxing(false);
}
return genType;
}
+ private boolean hasWhenOrMustConstraints(final SchemaNode node) {
+ boolean hasWhenCondition;
+ boolean hasMustConstraints;
+
+ if (node instanceof ContainerSchemaNode) {
+ ContainerSchemaNode contNode = (ContainerSchemaNode)node;
+ hasWhenCondition = contNode.getConstraints().getWhenCondition() != null;
+ hasMustConstraints = !isNullOrEmpty(contNode.getConstraints().getMustConstraints());
+
+ if (hasWhenCondition || hasMustConstraints)
+ return true;
+ }
+ return false;
+ }
+
private void containerToGenType(final Module module, final String basePackageName,
final GeneratedTypeBuilder parent, final GeneratedTypeBuilder childOf, final ContainerSchemaNode node) {
final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
+
if (genType != null) {
constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), genType);
resolveDataSchemaNodes(module, basePackageName, genType, genType, node.getChildNodes());
+
+ final String parentName = parent.getName();
+ final String childOfName = childOf.getName();
+
+ if (parent != null && !parent.getName().contains("Data"))
+ genType.setParentType(parent);
+ genType.setSuitableForBoxing(hasOnlyOneChild(node) && !hasWhenOrMustConstraints(node));
+
+ if (parentName.equals(childOfName))
+ genType.setSuitableForBoxing(false);
}
}
+ private boolean hasOnlyOneChild(final ContainerSchemaNode contNode) {
+ if (!isNullOrEmpty(contNode.getChildNodes()) && contNode.getChildNodes().size() == 1)
+ return true;
+ return false;
+ }
+
private void listToGenType(final Module module, final String basePackageName, final GeneratedTypeBuilder parent,
final GeneratedTypeBuilder childOf, final ListSchemaNode node) {
final GeneratedTypeBuilder genType = processDataSchemaNode(module, basePackageName, childOf, node);
+
if (genType != null) {
+ if (!parent.getName().equals(childOf) && !parent.getName().contains("Data")) {
+ genType.setParentType(parent);
+ }
constructGetter(parent, node.getQName().getLocalName(), node.getDescription(), Types.listTypeFor(genType));
final List<String> listKeys = listKeys(node);
if (targetTypeBuilder == null) {
throw new NullPointerException("Target type not yet generated: " + targetSchemaNode);
}
+ targetTypeBuilder.setSuitableForBoxing(false);
if (!(targetSchemaNode instanceof ChoiceNode)) {
String packageName = augmentPackageName;
constructGetter(parent, choiceNode.getQName().getLocalName(), choiceNode.getDescription(),
choiceTypeBuilder);
choiceTypeBuilder.addImplementsType(typeForClass(DataContainer.class));
+ choiceTypeBuilder.setParentType(parent);
genCtx.get(module).addChildNodeType(choiceNode, choiceTypeBuilder);
generateTypesFromChoiceCases(module, basePackageName, choiceTypeBuilder.toInstance(), choiceNode);
}
return values;
}
+ @Override
+ public boolean isSuitableForBoxing() {
+ return false;
+ }
+
@Override
public List<AnnotationType> getAnnotations() {
return annotations;
public String getModuleName() {
return moduleName;
}
+
}
}
public String getModuleName() {
return moduleName;
}
+
+ @Override
+ public boolean isSuitableForBoxing() {
+ return false;
+ }
}
}
package org.opendaylight.yangtools.binding.generator.util.generated.type.builder;
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
import org.opendaylight.yangtools.yang.common.QName;
private String reference;
private String moduleName;
private Iterable<QName> schemaPath;
+ private boolean isSuitableForBoxing;
+ private GeneratedTypeBuilder parentType;
public GeneratedTypeBuilderImpl(final String packageName, final String name) {
super(packageName, name);
this.reference = reference;
}
+ @Override
+ public void setSuitableForBoxing(boolean value) {
+ this.isSuitableForBoxing = value;
+ }
+
+ @Override
+ public void setParentType(GeneratedTypeBuilder parent) {
+ this.parentType = parent;
+ }
+
+ @Override
+ public Type getParent() {
+ return this.parentType;
+ }
+
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
private final String reference;
private final String moduleName;
private final Iterable<QName> schemaPath;
+ private final boolean isSuitableForBoxing;
+ private final GeneratedTypeBuilder parentType;
public GeneratedTypeImpl(GeneratedTypeBuilderImpl builder) {
super(builder);
this.reference = builder.reference;
this.moduleName = builder.moduleName;
this.schemaPath = builder.schemaPath;
+ this.isSuitableForBoxing = builder.isSuitableForBoxing;
+ this.parentType = builder.parentType;
}
@Override
public String getModuleName() {
return moduleName;
}
+
+ @Override
+ public boolean isSuitableForBoxing() {
+ return isSuitableForBoxing;
+ }
}
+
}
import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature
import org.opendaylight.yangtools.sal.binding.model.api.Type
import org.opendaylight.yangtools.yang.binding.Augmentable
+import org.opendaylight.yangtools.yang.binding.ChildOf
import org.opendaylight.yangtools.yang.binding.DataObject
import org.opendaylight.yangtools.yang.binding.Identifiable
«generateSetters»
- public «type.name» build() {
- return new «type.name»«IMPL»(this);
- }
+ «generateBuildMethod»
+
+ «generateBuildBoxedMethod»
private static final class «type.name»«IMPL» implements «type.name» {
}
'''
+ def private generateBuildMethod() '''
+ public «type.name» build() {
+ return new «type.name»«IMPL»(this);
+ }
+ '''
+
+ def private generateBuildBoxedMethod() {
+ if(type.suitableForBoxing && type.parentType != null && isContainerAndIsNotList(type)) {
+ val parentTypeBuilder = createParentTypeBuilder()
+ if (countMatches(parentTypeBuilder, "org") < 2) {
+ return '''
+ public «type.parentType.importedName» buildBoxed() {
+ return new «parentTypeBuilder»().set«type.name»(build()).build();
+ }
+ '''
+ }
+ }
+ return ''
+ }
+
+ def private int countMatches(String string, String subString) {
+ if (string.nullOrEmpty || subString.nullOrEmpty) {
+ return 0
+ }
+ var int count = 0;
+ var int idx = 0;
+ while ((idx = string.indexOf(subString, idx)) != -1) {
+ count = count + 1;
+ idx = idx + subString.length();
+ }
+ return count;
+ }
+
+ def private createParentTypeBuilder() {
+ return type.parentType.packageName + "." + type.parentType.importedName + "Builder"
+ }
+
+ def private boolean isContainerAndIsNotList(GeneratedType type) {
+ val isList = implementsIfc(type, Types.parameterizedTypeFor(Types.typeForClass(Identifiable), type))
+ val implementsChildOf = implementsIfc(type, Types.parameterizedTypeFor(Types.typeForClass(ChildOf), type))
+
+ if (implementsChildOf && !isList) {
+ return true
+ }
+ return false;
+ }
+
/**
* Generate default constructor and constructor for every implemented interface from uses statements.
*/
--- /dev/null
+
+module mod-box {
+ yang-version 1;
+ namespace "urn:opendaylight:mod:box";
+ prefix "box";
+
+ revision 2014-08-05 {
+ }
+
+ container cont1 {
+ choice choice1 {
+ case case1 {
+ container cont2 {
+ leaf leaf1 {
+ type string;
+ }
+ }
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * 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.model.api;
+
+/**
+ * Implementing this interface allows an object to hold information about that if
+ * is generated type suitable for boxing.
+ *
+ * Example:
+ * choice foo-choice {
+ * case foo-case {
+ * container foo {
+ * ...
+ * }
+ * }
+ * }
+ *
+ * Suitable type have to implements ChildOf<T>, where !(T instanceof Identifiable) and
+ * T does not place any structural requirements (must/when) on existence/value Foo.
+ */
+public interface BoxableType {
+
+ /**
+ * Check if generated type is suitable for boxing.
+ *
+ * @return true if generated type is suitable for boxing, false otherwise.
+ */
+ boolean isSuitableForBoxing();
+}
* definitions MUST be public, so there is no need to specify the scope of
* visibility.
*/
-public interface GeneratedType extends Type, DocumentedType {
+public interface GeneratedType extends Type, DocumentedType, BoxableType {
/**
* Returns the parent type if Generated Type is defined as enclosing type,
/**
* Generated Type Builder interface is helper interface for building and
* defining the GeneratedType.
- *
+ *
* @see GeneratedType
*/
public interface GeneratedTypeBuilder extends GeneratedTypeBuilderBase<GeneratedTypeBuilder> {
/**
* Returns the <code>new</code> <i>immutable</i> instance of Generated Type.
- *
+ *
* @return the <code>new</code> <i>immutable</i> instance of Generated Type.
*/
GeneratedType toInstance();
+ /**
+ * Set true if generated type is suitable for boxing, false otherwise.
+ *
+ * @param value
+ */
+ public void setSuitableForBoxing(boolean value);
+
+ /**
+ * Set parent for current generated type.
+ *
+ * @param parent
+ */
+ public void setParentType(GeneratedTypeBuilder parent);
}
package org.opendaylight.yangtools.sal.binding.model.api.type.builder;
import java.util.List;
-
import org.opendaylight.yangtools.sal.binding.model.api.Constant;
import org.opendaylight.yangtools.sal.binding.model.api.Type;
import org.opendaylight.yangtools.yang.common.QName;
}
typeDefinitionsConcreteDepth.add(unsortedTypeDefinition);
}
- // keys are in ascending order
- for (Map.Entry<Integer, List<TypeDefinition<?>>> entry : typeDefinitionsDepths.entrySet()) {
- sortedTypeDefinition.addAll(entry.getValue());
+
+ // SortedMap guarantees order corresponding to keys in ascending order
+ for (List<TypeDefinition<?>> v : typeDefinitionsDepths.values()) {
+ sortedTypeDefinition.addAll(v);
}
return sortedTypeDefinition;
<repository>
<id>opendaylight-mirror</id>
<name>opendaylight-mirror</name>
- <url>http://nexus.opendaylight.org/content/groups/public/</url>
+ <url>${nexusproxy}/groups/public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<repository>
<id>opendaylight-snapshot</id>
<name>opendaylight-snapshot</name>
- <url>http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/</url>
+ <url>${nexusproxy}/repositories/opendaylight.snapshot/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.gson.stream.JsonWriter;
-
import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.Deque;
-
import org.opendaylight.yangtools.concepts.Codec;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
/**
* This implementation will create JSON output as output stream.
this.tracker = SchemaTracker.create(schemaContext);
}
+ private JSONNormalizedNodeStreamWriter(final SchemaContext schemaContext, final SchemaPath path,
+ final Writer writer, final int indentSize) {
+ this.schemaContext = Preconditions.checkNotNull(schemaContext);
+ this.writer = Preconditions.checkNotNull(writer);
+
+ Preconditions.checkArgument(indentSize >= 0, "Indent size must be non-negative");
+ if (indentSize != 0) {
+ indent = Strings.repeat(" ", indentSize);
+ } else {
+ indent = null;
+ }
+
+ this.codecs = CodecFactory.create(schemaContext);
+ this.tracker = SchemaTracker.create(schemaContext,path);
+ }
+
/**
* Create a new stream writer, which writes to the specified {@link Writer}.
*
return new JSONNormalizedNodeStreamWriter(schemaContext, writer, 0);
}
+ /**
+ * Create a new stream writer, which writes to the specified {@link Writer}.
+ *
+ * @param schemaContext Schema context
+ * @param writer Output writer
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, SchemaPath path,final Writer writer) {
+ return new JSONNormalizedNodeStreamWriter(schemaContext, path, writer, 0);
+ }
+
/**
* Create a new stream writer, which writes to the specified output stream.
*
public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
throws IOException {
tracker.startListItem(identifier);
-
- stack.push(new TypeInfo(NodeType.OBJECT, identifier.getNodeType().getNamespace()));
separateElementFromPreviousElement();
+ stack.push(new TypeInfo(NodeType.OBJECT, identifier.getNodeType().getNamespace()));
+
+
writeStartObject();
indentRight();
}
import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
-
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Deque;
-
+import java.util.HashSet;
import javax.xml.stream.XMLStreamWriter;
-
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
+import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
private SchemaNode getSchema(final PathArgument name) {
final Object parent = getParent();
- Preconditions.checkState(parent instanceof DataNodeContainer);
-
+ SchemaNode schema = null;
final QName qname = name.getNodeType();
- final SchemaNode schema = ((DataNodeContainer)parent).getDataChildByName(qname);
+ if(parent instanceof DataNodeContainer) {
+ schema = ((DataNodeContainer)parent).getDataChildByName(qname);
+
+ } else if(parent instanceof ChoiceNode) {
+ for(ChoiceCaseNode caze : ((ChoiceNode) parent).getCases()) {
+ DataSchemaNode potential = caze.getDataChildByName(qname);
+ if(potential != null) {
+ schema = potential;
+ break;
+ }
+ }
+ } else {
+ throw new IllegalStateException("Unsupported schema type "+ parent.getClass() +" on stack.");
+ }
Preconditions.checkArgument(schema != null, "Could not find schema for node %s in %s", qname, parent);
return schema;
}
final Object parent = getParent();
Preconditions.checkArgument(parent instanceof AugmentationTarget, "Augmentation not allowed under %s", parent);
+ Preconditions.checkArgument(parent instanceof DataNodeContainer, "Augmentation allowed only in DataNodeContainer",parent);
final AugmentationSchema schema = SchemaUtils.findSchemaForAugment((AugmentationTarget) parent, identifier.getPossibleChildNames());
- schemaStack.push(schema);
- return schema;
+ HashSet<DataSchemaNode> realChildSchemas = new HashSet<>();
+ for(DataSchemaNode child : schema.getChildNodes()) {
+ realChildSchemas.add(((DataNodeContainer) parent).getDataChildByName(child.getQName()));
+ }
+ AugmentationSchema resolvedSchema = new AugmentationSchemaProxy(schema, realChildSchemas);
+ schemaStack.push(resolvedSchema);
+ return resolvedSchema;
}
public AnyXmlSchemaNode anyxmlNode(final NodeIdentifier name) {
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import com.google.common.base.Optional;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
+
+import static org.junit.Assert.assertEquals;
+
+public class BaseConstraintsTest {
+
+ @Test
+ public void canCreateConstraints() {
+ Number min = 5;
+ Number max = 99;
+ String description = "Any description";
+ String reference = "any_ref";
+ String reg_exp = "x|z";
+ Optional<String> desc = Optional.of(description);
+ Optional<String> ref = Optional.of(reference);
+
+ LengthConstraint lengthCons = BaseConstraints.newLengthConstraint(min, max, desc, ref);
+
+ assertEquals("LengthConstraints Get min", min, lengthCons.getMin());
+ assertEquals("LengthConstraints Get max", max, lengthCons.getMax());
+ assertEquals("LengthConstraints Get description", description, lengthCons.getDescription());
+ assertEquals("LengthConstraints Get reference", reference, lengthCons.getReference());
+
+ PatternConstraint patternCons = BaseConstraints.newPatternConstraint(reg_exp, desc, ref);
+
+ assertEquals("PatternConstraints Get regex", reg_exp, patternCons.getRegularExpression());
+ assertEquals("PatternConstraints Get description", description, patternCons.getDescription());
+ assertEquals("PatternConstraints Get reference", reference, patternCons.getReference());
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import com.google.common.base.Optional;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+public class BaseTypesTest {
+
+ @Test
+ public void canCreateBaseTypes() {
+ Optional<?> int8 = BaseTypes.defaultBaseTypeFor("int8");
+ assertEquals(Optional.of(Int8.getInstance()), int8);
+
+ Optional<?> int16 = BaseTypes.defaultBaseTypeFor("int16");
+ assertEquals(Optional.of(Int16.getInstance()), int16);
+
+ Optional<?> int32 = BaseTypes.defaultBaseTypeFor("int32");
+ assertEquals(Optional.of(Int32.getInstance()), int32);
+
+ Optional<?> int64 = BaseTypes.defaultBaseTypeFor("int64");
+ assertEquals(Optional.of(Int64.getInstance()), int64);
+
+ Optional<?> int128 = BaseTypes.defaultBaseTypeFor("int128");
+ assertEquals("wrong type", Optional.absent(), int128);
+
+ Optional<?> uint8 = BaseTypes.defaultBaseTypeFor("uint8");
+ assertEquals(Optional.of(Uint8.getInstance()), uint8);
+
+ Optional<?> uint16 = BaseTypes.defaultBaseTypeFor("uint16");
+ assertEquals(Optional.of(Uint16.getInstance()), uint16);
+
+ Optional<?> uint32 = BaseTypes.defaultBaseTypeFor("uint32");
+ assertEquals(Optional.of(Uint32.getInstance()), uint32);
+
+ Optional<?> uint64 = BaseTypes.defaultBaseTypeFor("uint64");
+ assertEquals(Optional.of(Uint64.getInstance()), uint64);
+
+ Optional<?> uint128 = BaseTypes.defaultBaseTypeFor("uint128");
+ assertEquals("wrong type", Optional.absent(), uint128);
+
+ Optional<?> stringType = BaseTypes.defaultBaseTypeFor("string");
+ assertEquals(Optional.of(StringType.getInstance()), stringType);
+
+ Optional<?> binaryType = BaseTypes.defaultBaseTypeFor("binary");
+ assertEquals(Optional.of(BinaryType.getInstance()), binaryType);
+
+ Optional<?> booleanType = BaseTypes.defaultBaseTypeFor("boolean");
+ assertEquals(Optional.of(BooleanType.getInstance()), booleanType);
+
+ Optional<?> emptyType = BaseTypes.defaultBaseTypeFor("empty");
+ assertEquals(Optional.of(EmptyType.getInstance()), emptyType);
+
+ Optional<?> instance_identifier = BaseTypes.defaultBaseTypeFor("instance_identifier");
+ assertEquals(Optional.absent(), instance_identifier);
+
+ Optional<?> whatever = BaseTypes.defaultBaseTypeFor("whatever");
+ assertEquals("wrong type", Optional.absent(), whatever);
+
+ assertFalse("whatever is not build-in type", BaseTypes.isYangBuildInType("whatever"));
+ assertTrue("int8 is build-in type", BaseTypes.isYangBuildInType("int8"));
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.Status;
+import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+public class BinaryTypeTest {
+
+ @Test
+ public void canCreateBinaryType() {
+ BinaryType binType = BinaryType.getInstance();
+ BinaryType binType1 = BinaryType.getInstance();
+ String stringBinType = binType.toString();
+
+
+ List<LengthConstraint> lengthConstraints = binType.getLengthConstraints();
+ assertTrue(lengthConstraints.toString().contains("max=9223372036854775807"));
+ assertTrue(lengthConstraints.toString().contains("min=0"));
+
+ assertEquals("Default value is []", Collections.EMPTY_LIST, binType.getDefaultValue());
+ assertEquals("CURRENT", Status.CURRENT, binType.getStatus());
+ assertEquals("Base type is null", null, binType.getBaseType());
+ assertEquals("getQName gives BINARY_QNAME", BaseTypes.BINARY_QNAME, binType.getQName());
+ assertEquals("empty string", "", binType.getUnits());
+ assertEquals("getPath gives List of BINARY_QNAME",
+ Collections.singletonList(BaseTypes.BINARY_QNAME), binType.getPath().getPathFromRoot());
+
+ assertTrue("BinType.toString should contain Description", stringBinType.contains(binType.getDescription()));
+ assertTrue("BinType.toString should contain Reference", stringBinType.contains(binType.getReference()));
+
+ assertTrue("binType1 should equal to binType",
+ binType.equals(binType1) && binType1.equals(binType));
+ assertTrue("Hash code of binType and binType1 should be equal",
+ binType.hashCode() == binType1.hashCode());
+ assertEquals("binType should equals to itself", binType, binType);
+ assertFalse("binType shouldn't equal to null", binType.equals(null));
+ assertFalse("binType shouldn't equal to object of other type", binType.equals("str"));
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.Status;
+
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class BooleanTypeTest {
+
+ @Test
+ public void canCreateBooleanType() {
+ BooleanType boolType = BooleanType.getInstance();
+ String stringBoolType = boolType.toString();
+
+ assertEquals("getPath gives List of BOOLEAN_QNAME",
+ Collections.singletonList(BaseTypes.BOOLEAN_QNAME), boolType.getPath().getPathFromRoot());
+
+ assertEquals("getQName gives BOOLEAN_QNAME", BaseTypes.BOOLEAN_QNAME, boolType.getQName());
+
+ assertEquals("The boolean built-in type represents a boolean value.", boolType.getDescription());
+
+ String strPath = boolType.getPath().toString();
+ assertTrue("Should contain string of getPath", stringBoolType.contains(strPath));
+
+ assertEquals("Should be empty string", "", boolType.getUnits());
+
+ assertEquals("Base type is null", null, boolType.getBaseType());
+
+ assertEquals("Default value is false", false, boolType.getDefaultValue());
+
+ assertEquals("Status CURRENT", Status.CURRENT, boolType.getStatus());
+
+ assertEquals("Should contain empty list", Collections.EMPTY_LIST, boolType.getUnknownSchemaNodes());
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
+
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+public class Decimal64Test {
+
+ @Test
+ public void canCreateDecimal64() {
+ Integer fractionDig = 2;
+ Decimal64 decimal64 = Decimal64.create(SchemaPath.ROOT, fractionDig);
+
+ assertEquals("Status should be CURRENT", Status.CURRENT, decimal64.getStatus());
+
+ assertEquals("Default value should be null", null, decimal64.getDefaultValue());
+
+ assertEquals("Should be empty list", Collections.EMPTY_LIST, decimal64.getUnknownSchemaNodes());
+
+ assertEquals("Should be null", null, decimal64.getBaseType());
+
+ assertNotEquals("Description is not null", null, decimal64.getDescription());
+
+ assertNotEquals("Reference is not null", null, decimal64.getReference());
+
+ assertEquals("Should be empty string", "", decimal64.getUnits());
+
+ assertTrue("Should contain factionDigits=2", decimal64.toString().contains("fractionDigits="+fractionDig));
+
+ assertEquals("Should get farctionDig", fractionDig, decimal64.getFractionDigits());
+
+ assertEquals("Should be empty list",
+ Collections.EMPTY_LIST, decimal64.getPath().getPathFromRoot());
+
+ assertEquals("Should be DECIMAL64_QNAME", BaseTypes.DECIMAL64_QNAME, decimal64.getQName());
+
+ assertTrue("Should contain max", decimal64.getRangeConstraints().toString().contains("max=922337203685477580.7"));
+ assertTrue("Should contain min", decimal64.getRangeConstraints().toString().contains("min=-922337203685477580.8"));
+
+ Decimal64 decimal641 = decimal64;
+ assertTrue("Hash code of decimal64 and decimal641 should be equal",
+ decimal64.hashCode() == decimal641.hashCode());
+
+ assertFalse("Decimal64 shouldn't equal to null", decimal64.equals(null));
+ assertEquals("Decimal64 should equals to itself", decimal64, decimal64);
+ assertFalse("Decimal64 shouldn't equal to object of other type", decimal64.equals("str"));
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.Status;
+
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+public class EmptyTypeTest {
+
+ @Test
+ public void canCreateEmptyType() {
+ EmptyType emptyType = EmptyType.getInstance();
+
+ assertEquals("QName", BaseTypes.EMPTY_QNAME, emptyType.getQName());
+ assertEquals("Path", Collections.singletonList(BaseTypes.EMPTY_QNAME),
+ emptyType.getPath().getPathFromRoot());
+ assertEquals("BaseType", null, emptyType.getBaseType());
+ assertEquals("DefaultValue", null, emptyType.getDefaultValue());
+ assertEquals("Status", Status.CURRENT, emptyType.getStatus());
+ assertTrue("Reference", emptyType.getReference().contains("rfc6020"));
+ assertEquals("Units", null, emptyType.getUnits());
+ assertNotEquals("Description is not null", null, emptyType.getDescription());
+ assertEquals("UnknownSchemaNodes", Collections.EMPTY_LIST, emptyType.getUnknownSchemaNodes());
+ assertTrue("toString", emptyType.toString().contains("empty"));
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import com.google.common.base.Optional;
+import org.junit.Test;
+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.LengthConstraint;
+
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+public class ExtendedTypeTest {
+ @Test
+ public void canCreateExtendedType() {
+ String namespace = "TestType";
+ String revision = "2014-08-26";
+ String localName = "testType";
+ QName testType = QName.create(namespace, revision, localName);
+ Int32 int32 = Int32.getInstance();
+ String description = "This type is used for testing purpose";
+ Optional<String> desc = Optional.of(description);
+ String reference = "Test Reference";
+ Optional<String> ref = Optional.of(reference);
+
+ ExtendedType.Builder extendedTypeBuilder = ExtendedType.builder(testType, int32, desc, ref, SchemaPath.ROOT);
+
+ int defValue = 12;
+ extendedTypeBuilder.defaultValue(defValue);
+
+ extendedTypeBuilder.status(Status.OBSOLETE);
+ extendedTypeBuilder.addedByUses(false);
+
+ int digits = 2;
+ extendedTypeBuilder.fractionDigits(digits);
+
+ String units = "KiloTest";
+ extendedTypeBuilder.units(units);
+
+ Number min = 3;
+ Number max = 15;
+ LengthConstraint lengthCons = BaseConstraints.newLengthConstraint(min, max, desc, ref);
+ extendedTypeBuilder.lengths(Collections.singletonList(lengthCons));
+
+ ExtendedType extendedType = extendedTypeBuilder.build();
+
+ assertEquals("BaseType is int32", int32, extendedType.getBaseType());
+ assertEquals("Description", description, extendedType.getDescription());
+ assertEquals("Reference", reference, extendedType.getReference());
+ assertEquals("Path", SchemaPath.ROOT, extendedType.getPath());
+ assertEquals("Default Value is 12", defValue, extendedType.getDefaultValue());
+ assertEquals("Status is OBSOLETE", Status.OBSOLETE, extendedType.getStatus());
+ assertFalse("AddedByUses", extendedType.isAddedByUses());
+ assertTrue("should be 2", digits == extendedType.getFractionDigits());
+ assertTrue("Should contain description", extendedType.toString().contains(description));
+ assertEquals("Units", units, extendedType.getUnits());
+ assertEquals("Length Constraints", Collections.singletonList(lengthCons), extendedType.getLengthConstraints());
+ assertTrue("Should contain name of type", extendedType.getQName().toString().contains(localName));
+
+ assertEquals("extendedType should equals to itself",extendedType, extendedType);
+ assertFalse("extendedType shouldn't equal to null", extendedType.equals(null));
+ assertTrue("Hash code of unionType should be equal to itself",
+ extendedType.hashCode() == extendedType.hashCode());
+
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.yang.model.util;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.Status;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.ArrayList;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+public class UnionTypeTest {
+
+ @Test
+ public void canCreateUnion() {
+ List<TypeDefinition<?>> listTypes = new ArrayList<>();
+ Int32 int32 = Int32.getInstance();
+ listTypes.add(int32);
+ UnionType unionType = UnionType.create(listTypes);
+
+ assertEquals("GetUnits should be null", null, unionType.getUnits());
+ assertTrue("String should contain int32", unionType.toString().contains("int32"));
+ assertEquals("Should be empty list", Collections.EMPTY_LIST, unionType.getUnknownSchemaNodes());
+ assertNotEquals("Description should not be null", null, unionType.getDescription());
+ assertNotEquals("Ref should not be null", null, unionType.getReference());
+ assertEquals("Should be CURRENT", Status.CURRENT, unionType.getStatus());
+ assertEquals("Should be int32 in list", Collections.singletonList(int32), unionType.getTypes());
+ assertEquals("Base type should be null", null, unionType.getBaseType());
+ assertEquals("Default value should be null", null, unionType.getDefaultValue());
+ assertEquals("Should be same as list of BaseTypes",
+ Collections.singletonList(BaseTypes.UNION_QNAME), unionType.getPath().getPathFromRoot());
+ assertEquals("Should be BaseTypes", BaseTypes.UNION_QNAME, unionType.getQName());
+
+ assertEquals("unionType should equals to itself", unionType, unionType);
+ assertFalse("unionType shouldn't equal to null", unionType.equals(null));
+ assertTrue("Hash code of unionType should be equal to itself",
+ unionType.hashCode() == unionType.hashCode());
+ }
+
+}
\ No newline at end of file
ANYXML_KEYWORD | IDENTIFIER) string? (SEMICOLON | (LEFT_BRACE (unknown_statement | identifier_stmt)* RIGHT_BRACE)*);
stmtend : (SEMICOLON) | (LEFT_BRACE identifier_stmt? RIGHT_BRACE);
+
+/* DO NOT replace stmtsep in rest of grammar with identifier_stmt!!! It might seems as code duplicity here, but this one is necessary.
+ Body of identifier_stmt generated from this grammar in YangParserListener is implemented in YangParserListenerImpl.
+ To ensure that all of the identifier_stmts will be resolved correctly the YangParserListenerImpl contains code that handles
+ specifcly identifier_stmts -> i.e. transforms identifier_stmt into QName. The stmtsep is used for parsing extension statements
+ placed outside of body_stmt.
+ */
stmtsep : IDENTIFIER string? (stmtend | (LEFT_BRACE unknown_statement* RIGHT_BRACE));
deviate_replace_stmt : DEVIATE_KEYWORD string /* REPLACE_KEYWORD */ (SEMICOLON | (LEFT_BRACE (identifier_stmt |type_stmt | units_stmt | default_stmt | config_stmt | mandatory_stmt | min_elements_stmt | max_elements_stmt )* RIGHT_BRACE));
deviate_delete_stmt : DEVIATE_KEYWORD string /* DELETE_KEYWORD */ (SEMICOLON | (LEFT_BRACE (identifier_stmt |units_stmt | must_stmt | unique_stmt | default_stmt )* RIGHT_BRACE));
data_def_stmt : container_stmt | leaf_stmt | leaf_list_stmt | list_stmt | choice_stmt | anyxml_stmt | uses_stmt;
body_stmts : (( identifier_stmt| extension_stmt | feature_stmt | identity_stmt | typedef_stmt | grouping_stmt | data_def_stmt | augment_stmt | rpc_stmt | notification_stmt | deviation_stmt) )*;
revision_stmts : (revision_stmt )* (stmtsep)*;
-linkage_stmts : (import_stmt stmtsep? | include_stmt stmtsep?)*;
-meta_stmts : (organization_stmt stmtsep? | contact_stmt stmtsep? | description_stmt stmtsep? | reference_stmt stmtsep?)*;
-submodule_header_stmts : (yang_version_stmt stmtsep? | belongs_to_stmt stmtsep?)+ ;
-module_header_stmts : (yang_version_stmt stmtsep? | namespace_stmt stmtsep? | prefix_stmt stmtsep?)+ ;
+linkage_stmts : (import_stmt stmtsep* | include_stmt stmtsep*)*;
+meta_stmts : (organization_stmt stmtsep* | contact_stmt stmtsep* | description_stmt stmtsep* | reference_stmt stmtsep*)*;
+submodule_header_stmts : (yang_version_stmt stmtsep* | belongs_to_stmt stmtsep*)+ ;
+module_header_stmts : (yang_version_stmt stmtsep* | namespace_stmt stmtsep* | prefix_stmt stmtsep*)+ ;
submodule_stmt : SUBMODULE_KEYWORD string LEFT_BRACE submodule_header_stmts linkage_stmts meta_stmts revision_stmts body_stmts RIGHT_BRACE;
-module_stmt : MODULE_KEYWORD string LEFT_BRACE stmtsep? module_header_stmts linkage_stmts meta_stmts revision_stmts body_stmts RIGHT_BRACE;
\ No newline at end of file
+module_stmt : MODULE_KEYWORD string LEFT_BRACE stmtsep* module_header_stmts linkage_stmts meta_stmts revision_stmts body_stmts RIGHT_BRACE;
\ No newline at end of file
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
-
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
-
import javax.annotation.concurrent.Immutable;
-
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
@Override
public SchemaPath getPath() {
- return null;
+ return SchemaPath.ROOT;
}
@Override
module ext-use {
+ ext:id "http://opendaylight.org";
yang-version 1;
+ ext:id "http://opendaylight.org";
+
+ ext:id2 "73354";
+ ext:name "test-name";
+
namespace "urn:simple.extension.use";
+ ext:id "http://opendaylight.org";
+
+ ext:name "test-name";
+
prefix "ext-use";
+ ext:id "http://opendaylight.org";
+ ext:name "test-name";
import ext-typedef { prefix "ext"; }