use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class InvokeRpcMethodTest method testInvokeRpcMethodWithInput.
@Test
@Ignore
public void testInvokeRpcMethodWithInput() {
final DOMRpcResult expResult = mock(DOMRpcResult.class);
final QName path = QName.create("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)make-toast");
final Module rpcModule = schemaContext.findModules("toaster").iterator().next();
assertNotNull(rpcModule);
final QName rpcQName = QName.create(rpcModule.getQNameModule(), "make-toast");
RpcDefinition rpcDef = null;
ContainerLike rpcInputSchemaNode = null;
for (final RpcDefinition rpc : rpcModule.getRpcs()) {
if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
rpcInputSchemaNode = rpc.getInput();
rpcDef = rpc;
break;
}
}
assertNotNull(rpcDef);
assertNotNull(rpcInputSchemaNode);
final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
final NormalizedNodeContext payload = new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schemaContext), containerBuilder.build());
doReturn(immediateFluentFuture(expResult)).when(brokerFacade).invokeRpc(eq(path), any(NormalizedNode.class));
final NormalizedNodeContext output = this.restconfImpl.invokeRpc("toaster:make-toast", payload, uriInfo);
assertNotNull(output);
assertEquals(null, output.getData());
// additional validation in the fact that the restconfImpl does not
// throw an exception.
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class XmlNormalizedNodeBodyReader method parse.
private NormalizedNodeContext parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
final SchemaNode schemaNodeContext = pathContext.getSchemaNode();
DataSchemaNode schemaNode;
boolean isRpc = false;
if (schemaNodeContext instanceof RpcDefinition) {
schemaNode = ((RpcDefinition) schemaNodeContext).getInput();
isRpc = true;
} else if (schemaNodeContext instanceof DataSchemaNode) {
schemaNode = (DataSchemaNode) schemaNodeContext;
} else {
throw new IllegalStateException("Unknown SchemaNode");
}
final String docRootElm = doc.getDocumentElement().getLocalName();
final String docRootNamespace = doc.getDocumentElement().getNamespaceURI();
final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
if (isPost() && !isRpc) {
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 (!isRpc) {
final QName scQName = schemaNode.getQName();
Preconditions.checkState(docRootElm.equals(scQName.getLocalName()) && docRootNamespace.equals(scQName.getNamespace().toString()), String.format("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.ofSchemaPath(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());
return new NormalizedNodeContext(outIIContext, parsed);
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project netconf by opendaylight.
the class CreateStreamUtilTest method prepareDomPayload.
private NormalizedNodePayload prepareDomPayload(final String rpcName, final Function<RpcDefinition, ContainerLike> rpcToContainer, final String toasterValue, final String inputOutputName) {
final EffectiveModelContext schema = refSchemaCtx;
final Module rpcModule = schema.findModules("sal-remote").iterator().next();
final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
ContainerLike rpcInputSchemaNode = null;
for (final RpcDefinition rpc : rpcModule.getRpcs()) {
if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
rpcInputSchemaNode = rpcToContainer.apply(rpc);
break;
}
}
assertNotNull(rpcInputSchemaNode);
final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container = SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.findDataChildByName(lfQName).orElseThrow();
assertThat(lfSchemaNode, instanceOf(LeafSchemaNode.class));
final Object o;
if ("toaster".equals(toasterValue)) {
final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
o = YangInstanceIdentifier.builder().node(rpcQname).build();
} else {
o = toasterValue;
}
final LeafNode<Object> lfNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lfSchemaNode).withValue(o).build();
container.withChild(lfNode);
return NormalizedNodePayload.of(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema), container.build());
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project yangtools by opendaylight.
the class NormalizedNodeStreamWriterStack method startContainerNode.
public SchemaNode startContainerNode(final NodeIdentifier name) {
LOG.debug("Enter container {}", name);
final SchemaNode schema;
if (schemaStack.isEmpty() && root instanceof NotificationDefinition) {
// Special case for stacks initialized at notification. We pretend the first container is contained within
// itself.
// FIXME: 8.0.0: factor this special case out to something more reasonable, like being initialized at the
// Notification's parent and knowing to enterSchemaTree() instead of enterDataTree().
schema = (NotificationDefinition) root;
} else {
schema = enterDataTree(name);
checkArgument(schema instanceof ContainerLike, "Node %s is not a container", schema);
}
schemaStack.push(schema);
return schema;
}
use of org.opendaylight.yangtools.yang.model.api.ContainerLike in project yangtools by opendaylight.
the class XmlParserStream method parse.
/**
* This method parses the XML source and emits node events into a NormalizedNodeStreamWriter based on the
* YANG-modeled data contained in the XML source.
*
* @param reader
* StAX reader which is to used to walk through the XML source
* @return
* instance of XmlParserStream
* @throws XMLStreamException
* if a well-formedness error or an unexpected processing condition occurs while parsing the XML
* @throws URISyntaxException
* if the namespace URI of an XML element contains a syntax error
* @throws IOException
* if an error occurs while parsing the value of an anyxml node
* @throws SAXException
* if an error occurs while parsing the value of an anyxml node
*/
public XmlParserStream parse(final XMLStreamReader reader) throws XMLStreamException, URISyntaxException, IOException, SAXException {
if (reader.hasNext()) {
reader.nextTag();
final AbstractNodeDataWithSchema<?> nodeDataWithSchema;
if (parentNode instanceof ContainerLike) {
nodeDataWithSchema = new ContainerNodeDataWithSchema((ContainerLike) parentNode);
} else if (parentNode instanceof ListSchemaNode) {
nodeDataWithSchema = new ListNodeDataWithSchema((ListSchemaNode) parentNode);
} else if (parentNode instanceof AnyxmlSchemaNode) {
nodeDataWithSchema = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) parentNode);
} else if (parentNode instanceof LeafSchemaNode) {
nodeDataWithSchema = new LeafNodeDataWithSchema((LeafSchemaNode) parentNode);
} else if (parentNode instanceof LeafListSchemaNode) {
nodeDataWithSchema = new LeafListNodeDataWithSchema((LeafListSchemaNode) parentNode);
} else if (parentNode instanceof AnydataSchemaNode) {
nodeDataWithSchema = new AnydataNodeDataWithSchema((AnydataSchemaNode) parentNode);
} else {
throw new IllegalStateException("Unsupported schema node type " + parentNode.getClass() + ".");
}
read(reader, nodeDataWithSchema, reader.getLocalName());
nodeDataWithSchema.write(writer);
}
return this;
}
Aggregations