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) {
- String jsonResult = null;
+ return convertCompositeNodeDataAndYangToJson(compositeNode, yangPath, outputPath, null, null);
+ }
+
+ static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath,
+ String outputPath, String searchedModuleName, String searchedDataSchemaName) {
+ Set<Module> modules = resolveModules(yangPath);
+ Module module = resolveModule(searchedModuleName, modules);
+ DataSchemaNode dataSchemaNode = resolveDataSchemaNode(module, searchedDataSchemaName);
+
+ try {
+ return writeCompNodeWithSchemaContextToJson(compositeNode, outputPath, modules, dataSchemaNode);
+ } catch (WebApplicationException | IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+
+ }
+
+ static Module resolveModule(String searchedModuleName, Set<Module> modules) {
+ 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();
+ }
+ return module;
+ }
+
+ static Set<Module> resolveModules(String yangPath) {
Set<Module> modules = null;
try {
} catch (FileNotFoundException e) {
e.printStackTrace();
}
- assertNotNull("modules can't be null.", modules);
- assertNotNull("Composite node can't be null", compositeNode);
+ return modules;
+ }
- 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();
+ static DataSchemaNode resolveDataSchemaNode(Module module, String searchedDataSchemaName) {
+ assertNotNull("Module is missing", module);
+
+ 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();
+ } else if (module.getChildNodes().size() == 1) {
+ dataSchemaNode = module.getChildNodes().iterator().next();
+ }
+ return dataSchemaNode;
+ }
+
+ static String writeCompNodeWithSchemaContextToJson(CompositeNode compositeNode, String outputPath,
+ Set<Module> modules, DataSchemaNode dataSchemaNode) throws IOException, WebApplicationException {
+ String jsonResult;
+
+ assertNotNull(dataSchemaNode);
+ assertNotNull("Composite node can't be null", compositeNode);
+ ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
+
+ ControllerContext contContext = ControllerContext.getInstance();
+ contContext.setSchemas(loadSchemaContext(modules));
+
+ StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE;
+ structuredDataToJsonProvider.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null,
+ null, byteArrayOS);
+
+ jsonResult = byteArrayOS.toString();
+ if (outputPath != null) {
try {
outputToFile(byteArrayOS, outputPath);
} catch (IOException e) {
System.out.println("Output file wasn't cloased sucessfuly.");
}
-
}
+
return jsonResult;
}
ControllerContext controllerContext = mock(ControllerContext.class);
BrokerFacade broker = mock(BrokerFacade.class);
- RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
+ RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().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);
}
}
}
+
+ public static void prepareMockForRestconfBeforeNormalization(Set<Module> modules, DataSchemaNode dataSchemaNode,
+ RestconfImpl restconf) {
+ ControllerContext instance = ControllerContext.getInstance();
+ instance.setSchemas(TestUtils.loadSchemaContext(modules));
+ restconf.setControllerContext(ControllerContext.getInstance());
+
+ BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class);
+ when(mockedBrokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
+ .thenReturn(
+ new DummyFuture.Builder().rpcResult(
+ new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.COMMITED)
+ .build()).build());
+ restconf.setBroker(mockedBrokerFacade);
+ }
}