import static org.junit.Assert.*;
+import java.io.FileNotFoundException;
import java.net.URI;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.opendaylight.controller.yang.model.api.ChoiceNode;
import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.controller.yang.model.api.DataSchemaNode;
import org.opendaylight.controller.yang.model.api.Deviation;
import org.opendaylight.controller.yang.model.api.Deviation.Deviate;
import org.opendaylight.controller.yang.model.api.ExtensionDefinition;
import org.opendaylight.controller.yang.model.api.FeatureDefinition;
+import org.opendaylight.controller.yang.model.api.GroupingDefinition;
+import org.opendaylight.controller.yang.model.api.LeafListSchemaNode;
import org.opendaylight.controller.yang.model.api.LeafSchemaNode;
import org.opendaylight.controller.yang.model.api.ListSchemaNode;
import org.opendaylight.controller.yang.model.api.Module;
import org.opendaylight.controller.yang.model.util.ExtendedType;
import org.opendaylight.controller.yang.model.util.Int16;
import org.opendaylight.controller.yang.model.util.Int32;
+import org.opendaylight.controller.yang.model.util.Int8;
+import org.opendaylight.controller.yang.model.util.Leafref;
import org.opendaylight.controller.yang.model.util.StringType;
import org.opendaylight.controller.yang.model.util.Uint32;
import org.opendaylight.controller.yang.model.util.UnionType;
public class YangParserTest {
+ private final DateFormat simpleDateFormat = new SimpleDateFormat(
+ "yyyy-MM-dd");
+
private Set<Module> modules;
@Before
- public void init() {
- modules = TestUtils.loadModules("src/test/resources/model");
+ public void init() throws FileNotFoundException {
+ modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
assertEquals(3, modules.size());
}
assertNull(constraints.getWhenCondition());
assertEquals(0, constraints.getMustConstraints().size());
assertFalse(constraints.isMandatory());
- assertNull(constraints.getMinElements());
- assertNull(constraints.getMaxElements());
+ assertEquals(1, (int) constraints.getMinElements());
+ assertEquals(11, (int) constraints.getMaxElements());
// test AugmentationTarget args
Set<AugmentationSchema> availableAugmentations = ifEntry
.getAvailableAugmentations();
assertTrue(ifMtu.getType() instanceof Int32);
}
+ @Test
+ public void testParseLeaf() throws ParseException {
+ Module test = TestUtils.findModule(modules, "types2");
+
+ // leaf if-name
+ LeafSchemaNode ifName = (LeafSchemaNode) test
+ .getDataChildByName("if-name");
+ Leafref ifNameType = (Leafref) ifName.getType();
+ QName qname = ifNameType.getQName();
+
+ URI baseYangTypeNS = URI.create("urn:ietf:params:xml:ns:yang:1");
+ assertEquals(baseYangTypeNS, qname.getNamespace());
+ assertNull(qname.getRevision());
+ assertEquals("", qname.getPrefix());
+ assertEquals("leafref", qname.getLocalName());
+
+ // leaf name
+ LeafSchemaNode name = (LeafSchemaNode) test.getDataChildByName("name");
+ StringType nameType = (StringType) name.getType();
+ QName nameQName = nameType.getQName();
+
+ assertEquals(baseYangTypeNS, nameQName.getNamespace());
+ assertNull(nameQName.getRevision());
+ assertEquals("", nameQName.getPrefix());
+ assertEquals("string", nameQName.getLocalName());
+
+ // leaf count
+ LeafSchemaNode count = (LeafSchemaNode) test
+ .getDataChildByName("count");
+ ExtendedType countType = (ExtendedType) count.getType();
+ QName countTypeQName = countType.getQName();
+
+ URI expectedNS = URI.create("urn:simple.types.data.demo");
+ Date expectedDate = simpleDateFormat.parse("2013-02-27");
+ assertEquals(expectedNS, countTypeQName.getNamespace());
+ assertEquals(expectedDate, countTypeQName.getRevision());
+ assertEquals("t2", countTypeQName.getPrefix());
+ assertEquals("int8", countTypeQName.getLocalName());
+
+ Int8 countTypeBase = (Int8) countType.getBaseType();
+ QName countTypeBaseQName = countTypeBase.getQName();
+
+ assertEquals(baseYangTypeNS, countTypeBaseQName.getNamespace());
+ assertNull(countTypeBaseQName.getRevision());
+ assertEquals("", countTypeBaseQName.getPrefix());
+ assertEquals("int8", countTypeBaseQName.getLocalName());
+ }
+
@Test
public void testAugmentResolving() {
// testfile1
}
@Test
- public void testTypedefRangesResolving() {
+ public void testTypedefRangesResolving() throws ParseException {
Module testModule = TestUtils.findModule(modules, "types1");
LeafSchemaNode testleaf = (LeafSchemaNode) testModule
.getDataChildByName("testleaf");
ExtendedType leafType = (ExtendedType) testleaf.getType();
- assertEquals("my-type1", leafType.getQName().getLocalName());
- assertEquals("t2", leafType.getQName().getPrefix());
+ QName leafTypeQName = leafType.getQName();
+ assertEquals("my-type1", leafTypeQName.getLocalName());
+ assertEquals("t1", leafTypeQName.getPrefix());
+ assertEquals(URI.create("urn:simple.container.demo"),
+ leafTypeQName.getNamespace());
+ Date expectedDate = simpleDateFormat.parse("2013-02-27");
+ assertEquals(expectedDate, leafTypeQName.getRevision());
+ assertEquals(1, leafType.getRanges().size());
+
ExtendedType baseType = (ExtendedType) leafType.getBaseType();
- assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
- assertEquals("t2", baseType.getQName().getPrefix());
+ QName baseTypeQName = baseType.getQName();
+ assertEquals("my-type1", baseTypeQName.getLocalName());
+ assertEquals("t2", baseTypeQName.getPrefix());
+ assertEquals(URI.create("urn:simple.types.data.demo"),
+ baseTypeQName.getNamespace());
+ assertEquals(expectedDate, baseTypeQName.getRevision());
+ assertEquals(2, baseType.getRanges().size());
List<RangeConstraint> ranges = leafType.getRanges();
assertEquals(1, ranges.size());
assertEquals("my-string-type-ext", testleafTypeQName.getLocalName());
assertEquals("t2", testleafTypeQName.getPrefix());
- Set<String> expectedRegex = new HashSet<String>();
- expectedRegex.add("[a-k]*");
- expectedRegex.add("[b-u]*");
- expectedRegex.add("[e-z]*");
-
- Set<String> actualRegex = new HashSet<String>();
List<PatternConstraint> patterns = testleafType.getPatterns();
- assertEquals(3, patterns.size());
- for (PatternConstraint pc : patterns) {
- actualRegex.add(pc.getRegularExpression());
- }
- assertEquals(expectedRegex, actualRegex);
+ assertEquals(1, patterns.size());
+ PatternConstraint pattern = patterns.iterator().next();
+ assertEquals("[e-z]*", pattern.getRegularExpression());
- TypeDefinition<?> baseType = testleafType.getBaseType();
+ ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
assertEquals("my-string-type2", baseType.getQName().getLocalName());
- List<LengthConstraint> lengths = testleafType.getLengths();
- assertEquals(1, lengths.size());
+ patterns = baseType.getPatterns();
+ assertEquals(1, patterns.size());
+ pattern = patterns.iterator().next();
+ assertEquals("[b-u]*", pattern.getRegularExpression());
- LengthConstraint length = lengths.get(0);
- assertEquals(5L, length.getMin());
- assertEquals(10L, length.getMax());
+ List<LengthConstraint> lengths = testleafType.getLengths();
+ assertTrue(lengths.isEmpty());
}
@Test
ExtendedType baseType = (ExtendedType) testleafType.getBaseType();
assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
- Int32 int32base = (Int32) baseType.getBaseType();
- List<RangeConstraint> ranges = int32base.getRangeStatements();
+ Int32 int32Type = (Int32) baseType.getBaseType();
+ QName qname = int32Type.getQName();
+ assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
+ qname.getNamespace());
+ assertNull(qname.getRevision());
+ assertEquals("", qname.getPrefix());
+ assertEquals("int32", qname.getLocalName());
+ List<RangeConstraint> ranges = baseType.getRanges();
assertEquals(1, ranges.size());
RangeConstraint range = ranges.get(0);
assertEquals(2L, range.getMin());
ExtendedType type = (ExtendedType) testleaf.getType();
assertEquals(4, (int) type.getFractionDigits());
- Decimal64 baseType = (Decimal64) type.getBaseType();
- assertEquals(6, (int) baseType.getFractionDigits());
+ ExtendedType typeBase = (ExtendedType) type.getBaseType();
+ assertEquals("my-decimal-type", typeBase.getQName().getLocalName());
+ assertNull(typeBase.getFractionDigits());
+
+ Decimal64 decimal = (Decimal64) typeBase.getBaseType();
+ assertEquals(6, (int) decimal.getFractionDigits());
}
@Test
UnionType unionBase = (UnionType) baseType.getBaseType();
List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
- Int16 unionType1 = (Int16) unionTypes.get(0);
- List<RangeConstraint> ranges = unionType1.getRangeStatements();
+ ExtendedType unionType1 = (ExtendedType) unionTypes.get(0);
+ List<RangeConstraint> ranges = unionType1.getRanges();
assertEquals(1, ranges.size());
RangeConstraint range = ranges.get(0);
assertEquals(1L, range.getMin());
assertEquals(100L, range.getMax());
- assertTrue(unionTypes.get(0) instanceof Int16);
+ assertTrue(unionType1.getBaseType() instanceof Int16);
assertTrue(unionTypes.get(1) instanceof Int32);
}
.getBaseType();
List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion
.getTypes();
- assertTrue(extendedTargetTypes.get(0) instanceof Int16);
+ assertTrue(extendedTargetTypes.get(0).getBaseType() instanceof Int16);
assertTrue(extendedTargetTypes.get(1) instanceof Int32);
- Int16 int16 = (Int16) extendedTargetTypes.get(0);
- List<RangeConstraint> ranges = int16.getRangeStatements();
+ ExtendedType int16 = (ExtendedType) extendedTargetTypes.get(0);
+ assertTrue(int16.getBaseType() instanceof Int16);
+ List<RangeConstraint> ranges = int16.getRanges();
assertEquals(1, ranges.size());
RangeConstraint range = ranges.get(0);
assertEquals(1L, range.getMin());
UnionType myUnionBase = (UnionType) myUnion.getBaseType();
List<TypeDefinition<?>> myUnionBaseTypes = myUnionBase.getTypes();
assertEquals(2, myUnionBaseTypes.size());
- assertTrue(myUnionBaseTypes.get(0) instanceof Int16);
+ assertTrue(myUnionBaseTypes.get(0).getBaseType() instanceof Int16);
assertTrue(myUnionBaseTypes.get(1) instanceof Int32);
- Int16 int16 = (Int16) myUnionBaseTypes.get(0);
- List<RangeConstraint> ranges = int16.getRangeStatements();
+ ExtendedType int16 = (ExtendedType) myUnionBaseTypes.get(0);
+ List<RangeConstraint> ranges = int16.getRanges();
assertEquals(1, ranges.size());
RangeConstraint range = ranges.get(0);
assertEquals(1L, range.getMin());
assertEquals(1, usesNodes.size());
UsesNode usesNode = usesNodes.iterator().next();
Map<SchemaPath, SchemaNode> refines = usesNode.getRefines();
- assertEquals(2, refines.size());
+ assertEquals(5, refines.size());
+ LeafSchemaNode refineLeaf = null;
+ ContainerSchemaNode refineContainer = null;
+ ListSchemaNode refineList = null;
+ GroupingDefinition refineGrouping = null;
+ TypeDefinition<?> typedef = null;
for (Map.Entry<SchemaPath, SchemaNode> entry : refines.entrySet()) {
SchemaNode value = entry.getValue();
-
if (value instanceof LeafSchemaNode) {
- LeafSchemaNode refineLeaf = (LeafSchemaNode) value;
- assertNotNull(refineLeaf);
- } else {
- ContainerSchemaNode refineContainer = (ContainerSchemaNode) value;
- Set<MustDefinition> mustConstraints = refineContainer
- .getConstraints().getMustConstraints();
- assertEquals(1, mustConstraints.size());
- MustDefinition must = mustConstraints.iterator().next();
- assertEquals("must-condition", must.toString());
- assertEquals("An error message test", must.getErrorMessage());
- assertEquals(("An error app tag test"), must.getErrorAppTag());
+ refineLeaf = (LeafSchemaNode) value;
+ } else if (value instanceof ContainerSchemaNode) {
+ refineContainer = (ContainerSchemaNode) value;
+ } else if (value instanceof ListSchemaNode) {
+ refineList = (ListSchemaNode) value;
+ } else if (value instanceof GroupingDefinition) {
+ refineGrouping = (GroupingDefinition) value;
+ } else if (value instanceof TypeDefinition<?>) {
+ typedef = (TypeDefinition<?>) value;
}
}
+
+ // leaf address
+ assertNotNull(refineLeaf);
+ assertEquals("address", refineLeaf.getQName().getLocalName());
+ assertEquals("description of address defined by refine",
+ refineLeaf.getDescription());
+ assertEquals("address reference added by refine",
+ refineLeaf.getReference());
+ assertFalse(refineLeaf.isConfiguration());
+ assertTrue(refineLeaf.getConstraints().isMandatory());
+ Set<MustDefinition> leafMustConstraints = refineLeaf.getConstraints()
+ .getMustConstraints();
+ assertEquals(1, leafMustConstraints.size());
+ MustDefinition leafMust = leafMustConstraints.iterator().next();
+ assertEquals(
+ "\"ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)\"",
+ leafMust.toString());
+
+ // container port
+ assertNotNull(refineContainer);
+ Set<MustDefinition> mustConstraints = refineContainer.getConstraints()
+ .getMustConstraints();
+ assertTrue(mustConstraints.isEmpty());
+ assertEquals("description of port defined by refine",
+ refineContainer.getDescription());
+ assertEquals("port reference added by refine",
+ refineContainer.getReference());
+ assertFalse(refineContainer.isConfiguration());
+ assertTrue(refineContainer.isPresenceContainer());
+
+ // list addresses
+ assertNotNull(refineList);
+ assertEquals("description of addresses defined by refine",
+ refineList.getDescription());
+ assertEquals("addresses reference added by refine",
+ refineList.getReference());
+ assertFalse(refineList.isConfiguration());
+ assertEquals(2, (int) refineList.getConstraints().getMinElements());
+ assertEquals(12, (int) refineList.getConstraints().getMaxElements());
+
+ // grouping target-inner
+ assertNotNull(refineGrouping);
+ Set<DataSchemaNode> refineGroupingChildren = refineGrouping
+ .getChildNodes();
+ assertEquals(1, refineGroupingChildren.size());
+ LeafSchemaNode refineGroupingLeaf = (LeafSchemaNode) refineGroupingChildren
+ .iterator().next();
+ assertEquals("inner-grouping-id", refineGroupingLeaf.getQName()
+ .getLocalName());
+ assertEquals("new target-inner grouping description",
+ refineGrouping.getDescription());
+
+ // typedef group-type
+ assertNotNull(typedef);
+ assertEquals("new group-type description", typedef.getDescription());
+ assertEquals("new group-type reference", typedef.getReference());
+ assertTrue(typedef.getBaseType() instanceof ExtendedType);
}
@Test
assertNotNull(output.getDataChildByName("data"));
}
+ @Test
+ public void testGrouping() {
+ Module testModule = TestUtils.findModule(modules, "types2");
+ Set<GroupingDefinition> groupings = testModule.getGroupings();
+ assertEquals(1, groupings.size());
+ GroupingDefinition grouping = groupings.iterator().next();
+ Set<DataSchemaNode> children = grouping.getChildNodes();
+ assertEquals(5, children.size());
+ }
+
+ @Test
+ public void testAugmentNodesTypesSchemaPath() throws Exception {
+ Module testModule = TestUtils.findModule(modules, "types1");
+ Set<AugmentationSchema> augments = testModule.getAugmentations();
+ assertEquals(1, augments.size());
+ AugmentationSchema augment = augments.iterator().next();
+
+ LeafSchemaNode ifcId = (LeafSchemaNode) augment
+ .getDataChildByName("interface-id");
+ Leafref ifcIdType = (Leafref) ifcId.getType();
+ SchemaPath ifcIdTypeSchemaPath = ifcIdType.getPath();
+ List<QName> ifcIdTypePath = ifcIdTypeSchemaPath.getPath();
+
+ URI types1URI = URI.create("urn:simple.container.demo");
+ URI types2URI = URI.create("urn:simple.types.data.demo");
+ URI types3URI = URI.create("urn:simple.container.demo.test");
+ Date expectedDate = simpleDateFormat.parse("2013-02-27");
+
+ QName q0 = new QName(types2URI,
+ expectedDate, "data", "interfaces");
+ QName q1 = new QName(types2URI,
+ expectedDate, "data", "ifEntry");
+ QName q2 = new QName(types3URI,
+ expectedDate, "data", "augment-holder");
+ QName q3 = new QName(types1URI,
+ expectedDate, "data", "interface-id");
+ assertEquals(q0, ifcIdTypePath.get(0));
+ assertEquals(q1, ifcIdTypePath.get(1));
+ assertEquals(q2, ifcIdTypePath.get(2));
+ assertEquals(q3, ifcIdTypePath.get(3));
+
+ LeafListSchemaNode higherLayer = (LeafListSchemaNode) augment
+ .getDataChildByName("higher-layer-if");
+ Leafref higherLayerType = (Leafref) higherLayer.getType();
+ SchemaPath higherLayerTypeSchemaPath = higherLayerType.getPath();
+ List<QName> higherLayerTypePath = higherLayerTypeSchemaPath.getPath();
+ assertEquals(q0, higherLayerTypePath.get(0));
+ assertEquals(q1, higherLayerTypePath.get(1));
+ assertEquals(q2, higherLayerTypePath.get(2));
+ q3 = new QName(types1URI,
+ expectedDate, "data", "higher-layer-if");
+ assertEquals(q3, higherLayerTypePath.get(3));
+
+ LeafSchemaNode myType = (LeafSchemaNode) augment
+ .getDataChildByName("my-type");
+ ExtendedType leafType = (ExtendedType)myType.getType();
+
+ testModule = TestUtils.findModule(modules, "types2");
+ TypeDefinition<?> typedef = TestUtils.findTypedef(testModule.getTypeDefinitions(), "my-type1");
+
+ assertEquals(typedef, leafType);
+ }
+
+ @Test
+ public void testTypePath() throws ParseException {
+ Module test = TestUtils.findModule(modules, "types2");
+ Set<TypeDefinition<?>> types = test.getTypeDefinitions();
+
+ // my-base-int32-type
+ ExtendedType int32Typedef = (ExtendedType) TestUtils.findTypedef(types,
+ "my-base-int32-type");
+ QName int32TypedefQName = int32Typedef.getQName();
+
+ URI expectedNS = URI.create("urn:simple.types.data.demo");
+ Date expectedDate = simpleDateFormat.parse("2013-02-27");
+ assertEquals(expectedNS, int32TypedefQName.getNamespace());
+ assertEquals(expectedDate, int32TypedefQName.getRevision());
+ assertEquals("t2", int32TypedefQName.getPrefix());
+ assertEquals("my-base-int32-type", int32TypedefQName.getLocalName());
+
+ SchemaPath typeSchemaPath = int32Typedef.getPath();
+ List<QName> typePath = typeSchemaPath.getPath();
+ assertEquals(1, typePath.size());
+ assertEquals(int32TypedefQName, typePath.get(0));
+
+ // my-base-int32-type/int32
+ Int32 int32 = (Int32) int32Typedef.getBaseType();
+ QName int32QName = int32.getQName();
+ assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
+ int32QName.getNamespace());
+ assertNull(int32QName.getRevision());
+ assertEquals("", int32QName.getPrefix());
+ assertEquals("int32", int32QName.getLocalName());
+
+ SchemaPath int32SchemaPath = int32.getPath();
+ List<QName> int32Path = int32SchemaPath.getPath();
+ assertEquals(3, int32Path.size());
+ assertEquals(int32TypedefQName, int32Path.get(0));
+ assertEquals(int32QName, int32Path.get(2));
+ }
+
+ @Test
+ public void testTypePath2() throws ParseException {
+ Module test = TestUtils.findModule(modules, "types2");
+ Set<TypeDefinition<?>> types = test.getTypeDefinitions();
+
+ // my-base-int32-type
+ ExtendedType myDecType = (ExtendedType) TestUtils.findTypedef(types,
+ "my-decimal-type");
+ QName myDecTypeQName = myDecType.getQName();
+
+ URI expectedNS = URI.create("urn:simple.types.data.demo");
+ Date expectedDate = simpleDateFormat.parse("2013-02-27");
+ assertEquals(expectedNS, myDecTypeQName.getNamespace());
+ assertEquals(expectedDate, myDecTypeQName.getRevision());
+ assertEquals("t2", myDecTypeQName.getPrefix());
+ assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
+
+ SchemaPath typeSchemaPath = myDecType.getPath();
+ List<QName> typePath = typeSchemaPath.getPath();
+ assertEquals(1, typePath.size());
+ assertEquals(myDecTypeQName, typePath.get(0));
+
+ // my-base-int32-type/int32
+ Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
+ QName dec64QName = dec64.getQName();
+
+ assertEquals(URI.create("urn:ietf:params:xml:ns:yang:1"),
+ dec64QName.getNamespace());
+ assertNull(dec64QName.getRevision());
+ assertEquals("", dec64QName.getPrefix());
+ assertEquals("decimal64", dec64QName.getLocalName());
+
+ SchemaPath dec64SchemaPath = dec64.getPath();
+ List<QName> dec64Path = dec64SchemaPath.getPath();
+ assertEquals(2, dec64Path.size());
+ assertEquals(myDecTypeQName, dec64Path.get(0));
+ assertEquals(dec64QName, dec64Path.get(1));
+ }
+
}