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.impl.YangParserImpl;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
38 final class TestUtils {
40 private static final QName ROOT_QNAME = QName.create("urn:opendaylight.test2", "2015-08-08", "root");
41 private static final QName LIST_A_QNAME = QName.create(ROOT_QNAME, "list-a");
42 private static final QName LIST_B_QNAME = QName.create(ROOT_QNAME, "list-b");
43 private static final QName LEAF_A_QNAME = QName.create(ROOT_QNAME, "leaf-a");
44 private static final QName LEAF_B_QNAME = QName.create(ROOT_QNAME, "leaf-b");
45 private static final QName LEAF_C_QNAME = QName.create(ROOT_QNAME, "leaf-c");
46 private static final QName LEAF_D_QNAME = QName.create(ROOT_QNAME, "leaf-d");
47 private static final QName CONTAINER_A_QNAME = QName.create(ROOT_QNAME, "container-a");
48 private static final QName CONTAINER_B_QNAME = QName.create(ROOT_QNAME, "container-b");
49 private static final String FOO = "foo";
50 private static final String BAR = "bar";
51 private static final String WAZ = "waz";
52 private static final String ONE = "one";
53 private static final String TWO = "two";
54 private static final String THREE = "three";
59 static SchemaContext loadModules(final String resourceDirectory) throws IOException, URISyntaxException,
61 URI path = TestUtils.class.getResource(resourceDirectory).toURI();
62 final File testDir = new File(path);
63 final String[] fileList = testDir.list();
64 final List<File> testFiles = new ArrayList<>();
65 if (fileList == null) {
66 throw new FileNotFoundException(resourceDirectory);
68 for (String fileName : fileList) {
69 if (!new File(testDir, fileName).isDirectory()) {
70 testFiles.add(new File(testDir, fileName));
73 return parseYangSources(testFiles);
76 public static SchemaContext parseYangSources(StatementStreamSource... sources)
77 throws SourceException, ReactorException {
79 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
81 reactor.addSources(sources);
83 return reactor.buildEffective();
86 public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException, FileNotFoundException {
88 StatementStreamSource[] sources = new StatementStreamSource[files.length];
90 for(int i = 0; i<files.length; i++) {
91 sources[i] = new YangStatementSourceImpl(new FileInputStream(files[i]));
94 return parseYangSources(sources);
97 public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException, FileNotFoundException {
98 return parseYangSources(files.toArray(new File[files.size()]));
102 * Returns a test document
122 public static NormalizedNode<?, ?> createNormalizedNodes() {
123 return ImmutableContainerNodeBuilder
125 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(ROOT_QNAME))
126 .withChild(ImmutableNodes.leafNode(LEAF_C_QNAME, WAZ))
127 .withChild(mapNodeBuilder(LIST_A_QNAME)
128 .withChild(mapEntry(LIST_A_QNAME, LEAF_A_QNAME, FOO))
129 .withChild(mapEntryBuilder(LIST_A_QNAME, LEAF_A_QNAME, BAR)
130 .withChild(mapNodeBuilder(LIST_B_QNAME)
131 .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, ONE))
132 .withChild(mapEntry(LIST_B_QNAME, LEAF_B_QNAME, TWO))
136 .withChild(ImmutableContainerNodeBuilder.create()
137 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(CONTAINER_A_QNAME))
138 .withChild(ImmutableContainerNodeBuilder.create().
139 withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier
141 .withChild(ImmutableNodes.leafNode(LEAF_D_QNAME, THREE))