import javax.activation.UnsupportedDataTypeException;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.Node;
-import org.opendaylight.yangtools.yang.data.api.SimpleNode;
+import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
+import org.opendaylight.yangtools.yang.data.api.*;
import org.opendaylight.yangtools.yang.model.api.*;
import org.opendaylight.yangtools.yang.model.api.type.*;
}
private void writeContainer(JsonWriter writer, CompositeNode node, ContainerSchemaNode schema) throws IOException {
- writer.name(node.getNodeType().getLocalName());
+ writeName(node, schema, writer);
writer.beginObject();
writeChildrenOfParent(writer, node, schema);
writer.endObject();
}
- private void writeList(JsonWriter writer, CompositeNode nodeParent, CompositeNode node, ListSchemaNode schema) throws IOException {
- writer.name(node.getNodeType().getLocalName());
+ private void writeList(JsonWriter writer, CompositeNode nodeParent, CompositeNode node, ListSchemaNode schema)
+ throws IOException {
+ writeName(node, schema, writer);
writer.beginArray();
if (nodeParent != null) {
writer.endArray();
}
- private void writeLeafList(JsonWriter writer, CompositeNode nodeParent, SimpleNode<?> node, LeafListSchemaNode schema) throws IOException {
- writer.name(node.getNodeType().getLocalName());
+ private void writeLeafList(JsonWriter writer, CompositeNode nodeParent, SimpleNode<?> node,
+ LeafListSchemaNode schema) throws IOException {
+ writeName(node, schema, writer);
writer.beginArray();
List<SimpleNode<?>> nodeLeafLists = nodeParent.getSimpleNodesByName(node.getNodeType());
}
private void writeLeaf(JsonWriter writer, SimpleNode<?> node, LeafSchemaNode schema) throws IOException {
- writer.name(node.getNodeType().getLocalName());
+ writeName(node, schema, writer);
writeValueOfNodeByType(writer, node, schema.getType());
}
return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type;
}
+ private void writeName(Node<?> node, DataSchemaNode schema, JsonWriter writer) throws IOException {
+ String nameForOutput = node.getNodeType().getLocalName();
+ if (schema.isAugmenting()) {
+ ControllerContext contContext = ControllerContext.getInstance();
+ CharSequence moduleName;
+ moduleName = contContext.toRestconfIdentifier(schema.getQName());
+ if (moduleName != null) {
+ nameForOutput = moduleName.toString();
+ }
+ }
+ writer.name(nameForOutput);
+ }
+
private static final class NumberForJsonWriter extends Number {
private static final long serialVersionUID = -3147729419814417666L;
import java.util.concurrent.Future;
import javax.ws.rs.WebApplicationException;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.*;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
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.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.*;
import org.opendaylight.yangtools.yang.data.api.*;
import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder;
import org.opendaylight.yangtools.yang.model.api.*;
}
return (CompositeNode) dataTree;
}
-
+
public static Document loadDocumentFrom(InputStream inputStream) {
try {
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
}
static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, String outputPath) {
+ return convertCompositeNodeDataAndYangToJson(compositeNode, yangPath, outputPath, null, null);
+ }
+
+ static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath,
+ String outputPath, String searchedModuleName, String searchedDataSchemaName) {
String jsonResult = null;
Set<Module> modules = null;
}
assertNotNull("modules can't be null.", modules);
+ Module module = null;
+ if (searchedModuleName != null) {
+ for (Module m : modules) {
+ if (m.getName().equals(searchedModuleName)) {
+ module = m;
+ break;
+ }
+ }
+ } else if (modules.size() == 1) {
+ module = modules.iterator().next();
+ }
+ assertNotNull("Module is missing", module);
+
assertNotNull("Composite node can't be null", compositeNode);
StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE;
- for (Module module : modules) {
- ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
- for (DataSchemaNode dataSchemaNode : module.getChildNodes()) {
- StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode);
- try {
- structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS);
- } catch (WebApplicationException | IOException e) {
- e.printStackTrace();
+ ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
+ DataSchemaNode dataSchemaNode = null;
+ if (searchedDataSchemaName != null) {
+ for (DataSchemaNode dsn : module.getChildNodes()) {
+ if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
+ dataSchemaNode = dsn;
}
- assertFalse(
- "Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(),
- byteArrayOS.toString().isEmpty());
- }
- jsonResult = byteArrayOS.toString();
- try {
- outputToFile(byteArrayOS, outputPath);
- } catch (IOException e) {
- System.out.println("Output file wasn't cloased sucessfuly.");
}
+ } else if (module.getChildNodes().size() == 1) {
+ dataSchemaNode = module.getChildNodes().iterator().next();
+ }
+ assertNotNull(dataSchemaNode);
+ // SchemaContextUtil.
+ ControllerContext controllerContext = ControllerContext.getInstance();
+ controllerContext.setSchemas(loadSchemaContext(modules));
+ StructuredData structuredData = new StructuredData(compositeNode, dataSchemaNode);
+ try {
+ structuredDataToJsonProvider.writeTo(structuredData, null, null, null, null, null, byteArrayOS);
+ } catch (WebApplicationException | IOException e) {
+ e.printStackTrace();
}
+ assertFalse("Returning JSON string can't be empty for node " + dataSchemaNode.getQName().getLocalName(),
+ byteArrayOS.toString().isEmpty());
+
+ jsonResult = byteArrayOS.toString();
+ try {
+ outputToFile(byteArrayOS, outputPath);
+ } catch (IOException e) {
+ System.out.println("Output file wasn't cloased sucessfuly.");
+ }
+
return jsonResult;
}
RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
Future<RpcResult<TransactionStatus>> future = DummyFuture.builder().rpcResult(rpcResult).build();
when(controllerContext.toInstanceIdentifier(any(String.class))).thenReturn(instIdAndSchema);
- when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(future);
+ when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(
+ future);
restconf.setControllerContext(controllerContext);
restconf.setBroker(broker);
--- /dev/null
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class ToJsonWithAugmentTest {
+
+ /**
+ * Test of json output when as input are specified composite node with empty
+ * data + YANG file
+ */
+ @Test
+ public void augmentedElementsToJson() {
+ String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson(
+ TestUtils.loadCompositeNode("/yang-to-json-conversion/augmentation/xml/data.xml"),
+ "/yang-to-json-conversion/augmentation", "/yang-to-json-conversion/augmentation/xml", "yang", "cont");
+
+ assertTrue(jsonOutput.contains("\"augment-leaf:lf2\": \"lf2\""));
+ assertTrue(jsonOutput.contains("\"augment-container:cont1\": {"));
+ assertTrue(jsonOutput.contains("\"augment-container:lf11\": \"lf11\""));
+ assertTrue(jsonOutput.contains("\"augment-list:lst1\": ["));
+ assertTrue(jsonOutput.contains("\"augment-list:lf11\": \"lf1_1\""));
+ assertTrue(jsonOutput.contains("\"augment-list:lf11\": \"lf1_2\""));
+ assertTrue(jsonOutput.contains("\"augment-leaflist:lflst1\": ["));
+ }
+}