import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
+import com.google.common.io.ByteSource;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
-import java.util.Set;
+import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
import org.opendaylight.yangtools.util.ClassLoaderUtils;
import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class ModuleInfoBackedContext extends GeneratedClassLoadingStrategy //
- implements //
- ModuleInfoRegistry, SchemaContextProvider {
+public class ModuleInfoBackedContext extends GeneratedClassLoadingStrategy
+ implements ModuleInfoRegistry, SchemaContextProvider {
private ModuleInfoBackedContext(final ClassLoadingStrategy loadingStrategy) {
this.backingLoadingStrategy = loadingStrategy;
private synchronized Optional<SchemaContext> recreateSchemaContext() {
try {
- ImmutableList<InputStream> streams = getAvailableStreams();
+ Collection<ByteSource> streams = getAvailableStreams();
YangParserImpl parser = new YangParserImpl();
- Set<Module> modules = parser.parseYangModelsFromStreams(streams);
- SchemaContext schemaContext = parser.resolveSchemaContext(modules);
+ SchemaContext schemaContext = parser.parseSources(streams);
return Optional.of(schemaContext);
- } catch (IOException e) {
- LOG.error("Schema was not recreated.",e);
+ } catch (IOException | YangSyntaxErrorException e) {
+ LOG.error("Schema was not recreated.", e);
}
return Optional.absent();
}
return recreateSchemaContext();
}
- private ImmutableList<InputStream> getAvailableStreams() throws IOException {
+ private Collection<ByteSource> getAvailableStreams() throws IOException {
ImmutableSet<YangModuleInfo> moduleInfos = ImmutableSet.copyOf(sourceIdentifierToModuleInfo.values());
- ImmutableList.Builder<InputStream> sourceStreams = ImmutableList.<InputStream> builder();
- for (YangModuleInfo moduleInfo : moduleInfos) {
- sourceStreams.add(moduleInfo.getModuleSourceStream());
+ ImmutableList.Builder<ByteSource> sourceStreams = ImmutableList.<ByteSource> builder();
+ for (final YangModuleInfo moduleInfo : moduleInfos) {
+ sourceStreams.add(new ByteSource() {
+
+ @Override
+ public InputStream openStream() throws IOException {
+ return moduleInfo.getModuleSourceStream();
+ }
+ });
+ ;
}
return sourceStreams.build();
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import java.io.InputStream;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
+import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil;
import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
import org.opendaylight.yangtools.sal.binding.model.api.Type;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
private static EmptyTypeDefinition empty = null;
private static BooleanTypeDefinition bool = null;
- @SuppressWarnings("deprecation")
@BeforeClass
- public static void setup() {
- final List<InputStream> modelsToParse = Collections
- .singletonList(BaseYangTypesTest.class.getResourceAsStream("/base-yang-types.yang"));
+ public static void setup() throws IOException, YangSyntaxErrorException {
+ final List<ByteSource> modelsToParse = Collections
+ .singletonList(Resources.asByteSource(BaseYangTypesTest.class.getResource("/base-yang-types.yang")));
final YangContextParser parser = new YangParserImpl();
- final Set<Module> modules = parser.parseYangModelsFromStreams(modelsToParse);
- assertTrue(!modules.isEmpty());
- schemaContext = parser.resolveSchemaContext(modules);
+ schemaContext = parser.parseSources(modelsToParse);
assertNotNull(schemaContext);
initTypeDefinitionsFromSchemaContext();
}
*/
package org.opendaylight.yangtools.sal.binding.yang.types;
-import java.io.InputStream;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import java.util.Set;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
/**
private static final String TEST_TYPE_PROVIDER_PATH = "/"+TEST_TYPE_PROVIDER_MODULE_NAME+".yang";
private static final String TEST_TYPE_PROVIDER_B_PATH = "/test-type-provider-b.yang";
- private static InputStream getInputStream(final String resourceName) {
- return TypeProviderModel.class.getResourceAsStream(resourceName);
+ private static ByteSource getByteSource(final String resourceName) {
+ return Resources.asByteSource(TypeProviderModel.class.getResource(resourceName));
}
- private static List<InputStream> provideTestModelStreams() {
- final List<InputStream> arrayList = new ArrayList<>();
+ private static List<ByteSource> provideTestModelStreams() {
+ final List<ByteSource> arrayList = new ArrayList<>();
- arrayList.add(getInputStream(BASE_YANG_TYPES_PATH));
- arrayList.add(getInputStream(TEST_TYPE_PROVIDER_PATH));
- arrayList.add(getInputStream(TEST_TYPE_PROVIDER_B_PATH));
+ arrayList.add(getByteSource(BASE_YANG_TYPES_PATH));
+ arrayList.add(getByteSource(TEST_TYPE_PROVIDER_PATH));
+ arrayList.add(getByteSource(TEST_TYPE_PROVIDER_B_PATH));
return arrayList;
}
- public static SchemaContext createTestContext() {
- YangParserImpl parser = new YangParserImpl();
- Set<Module> modules = parser.parseYangModelsFromStreams(provideTestModelStreams());
- return parser.resolveSchemaContext(modules);
+ public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
+ return new YangParserImpl().parseSources(provideTestModelStreams());
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-
+import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl;
import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
import org.opendaylight.yangtools.sal.binding.generator.spi.TypeProvider;
-import org.opendaylight.yangtools.sal.binding.model.api.*;
+import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType;
+import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.yangtools.sal.binding.model.api.ParameterizedType;
+import org.opendaylight.yangtools.sal.binding.model.api.Restrictions;
+import org.opendaylight.yangtools.sal.binding.model.api.Type;
import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
-import org.opendaylight.yangtools.yang.model.api.*;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
+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.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
/**
* Test suite for testing public methods in TypeProviderImpl class
*
* @see org.opendaylight.yangtools.sal.binding.yang.types.TypeProviderImpl
*
- * @author Lukas Sedlak <lsedlak@cisco.com>
+ * @author Lukas Sedlak <lsedlak@cisco.com>
*/
@RunWith(JUnit4.class)
public class TypeProviderTest {
private SchemaNode schemaNode;
@Before
- public void setUp() {
+ public void setUp() throws IOException, YangSyntaxErrorException {
MockitoAnnotations.initMocks(this);
schemaContext = TypeProviderModel.createTestContext();
assertNotNull(schemaContext);
assertTrue(leafrefResolvedType2 instanceof ParameterizedType);
}
- private void setReferencedTypeForTypeProvider(TypeProvider provider) {
+ private void setReferencedTypeForTypeProvider(final TypeProvider provider) {
final LeafSchemaNode enumLeafNode = provideLeafNodeFromTopLevelContainer(testTypeProviderModule, "foo",
"resolve-direct-use-of-enum");
final TypeDefinition<?> enumLeafTypedef = enumLeafNode.getType();
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
-import java.util.Set;
-
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
@Test
public void testListGeneration() throws Exception {
final List<File> sourceFiles = getSourceFiles("/doc-gen");
- final Set<Module> modulesToBuild = parser.parseYangModels(sourceFiles);
- final SchemaContext context = parser.resolveSchemaContext(modulesToBuild);
+ final SchemaContext context = parser.parseFiles(sourceFiles);
final BasicCodeGenerator generator = new DocumentationGeneratorImpl();
- Collection<File> generatedFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modulesToBuild);
+ Collection<File> generatedFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, context.getModules());
assertEquals(4, generatedFiles.size());
}
- private static List<File> getSourceFiles(String path) throws Exception {
+ private static List<File> getSourceFiles(final String path) throws Exception {
final URI resPath = DocGenTest.class.getResource(path).toURI();
final File sourcesDir = new File(resPath);
if (sourcesDir.exists()) {
}
}
- private static void deleteTestDir(File file) {
+ private static void deleteTestDir(final File file) {
if (file.isDirectory()) {
File[] filesToDelete = file.listFiles();
if (filesToDelete != null) {
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;
cleanUp(sourcesOutputDir, compiledOutputDir);
}
- private void generateTestSources(String resourceDirPath, File sourcesOutputDir) throws Exception {
+ private static void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
final List<File> sourceFiles = getSourceFiles(resourceDirPath);
YangContextParser parser = new YangParserImpl();
final SchemaContext context = parser.parseFiles(sourceFiles);
codegen.generateSources(context, sourcesOutputDir, context.getModules());
}
- private static void testCompilation(File sourcesOutputDir, File compiledOutputDir) {
+ private static void testCompilation(final File sourcesOutputDir, final File compiledOutputDir) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
List<File> filesList = getJavaFiles(sourcesOutputDir);
assertTrue(compiled);
}
- private static List<File> getJavaFiles(File directory) {
+ private static List<File> getJavaFiles(final File directory) {
List<File> result = new ArrayList<>();
File[] filesToRead = directory.listFiles();
if (filesToRead != null) {
return result;
}
- private static List<File> getSourceFiles(String path) throws Exception {
+ private static List<File> getSourceFiles(final String path) throws Exception {
final URI resPath = YangModuleInfoCompilationTest.class.getResource(path).toURI();
final File sourcesDir = new File(resPath);
final URI currentDir = new File(System.getProperty("user.dir")).toURI();
}
}
- private static void deleteTestDir(File file) {
+ private static void deleteTestDir(final File file) {
if (file.isDirectory()) {
File[] filesToDelete = file.listFiles();
if (filesToDelete != null) {
}
}
- private static Method assertContainsMethod(Class<?> clazz, Class<?> returnType, String name, Class<?>... args) {
+ private static Method assertContainsMethod(final Class<?> clazz, final Class<?> returnType, final String name, final Class<?>... args) {
try {
Method m = clazz.getDeclaredMethod(name, args);
assertEquals(returnType, m.getReturnType());
}
}
- private static void cleanUp(File... resourceDirs) {
+ private static void cleanUp(final File... resourceDirs) {
for (File resourceDir : resourceDirs) {
if (resourceDir.exists()) {
deleteTestDir(resourceDir);
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
-
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
-import java.util.Set;
-
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
@Test
public void testListGeneration() throws Exception {
final List<File> sourceFiles = getSourceFiles("/wadl-gen");
- final Set<Module> modulesToBuild = parser.parseYangModels(sourceFiles);
- final SchemaContext context = parser.resolveSchemaContext(modulesToBuild);
+ final SchemaContext context = parser.parseFiles(sourceFiles);
final BasicCodeGenerator generator = new WadlGenerator();
- Collection<File> generatedWadlFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, modulesToBuild);
+ Collection<File> generatedWadlFiles = generator.generateSources(context, GENERATOR_OUTPUT_DIR, context.getModules());
assertEquals(3, generatedWadlFiles.size());
}
- private static List<File> getSourceFiles(String path) throws Exception {
+ private static List<File> getSourceFiles(final String path) throws Exception {
final URI resPath = WadlGenTest.class.getResource(path).toURI();
final File sourcesDir = new File(resPath);
if (sourcesDir.exists()) {
}
}
- private static void deleteTestDir(File file) {
+ private static void deleteTestDir(final File file) {
if (file.isDirectory()) {
File[] filesToDelete = file.listFiles();
if (filesToDelete != null) {
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
-import java.io.InputStream;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
+import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.List;
-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.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
public class NormalizedDataBuilderTest {
private ContainerSchemaNode containerNode;
private SchemaContext schema;
- SchemaContext parseTestSchema(final String... yangPath) {
+ SchemaContext parseTestSchema(final String... yangPath) throws IOException, YangSyntaxErrorException {
YangParserImpl yangParserImpl = new YangParserImpl();
- Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath));
- return yangParserImpl.resolveSchemaContext(modules);
+ return yangParserImpl.parseSources(getTestYangs(yangPath));
}
- List<InputStream> getTestYangs(final String... yangPaths) {
+ List<ByteSource> getTestYangs(final String... yangPaths) {
return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
- new Function<String, InputStream>() {
+ new Function<String, ByteSource>() {
@Override
- public InputStream apply(final String input) {
- InputStream resourceAsStream = getClass().getResourceAsStream(input);
+ public ByteSource apply(final String input) {
+ ByteSource resourceAsStream = Resources.asByteSource(getClass().getResource(input));
Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream);
return resourceAsStream;
}
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
return new YangInstanceIdentifier.AugmentationIdentifier(qn);
}
- public NormalizedNodeXmlTranslationTest(final String yangPath, final String xmlPath, final ContainerNode expectedNode) {
+ public NormalizedNodeXmlTranslationTest(final String yangPath, final String xmlPath, final ContainerNode expectedNode) throws IOException, YangSyntaxErrorException {
schema = parseTestSchema(yangPath);
this.xmlPath = xmlPath;
this.containerNode = (ContainerSchemaNode) NormalizedDataBuilderTest.getSchemaNode(schema, "test", "container");
private final String xmlPath;
- SchemaContext parseTestSchema(final String... yangPath) {
+ SchemaContext parseTestSchema(final String... yangPath) throws IOException, YangSyntaxErrorException {
final YangParserImpl yangParserImpl = new YangParserImpl();
- final Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(getTestYangs(yangPath));
- return yangParserImpl.resolveSchemaContext(modules);
+ return yangParserImpl.parseSources(getTestYangs(yangPath));
}
- List<InputStream> getTestYangs(final String... yangPaths) {
+ List<ByteSource> getTestYangs(final String... yangPaths) {
return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
- new Function<String, InputStream>() {
+ new Function<String, ByteSource>() {
@Override
- public InputStream apply(final String input) {
- final InputStream resourceAsStream = NormalizedDataBuilderTest.class.getResourceAsStream(input);
- Preconditions.checkNotNull(resourceAsStream, "File %s was null", resourceAsStream);
- return resourceAsStream;
+ public ByteSource apply(final String input) {
+ final ByteSource source = Resources.asByteSource(
+ NormalizedDataBuilderTest.class.getResource(input));
+ Preconditions.checkNotNull(source, "File %s was null", source);
+ return source;
}
}));
}
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.google.common.base.Optional;
-import java.io.InputStream;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
+import java.io.IOException;
import java.util.Collections;
-import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
public class Bug2690FixTest {
private InMemoryDataTree inMemoryDataTree;
@Before
- public void prepare() {
+ public void prepare() throws IOException, YangSyntaxErrorException {
schemaContext = createTestContext();
assertNotNull("Schema context must not be null.", schemaContext);
rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext, TreeType.OPERATIONAL));
inMemoryDataTree.setSchemaContext(schemaContext);
}
- public static final InputStream getDatastoreTestInputStream() {
- return getInputStream(ODL_DATASTORE_TEST_YANG);
+ private static ByteSource getInputStream() {
+ return Resources.asByteSource(TestModel.class.getResource(ODL_DATASTORE_TEST_YANG));
}
- private static InputStream getInputStream(final String resourceName) {
- return TestModel.class.getResourceAsStream(ODL_DATASTORE_TEST_YANG);
- }
-
- public static SchemaContext createTestContext() {
+ public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
final YangParserImpl parser = new YangParserImpl();
- final Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(getDatastoreTestInputStream()));
- return parser.resolveSchemaContext(modules);
+ return parser.parseSources(Collections.singletonList(getInputStream()));
}
@Test
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.google.common.base.Optional;
-import java.io.InputStream;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
-import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.node(UNKEYED_LIST_QNAME).build();
@Before
- public void prepare() {
+ public void prepare() throws IOException, YangSyntaxErrorException {
schemaContext = createTestContext();
assertNotNull("Schema context must not be null.", schemaContext);
rootOper = RootModificationApplyOperation.from(SchemaAwareApplyOperation.from(schemaContext, TreeType.OPERATIONAL));
}
- public static final InputStream getDatastoreTestInputStream() {
- return getInputStream(CONSTRAINTS_VALIDATION_TEST_YANG);
+ private static ByteSource getInputStream() {
+ return Resources.asByteSource(TestModel.class.getResource(CONSTRAINTS_VALIDATION_TEST_YANG));
}
- private static InputStream getInputStream(final String resourceName) {
- return TestModel.class.getResourceAsStream(CONSTRAINTS_VALIDATION_TEST_YANG);
- }
-
- public static SchemaContext createTestContext() {
+ public static SchemaContext createTestContext() throws IOException, YangSyntaxErrorException {
final YangParserImpl parser = new YangParserImpl();
- final Set<Module> modules = parser.parseYangModelsFromStreams(Collections.singletonList(getDatastoreTestInputStream()));
- return parser.resolveSchemaContext(modules);
+ return parser.parseSources(Collections.singletonList(getInputStream()));
}
@Test
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;
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 static void checkOrder(final Collection<Module> modules) {
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.Lists;
+import com.google.common.io.ByteSource;
+import com.google.common.io.Resources;
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.URI;
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.YangContextParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.util.ExtendedType;
public class YangParserWithContextTest {
private final DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
private final YangParserImpl parser = new YangParserImpl();
+ private final SchemaContext createContext(final String... resources) throws IOException, YangSyntaxErrorException {
+ final List<ByteSource> srcs = new ArrayList<>(resources.length);
+ for (String resource : resources) {
+ srcs.add(Resources.asByteSource(getClass().getResource(resource)));
+ }
+
+ return parser.parseSources(srcs);
+ }
+
@Test
public void testTypeFromContext() throws Exception {
- String resource = "/ietf/ietf-inet-types@2010-09-24.yang";
- InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()));
- SchemaContext context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
- stream.close();
+ SchemaContext context = createContext("/ietf/ietf-inet-types@2010-09-24.yang");
- resource = "/context-test/test1.yang";
- InputStream stream2 = new FileInputStream(new File(getClass().getResource(resource).toURI()));
+ InputStream stream2 = new FileInputStream(new File(getClass().getResource("/context-test/test1.yang").toURI()));
Module module = TestUtils.loadModuleWithContext("test1", stream2, context);
stream2.close();
assertNotNull(module);
@Test
public void testUsesFromContext() throws Exception {
- SchemaContext context;
- try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
- InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
- InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()));
- InputStream stream4 = new FileInputStream(
- new File(getClass().getResource("/model/subfoo.yang").toURI()))) {
- context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3,
- stream4)));
- }
+ SchemaContext context = createContext("/model/baz.yang", "/model/bar.yang", "/model/foo.yang", "/model/subfoo.yang");
Module testModule;
try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
.toURI()))) {
@Test
public void testUsesRefineFromContext() throws Exception {
- SchemaContext context;
- try (InputStream stream1 = new FileInputStream(new File(getClass().getResource("/model/baz.yang").toURI()));
- InputStream stream2 = new FileInputStream(new File(getClass().getResource("/model/bar.yang").toURI()));
- InputStream stream3 = new FileInputStream(new File(getClass().getResource("/model/foo.yang").toURI()));
- InputStream stream4 = new FileInputStream(
- new File(getClass().getResource("/model/subfoo.yang").toURI()))) {
- context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream1, stream2, stream3,
- stream4)));
- }
+ SchemaContext context = createContext("/model/baz.yang", "/model/bar.yang", "/model/foo.yang", "/model/subfoo.yang");
+
Module module;
try (InputStream stream = new FileInputStream(new File(getClass().getResource("/context-test/test2.yang")
.toURI()))) {
@Test
public void testDeviation() throws Exception {
// load first module
- SchemaContext context;
- String resource = "/model/bar.yang";
-
- try (InputStream stream = new FileInputStream(new File(getClass().getResource(resource).toURI()))) {
- context = parser.resolveSchemaContext(TestUtils.loadModules(Lists.newArrayList(stream)));
- }
+ SchemaContext context = parser.parseFiles(Collections.singleton(
+ new File(getClass().getResource("/model/bar.yang").toURI())));
// load another modules and parse them against already existing context
Set<Module> modules;