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.restconf.nb.rfc8040;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import javax.xml.parsers.ParserConfigurationException;
22 import javax.xml.stream.XMLStreamException;
23 import javax.xml.transform.dom.DOMSource;
24 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
25 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
26 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
27 import org.opendaylight.yangtools.util.xml.UntrustedXML;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.w3c.dom.Document;
44 import org.xml.sax.SAXException;
46 public class TestRestconfUtils {
48 private static final Logger LOG = LoggerFactory.getLogger(TestRestconfUtils.class);
50 private TestRestconfUtils() {
51 throw new UnsupportedOperationException("Test utility class");
54 @SuppressWarnings("checkstyle:IllegalCatch")
55 public static SchemaContext loadSchemaContext(final String yangPath, final SchemaContext schemaContext) {
57 Preconditions.checkArgument(yangPath != null, "Path can not be null.");
58 Preconditions.checkArgument((!yangPath.isEmpty()), "Path can not be empty.");
59 if (schemaContext == null) {
60 return YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(yangPath));
62 throw new UnsupportedOperationException("Unable to add new yang sources to existing schema context.");
64 } catch (final Exception e) {
65 LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
70 public static NormalizedNodeContext loadNormalizedContextFromJsonFile() {
71 throw new AbstractMethodError("Not implemented yet");
74 @SuppressWarnings("checkstyle:IllegalCatch")
75 public static NormalizedNodeContext loadNormalizedContextFromXmlFile(final String pathToInputFile,
76 final String uri, final SchemaContext schemaContext) {
77 final InstanceIdentifierContext<?> iiContext =
78 ParserIdentifier.toInstanceIdentifier(uri, schemaContext, Optional.absent());
79 final InputStream inputStream = TestRestconfUtils.class.getResourceAsStream(pathToInputFile);
81 final Document doc = UntrustedXML.newDocumentBuilder().parse(inputStream);
82 final NormalizedNode<?, ?> nn = parse(iiContext, doc);
83 return new NormalizedNodeContext(iiContext, nn);
84 } catch (final Exception e) {
85 LOG.error("Load xml file " + pathToInputFile + " fail.", e);
90 private static NormalizedNode<?, ?> parse(final InstanceIdentifierContext<?> iiContext, final Document doc)
91 throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
92 final SchemaNode schemaNodeContext = iiContext.getSchemaNode();
93 DataSchemaNode schemaNode = null;
94 if (schemaNodeContext instanceof RpcDefinition) {
95 if ("input".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
96 schemaNode = ((RpcDefinition) schemaNodeContext).getInput();
97 } else if ("output".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
98 schemaNode = ((RpcDefinition) schemaNodeContext).getOutput();
100 throw new IllegalStateException("Unknown Rpc input node");
103 } else if (schemaNodeContext instanceof DataSchemaNode) {
104 schemaNode = (DataSchemaNode) schemaNodeContext;
106 throw new IllegalStateException("Unknow SchemaNode");
109 final String docRootElm = doc.getDocumentElement().getLocalName();
110 final String schemaNodeName = iiContext.getSchemaNode().getQName().getLocalName();
112 if (!schemaNodeName.equalsIgnoreCase(docRootElm)) {
113 final Collection<DataSchemaNode> children = ((DataNodeContainer) schemaNode).getChildNodes();
114 for (final DataSchemaNode child : children) {
115 if (child.getQName().getLocalName().equalsIgnoreCase(docRootElm)) {
122 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
123 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
124 final XmlParserStream xmlParser = XmlParserStream.create(writer, iiContext.getSchemaContext(), schemaNode);
126 if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode) {
127 xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
128 return resultHolder.getResult();
130 // FIXME : add another DataSchemaNode extensions e.g. LeafSchemaNode
134 public static Collection<File> loadFiles(final String resourceDirectory) throws FileNotFoundException {
135 final String path = TestRestconfUtils.class.getResource(resourceDirectory).getPath();
136 final File testDir = new File(path);
137 final String[] fileList = testDir.list();
138 final List<File> testFiles = new ArrayList<>();
139 if (fileList == null) {
140 throw new FileNotFoundException(resourceDirectory);
142 for (final String fileName : fileList) {
143 if (new File(testDir, fileName).isDirectory() == false) {
144 testFiles.add(new File(testDir, fileName));