use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.
the class XmlPatchBodyReaderMountPointTest method moduleDataTest.
@Test
public void moduleDataTest() throws Exception {
final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
mockBodyReader(uri, xmlToPatchBodyReader, false);
final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
checkPatchContextMountPoint(returnValue);
}
use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.
the class BrokerFacadeTest method testPatchConfigurationDataWithinTransactionServer.
/**
* Test Patch method on the server with no data.
*/
@Test
public void testPatchConfigurationDataWithinTransactionServer() throws Exception {
final PatchContext patchContext = mock(PatchContext.class);
final InstanceIdentifierContext<?> identifierContext = mock(InstanceIdentifierContext.class);
when(patchContext.getData()).thenReturn(new ArrayList<>());
doReturn(identifierContext).when(patchContext).getInstanceIdentifierContext();
// no mount point
when(identifierContext.getMountPoint()).thenReturn(null);
doReturn(CommitInfo.emptyFluentFuture()).when(this.rwTransaction).commit();
final PatchStatusContext status = this.brokerFacade.patchConfigurationDataWithinTransaction(patchContext);
// assert success
assertTrue("Patch operation should be successful on server", status.isOk());
}
use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.
the class XmlPatchBodyReader method parse.
private static PatchContext parse(final InstanceIdentifierContext<?> pathContext, final Document doc) throws XMLStreamException, IOException, ParserConfigurationException, SAXException, URISyntaxException {
final List<PatchEntity> resultCollection = new ArrayList<>();
final String patchId = doc.getElementsByTagName("patch-id").item(0).getFirstChild().getNodeValue();
final NodeList editNodes = doc.getElementsByTagName("edit");
for (int i = 0; i < editNodes.getLength(); i++) {
DataSchemaNode schemaNode = (DataSchemaNode) pathContext.getSchemaNode();
final Element element = (Element) editNodes.item(i);
final String operation = element.getElementsByTagName("operation").item(0).getFirstChild().getNodeValue();
final PatchEditOperation oper = PatchEditOperation.valueOf(operation.toUpperCase(Locale.ROOT));
final String editId = element.getElementsByTagName("edit-id").item(0).getFirstChild().getNodeValue();
final String target = element.getElementsByTagName("target").item(0).getFirstChild().getNodeValue();
final List<Element> values = readValueNodes(element, oper);
final Element firstValueElement = values != null ? values.get(0) : null;
// find complete path to target and target schema node
// target can be also empty (only slash)
YangInstanceIdentifier targetII;
final SchemaNode targetNode;
final Inference inference;
if (target.equals("/")) {
targetII = pathContext.getInstanceIdentifier();
targetNode = pathContext.getSchemaContext();
inference = Inference.ofDataTreePath(pathContext.getSchemaContext(), schemaNode.getQName());
} else {
// interpret as simple context
targetII = ParserIdentifier.parserPatchTarget(pathContext, target);
// move schema node
schemaNode = verifyNotNull(DataSchemaContextTree.from(pathContext.getSchemaContext()).findChild(targetII).orElseThrow().getDataSchemaNode());
final SchemaInferenceStack stack = SchemaInferenceStack.of(pathContext.getSchemaContext());
targetII.getPathArguments().stream().filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier)).forEach(p -> stack.enterSchemaTree(p.getNodeType()));
final EffectiveStatement<?, ?> parentStmt = stack.exit();
verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt);
targetNode = (SchemaNode) parentStmt;
inference = stack.toInference();
}
if (targetNode == null) {
LOG.debug("Target node {} not found in path {} ", target, pathContext.getSchemaNode());
throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
}
if (oper.isWithValue()) {
final NormalizedNode parsed;
if (schemaNode instanceof ContainerSchemaNode || schemaNode instanceof ListSchemaNode) {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final XmlParserStream xmlParser = XmlParserStream.create(writer, inference);
xmlParser.traverse(new DOMSource(firstValueElement));
parsed = resultHolder.getResult();
} else {
parsed = null;
}
// for lists allow to manipulate with list items through their parent
if (targetII.getLastPathArgument() instanceof NodeIdentifierWithPredicates) {
targetII = targetII.getParent();
}
resultCollection.add(new PatchEntity(editId, oper, targetII, parsed));
} else {
resultCollection.add(new PatchEntity(editId, oper, targetII));
}
}
return new PatchContext(pathContext, ImmutableList.copyOf(resultCollection), patchId);
}
use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.
the class TestJsonPatchBodyReader method modulePatchDataTest.
@Test
public void modulePatchDataTest() throws Exception {
final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
mockBodyReader(uri, jsonToPatchBodyReader, false);
final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
checkPatchContext(returnValue);
}
use of org.opendaylight.restconf.common.patch.PatchContext in project netconf by opendaylight.
the class TestJsonPatchBodyReader method modulePatchCreateAndDeleteTest.
/**
* Test of successful Patch consisting of create and delete Patch operations.
*/
@Test
public void modulePatchCreateAndDeleteTest() throws Exception {
final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
mockBodyReader(uri, jsonToPatchBodyReader, false);
final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
checkPatchContext(returnValue);
}
Aggregations