package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;
import com.google.common.base.Throwables;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
import org.slf4j.Logger;
@SuppressWarnings("checkstyle:IllegalCatch")
@Override
- protected PatchContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
+ protected PatchContext readBody(final InstanceIdentifierContext path, final InputStream entityStream)
throws WebApplicationException {
try {
return readFrom(path, entityStream);
}
}
- private PatchContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream)
+ private PatchContext readFrom(final InstanceIdentifierContext path, final InputStream entityStream)
throws IOException {
final JsonReader jsonReader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
AtomicReference<String> patchId = new AtomicReference<>();
}
@SuppressWarnings("checkstyle:IllegalCatch")
- public PatchContext readFrom(final String uriPath, final InputStream entityStream) throws
- RestconfDocumentedException {
+ public PatchContext readFrom(final String uriPath, final InputStream entityStream)
+ throws RestconfDocumentedException {
try {
return readFrom(
ParserIdentifier.toInstanceIdentifier(uriPath, getSchemaContext(),
Optional.ofNullable(getMountPointService())), entityStream);
} catch (final Exception e) {
- propagateExceptionAs(e);
- return null; // no-op
+ throw propagateExceptionAs(e);
}
}
- private static RuntimeException propagateExceptionAs(final Exception exception) throws RestconfDocumentedException {
+ private static RestconfDocumentedException propagateExceptionAs(final Exception exception)
+ throws RestconfDocumentedException {
Throwables.throwIfInstanceOf(exception, RestconfDocumentedException.class);
LOG.debug("Error parsing json input", exception);
ErrorTag.MALFORMED_MESSAGE, exception);
}
- private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext<?> path,
+ private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext path,
final AtomicReference<String> patchId) throws IOException {
+ final DataSchemaContextTree schemaTree = DataSchemaContextTree.from(path.getSchemaContext());
final List<PatchEntity> resultCollection = new ArrayList<>();
final JsonPatchBodyReader.PatchEdit edit = new JsonPatchBodyReader.PatchEdit();
case END_DOCUMENT:
break;
case NAME:
- parseByName(in.nextName(), edit, in, path, resultCollection, patchId);
+ parseByName(in.nextName(), edit, in, path, schemaTree, resultCollection, patchId);
break;
case END_OBJECT:
in.endObject();
* @param edit PatchEdit instance
* @param in JsonReader reader
* @param path InstanceIdentifierContext context
+ * @param codec Draft11StringModuleInstanceIdentifierCodec codec
* @param resultCollection collection of parsed edits
- * @param patchId id of edit patch
* @throws IOException if operation fails
*/
private void parseByName(final @NonNull String name, final @NonNull PatchEdit edit,
- final @NonNull JsonReader in, final @NonNull InstanceIdentifierContext<?> path,
+ final @NonNull JsonReader in, final @NonNull InstanceIdentifierContext path,
+ final @NonNull DataSchemaContextTree schemaTree,
final @NonNull List<PatchEntity> resultCollection,
final @NonNull AtomicReference<String> patchId) throws IOException {
switch (name) {
in.beginArray();
while (in.hasNext()) {
- readEditDefinition(edit, in, path);
+ readEditDefinition(edit, in, path, schemaTree);
resultCollection.add(prepareEditOperation(edit));
edit.clear();
}
in.endArray();
} else {
- readEditDefinition(edit, in, path);
+ readEditDefinition(edit, in, path, schemaTree);
resultCollection.add(prepareEditOperation(edit));
edit.clear();
}
* @param edit PatchEdit instance to be filled with read data
* @param in JsonReader reader
* @param path InstanceIdentifierContext path context
+ * @param codec Draft11StringModuleInstanceIdentifierCodec codec
* @throws IOException if operation fails
*/
private void readEditDefinition(final @NonNull PatchEdit edit, final @NonNull JsonReader in,
- final @NonNull InstanceIdentifierContext<?> path) throws IOException {
+ final @NonNull InstanceIdentifierContext path,
+ final @NonNull DataSchemaContextTree schemaTree) throws IOException {
String deferredValue = null;
in.beginObject();
case "target":
// target can be specified completely in request URI
final String target = in.nextString();
- final SchemaInferenceStack stack = SchemaInferenceStack.of(path.getSchemaContext());
if (target.equals("/")) {
edit.setTarget(path.getInstanceIdentifier());
- edit.setTargetInference(stack.toInference());
+ edit.setTargetSchemaNode(SchemaInferenceStack.of(path.getSchemaContext()).toInference());
} else {
edit.setTarget(ParserIdentifier.parserPatchTarget(path, target));
- edit.getTarget().getPathArguments().stream()
- .filter(arg -> !(arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates))
- .filter(arg -> !(arg instanceof YangInstanceIdentifier.AugmentationIdentifier))
- .forEach(p -> stack.enterSchemaTree(p.getNodeType()));
- stack.exit();
- edit.setTargetInference(stack.toInference());
+
+ final var stack = schemaTree.enterPath(edit.getTarget()).orElseThrow().stack();
+ if (!stack.isEmpty()) {
+ stack.exit();
+ }
+
+ if (!stack.isEmpty()) {
+ final EffectiveStatement<?, ?> parentStmt = stack.currentStatement();
+ verify(parentStmt instanceof SchemaNode, "Unexpected parent %s", parentStmt);
+ }
+ edit.setTargetSchemaNode(stack.toInference());
}
+
break;
case "value":
checkArgument(edit.getData() == null && deferredValue == null, "Multiple value entries found");
- if (edit.getTargetInference() == null) {
+ if (edit.getTargetSchemaNode() == null) {
// save data defined in value node for next (later) processing, because target needs to be read
// always first and there is no ordering in Json input
deferredValue = readValueNode(in);
} else {
// We have a target schema node, reuse this reader without buffering the value.
- edit.setData(readEditData(in, edit.getTargetInference(), path));
+ edit.setData(readEditData(in, edit.getTargetSchemaNode(), path));
}
break;
default:
if (deferredValue != null) {
// read saved data to normalized node when target schema is already known
- edit.setData(readEditData(new JsonReader(new StringReader(deferredValue)), edit.getTargetInference(),
+ edit.setData(readEditData(new JsonReader(new StringReader(deferredValue)), edit.getTargetSchemaNode(),
path));
}
}
/**
* Parse data defined in value node and saves it to buffer.
+ * @param sb Buffer to read value node
* @param in JsonReader reader
* @throws IOException if operation fails
*/
* @return NormalizedNode representing data
*/
private static NormalizedNode readEditData(final @NonNull JsonReader in,
- final @NonNull Inference inference, final @NonNull InstanceIdentifierContext<?> path) {
+ final @NonNull Inference targetSchemaNode, final @NonNull InstanceIdentifierContext path) {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
- JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()), inference)
- .parse(in);
+ JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()),
+ targetSchemaNode).parse(in);
return resultHolder.getResult();
}
* @return PatchEntity Patch entity
*/
private static PatchEntity prepareEditOperation(final @NonNull PatchEdit edit) {
- if (edit.getOperation() != null && edit.getTargetInference() != null
+ if (edit.getOperation() != null && edit.getTargetSchemaNode() != null
&& checkDataPresence(edit.getOperation(), edit.getData() != null)) {
if (!edit.getOperation().isWithValue()) {
return new PatchEntity(edit.getId(), edit.getOperation(), edit.getTarget());
private String id;
private PatchEditOperation operation;
private YangInstanceIdentifier target;
- private Inference targetInference;
+ private Inference targetSchemaNode;
private NormalizedNode data;
String getId() {
this.target = requireNonNull(target);
}
- Inference getTargetInference() {
- return targetInference;
+ Inference getTargetSchemaNode() {
+ return targetSchemaNode;
}
- void setTargetInference(final Inference targetInference) {
- this.targetInference = requireNonNull(targetInference);
+ void setTargetSchemaNode(final Inference targetSchemaNode) {
+ this.targetSchemaNode = requireNonNull(targetSchemaNode);
}
NormalizedNode getData() {
id = null;
operation = null;
target = null;
- targetInference = null;
+ targetSchemaNode = null;
data = null;
}
}