*/
package org.opendaylight.yangtools.yang.parser.impl;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.InputStream;
import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Date;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.*;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Deviation;
import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
+import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
+import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
+import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
+import org.opendaylight.yangtools.yang.model.api.ModuleImport;
+import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
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.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
import org.opendaylight.yangtools.yang.model.util.Decimal64;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
import org.opendaylight.yangtools.yang.model.util.Int16;
import org.opendaylight.yangtools.yang.model.util.StringType;
import org.opendaylight.yangtools.yang.model.util.Uint32;
import org.opendaylight.yangtools.yang.model.util.UnionType;
+import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
public class YangParserTest {
+ public static final String FS = File.separator;
+
private final URI fooNS = URI.create("urn:opendaylight.foo");
private final URI barNS = URI.create("urn:opendaylight.bar");
private final URI bazNS = URI.create("urn:opendaylight.baz");
private Set<Module> modules;
@Before
- public void init() throws FileNotFoundException, ParseException {
+ public void init() throws Exception {
DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
fooRev = simpleDateFormat.parse("2013-02-27");
barRev = simpleDateFormat.parse("2013-07-03");
bazRev = simpleDateFormat.parse("2013-02-27");
- modules = TestUtils.loadModules(getClass().getResource("/model").getPath());
+ modules = TestUtils.loadModules(getClass().getResource("/model").toURI());
assertEquals(3, modules.size());
}
assertEquals(0, ifEntry.getUses().size());
LeafSchemaNode ifIndex = (LeafSchemaNode) ifEntry.getDataChildByName("ifIndex");
+ assertEquals(ifEntry.getKeyDefinition().get(0), ifIndex.getQName());
assertTrue(ifIndex.getType() instanceof Uint32);
LeafSchemaNode ifMtu = (LeafSchemaNode) ifEntry.getDataChildByName("ifMtu");
assertTrue(ifMtu.getType() instanceof Int32);
List<QName> path = new ArrayList<>();
path.add(new QName(barNS, barRev, "br", "interfaces"));
path.add(new QName(barNS, barRev, "br", "ifEntry"));
- SchemaPath expectedPath = new SchemaPath(path, true);
+ SchemaPath expectedPath = SchemaPath.create(path, true);
assertEquals(expectedPath, dev.getTargetPath());
assertEquals(Deviate.ADD, dev.getDeviate());
assertEquals("int32-ext1", int32TypedefQName.getLocalName());
SchemaPath typeSchemaPath = int32ext1.getPath();
- List<QName> typePath = typeSchemaPath.getPath();
- assertEquals(1, typePath.size());
- assertEquals(int32TypedefQName, typePath.get(0));
+ Iterable<QName> typePath = typeSchemaPath.getPathFromRoot();
+ Iterator<QName> typePathIt = typePath.iterator();
+ assertEquals(int32TypedefQName, typePathIt.next());
+ assertFalse(typePathIt.hasNext());
+
// int32-ext1/int32
Int32 int32 = (Int32) int32ext1.getBaseType();
assertEquals("my-decimal-type", myDecTypeQName.getLocalName());
SchemaPath typeSchemaPath = myDecType.getPath();
- List<QName> typePath = typeSchemaPath.getPath();
- assertEquals(1, typePath.size());
- assertEquals(myDecTypeQName, typePath.get(0));
+ Iterable<QName> typePath = typeSchemaPath.getPathFromRoot();
+ Iterator<QName> typePathIt = typePath.iterator();
+ assertEquals(myDecTypeQName, typePathIt.next());
+ assertFalse(typePathIt.hasNext());
// my-base-int32-type/int32
Decimal64 dec64 = (Decimal64) myDecType.getBaseType();
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));
+ Iterable<QName> dec64Path = dec64SchemaPath.getPathFromRoot();
+ Iterator<QName> dec64PathIt = dec64Path.iterator();
+ assertEquals(myDecTypeQName, dec64PathIt.next());
+ assertEquals(dec64QName, dec64PathIt.next());
+ assertFalse(dec64PathIt.hasNext());
}
@Test
- public void testParseMethod1() throws ParseException {
- File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
- File dependenciesDir = new File(getClass().getResource("/parse-methods").getPath());
- YangModelParser parser = new YangParserImpl();
- modules = parser.parseYangModels(yangFile, dependenciesDir);
+ public void testParseMethod1() throws Exception {
+ File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
+ File dependenciesDir = new File(getClass().getResource("/parse-methods").toURI());
+ YangContextParser parser = new YangParserImpl();
+ modules = parser.parseFile(yangFile, dependenciesDir).getModules();
assertEquals(6, modules.size());
}
@Test
- public void testParseMethod2() throws ParseException {
- File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").getPath());
- File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").getPath());
- YangModelParser parser = new YangParserImpl();
- modules = parser.parseYangModels(yangFile, dependenciesDir);
+ public void testParseMethod2() throws Exception {
+ File yangFile = new File(getClass().getResource("/parse-methods/m1.yang").toURI());
+ File dependenciesDir = new File(getClass().getResource("/parse-methods/dependencies").toURI());
+ YangContextParser parser = new YangParserImpl();
+ modules = parser.parseFile(yangFile, dependenciesDir).getModules();
assertEquals(6, modules.size());
}
+ @Test
+ public void testSorting() throws Exception {
+ // Correct order: m2, m4, m6, m8, m7, m6, m3, m1
+ File yangFile = new File(getClass().getResource("/sorting-test/m1.yang").toURI());
+ File dependenciesDir = new File(getClass().getResource("/sorting-test").toURI());
+ YangContextParser parser = new YangParserImpl();
+ modules = parser.parseFile(yangFile, dependenciesDir).getModules();
+ SchemaContext ctx = new SchemaContextImpl(modules, Collections.<ModuleIdentifier, String>emptyMap());
+ checkOrder(modules);
+ assertSetEquals(modules, ctx.getModules());
+
+ // ##########
+ parser = new YangParserImpl();
+ final File testDir = dependenciesDir;
+ final String[] fileList = testDir.list();
+ final List<File> testFiles = new ArrayList<>();
+ if (fileList == null) {
+ throw new FileNotFoundException(dependenciesDir.getAbsolutePath());
+ }
+ for (String fileName : fileList) {
+ testFiles.add(new File(testDir, fileName));
+ }
+ Set<Module> newModules = parser.parseFiles(testFiles).getModules();
+ assertSetEquals(newModules, modules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+ assertSetEquals(newModules, ctx.getModules());
+ // ##########
+ newModules = parser.parseFiles(testFiles, null).getModules();
+ assertSetEquals(newModules, modules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+ assertSetEquals(newModules, ctx.getModules());
+ // ##########
+ List<InputStream> streams = new ArrayList<>();
+ for (File f : testFiles) {
+ streams.add(new FileInputStream(f));
+ }
+ newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles)).getModules();
+ assertSetEquals(newModules, modules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+ assertSetEquals(newModules, ctx.getModules());
+ // ##########
+ streams.clear();
+ for (File f : testFiles) {
+ streams.add(new FileInputStream(f));
+ }
+ newModules = parser.parseSources(BuilderUtils.filesToByteSources(testFiles), null).getModules();
+ assertSetEquals(newModules, modules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+ assertSetEquals(newModules, ctx.getModules());
+ // ##########
+ Map<File, Module> mapped = parser.parseYangModelsMapped(testFiles);
+ newModules = new LinkedHashSet<>(mapped.values());
+ assertSetEquals(newModules, modules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+ assertSetEquals(newModules, ctx.getModules());
+ // ##########
+ streams.clear();
+ for (File f : testFiles) {
+ streams.add(new FileInputStream(f));
+ }
+ Map<InputStream, Module> mappedStreams = parser.parseYangModelsFromStreamsMapped(streams);
+ newModules = new LinkedHashSet<>(mappedStreams.values());
+ assertSetEquals(newModules, modules);
+ ctx = new SchemaContextImpl(newModules, Collections.<ModuleIdentifier, String>emptyMap());
+ assertSetEquals(newModules, ctx.getModules());
+ }
+
+ private void checkOrder(final Collection<Module> modules) {
+ Iterator<Module> it = modules.iterator();
+ Module m = it.next();
+ assertEquals("m2", m.getName());
+ m = it.next();
+ assertEquals("m4", m.getName());
+ m = it.next();
+ assertEquals("m6", m.getName());
+ m = it.next();
+ assertEquals("m8", m.getName());
+ m = it.next();
+ assertEquals("m7", m.getName());
+ m = it.next();
+ assertEquals("m5", m.getName());
+ m = it.next();
+ assertEquals("m3", m.getName());
+ m = it.next();
+ assertEquals("m1", m.getName());
+ }
+
+ private void assertSetEquals(final Set<Module> s1, final Set<Module> s2) {
+ assertEquals(s1, s2);
+ Iterator<Module> it = s1.iterator();
+ for (Module m : s2) {
+ assertEquals(m, it.next());
+ }
+ }
+
+ @Test
+ public void testSubmodules() throws Exception {
+ URI yangFilePath = getClass().getResource("/submodule-test/subfoo.yang").toURI();
+ URI directoryPath = getClass().getResource("/model").toURI();
+
+ File directory = new File(directoryPath);
+ File yangFile = new File(yangFilePath);
+
+ Set<Module> modules = new YangParserImpl().parseFile(yangFile, directory).getModules();
+ assertEquals(3, modules.size());
+
+ Module foo = TestUtils.findModule(modules, "foo");
+
+ DataSchemaNode id = foo.getDataChildByName("id");
+ assertNotNull(id);
+ DataSchemaNode subExt = foo.getDataChildByName("sub-ext");
+ assertNotNull(subExt);
+ DataSchemaNode subTransfer = foo.getDataChildByName("sub-transfer");
+ assertNotNull(subTransfer);
+
+ assertEquals(2, foo.getExtensionSchemaNodes().size());
+ assertEquals(2, foo.getAugmentations().size());
+ }
+
}