Merge "Change fields in ShardStats to non-volatile"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / md / sal / rest / common / TestRestconfUtils.java
index 113bcfab81fd8814966fdc1c110b545bc6d49b3f..2b449ae265410208e235a53bb80c114267e4a5c6 100644 (file)
@@ -12,15 +12,35 @@ import com.google.common.base.Preconditions;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import org.opendaylight.controller.sal.rest.impl.test.providers.TestJsonBodyWriter;
+import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
+import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
+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.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
 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;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 
 /**
  * sal-rest-connector
@@ -36,7 +56,27 @@ public class TestRestconfUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(TestRestconfUtils.class);
 
-    private final static YangContextParser parser = new YangParserImpl();
+    private static final YangContextParser parser = new YangParserImpl();
+
+    private static final DocumentBuilderFactory BUILDERFACTORY;
+
+    static {
+        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        try {
+            factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
+            factory.setXIncludeAware(false);
+            factory.setExpandEntityReferences(false);
+        } catch (final ParserConfigurationException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+        factory.setNamespaceAware(true);
+        factory.setCoalescing(true);
+        factory.setIgnoringElementContentWhitespace(true);
+        factory.setIgnoringComments(true);
+        BUILDERFACTORY = factory;
+    }
 
     private TestRestconfUtils () {
         throw new UnsupportedOperationException("Test utility class");
@@ -58,6 +98,68 @@ public class TestRestconfUtils {
         return schemaContext;
     }
 
+    public static NormalizedNodeContext loadNormalizedContextFromJsonFile() {
+        throw new AbstractMethodError("Not implemented yet");
+    }
+
+    public static NormalizedNodeContext loadNormalizedContextFromXmlFile(final String pathToInputFile, final String uri) {
+        final InstanceIdentifierContext<?> iiContext = ControllerContext.getInstance().toInstanceIdentifier(uri);
+        final InputStream inputStream = TestJsonBodyWriter.class.getResourceAsStream(pathToInputFile);
+        try {
+            final DocumentBuilder dBuilder = BUILDERFACTORY.newDocumentBuilder();
+            final Document doc = dBuilder.parse(inputStream);
+            final NormalizedNode<?, ?> nn = parse(iiContext, doc);
+            return new NormalizedNodeContext(iiContext, nn);
+        }
+        catch (final Exception e) {
+            LOG.error("Load xml file " + pathToInputFile + " fail.", e);
+        }
+        return null;
+    }
+
+    private static NormalizedNode<?, ?> parse(final InstanceIdentifierContext<?> iiContext, final Document doc) {
+        final List<Element> elements = Collections.singletonList(doc.getDocumentElement());
+        final SchemaNode schemaNodeContext = iiContext.getSchemaNode();
+        DataSchemaNode schemaNode = null;
+        if (schemaNodeContext instanceof RpcDefinition) {
+            if ("input".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
+                schemaNode = ((RpcDefinition) schemaNodeContext).getInput();
+            } else if ("output".equalsIgnoreCase(doc.getDocumentElement().getLocalName())) {
+                schemaNode = ((RpcDefinition) schemaNodeContext).getOutput();
+            } else {
+                throw new IllegalStateException("Unknown Rpc input node");
+            }
+
+        } else if (schemaNodeContext instanceof DataSchemaNode) {
+            schemaNode = (DataSchemaNode) schemaNodeContext;
+        } else {
+            throw new IllegalStateException("Unknow SchemaNode");
+        }
+
+        final String docRootElm = doc.getDocumentElement().getLocalName();
+        final String schemaNodeName = iiContext.getSchemaNode().getQName().getLocalName();
+
+        if (!schemaNodeName.equalsIgnoreCase(docRootElm)) {
+            final Collection<DataSchemaNode> children = ((DataNodeContainer) schemaNode).getChildNodes();
+            for (final DataSchemaNode child : children) {
+                if (child.getQName().getLocalName().equalsIgnoreCase(docRootElm)) {
+                    schemaNode = child;
+                    break;
+                }
+            }
+        }
+        final DomToNormalizedNodeParserFactory parserFactory =
+                DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, iiContext.getSchemaContext());
+
+        if(schemaNode instanceof ContainerSchemaNode) {
+            return parserFactory.getContainerNodeParser().parse(Collections.singletonList(doc.getDocumentElement()), (ContainerSchemaNode) schemaNode);
+        } else if(schemaNode instanceof ListSchemaNode) {
+            final ListSchemaNode casted = (ListSchemaNode) schemaNode;
+            return parserFactory.getMapEntryNodeParser().parse(elements, casted);
+        } // FIXME : add another DataSchemaNode extensions e.g. LeafSchemaNode
+        return null;
+    }
+
     private static Collection<File> loadFiles(final String resourceDirectory) throws FileNotFoundException {
         final String path = TestRestconfUtils.class.getResource(resourceDirectory).getPath();
         final File testDir = new File(path);