2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.yangtools.yang.data.jaxen;
11 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntry;
12 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapEntryBuilder;
13 import static org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes.mapNodeBuilder;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
37 final class TestUtils {
39 private static final QName ROOT_QNAME = QName.create("urn:opendaylight.test2", "2015-08-08", "root");
40 private static final QName LIST_A_QNAME = QName.create(ROOT_QNAME, "list-a");
41 private static final QName LIST_B_QNAME = QName.create(ROOT_QNAME, "list-b");
42 private static final QName LEAF_A_QNAME = QName.create(ROOT_QNAME, "leaf-a");
43 private static final QName LEAF_B_QNAME = QName.create(ROOT_QNAME, "leaf-b");
44 private static final QName LEAF_C_QNAME = QName.create(ROOT_QNAME, "leaf-c");
45 private static final QName LEAF_D_QNAME = QName.create(ROOT_QNAME, "leaf-d");
46 private static final QName CONTAINER_A_QNAME = QName.create(ROOT_QNAME, "container-a");
47 private static final QName CONTAINER_B_QNAME = QName.create(ROOT_QNAME, "container-b");
48 private static final String FOO = "foo";
49 private static final String BAR = "bar";
50 private static final String WAZ = "waz";
51 private static final String ONE = "one";
52 private static final String TWO = "two";
53 private static final String THREE = "three";
58 static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
60 URI path = TestUtils.class.getResource(resourceDirectory).toURI();
61 final File testDir = new File(path);
62 final String[] fileList = testDir.list();
63 final List<File> testFiles = new ArrayList<>();
64 if (fileList == null) {
65 throw new FileNotFoundException(resourceDirectory);
67 for (String fileName : fileList) {
68 if (!new File(testDir, fileName).isDirectory()) {
69 testFiles.add(new File(testDir, fileName));
72 return parseYangSources(testFiles);
75 public static SchemaContext parseYangSources(StatementStreamSource... sources)
76 throws SourceException, ReactorException {
78 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
80 reactor.addSources(sources);
82 return reactor.buildEffective();
85 public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException, FileNotFoundException {
87 StatementStreamSource[] sources = new StatementStreamSource[files.length];
89 for (int i = 0; i<files.length; i++) {
90 sources[i] = new YangStatementSourceImpl(new FileInputStream(files[i]));
93 return parseYangSources(sources);
96 public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException, FileNotFoundException {
97 return parseYangSources(files.toArray(new File[files.size()]));
101 * Returns a test document
121 public static NormalizedNode<?, ?> createNormalizedNodes() {
122 return ImmutableContainerNodeBuilder
124 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ROOT_QNAME))
125 .withChild(ImmutableNodes.leafNode(LEAF_C_QNAME, WAZ))
126 .withChild(mapNodeBuilder(LIST_A_QNAME)
127 .withChild(mapEntry(LIST_A_QNAME, LEAF_A_QNAME, FOO))
128 .withChild(mapEntryBuilder(LIST_A_QNAME, LEAF_A_QNAME, BAR)
129 .withChild(mapNodeBuilder(LIST_B_QNAME)
130 .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, ONE))
131 .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, TWO))
135 .withChild(ImmutableContainerNodeBuilder.create()
136 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONTAINER_A_QNAME))
137 .withChild(ImmutableContainerNodeBuilder.create().
138 withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier
140 .withChild(ImmutableNodes.leafNode(LEAF_D_QNAME, THREE))