use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.
the class Netconf799Test method testInvokeAction.
@Test
public void testInvokeAction() throws FileNotFoundException {
final EffectiveModelContext contextRef = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/instanceidentifier/yang"));
final DOMDataBroker mockDataBroker = mock(DOMDataBroker.class);
final SchemaContextHandler schemaContextHandler = new SchemaContextHandler(mockDataBroker, mock(DOMSchemaService.class));
schemaContextHandler.onModelContextUpdated(contextRef);
final DOMActionService actionService = mock(DOMActionService.class);
doReturn(Futures.immediateFuture(new SimpleDOMActionResult(Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(OUTPUT_QNAME)).build()))).when(actionService).invokeAction(eq(Absolute.of(CONT_QNAME, CONT1_QNAME, RESET_QNAME)), any(), any());
final RestconfDataServiceImpl dataService = new RestconfDataServiceImpl(schemaContextHandler, mockDataBroker, mock(DOMMountPointService.class), mock(RestconfStreamsSubscriptionService.class), actionService, mock(Configuration.class));
final var schemaNode = loadAction(contextRef, RESET_QNAME, ACTION_YII).orElseThrow();
final var response = dataService.invokeAction(NormalizedNodePayload.of(new InstanceIdentifierContext<>(ACTION_YII, schemaNode, null, contextRef), Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(INPUT_QNAME)).withChild(ImmutableNodes.leafNode(DELAY_QNAME, Uint32.TEN)).build()));
assertEquals(204, response.getStatus());
assertNull(response.getEntity());
}
use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.
the class RestconfDataServiceImplTest method testPostData.
@Test
public void testPostData() {
final QName listQname = QName.create(baseQName, "playlist");
final QName listKeyQname = QName.create(baseQName, "name");
final NodeIdentifierWithPredicates nodeWithKey = NodeIdentifierWithPredicates.of(listQname, listKeyQname, "name of band");
doReturn(new MultivaluedHashMap<String, String>()).when(uriInfo).getQueryParameters();
final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(iidBase, null, null, contextRef);
final NormalizedNodePayload payload = NormalizedNodePayload.of(iidContext, Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(listQname)).withChild(Builders.mapEntryBuilder().withNodeIdentifier(nodeWithKey).withChild(ImmutableNodes.leafNode(QName.create(baseQName, "name"), "name of band")).withChild(ImmutableNodes.leafNode(QName.create(baseQName, "description"), "band description")).build()).build());
final MapNode data = (MapNode) payload.getData();
final MapEntryNode entryNode = data.body().iterator().next();
final NodeIdentifierWithPredicates identifier = entryNode.getIdentifier();
final YangInstanceIdentifier node = payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(uriInfo).getBaseUriBuilder();
final Response response = dataService.postData(null, payload, uriInfo);
assertEquals(201, response.getStatus());
}
use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.
the class JsonNormalizedNodeBodyReader method readFrom.
public static NormalizedNodePayload readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost) {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final EffectiveStatementInference parentSchema;
final SchemaInferenceStack stack;
if (path.getSchemaNode() instanceof RpcEffectiveStatement) {
stack = SchemaInferenceStack.of(path.getSchemaContext(), Absolute.of(path.getSchemaNode().getQName()));
} else {
stack = SchemaInferenceStack.of(path.getSchemaContext());
path.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).forEach(p -> stack.enterSchemaTree(p.getNodeType()));
}
if (!isPost) {
stack.exit();
}
parentSchema = stack.toInference();
final JsonParserStream jsonParser = JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()), parentSchema);
final JsonReader reader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
jsonParser.parse(reader);
NormalizedNode result = resultHolder.getResult();
final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
InstanceIdentifierContext<? extends SchemaNode> newIIContext;
while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
final Object childNode = ((DataContainerNode) result).body().iterator().next();
if (isPost) {
iiToDataList.add(result.getIdentifier());
}
result = (NormalizedNode) childNode;
}
if (isPost) {
if (result instanceof MapEntryNode) {
iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getIdentifier().getNodeType()));
iiToDataList.add(result.getIdentifier());
} else {
final List<? extends @NonNull EffectiveStatement<?, ?>> parentPath = parentSchema.statementPath();
if (parentPath.isEmpty() || !(parentPath.get(parentPath.size() - 1) instanceof OperationDefinition)) {
iiToDataList.add(result.getIdentifier());
}
}
} else {
if (result instanceof MapNode) {
result = Iterables.getOnlyElement(((MapNode) result).body());
}
}
final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(path.getInstanceIdentifier().getPathArguments(), iiToDataList));
newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(), path.getSchemaContext());
// FIXME: can result really be null?
return NormalizedNodePayload.ofNullable(newIIContext, result);
}
use of org.opendaylight.restconf.common.context.InstanceIdentifierContext in project netconf by opendaylight.
the class JsonNormalizedNodeBodyWriter method writeTo.
@Override
public void writeTo(final NormalizedNodePayload context, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException {
final NormalizedNode data = context.getData();
if (data == null) {
return;
}
@SuppressWarnings("unchecked") final InstanceIdentifierContext<SchemaNode> identifierCtx = (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
final SchemaInferenceStack stack = SchemaInferenceStack.of(identifierCtx.getSchemaContext());
identifierCtx.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).forEach(p -> stack.enterSchemaTree(p.getNodeType()));
final SchemaPath path = stack.toSchemaPath();
final var pretty = context.getWriterParameters().prettyPrint();
try (JsonWriter jsonWriter = createJsonWriter(entityStream, pretty == null ? false : pretty.value())) {
jsonWriter.beginObject();
writeNormalizedNode(jsonWriter, path, identifierCtx, data, context.getWriterParameters().depth(), context.getWriterParameters().fields());
jsonWriter.endObject();
jsonWriter.flush();
}
if (httpHeaders != null) {
for (final Map.Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
httpHeaders.add(entry.getKey(), entry.getValue());
}
}
}
use of org.opendaylight.restconf.common.context.InstanceIdentifierContext 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);
}
Aggregations