Refactored YANG types resolving.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / YangModelParserTest.java
index 889290f65c1ce20dc1ffe0dd6e690217062305c5..650eaeab8221f2c89fa53ff47c11d20c45661ea8 100644 (file)
@@ -15,24 +15,28 @@ import java.util.Set;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.controller.model.api.type.IntegerTypeDefinition;
-import org.opendaylight.controller.model.api.type.PatternConstraint;
-import org.opendaylight.controller.model.api.type.RangeConstraint;
-import org.opendaylight.controller.model.util.Decimal64;
-import org.opendaylight.controller.model.util.Int32;
-import org.opendaylight.controller.model.util.StringType;
+import org.opendaylight.controller.yang.common.QName;
 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
 import org.opendaylight.controller.yang.model.api.ContainerSchemaNode;
 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.api.TypeDefinition;
+import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
+import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
+import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
+import org.opendaylight.controller.yang.model.util.Decimal64;
+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.StringType;
+import org.opendaylight.controller.yang.model.util.UnionType;
 
 public class YangModelParserTest {
 
-    private final String testFile1 = "src/test/resources/model/testfile1.yang";
-    private final String testFile2 = "src/test/resources/model/testfile2.yang";
+    private static final String TEST_FILE1 = "src/test/resources/model/testfile1.yang";
+    private static final String TEST_FILE2 = "src/test/resources/model/testfile2.yang";
     private YangModelParser tested;
 
     @Before
@@ -42,35 +46,25 @@ public class YangModelParserTest {
 
     @Test
     public void testAugment() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m2 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types2")) {
-                m2 = m;
-            }
-        }
-        assertNotNull(m2);
+        Module testModule = findModule(modules, "types2");
+        assertNotNull(testModule);
 
-        AugmentationSchema augment = m2.getAugmentations().iterator().next();
+        AugmentationSchema augment = testModule.getAugmentations().iterator().next();
         assertNotNull(augment);
     }
 
     @Test
     public void testAugmentTarget() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m1 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types1")) {
-                m1 = m;
-            }
-        }
-        assertNotNull(m1);
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
 
-        ContainerSchemaNode container = (ContainerSchemaNode)m1.getDataChildByName("interfaces");
+        ContainerSchemaNode container = (ContainerSchemaNode)testModule.getDataChildByName("interfaces");
         assertNotNull(container);
 
         ListSchemaNode list = (ListSchemaNode)container.getDataChildByName("ifEntry");
@@ -83,110 +77,103 @@ public class YangModelParserTest {
 
     @Test
     public void testTypedefRangesResolving() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m1 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types1")) {
-                m1 = m;
-            }
-        }
-        assertNotNull(m1);
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
 
-        LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("testleaf");
-        TypeDefinition<?> baseType = testleaf.getType().getBaseType();
-        assertTrue(testleaf.getType().getBaseType() instanceof Int32);
-        Int32 baseTypeCast = (Int32)baseType;
-        List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
-        assertEquals(2, ranges.size());
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("testleaf");
+        ExtendedType leafType = (ExtendedType)testleaf.getType();
+        assertEquals("my-type1", leafType.getQName().getLocalName());
+        assertEquals("t2", leafType.getQName().getPrefix());
+        ExtendedType baseType = (ExtendedType)leafType.getBaseType();
+        assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
+        assertEquals("t2", baseType.getQName().getPrefix());
+
+        List<RangeConstraint> ranges = leafType.getRanges();
+        assertEquals(1, ranges.size());
         RangeConstraint range = ranges.get(0);
-        assertEquals(2L, range.getMin());
+        assertEquals(11L, range.getMin());
         assertEquals(20L, range.getMax());
     }
 
     @Test
     public void testTypedefPatternsResolving() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m1 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types1")) {
-                m1 = m;
-            }
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
+
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("test-string-leaf");
+        ExtendedType testleafType = (ExtendedType)testleaf.getType();
+        QName testleafTypeQName = testleafType.getQName();
+        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());
         }
-        assertNotNull(m1);
+        assertEquals(expectedRegex, actualRegex);
 
-        LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-string-leaf");
-        TypeDefinition<?> baseType = testleaf.getType().getBaseType();
-        assertTrue(testleaf.getType().getBaseType() instanceof StringType);
-        StringType baseTypeCast = (StringType)baseType;
-
-        Set<String> expectedRegularExpressions = new HashSet<String>();
-        expectedRegularExpressions.add("[a-k]*");
-        expectedRegularExpressions.add("[b-u]*");
-        expectedRegularExpressions.add("[e-z]*");
-
-        Set<String> actualRegularExpressions = new HashSet<String>();
-        List<PatternConstraint> patterns = baseTypeCast.getPatterns();
-        for(PatternConstraint pc : patterns) {
-            actualRegularExpressions.add(pc.getRegularExpression());
-        }
+        TypeDefinition<?> baseType = testleafType.getBaseType();
+        assertEquals("my-string-type2", baseType.getQName().getLocalName());
 
-        assertEquals(expectedRegularExpressions, actualRegularExpressions);
+        List<LengthConstraint> lengths = testleafType.getLengths();
+        assertEquals(1, lengths.size());
+
+        LengthConstraint length = lengths.get(0);
+        assertEquals(5L, length.getMin());
+        assertEquals(10L, length.getMax());
     }
 
     @Test
     public void testTypedefLengthsResolving() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m1 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types1")) {
-                m1 = m;
-            }
-        }
-        assertNotNull(m1);
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
 
-        LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-int-leaf");
-        TypeDefinition<?> baseType = testleaf.getType().getBaseType();
-        assertTrue(testleaf.getType().getBaseType() instanceof IntegerTypeDefinition);
-        Int32 baseTypeCast = (Int32)baseType;
-
-        Long[][] expectedRanges = new Long[3][2];
-        expectedRanges[0] = new Long[]{10L, 20L};
-        expectedRanges[1] = new Long[]{12L, 18L};
-        expectedRanges[2] = new Long[]{14L, 16L};
-
-        List<RangeConstraint> actualRanges = baseTypeCast.getRangeStatements();
-        assertEquals(3, actualRanges.size());
-        for(int i = 0; i < actualRanges.size(); i++) {
-            assertEquals(expectedRanges[i][0], actualRanges.get(i).getMin());
-            assertEquals(expectedRanges[i][1], actualRanges.get(i).getMax());
-        }
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("leaf-with-length");
+        ExtendedType testleafType = (ExtendedType)testleaf.getType();
+        assertEquals("my-string-type", testleafType.getQName().getLocalName());
+
+        List<LengthConstraint> lengths = testleafType.getLengths();
+        assertEquals(1, lengths.size());
+
+        LengthConstraint length = lengths.get(0);
+        assertEquals(7L, length.getMin());
+        assertEquals(10L, length.getMax());
     }
 
     @Test
     public void testTypeDef() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m2 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types2")) {
-                m2 = m;
-            }
-        }
-        assertNotNull(m2);
+        Module testModule = findModule(modules, "types2");
+        assertNotNull(testModule);
 
-        LeafSchemaNode testleaf = (LeafSchemaNode)m2.getDataChildByName("nested-type-leaf");
-        TypeDefinition<?> baseType = testleaf.getType().getBaseType();
-        assertTrue(testleaf.getType().getBaseType() instanceof Int32);
-        Int32 baseTypeCast = (Int32)baseType;
-        List<RangeConstraint> ranges = baseTypeCast.getRangeStatements();
-        assertEquals(2, ranges.size());
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("nested-type-leaf");
+        ExtendedType testleafType = (ExtendedType)testleaf.getType();
+        assertEquals("my-type1", testleafType.getQName().getLocalName());
+
+        ExtendedType baseType = (ExtendedType)testleafType.getBaseType();
+        assertEquals("my-base-int32-type", baseType.getQName().getLocalName());
+
+        Int32 int32base = (Int32)baseType.getBaseType();
+        List<RangeConstraint> ranges = int32base.getRangeStatements();
+        assertEquals(1, ranges.size());
         RangeConstraint range = ranges.get(0);
         assertEquals(2L, range.getMin());
         assertEquals(20L, range.getMax());
@@ -194,42 +181,113 @@ public class YangModelParserTest {
 
     @Test
     public void testTypedefDecimal1() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m1 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types1")) {
-                m1 = m;
-            }
-        }
-        assertNotNull(m1);
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
 
-        LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf");
-        TypeDefinition<?> baseType = testleaf.getType().getBaseType();
-        assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("test-decimal-leaf");
+        ExtendedType type = (ExtendedType)testleaf.getType();
+
+        TypeDefinition<?> baseType = type.getBaseType();
+        assertTrue(baseType instanceof Decimal64);
         Decimal64 baseTypeCast = (Decimal64)baseType;
-        assertEquals(4, (int)baseTypeCast.getFractionDigits());
+        assertEquals(6, (int)baseTypeCast.getFractionDigits());
     }
 
     @Test
     public void testTypedefDecimal2() {
-        Set<Module> modules = tested.parseYangModels(testFile1, testFile2);
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
         assertEquals(2, modules.size());
 
-        Module m1 = null;
-        for(Module m : modules) {
-            if(m.getName().equals("types1")) {
-                m1 = m;
-            }
-        }
-        assertNotNull(m1);
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
 
-        LeafSchemaNode testleaf = (LeafSchemaNode)m1.getDataChildByName("test-decimal-leaf2");
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("test-decimal-leaf2");
         TypeDefinition<?> baseType = testleaf.getType().getBaseType();
         assertTrue(testleaf.getType().getBaseType() instanceof Decimal64);
         Decimal64 baseTypeCast = (Decimal64)baseType;
         assertEquals(5, (int)baseTypeCast.getFractionDigits());
     }
 
+    @Test
+    public void testTypedefUnion() {
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
+        assertEquals(2, modules.size());
+
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
+
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("union-leaf");
+        ExtendedType testleafType = (ExtendedType)testleaf.getType();
+        assertEquals("my-union-ext", testleafType.getQName().getLocalName());
+
+        ExtendedType baseType = (ExtendedType)testleafType.getBaseType();
+        assertEquals("my-union", baseType.getQName().getLocalName());
+
+        UnionType unionBase = (UnionType) baseType.getBaseType();
+
+        List<TypeDefinition<?>> unionTypes = unionBase.getTypes();
+        Int16 unionType1 = (Int16)unionTypes.get(0);
+        List<RangeConstraint> ranges = unionType1.getRangeStatements();
+        assertEquals(1, ranges.size());
+        RangeConstraint range = ranges.get(0);
+        assertEquals(1L, range.getMin());
+        assertEquals(100L, range.getMax());
+
+        assertTrue(unionTypes.get(0) instanceof Int16);
+        assertTrue(unionTypes.get(1) instanceof Int32);
+    }
+
+    @Test
+    public void testNestedUnionResolving() {
+        Set<Module> modules = tested.parseYangModels(TEST_FILE1, TEST_FILE2);
+        assertEquals(2, modules.size());
+
+        Module testModule = findModule(modules, "types1");
+        assertNotNull(testModule);
+
+        LeafSchemaNode testleaf = (LeafSchemaNode)testModule.getDataChildByName("nested-union-leaf");
+
+        ExtendedType nestedUnion1 = (ExtendedType)testleaf.getType();
+        assertEquals("nested-union1", nestedUnion1.getQName().getLocalName());
+
+        ExtendedType nestedUnion2 = (ExtendedType)nestedUnion1.getBaseType();
+        assertEquals("nested-union2", nestedUnion2.getQName().getLocalName());
+
+        UnionType unionType1 = (UnionType)nestedUnion2.getBaseType();
+        List<TypeDefinition<?>> unionTypes = unionType1.getTypes();
+        assertEquals(2, unionTypes.size());
+        assertTrue(unionTypes.get(0) instanceof StringType);
+        assertTrue(unionTypes.get(1) instanceof ExtendedType);
+
+        ExtendedType extendedUnion = (ExtendedType)unionTypes.get(1);
+        ExtendedType extendedUnionBase = (ExtendedType)extendedUnion.getBaseType();
+        assertEquals("my-union", extendedUnionBase.getQName().getLocalName());
+
+        UnionType extendedTargetUnion = (UnionType)extendedUnionBase.getBaseType();
+        List<TypeDefinition<?>> extendedTargetTypes = extendedTargetUnion.getTypes();
+        assertTrue(extendedTargetTypes.get(0) instanceof Int16);
+        assertTrue(extendedTargetTypes.get(1) instanceof Int32);
+
+        Int16 int16 = (Int16) extendedTargetTypes.get(0);
+        List<RangeConstraint> ranges = int16.getRangeStatements();
+        assertEquals(1, ranges.size());
+        RangeConstraint range = ranges.get(0);
+        assertEquals(1L, range.getMin());
+        assertEquals(100L, range.getMax());
+    }
+
+    private Module findModule(Set<Module> modules, String moduleName) {
+        Module result = null;
+        for(Module module : modules) {
+            if(module.getName().equals(moduleName)) {
+                result = module;
+                break;
+            }
+        }
+        return result;
+    }
+
 }