2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.gson.stream.JsonReader;
13 import com.google.gson.stream.JsonToken;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.io.StringReader;
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Locale;
22 import java.util.Optional;
23 import java.util.concurrent.atomic.AtomicReference;
24 import javax.annotation.Nonnull;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.WebApplicationException;
27 import javax.ws.rs.ext.Provider;
28 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
31 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
32 import org.opendaylight.restconf.common.patch.PatchContext;
33 import org.opendaylight.restconf.common.patch.PatchEditOperation;
34 import org.opendaylight.restconf.common.patch.PatchEntity;
35 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
36 import org.opendaylight.restconf.nb.rfc8040.codecs.StringModuleInstanceIdentifierCodec;
37 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
38 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
39 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
40 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
45 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
46 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
47 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
48 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
49 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
50 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
51 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 @Consumes({Rfc8040.MediaTypes.PATCH + RestconfConstants.JSON})
57 public class JsonToPatchBodyReader extends AbstractToPatchBodyReader {
58 private static final Logger LOG = LoggerFactory.getLogger(JsonToPatchBodyReader.class);
60 public JsonToPatchBodyReader(final SchemaContextHandler schemaContextHandler,
61 final DOMMountPointServiceHandler mountPointServiceHandler) {
62 super(schemaContextHandler, mountPointServiceHandler);
65 @SuppressWarnings("checkstyle:IllegalCatch")
67 protected PatchContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
68 throws WebApplicationException {
70 return readFrom(path, entityStream);
71 } catch (final Exception e) {
72 throw propagateExceptionAs(e);
76 private PatchContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream)
78 final JsonReader jsonReader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
79 AtomicReference<String> patchId = new AtomicReference<>();
80 final List<PatchEntity> resultList = read(jsonReader, path, patchId);
83 return new PatchContext(path, resultList, patchId.get());
86 @SuppressWarnings("checkstyle:IllegalCatch")
87 public PatchContext readFrom(final String uriPath, final InputStream entityStream) throws
88 RestconfDocumentedException {
91 ParserIdentifier.toInstanceIdentifier(uriPath, getSchemaContext(),
92 Optional.ofNullable(getMountPointService())), entityStream);
93 } catch (final Exception e) {
94 propagateExceptionAs(e);
99 private static RuntimeException propagateExceptionAs(final Exception exception) throws RestconfDocumentedException {
100 if (exception instanceof RestconfDocumentedException) {
101 throw (RestconfDocumentedException)exception;
104 if (exception instanceof ResultAlreadySetException) {
105 LOG.debug("Error parsing json input:", exception);
106 throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. ");
109 throw new RestconfDocumentedException("Error parsing json input: " + exception.getMessage(), ErrorType.PROTOCOL,
110 ErrorTag.MALFORMED_MESSAGE, exception);
113 private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext<?> path,
114 final AtomicReference<String> patchId) throws IOException {
115 final List<PatchEntity> resultCollection = new ArrayList<>();
116 final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
117 path.getSchemaContext());
118 final JsonToPatchBodyReader.PatchEdit edit = new JsonToPatchBodyReader.PatchEdit();
120 while (in.hasNext()) {
127 Boolean.toString(in.nextBoolean());
141 parseByName(in.nextName(), edit, in, path, codec, resultCollection, patchId);
155 return ImmutableList.copyOf(resultCollection);
159 * Switch value of parsed JsonToken.NAME and read edit definition or patch id.
161 * @param name value of token
162 * @param edit PatchEdit instance
163 * @param in JsonReader reader
164 * @param path InstanceIdentifierContext context
165 * @param codec Draft11StringModuleInstanceIdentifierCodec codec
166 * @param resultCollection collection of parsed edits
167 * @throws IOException if operation fails
169 private void parseByName(@Nonnull final String name, @Nonnull final PatchEdit edit,
170 @Nonnull final JsonReader in, @Nonnull final InstanceIdentifierContext<?> path,
171 @Nonnull final StringModuleInstanceIdentifierCodec codec,
172 @Nonnull final List<PatchEntity> resultCollection,
173 @Nonnull final AtomicReference<String> patchId) throws IOException {
176 if (in.peek() == JsonToken.BEGIN_ARRAY) {
179 while (in.hasNext()) {
180 readEditDefinition(edit, in, path, codec);
181 resultCollection.add(prepareEditOperation(edit));
187 readEditDefinition(edit, in, path, codec);
188 resultCollection.add(prepareEditOperation(edit));
194 patchId.set(in.nextString());
202 * Read one patch edit object from Json input.
204 * @param edit PatchEdit instance to be filled with read data
205 * @param in JsonReader reader
206 * @param path InstanceIdentifierContext path context
207 * @param codec Draft11StringModuleInstanceIdentifierCodec codec
208 * @throws IOException if operation fails
210 private void readEditDefinition(@Nonnull final PatchEdit edit, @Nonnull final JsonReader in,
211 @Nonnull final InstanceIdentifierContext<?> path,
212 @Nonnull final StringModuleInstanceIdentifierCodec codec) throws IOException {
213 String deferredValue = null;
216 while (in.hasNext()) {
217 final String editDefinition = in.nextName();
218 switch (editDefinition) {
220 edit.setId(in.nextString());
223 edit.setOperation(PatchEditOperation.valueOf(in.nextString().toUpperCase(Locale.ROOT)));
226 // target can be specified completely in request URI
227 final String target = in.nextString();
228 if (target.equals("/")) {
229 edit.setTarget(path.getInstanceIdentifier());
230 edit.setTargetSchemaNode(path.getSchemaContext());
232 edit.setTarget(codec.deserialize(codec.serialize(path.getInstanceIdentifier()).concat(target)));
233 edit.setTargetSchemaNode(SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(),
234 codec.getDataContextTree().getChild(edit.getTarget()).getDataSchemaNode().getPath()
240 Preconditions.checkArgument(edit.getData() == null && deferredValue == null,
241 "Multiple value entries found");
243 if (edit.getTargetSchemaNode() == null) {
244 final StringBuilder sb = new StringBuilder();
246 // save data defined in value node for next (later) processing, because target needs to be read
247 // always first and there is no ordering in Json input
248 readValueNode(sb, in);
249 deferredValue = sb.toString();
251 // We have a target schema node, reuse this reader without buffering the value.
252 edit.setData(readEditData(in, edit.getTargetSchemaNode(), path));
256 // FIXME: this does not look right, as it can wreck our logic
263 if (deferredValue != null) {
264 // read saved data to normalized node when target schema is already known
265 edit.setData(readEditData(new JsonReader(new StringReader(deferredValue)), edit.getTargetSchemaNode(),
271 * Parse data defined in value node and saves it to buffer.
272 * @param sb Buffer to read value node
273 * @param in JsonReader reader
274 * @throws IOException if operation fails
276 private void readValueNode(@Nonnull final StringBuilder sb, @Nonnull final JsonReader in) throws IOException {
279 sb.append("{\"").append(in.nextName()).append("\":");
286 while (in.hasNext()) {
287 if (in.peek() == JsonToken.STRING) {
288 sb.append('"').append(in.nextString()).append('"');
290 readValueObject(sb, in);
292 if (in.peek() != JsonToken.END_ARRAY) {
301 readValueObject(sb, in);
310 * Parse one value object of data and saves it to buffer.
311 * @param sb Buffer to read value object
312 * @param in JsonReader reader
313 * @throws IOException if operation fails
315 private void readValueObject(@Nonnull final StringBuilder sb, @Nonnull final JsonReader in) throws IOException {
316 // read simple leaf value
317 if (in.peek() == JsonToken.STRING) {
318 sb.append('"').append(in.nextString()).append('"');
325 while (in.hasNext()) {
326 sb.append('"').append(in.nextName()).append("\":");
330 sb.append('"').append(in.nextString()).append('"');
336 while (in.hasNext()) {
337 if (in.peek() == JsonToken.STRING) {
338 sb.append('"').append(in.nextString()).append('"');
340 readValueObject(sb, in);
343 if (in.peek() != JsonToken.END_ARRAY) {
352 readValueObject(sb, in);
355 if (in.peek() != JsonToken.END_OBJECT) {
365 * Read patch edit data defined in value node to NormalizedNode.
366 * @param in reader JsonReader reader
367 * @return NormalizedNode representing data
369 private static NormalizedNode<?, ?> readEditData(@Nonnull final JsonReader in,
370 @Nonnull final SchemaNode targetSchemaNode, @Nonnull final InstanceIdentifierContext<?> path) {
371 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
372 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
373 JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()),
374 targetSchemaNode).parse(in);
376 return resultHolder.getResult();
380 * Prepare PatchEntity from PatchEdit instance when it satisfies conditions, otherwise throws exception.
381 * @param edit Instance of PatchEdit
382 * @return PatchEntity Patch entity
384 private static PatchEntity prepareEditOperation(@Nonnull final PatchEdit edit) {
385 if (edit.getOperation() != null && edit.getTargetSchemaNode() != null
386 && checkDataPresence(edit.getOperation(), edit.getData() != null)) {
387 if (!edit.getOperation().isWithValue()) {
388 return new PatchEntity(edit.getId(), edit.getOperation(), edit.getTarget());
391 // for lists allow to manipulate with list items through their parent
392 final YangInstanceIdentifier targetNode;
393 if (edit.getTarget().getLastPathArgument() instanceof NodeIdentifierWithPredicates) {
394 targetNode = edit.getTarget().getParent();
396 targetNode = edit.getTarget();
399 return new PatchEntity(edit.getId(), edit.getOperation(), targetNode, edit.getData());
402 throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
406 * Check if data is present when operation requires it and not present when operation data is not allowed.
407 * @param operation Name of operation
408 * @param hasData Data in edit are present/not present
409 * @return true if data is present when operation requires it or if there are no data when operation does not
410 * allow it, false otherwise
412 private static boolean checkDataPresence(@Nonnull final PatchEditOperation operation, final boolean hasData) {
413 return operation.isWithValue() == hasData;
417 * Helper class representing one patch edit.
419 private static final class PatchEdit {
421 private PatchEditOperation operation;
422 private YangInstanceIdentifier target;
423 private SchemaNode targetSchemaNode;
424 private NormalizedNode<?, ?> data;
430 void setId(final String id) {
431 this.id = Preconditions.checkNotNull(id);
434 PatchEditOperation getOperation() {
438 void setOperation(final PatchEditOperation operation) {
439 this.operation = Preconditions.checkNotNull(operation);
442 YangInstanceIdentifier getTarget() {
446 void setTarget(final YangInstanceIdentifier target) {
447 this.target = Preconditions.checkNotNull(target);
450 SchemaNode getTargetSchemaNode() {
451 return targetSchemaNode;
454 void setTargetSchemaNode(final SchemaNode targetSchemaNode) {
455 this.targetSchemaNode = Preconditions.checkNotNull(targetSchemaNode);
458 NormalizedNode<?, ?> getData() {
462 void setData(final NormalizedNode<?, ?> data) {
463 this.data = Preconditions.checkNotNull(data);
470 targetSchemaNode = null;