Added new parse method to YangModelParser. 18/1418/1
authorMartin Vitez <mvitez@cisco.com>
Wed, 25 Sep 2013 12:53:23 +0000 (14:53 +0200)
committerMartin Vitez <mvitez@cisco.com>
Wed, 25 Sep 2013 12:53:23 +0000 (14:53 +0200)
Added method 'parseYangModels(File yangFile, File directory)'. Method is usefull if user don't want to explicitly specify all yang files, but define only yang files destination directory instead.

Signed-off-by: Martin Vitez <mvitez@cisco.com>
yang/yang-parser-api/src/main/java/org/opendaylight/yangtools/yang/model/parser/api/YangModelParser.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserNegativeTest.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserWithContextTest.java
yang/yang-parser-impl/src/test/resources/ietf/iana-afn-safi@2012-06-04.yang [moved from yang/yang-parser-impl/src/test/resources/types/iana-afn-safi@2012-06-04.yang with 100% similarity]
yang/yang-parser-impl/src/test/resources/ietf/iana-if-type@2012-06-05.yang [moved from yang/yang-parser-impl/src/test/resources/types/iana-if-type@2012-06-05.yang with 100% similarity]
yang/yang-parser-impl/src/test/resources/ietf/iana-timezones@2012-07-09.yang [moved from yang/yang-parser-impl/src/test/resources/types/iana-timezones@2012-07-09.yang with 100% similarity]
yang/yang-parser-impl/src/test/resources/ietf/ietf-inet-types@2010-09-24.yang [moved from yang/yang-parser-impl/src/test/resources/types/ietf-inet-types@2010-09-24.yang with 100% similarity]
yang/yang-parser-impl/src/test/resources/ietf/ietf-yang-types@2010-09-24.yang [moved from yang/yang-parser-impl/src/test/resources/types/ietf-yang-types@2010-09-24.yang with 100% similarity]
yang/yang-parser-impl/src/test/resources/types/custom-types-test@2012-4-4.yang

index 9a1c68b79fc316bf1489350d72125766efa9fddd..0ab1a29f302e29de50a7c6374ba5ff8414f70ae9 100644 (file)
@@ -24,6 +24,17 @@ import org.opendaylight.yangtools.yang.model.api.type.UnknownTypeDefinition;
  */
 public interface YangModelParser {
 
+    /**
+     * Parse yangFile file and all yang files found in directory.
+     *
+     * @param yangFile
+     *            file to parse
+     * @param directory
+     *            directory which contains additional yang files
+     * @return Set of Yang Modules
+     */
+    Set<Module> parseYangModels(final File yangFile, final File directory);
+
     /**
      * Parse one or more Yang model files and return the definitions of Yang
      * modules defined in *.yang files; <br>
index f2ab67792a4693431064ba4f8e51c77f1e9feeef..3773ab4da46cb3305e389e62dae007a71a7e4b2f 100644 (file)
@@ -29,6 +29,7 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;
 
+import com.google.common.base.Preconditions;
 import org.antlr.v4.runtime.ANTLRInputStream;
 import org.antlr.v4.runtime.CommonTokenStream;
 import org.antlr.v4.runtime.tree.ParseTree;
@@ -78,6 +79,28 @@ public final class YangParserImpl implements YangModelParser {
 
     private static final String FAIL_DEVIATION_TARGET = "Failed to find deviation target.";
 
+    @Override
+    public Set<Module> parseYangModels(final File yangFile, final File directory) {
+        Preconditions.checkState(yangFile.exists(), yangFile + " does not exists");
+        Preconditions.checkState(directory.exists(), directory + " does not exists");
+        Preconditions.checkState(directory.isDirectory(), directory + " is not a directory");
+
+        final String yangFileName = yangFile.getName();
+        final String[] fileList = directory.list();
+        Preconditions.checkNotNull(fileList, directory + " not found");
+
+        final List<File> modules = new ArrayList<>();
+        for (String fileName : fileList) {
+            if (fileName.equals(yangFileName)) {
+                continue;
+            }
+            modules.add(new File(directory, fileName));
+        }
+        modules.add(yangFile);
+
+        return parseYangModels(modules);
+    }
+
     @Override
     public Set<Module> parseYangModels(final List<File> yangFiles) {
         return Sets.newLinkedHashSet(parseYangModelsMapped(yangFiles).values());
index 70e7ea229fd226523dc8fcdad9259ad60915de06..0f2807efaa505f93120e32179f30de598690fdf9 100644 (file)
@@ -9,8 +9,10 @@ package org.opendaylight.yangtools.yang.parser.impl;
 
 import static org.junit.Assert.*;
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.net.URI;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
@@ -27,6 +29,7 @@ import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPai
 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.StringTypeDefinition;
+import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
 import org.opendaylight.yangtools.yang.model.util.BitsType;
 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
@@ -39,7 +42,10 @@ public class TypesResolutionTest {
 
     @Before
     public void init() throws FileNotFoundException {
-        testedModules = TestUtils.loadModules(getClass().getResource("/types").getPath());
+        File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
+        File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
+        YangModelParser parser = new YangParserImpl();
+        testedModules = parser.parseYangModels(yangFile, dependenciesDir);
     }
 
     @Test
index e05e0224a47ff0a603bcd37e39311f43644fefac..7f2400a9587fbcb3524ed00aa18cdb9afbd09dea 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.parser.impl;
 
 import static org.junit.Assert.*;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -17,6 +18,7 @@ import java.util.Arrays;
 import java.util.List;
 
 import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
 
@@ -212,4 +214,32 @@ public class YangParserNegativeTest {
         }
     }
 
+    @Test
+    public void testWrongDependenciesDir() throws Exception {
+        try {
+            File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
+            File dependenciesDir = new File("/invalid");
+            YangModelParser parser = new YangParserImpl();
+            parser.parseYangModels(yangFile, dependenciesDir);
+            fail("Exception should by thrown");
+        } catch (IllegalStateException e) {
+            String expected = "/invalid does not exists";
+            assertEquals(expected, e.getMessage());
+        }
+    }
+
+    @Test
+    public void testWrongDependenciesDir2() throws Exception {
+        try {
+            File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
+            File dependenciesDir = new File(getClass().getResource("/model").getPath());
+            YangModelParser parser = new YangParserImpl();
+            parser.parseYangModels(yangFile, dependenciesDir);
+            fail("Exception should by thrown");
+        } catch (YangValidationException e) {
+            String expected = "Not existing module imported";
+            assertTrue(e.getMessage().contains(expected));
+        }
+    }
+
 }
index 51bb14c3fd78ce329bec0215a18703d9d168754a..3975cdd2e9787c4b36144222ddc5e5645ab9d5db 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.parser.impl;
 
 import static org.junit.Assert.*;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.net.URI;
@@ -40,6 +41,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
+import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
 
 import com.google.common.collect.Lists;
@@ -51,7 +53,7 @@ public class YangParserWithContextTest {
     @Test
     public void testTypeFromContext() throws Exception {
         SchemaContext context = null;
-        String resource = "/types/ietf-inet-types@2010-09-24.yang";
+        String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
         InputStream stream = new FileInputStream(getClass().getResource(resource).getPath());
         context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
         stream.close();
@@ -269,10 +271,11 @@ public class YangParserWithContextTest {
     @Test
     public void testIdentity() throws Exception {
         SchemaContext context = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
-                .getPath())) {
-            context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
-        }
+        File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
+        File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
+        YangModelParser parser = new YangParserImpl();
+        context = parser.resolveSchemaContext(parser.parseYangModels(yangFile, dependenciesDir));
+
         Module module = null;
         try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
             module = TestUtils.loadModuleWithContext(stream, context);
@@ -300,10 +303,10 @@ public class YangParserWithContextTest {
     @Test
     public void testUnknownNodes() throws Exception {
         SchemaContext context = null;
-        try (InputStream stream = new FileInputStream(getClass().getResource("/types/custom-types-test@2012-4-4.yang")
-                .getPath())) {
-            context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
-        }
+        File yangFile = new File(getClass().getResource("/types/custom-types-test@2012-4-4.yang").getPath());
+        File dependenciesDir = new File(getClass().getResource("/ietf").getPath());
+        YangModelParser parser = new YangParserImpl();
+        context = parser.resolveSchemaContext(parser.parseYangModels(yangFile, dependenciesDir));
 
         Module module = null;
         try (InputStream stream = new FileInputStream(getClass().getResource("/context-test/test3.yang").getPath())) {
index 7479e377720b2f358c6b1b14300f56f24574e177..b7f07fcae194a1482d8bdc9679bd53699c3efeb2 100644 (file)
@@ -4,6 +4,23 @@ module custom-types-test {
     namespace "urn:custom.types.demo";
     prefix "iit";
 
+    // imports added only for testing purposes
+    import iana-afn-safi {
+        prefix "afn";
+    }
+    import iana-if-type {
+        prefix "if";
+    }
+    import iana-timezones {
+        prefix "tz";
+    }
+    import ietf-inet-types {
+        prefix "inet";
+    }
+    import ietf-yang-types {
+        prefix "yang";
+    }
+
     organization "opendaylight";
     contact "WILL-BE-DEFINED-LATER";
         revision 2012-04-16 {