use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.
the class SchemalessRpcStructureTransformerTest method testCreateEditConfigStructure.
@Test
public void testCreateEditConfigStructure() throws Exception {
DOMSourceAnyxmlNode data = Builders.anyXmlBuilder().withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName())).withValue(source).build();
if (expectedException != null) {
assertThrows(expectedException, () -> adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE)));
return;
}
final DOMSourceAnyxmlNode anyXmlNode = adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
final String s = XmlUtil.toString((Element) anyXmlNode.body().getNode());
Diff diff = new Diff(expectedConfig, s);
assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.
the class SchemalessRpcStructureTransformerTest method testSelectFromDataStructure.
@Test
public void testSelectFromDataStructure() throws Exception {
DOMSourceAnyxmlNode data = Builders.anyXmlBuilder().withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName())).withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement())).build();
final DOMSourceAnyxmlNode dataStructure = (DOMSourceAnyxmlNode) adapter.selectFromDataStructure(data, path).get();
final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.body().getNode());
final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
final String expectedData = XmlUtil.toString((Element) source.getNode());
Diff diff = new Diff(expectedData, dataFromReply);
assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.
the class NetconfStateSchemas method findSchemasNode.
private static Optional<? extends NormalizedNode> findSchemasNode(final NormalizedNode result, final EffectiveModelContext schemaContext) {
if (result == null) {
return Optional.empty();
}
final Optional<DataContainerChild> rpcResultOpt = ((ContainerNode) result).findChildByArg(NETCONF_DATA_NODEID);
if (rpcResultOpt.isEmpty()) {
return Optional.empty();
}
final DataContainerChild rpcResult = rpcResultOpt.get();
verify(rpcResult instanceof DOMSourceAnyxmlNode, "Unexpected result %s", rpcResult);
final NormalizedNode dataNode;
try {
dataNode = NetconfUtil.transformDOMSourceToNormalizedNode(schemaContext, ((DOMSourceAnyxmlNode) rpcResult).body()).getResult();
} catch (XMLStreamException | URISyntaxException | IOException | SAXException e) {
LOG.warn("Failed to transform {}", rpcResult, e);
return Optional.empty();
}
final Optional<DataContainerChild> nStateNode = ((DataContainerNode) dataNode).findChildByArg(toId(NetconfState.QNAME));
if (nStateNode.isEmpty()) {
return Optional.empty();
}
return ((DataContainerNode) nStateNode.get()).findChildByArg(toId(Schemas.QNAME));
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.
the class NormalizedNodeXmlBodyWriter method writeNormalizedNode.
private static void writeNormalizedNode(final XMLStreamWriter xmlWriter, final InstanceIdentifierContext<?> pathContext, NormalizedNode data, @Nullable final Integer depth) throws IOException {
final RestconfNormalizedNodeWriter nnWriter;
final EffectiveModelContext schemaCtx = pathContext.getSchemaContext();
if (pathContext.getSchemaNode() instanceof SchemaContext) {
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, SchemaPath.ROOT, depth);
if (data instanceof DOMSourceAnyxmlNode) {
try {
writeElements(xmlWriter, nnWriter, (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(schemaCtx, ((DOMSourceAnyxmlNode) data).body()).getResult());
} catch (XMLStreamException | URISyntaxException | SAXException e) {
throw new IOException("Cannot write anyxml", e);
}
} else {
writeElements(xmlWriter, nnWriter, (ContainerNode) data);
}
} else if (pathContext.getSchemaNode() instanceof RpcDefinition) {
final RpcDefinition rpc = (RpcDefinition) pathContext.getSchemaNode();
final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
writeElements(xmlWriter, nnWriter, (ContainerNode) data);
} else {
final List<QName> qNames = pathContext.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
final SchemaPath path = SchemaPath.of(Absolute.of(qNames)).getParent();
nnWriter = createNormalizedNodeWriter(xmlWriter, schemaCtx, path, depth);
if (data instanceof MapEntryNode) {
// Restconf allows returning one list item. We need to wrap it
// in map node in order to serialize it properly
data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).addChild((MapEntryNode) data).build();
}
nnWriter.write(data);
}
nnWriter.flush();
}
use of org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode in project netconf by opendaylight.
the class NormalizedNodeJsonBodyWriter method writeNormalizedNode.
private static void writeNormalizedNode(final JsonWriter jsonWriter, final InstanceIdentifierContext<SchemaNode> context, NormalizedNode data, @Nullable final Integer depth) throws IOException {
final RestconfNormalizedNodeWriter nnWriter;
if (context.getSchemaNode() instanceof SchemaContext) {
/*
* Creates writer without initialNs and we write children of root data container
* which is not visible in restconf
*/
nnWriter = createNormalizedNodeWriter(context, SchemaPath.ROOT, jsonWriter, depth);
if (data instanceof ContainerNode) {
writeChildren(nnWriter, (ContainerNode) data);
} else if (data instanceof DOMSourceAnyxmlNode) {
try {
writeChildren(nnWriter, (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(context.getSchemaContext(), ((DOMSourceAnyxmlNode) data).body()).getResult());
} catch (XMLStreamException | URISyntaxException | SAXException e) {
throw new IOException("Cannot write anyxml.", e);
}
}
} else if (context.getSchemaNode() instanceof RpcDefinition) {
/*
* RpcDefinition is not supported as initial codec in JSONStreamWriter,
* so we need to emit initial output declaratation..
*/
final RpcDefinition rpc = (RpcDefinition) context.getSchemaNode();
final SchemaPath path = SchemaPath.of(Absolute.of(rpc.getQName(), rpc.getOutput().getQName()));
nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
jsonWriter.name("output");
jsonWriter.beginObject();
writeChildren(nnWriter, (ContainerNode) data);
jsonWriter.endObject();
} else {
final List<QName> qnames = context.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
final SchemaPath path = SchemaPath.of(Absolute.of(qnames)).getParent();
if (data instanceof MapEntryNode) {
data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build();
}
nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
nnWriter.write(data);
}
nnWriter.flush();
}
Aggregations