use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class URIParametersParsing method prepareDomRpcNode.
private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
final EffectiveModelContext schema = this.controllerContext.getGlobalSchema();
final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
final QName rpcQName = QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
ContainerLike rpcInputSchemaNode = null;
for (final RpcDefinition rpc : rpcSalRemoteModule.getRpcs()) {
if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
rpcInputSchemaNode = rpc.getInput();
break;
}
}
assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
final QName pathQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
assertTrue(pathSchemaNode instanceof LeafSchemaNode);
final LeafNode<Object> pathNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) pathSchemaNode).withValue(YangInstanceIdentifier.builder().node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
container.withChild(pathNode);
final AugmentationSchemaNode augmentationSchema = requireNonNull(rpcInputSchemaNode.getAvailableAugmentations().iterator().next());
final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder = SchemaAwareBuilders.augmentationBuilder(augmentationSchema);
final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
assertTrue(dsSchemaNode instanceof LeafSchemaNode);
final LeafNode<Object> dsNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) dsSchemaNode).withValue(datastore).build();
augmentationBuilder.withChild(dsNode);
final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
final LeafNode<Object> scopeNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) scopeSchemaNode).withValue(scope).build();
augmentationBuilder.withChild(scopeNode);
final QName outputQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
assertTrue(outputSchemaNode instanceof LeafSchemaNode);
final LeafNode<Object> outputNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
augmentationBuilder.withChild(outputNode);
container.withChild(augmentationBuilder.build());
when(rpcDef.getInput()).thenReturn((InputSchemaNode) rpcInputSchemaNode);
when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
when(rpcDef.getQName()).thenReturn(rpcQName);
return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema), container.build());
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class InvokeRpcMethodTest method testInvokeRpcWithNoPayloadWithOutput_Success.
@Test
public void testInvokeRpcWithNoPayloadWithOutput_Success() {
final SchemaContext schema = controllerContext.getGlobalSchema();
final Module rpcModule = schema.findModules("toaster").iterator().next();
assertNotNull(rpcModule);
final QName rpcQName = QName.create(rpcModule.getQNameModule(), "testOutput");
final QName rpcOutputQName = QName.create(rpcModule.getQNameModule(), "output");
RpcDefinition rpcDef = null;
ContainerLike rpcOutputSchemaNode = null;
for (final RpcDefinition rpc : rpcModule.getRpcs()) {
if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
rpcOutputSchemaNode = rpc.getOutput();
rpcDef = rpc;
break;
}
}
assertNotNull(rpcDef);
assertNotNull(rpcOutputSchemaNode);
final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = SchemaAwareBuilders.containerBuilder(rpcOutputSchemaNode);
final DataSchemaNode leafSchema = rpcOutputSchemaNode.getDataChildByName(QName.create(rpcModule.getQNameModule(), "textOut"));
assertTrue(leafSchema instanceof LeafSchemaNode);
final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafBuilder = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) leafSchema);
leafBuilder.withValue("brm");
containerBuilder.withChild(leafBuilder.build());
final ContainerNode container = containerBuilder.build();
final DOMRpcResult result = new DefaultDOMRpcResult(container);
doReturn(immediateFluentFuture(result)).when(brokerFacade).invokeRpc(eq(rpcDef.getQName()), any());
final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:testOutput", null, uriInfo);
assertNotNull(output);
assertNotNull(output.getData());
assertSame(container, output.getData());
assertNotNull(output.getInstanceIdentifierContext());
assertNotNull(output.getInstanceIdentifierContext().getSchemaContext());
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class InvokeRpcMethodTest method prepareDomPayload.
private NormalizedNodeContext prepareDomPayload() {
final EffectiveModelContext schema = controllerContext.getGlobalSchema();
final Module rpcModule = schema.findModules("invoke-rpc-module").iterator().next();
assertNotNull(rpcModule);
final QName rpcQName = QName.create(rpcModule.getQNameModule(), "rpc-test");
ContainerLike rpcInputSchemaNode = null;
for (final RpcDefinition rpc : rpcModule.getRpcs()) {
if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
rpcInputSchemaNode = rpc.getInput();
break;
}
}
assertNotNull(rpcInputSchemaNode);
final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
final QName contQName = QName.create(rpcModule.getQNameModule(), "cont");
final DataSchemaNode contSchemaNode = rpcInputSchemaNode.getDataChildByName(contQName);
assertTrue(contSchemaNode instanceof ContainerSchemaNode);
final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> contNode = SchemaAwareBuilders.containerBuilder((ContainerSchemaNode) contSchemaNode);
final QName lfQName = QName.create(rpcModule.getQNameModule(), "lf");
final DataSchemaNode lfSchemaNode = ((ContainerSchemaNode) contSchemaNode).getDataChildByName(lfQName);
assertTrue(lfSchemaNode instanceof LeafSchemaNode);
final LeafNode<Object> lfNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lfSchemaNode).withValue("any value").build();
contNode.withChild(lfNode);
container.withChild(contNode.build());
return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema), container.build());
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class XmlNormalizedNodeBodyReader method parse.
private NormalizedNodePayload parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
final SchemaNode schemaNodeContext = pathContext.getSchemaNode();
DataSchemaNode schemaNode;
final boolean isOperation;
if (schemaNodeContext instanceof OperationDefinition) {
schemaNode = ((OperationDefinition) schemaNodeContext).getInput();
isOperation = true;
} else if (schemaNodeContext instanceof DataSchemaNode) {
schemaNode = (DataSchemaNode) schemaNodeContext;
isOperation = false;
} else {
throw new IllegalStateException("Unknown SchemaNode " + schemaNodeContext);
}
final String docRootElm = doc.getDocumentElement().getLocalName();
final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
if (isPost() && !isOperation) {
final Deque<Object> foundSchemaNodes = findPathToSchemaNodeByName(schemaNode, docRootElm, docRootNamespace);
if (foundSchemaNodes.isEmpty()) {
throw new IllegalStateException(String.format("Child \"%s\" was not found in parent schema node \"%s\"", docRootElm, schemaNode.getQName()));
}
while (!foundSchemaNodes.isEmpty()) {
final Object child = foundSchemaNodes.pop();
if (child instanceof AugmentationSchemaNode) {
final AugmentationSchemaNode augmentSchemaNode = (AugmentationSchemaNode) child;
iiToDataList.add(DataSchemaContextNode.augmentationIdentifierFrom(augmentSchemaNode));
} else if (child instanceof DataSchemaNode) {
schemaNode = (DataSchemaNode) child;
iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(schemaNode.getQName()));
}
}
// PUT
} else if (!isOperation) {
final QName scQName = schemaNode.getQName();
checkState(docRootElm.equals(scQName.getLocalName()) && docRootNamespace.equals(scQName.getNamespace().toString()), "Not correct message root element \"%s\", should be \"%s\"", docRootElm, scQName);
}
NormalizedNode parsed;
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
if (schemaNode instanceof ContainerLike || schemaNode instanceof ListSchemaNode || schemaNode instanceof LeafSchemaNode) {
final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(pathContext.getSchemaContext(), schemaNode.getPath()).toInference());
xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
parsed = resultHolder.getResult();
// Therefore we now have to extract the MapEntryNode from the parsed MapNode.
if (parsed instanceof MapNode) {
final MapNode mapNode = (MapNode) parsed;
// extracting the MapEntryNode
parsed = mapNode.body().iterator().next();
}
if (schemaNode instanceof ListSchemaNode && isPost()) {
iiToDataList.add(parsed.getIdentifier());
}
} else {
LOG.warn("Unknown schema node extension {} was not parsed", schemaNode.getClass());
parsed = null;
}
final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(pathContext.getInstanceIdentifier().getPathArguments(), iiToDataList));
final InstanceIdentifierContext<? extends SchemaNode> outIIContext = new InstanceIdentifierContext<>(fullIIToData, pathContext.getSchemaNode(), pathContext.getMountPoint(), pathContext.getSchemaContext());
// FIXME: can result really be null?
return NormalizedNodePayload.ofNullable(outIIContext, parsed);
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class TestRestconfUtils method parse.
private static NormalizedNode parse(final InstanceIdentifierContext<?> iiContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
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)) {
for (final DataSchemaNode child : ((DataNodeContainer) schemaNode).getChildNodes()) {
if (child.getQName().getLocalName().equalsIgnoreCase(docRootElm)) {
schemaNode = child;
break;
}
}
}
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final XmlParserStream xmlParser = XmlParserStream.create(writer, SchemaInferenceStack.ofInstantiatedPath(iiContext.getSchemaContext(), schemaNode.getPath()).toInference());
if (schemaNode instanceof ContainerLike || schemaNode instanceof ListSchemaNode) {
xmlParser.traverse(new DOMSource(doc.getDocumentElement()));
return resultHolder.getResult();
}
// FIXME : add another DataSchemaNode extensions e.g. LeafSchemaNode
return null;
}
Aggregations