import org.opendaylight.yangtools.yang.data.api.CompositeNode;
@Provider
-@Consumes({API+RestconfService.XML})
+@Consumes({ API + RestconfService.XML })
public enum XmlToCompositeNodeProvider implements MessageBodyReader<CompositeNode> {
INSTANCE;
-
+
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return true;
try {
return xmlReader.read(entityStream);
} catch (XMLStreamException | UnsupportedFormatException e) {
- throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
- .entity(e.getMessage()).build());
+ throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage())
+ .build());
}
}
--- /dev/null
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import java.util.concurrent.*;
+
+import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+
+public class DummyFuture implements Future<RpcResult<TransactionStatus>> {
+
+ @Override
+ public boolean cancel(boolean mayInterruptIfRunning) {
+ return false;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return false;
+ }
+
+ @Override
+ public boolean isDone() {
+ return false;
+ }
+
+ @Override
+ public RpcResult<TransactionStatus> get() throws InterruptedException, ExecutionException {
+ return null;
+ }
+
+ @Override
+ public RpcResult<TransactionStatus> get(long timeout, TimeUnit unit) throws InterruptedException,
+ ExecutionException, TimeoutException {
+ return null;
+ }
+}
\ No newline at end of file
import javax.ws.rs.WebApplicationException;
-import org.junit.Test;
+import org.junit.*;
import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
import org.opendaylight.controller.sal.restconf.impl.*;
import org.opendaylight.yangtools.yang.data.api.*;
public class FromJsonToCompositeNode {
- /**
- *
- * It is just dummy class which is used in mock method to specify return
- * type
- */
- private class DummyFuture implements Future<RpcResult<TransactionStatus>> {
-
- @Override
- public boolean cancel(boolean mayInterruptIfRunning) {
- return false;
- }
-
- @Override
- public boolean isCancelled() {
- return false;
- }
-
- @Override
- public boolean isDone() {
- return false;
- }
-
- @Override
- public RpcResult<TransactionStatus> get() throws InterruptedException, ExecutionException {
- return null;
- }
-
- @Override
- public RpcResult<TransactionStatus> get(long timeout, TimeUnit unit) throws InterruptedException,
- ExecutionException, TimeoutException {
- return null;
- }
- };
-
- Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNode.class);
+ private static Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNode.class);
@Test
public void simpleListTest() {
@Test
public void simpleContainerTest() {
- simpleTest("/json-to-composite-node/simple-container.json", "/json-to-composite-node/simple-list-yang", "cont",
- "simple:data:types");
+ simpleTest("/json-to-composite-node/simple-container.json", "/json-to-composite-node/simple-container-yang",
+ "cont", "simple:data:types");
}
/**
CompositeNode compositeNode = compositeContainerFromJson(jsonPath);
assertNotNull(compositeNode);
- DataSchemaNode dataSchemaNode = obtainSchemaFromYang(yangPath);
- assertNotNull(dataSchemaNode);
-
- supplementNamespace(dataSchemaNode, compositeNode);
-
- assertTrue(compositeNode instanceof CompositeNodeWrapper);
- CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
-
- assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
- verifyCompositeNode(compNode, namespace);
- }
-
- private DataSchemaNode obtainSchemaFromYang(String yangFolder) {
- Set<Module> modules = null;
+ DataSchemaNode dataSchemaNode = null;
try {
- modules = TestUtils.loadModules(ToJsonBasicDataTypesTest.class.getResource(yangFolder).getPath());
+ dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath);
} catch (FileNotFoundException e) {
LOG.error(e.getMessage());
assertTrue(false);
}
+ assertNotNull(dataSchemaNode);
- assertNotNull(modules);
- assertEquals(1, modules.size());
- Module module = modules.iterator().next();
- assertNotNull(module.getChildNodes());
- assertEquals(1, module.getChildNodes().size());
- DataSchemaNode dataSchemaNode = module.getChildNodes().iterator().next();
- return dataSchemaNode;
- }
-
- private void supplementNamespace(DataSchemaNode dataSchemaNode, CompositeNode compositeNode) {
- RestconfImpl restconf = RestconfImpl.getInstance();
-
- InstanceIdWithSchemaNode instIdAndSchema = new InstanceIdWithSchemaNode(mock(InstanceIdentifier.class),
- dataSchemaNode);
-
- ControllerContext controllerContext = mock(ControllerContext.class);
- BrokerFacade broker = mock(BrokerFacade.class);
-
- when(controllerContext.toInstanceIdentifier(any(String.class))).thenReturn(instIdAndSchema);
- when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(
- new DummyFuture());
+ TestUtils.supplementNamespace(dataSchemaNode, compositeNode);
- restconf.setControllerContext(controllerContext);
- restconf.setBroker(broker);
+ assertTrue(compositeNode instanceof CompositeNodeWrapper);
+ CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(null);
- // method is called only because it contains call of method which
- // supplement namespaces to compositeNode
- restconf.createConfigurationData("something", compositeNode);
+ assertEquals(topLevelElementName, compNode.getNodeType().getLocalName());
+ verifyCompositeNode(compNode, namespace);
}
private void verityMultipleItemsInList(CompositeNode compositeNode) {
assertTrue(compositeNode instanceof CompositeNodeWrapper);
if (dummyNamespaces) {
try {
- addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
+ TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
return ((CompositeNodeWrapper) compositeNode).unwrap(null);
} catch (URISyntaxException e) {
LOG.error(e.getMessage());
return null;
}
- private void addDummyNamespaceToAllNodes(NodeWrapper<?> wrappedNode) throws URISyntaxException {
- wrappedNode.setNamespace(new URI(""));
- if (wrappedNode instanceof CompositeNodeWrapper) {
- for (NodeWrapper<?> childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) {
- addDummyNamespaceToAllNodes(childNodeWrapper);
- }
- }
- }
}
import static org.junit.Assert.assertTrue;
import java.io.*;
+import java.net.URISyntaxException;
import javax.ws.rs.WebApplicationException;
-import org.junit.Test;
+import org.junit.*;
import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
+import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
import org.opendaylight.yangtools.yang.data.api.*;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.slf4j.*;
public class FromXmlToCompositeNode {
- Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNode.class);
+ private static Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNode.class);
/**
* top level element represents container. second level element is list with
*/
@Test
public void testXmlDataContainer() {
- CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-container.xml");
- verifyCommonPartAOfXml(compNode, "");
+ CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-container.xml", false);
+ assertNotNull(compNode);
+ DataSchemaNode dataSchemaNode = null;
+ try {
+ dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-container-yang");
+ } catch (FileNotFoundException e) {
+ LOG.error(e.getMessage());
+ assertTrue(false);
+ }
+
+ assertNotNull(dataSchemaNode);
+ TestUtils.supplementNamespace(dataSchemaNode, compNode);
+
+ String nameSpace = "data:container:yang";
+ assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString());
+
+ verifyCommonPartAOfXml(compNode, "", nameSpace);
}
@Test
public void testXmlDataList() {
- CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml");
+ CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml", false);
assertNotNull(compNode);
+
+ DataSchemaNode dataSchemaNode = null;
+ try {
+ dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-list-yang",
+ "data-container-yang");
+ } catch (FileNotFoundException e) {
+ LOG.error(e.getMessage());
+ }
+ assertNotNull(dataSchemaNode);
+ TestUtils.supplementNamespace(dataSchemaNode, compNode);
+
+ String nameSpaceList = "data:list:yang";
+ String nameSpaceCont = "data:container:yang";
+ assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
assertEquals("cont", compNode.getNodeType().getLocalName());
- assertEquals(2, compNode.getChildren().size());
+ assertEquals(3, compNode.getChildren().size());
CompositeNode lst1_1 = null;
CompositeNode lst1_2 = null;
int loopCount = 0;
for (Node<?> node : compNode.getChildren()) {
- assertTrue(node instanceof CompositeNode);
- switch (loopCount++) {
- case 0:
- lst1_1 = (CompositeNode) node;
- break;
- case 1:
- lst1_2 = (CompositeNode) node;
- break;
+ if (node.getNodeType().getLocalName().equals("lf1")) {
+ assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString());
+ assertTrue(node instanceof SimpleNode<?>);
+ assertEquals("lf1", node.getValue());
+ } else {
+ assertTrue(node instanceof CompositeNode);
+ switch (loopCount++) {
+ case 0:
+ lst1_1 = (CompositeNode) node;
+ break;
+ case 1:
+ lst1_2 = (CompositeNode) node;
+ break;
+ }
+ assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString());
}
-
}
// lst1_1
- verifyCommonPartAOfXml(lst1_1, "1");
+ verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont);
// :lst1_1
// lst1_2
assertTrue(node instanceof CompositeNode);
cont11 = (CompositeNode) node;
}
+ assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString());
}
assertEquals("221", lflst11.getValue());
assertEquals(1, cont11.getChildren().size());
assertTrue(cont11.getChildren().get(0) instanceof SimpleNode<?>);
SimpleNode<?> cont11_lf111 = (SimpleNode<?>) cont11.getChildren().get(0);
+ assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString());
assertEquals("lf111", cont11_lf111.getNodeType().getLocalName());
assertEquals("100", cont11_lf111.getValue());
// :lst1_2
@Test
public void testXmlEmptyData() {
- CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/empty-data.xml");
+ CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/empty-data.xml", true);
assertEquals("cont", compNode.getNodeType().getLocalName());
SimpleNode<?> lf1 = null;
SimpleNode<?> lflst1_1 = null;
}
- private void verifyCommonPartAOfXml(CompositeNode compNode, String suf) {
+ private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) {
SimpleNode<?> lf1suf = null;
SimpleNode<?> lflst1suf_1 = null;
SimpleNode<?> lflst1suf_2 = null;
assertTrue(node instanceof CompositeNode);
cont1suf = (CompositeNode) node;
}
+ assertEquals(nameSpace, node.getNodeType().getNamespace().toString());
}
+
assertNotNull(lf1suf);
assertNotNull(lflst1suf_1);
assertNotNull(lflst1suf_2);
assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode<?>);
SimpleNode<?> lst11_lf11 = (SimpleNode<?>) lst1suf.getChildren().get(0);
+ assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString());
assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName());
assertEquals("str2", lst11_lf11.getValue());
assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode<?>);
SimpleNode<?> cont1_lf11 = (SimpleNode<?>) cont1suf.getChildren().get(0);
+ assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString());
assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName());
assertEquals("100", cont1_lf11.getValue());
}
- private CompositeNode compositeContainerFromXml(String xmlPath) {
+ private CompositeNode compositeContainerFromXml(String xmlPath, boolean dummyNamespaces) {
XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE;
try {
InputStream xmlStream = FromXmlToCompositeNode.class.getResourceAsStream(xmlPath);
- return xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
+ CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream);
+ if (dummyNamespaces) {
+ try {
+ TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode);
+ return ((CompositeNodeWrapper) compositeNode).unwrap(null);
+ } catch (URISyntaxException e) {
+ LOG.error(e.getMessage());
+ assertTrue(e.getMessage(), false);
+ }
+ }
+ return compositeNode;
+
} catch (WebApplicationException | IOException e) {
LOG.error(e.getMessage());
assertTrue(false);
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import java.io.*;
import java.net.*;
import java.sql.Date;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
import javax.ws.rs.WebApplicationException;
import javax.xml.stream.XMLStreamException;
-import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
-import org.opendaylight.controller.sal.rest.impl.*;
-import org.opendaylight.controller.sal.restconf.impl.StructuredData;
+import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
+import org.opendaylight.controller.sal.restconf.impl.*;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.*;
-import org.opendaylight.yangtools.yang.data.impl.*;
+import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
import org.opendaylight.yangtools.yang.model.api.*;
import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.slf4j.*;
import org.w3c.dom.Document;
final class TestUtils {
static QName buildQName(String name) {
return buildQName(name, "", null);
}
+
+ static void supplementNamespace(DataSchemaNode dataSchemaNode, CompositeNode compositeNode) {
+ RestconfImpl restconf = RestconfImpl.getInstance();
+
+ InstanceIdWithSchemaNode instIdAndSchema = new InstanceIdWithSchemaNode(mock(InstanceIdentifier.class),
+ dataSchemaNode);
+
+ ControllerContext controllerContext = mock(ControllerContext.class);
+ BrokerFacade broker = mock(BrokerFacade.class);
+
+ when(controllerContext.toInstanceIdentifier(any(String.class))).thenReturn(instIdAndSchema);
+ when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(
+ new DummyFuture());
+
+ restconf.setControllerContext(controllerContext);
+ restconf.setBroker(broker);
+
+ // method is called only because it contains call of method which
+ // supplement namespaces to compositeNode
+ restconf.createConfigurationData("something", compositeNode);
+ }
+
+ static DataSchemaNode obtainSchemaFromYang(String yangFolder) throws FileNotFoundException {
+ return obtainSchemaFromYang(yangFolder, null);
+ }
+
+ static DataSchemaNode obtainSchemaFromYang(String yangFolder, String moduleName) throws FileNotFoundException {
+ Set<Module> modules = null;
+ modules = TestUtils.loadModules(ToJsonBasicDataTypesTest.class.getResource(yangFolder).getPath());
+
+ if (modules == null) {
+ return null;
+ }
+ if (modules.size() < 1) {
+ return null;
+ }
+
+ Module moduleRes = null;
+ if (modules.size() > 1) {
+ if (moduleName == null) {
+ return null;
+ } else {
+ for (Module module: modules) {
+ if (module.getName().equals(moduleName)) {
+ moduleRes = module;
+ }
+ }
+ if (moduleRes == null) {
+ return null;
+ }
+ }
+ } else {
+ moduleRes = modules.iterator().next();
+ }
+
+ if (moduleRes.getChildNodes() == null) {
+ return null;
+ }
+
+ if (moduleRes.getChildNodes().size() != 1) {
+ return null;
+ }
+ DataSchemaNode dataSchemaNode = moduleRes.getChildNodes().iterator().next();
+ return dataSchemaNode;
+
+ }
+
+ static void addDummyNamespaceToAllNodes(NodeWrapper<?> wrappedNode) throws URISyntaxException {
+ wrappedNode.setNamespace(new URI(""));
+ if (wrappedNode instanceof CompositeNodeWrapper) {
+ for (NodeWrapper<?> childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) {
+ addDummyNamespaceToAllNodes(childNodeWrapper);
+ }
+ }
+ }
+
}
--- /dev/null
+module data-container-yang {
+ namespace "data:container:yang";
+
+ prefix "dtconyg";
+ revision 2013-11-19 {
+ }
+
+ container cont {
+ leaf lf1 {
+ type string;
+ }
+ leaf-list lflst1 {
+ type string;
+ }
+ list lst1 {
+ leaf lf11 {
+ type string;
+ }
+ }
+ container cont1 {
+ leaf lf11 {
+ type uint8;
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+module data-container-yang {
+ namespace "data:container:yang";
+
+ prefix "dtconyg";
+ revision 2013-11-19 {
+ }
+
+ container cont {
+ list lst1 {
+ leaf lf11 {
+ type string;
+ }
+ leaf-list lflst11 {
+ type string;
+ }
+ list lst11 {
+ leaf lf111 {
+ type string;
+ }
+ }
+ container cont11 {
+ leaf lf111 {
+ type uint8;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+module data-list-yang {
+ namespace "data:list:yang";
+
+ prefix "dtlstyg";
+
+ import data-container-yang {
+ prefix "dtconyg";
+ revision-date 2013-11-19;
+ }
+
+
+ revision 2013-11-19 {
+ }
+
+
+
+ augment "/dtconyg:cont" {
+ leaf lf1 {
+ type string;
+ }
+ }
+}
<lf111>100</lf111>
</cont11>
</lst1>
+ <lf1>lf1</lf1>
</cont>