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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import com.google.gson.stream.JsonReader;
15 import com.google.gson.stream.JsonToken;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.io.StringReader;
20 import java.nio.charset.StandardCharsets;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Locale;
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.RestConnectorProvider;
36 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
37 import org.opendaylight.restconf.nb.rfc8040.codecs.StringModuleInstanceIdentifierCodec;
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 private String patchId;
62 @SuppressWarnings("checkstyle:IllegalCatch")
64 protected PatchContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
65 throws IOException, WebApplicationException {
67 return readFrom(path, entityStream);
68 } catch (final Exception e) {
69 throw propagateExceptionAs(e);
73 private PatchContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream)
75 final JsonReader jsonReader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
76 final List<PatchEntity> resultList = read(jsonReader, path);
79 return new PatchContext(path, resultList, patchId);
82 @SuppressWarnings("checkstyle:IllegalCatch")
83 public PatchContext readFrom(final String uriPath, final InputStream entityStream) throws
84 RestconfDocumentedException {
87 ParserIdentifier.toInstanceIdentifier(uriPath, SchemaContextHandler.getSchemaContext(),
88 Optional.of(RestConnectorProvider.getMountPointService())), entityStream);
89 } catch (final Exception e) {
90 propagateExceptionAs(e);
95 private static RuntimeException propagateExceptionAs(final Exception exception) throws RestconfDocumentedException {
96 if (exception instanceof RestconfDocumentedException) {
97 throw (RestconfDocumentedException)exception;
100 if (exception instanceof ResultAlreadySetException) {
101 LOG.debug("Error parsing json input:", exception);
102 throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. ");
105 throw new RestconfDocumentedException("Error parsing json input: " + exception.getMessage(), ErrorType.PROTOCOL,
106 ErrorTag.MALFORMED_MESSAGE, exception);
109 private List<PatchEntity> read(final JsonReader in, final InstanceIdentifierContext<?> path) throws IOException {
110 final List<PatchEntity> resultCollection = new ArrayList<>();
111 final StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(
112 path.getSchemaContext());
113 final JsonToPatchBodyReader.PatchEdit edit = new JsonToPatchBodyReader.PatchEdit();
115 while (in.hasNext()) {
122 Boolean.toString(in.nextBoolean());
136 parseByName(in.nextName(), edit, in, path, codec, resultCollection);
150 return ImmutableList.copyOf(resultCollection);
154 * Switch value of parsed JsonToken.NAME and read edit definition or patch id.
156 * @param name value of token
157 * @param edit PatchEdit instance
158 * @param in JsonReader reader
159 * @param path InstanceIdentifierContext context
160 * @param codec Draft11StringModuleInstanceIdentifierCodec codec
161 * @param resultCollection collection of parsed edits
162 * @throws IOException if operation fails
164 private void parseByName(@Nonnull final String name, @Nonnull final PatchEdit edit,
165 @Nonnull final JsonReader in, @Nonnull final InstanceIdentifierContext<?> path,
166 @Nonnull final StringModuleInstanceIdentifierCodec codec,
167 @Nonnull final List<PatchEntity> resultCollection) throws IOException {
170 if (in.peek() == JsonToken.BEGIN_ARRAY) {
173 while (in.hasNext()) {
174 readEditDefinition(edit, in, path, codec);
175 resultCollection.add(prepareEditOperation(edit));
181 readEditDefinition(edit, in, path, codec);
182 resultCollection.add(prepareEditOperation(edit));
188 this.patchId = in.nextString();
196 * Read one patch edit object from Json input.
198 * @param edit PatchEdit instance to be filled with read data
199 * @param in JsonReader reader
200 * @param path InstanceIdentifierContext path context
201 * @param codec Draft11StringModuleInstanceIdentifierCodec codec
202 * @throws IOException if operation fails
204 private void readEditDefinition(@Nonnull final PatchEdit edit, @Nonnull final JsonReader in,
205 @Nonnull final InstanceIdentifierContext<?> path,
206 @Nonnull final StringModuleInstanceIdentifierCodec codec) throws IOException {
207 String deferredValue = null;
210 while (in.hasNext()) {
211 final String editDefinition = in.nextName();
212 switch (editDefinition) {
214 edit.setId(in.nextString());
217 edit.setOperation(PatchEditOperation.valueOf(in.nextString().toUpperCase(Locale.ROOT)));
220 // target can be specified completely in request URI
221 final String target = in.nextString();
222 if (target.equals("/")) {
223 edit.setTarget(path.getInstanceIdentifier());
224 edit.setTargetSchemaNode(path.getSchemaContext());
226 edit.setTarget(codec.deserialize(codec.serialize(path.getInstanceIdentifier()).concat(target)));
227 edit.setTargetSchemaNode(SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(),
228 codec.getDataContextTree().getChild(edit.getTarget()).getDataSchemaNode().getPath()
234 Preconditions.checkArgument(edit.getData() == null && deferredValue == null,
235 "Multiple value entries found");
237 if (edit.getTargetSchemaNode() == null) {
238 final StringBuilder sb = new StringBuilder();
240 // save data defined in value node for next (later) processing, because target needs to be read
241 // always first and there is no ordering in Json input
242 readValueNode(sb, in);
243 deferredValue = sb.toString();
245 // We have a target schema node, reuse this reader without buffering the value.
246 edit.setData(readEditData(in, edit.getTargetSchemaNode(), path));
250 // FIXME: this does not look right, as it can wreck our logic
257 if (deferredValue != null) {
258 // read saved data to normalized node when target schema is already known
259 edit.setData(readEditData(new JsonReader(new StringReader(deferredValue)), edit.getTargetSchemaNode(),
265 * Parse data defined in value node and saves it to buffer.
266 * @param sb Buffer to read value node
267 * @param in JsonReader reader
268 * @throws IOException if operation fails
270 private void readValueNode(@Nonnull final StringBuilder sb, @Nonnull final JsonReader in) throws IOException {
273 sb.append("{\"").append(in.nextName()).append("\":");
280 while (in.hasNext()) {
281 if (in.peek() == JsonToken.STRING) {
282 sb.append('"').append(in.nextString()).append('"');
284 readValueObject(sb, in);
286 if (in.peek() != JsonToken.END_ARRAY) {
295 readValueObject(sb, in);
304 * Parse one value object of data and saves it to buffer.
305 * @param sb Buffer to read value object
306 * @param in JsonReader reader
307 * @throws IOException if operation fails
309 private void readValueObject(@Nonnull final StringBuilder sb, @Nonnull final JsonReader in) throws IOException {
310 // read simple leaf value
311 if (in.peek() == JsonToken.STRING) {
312 sb.append('"').append(in.nextString()).append('"');
319 while (in.hasNext()) {
320 sb.append('"').append(in.nextName()).append("\":");
324 sb.append('"').append(in.nextString()).append('"');
330 while (in.hasNext()) {
331 if (in.peek() == JsonToken.STRING) {
332 sb.append('"').append(in.nextString()).append('"');
334 readValueObject(sb, in);
337 if (in.peek() != JsonToken.END_ARRAY) {
346 readValueObject(sb, in);
349 if (in.peek() != JsonToken.END_OBJECT) {
359 * Read patch edit data defined in value node to NormalizedNode.
360 * @param in reader JsonReader reader
361 * @return NormalizedNode representing data
363 private static NormalizedNode<?, ?> readEditData(@Nonnull final JsonReader in,
364 @Nonnull final SchemaNode targetSchemaNode, @Nonnull final InstanceIdentifierContext<?> path) {
365 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
366 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
367 JsonParserStream.create(writer, JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()),
368 targetSchemaNode).parse(in);
370 return resultHolder.getResult();
374 * Prepare PatchEntity from PatchEdit instance when it satisfies conditions, otherwise throws exception.
375 * @param edit Instance of PatchEdit
376 * @return PatchEntity Patch entity
378 private static PatchEntity prepareEditOperation(@Nonnull final PatchEdit edit) {
379 if (edit.getOperation() != null && edit.getTargetSchemaNode() != null
380 && checkDataPresence(edit.getOperation(), edit.getData() != null)) {
381 if (!edit.getOperation().isWithValue()) {
382 return new PatchEntity(edit.getId(), edit.getOperation(), edit.getTarget());
385 // for lists allow to manipulate with list items through their parent
386 final YangInstanceIdentifier targetNode;
387 if (edit.getTarget().getLastPathArgument() instanceof NodeIdentifierWithPredicates) {
388 targetNode = edit.getTarget().getParent();
390 targetNode = edit.getTarget();
393 return new PatchEntity(edit.getId(), edit.getOperation(), targetNode, edit.getData());
396 throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
400 * Check if data is present when operation requires it and not present when operation data is not allowed.
401 * @param operation Name of operation
402 * @param hasData Data in edit are present/not present
403 * @return true if data is present when operation requires it or if there are no data when operation does not
404 * allow it, false otherwise
406 private static boolean checkDataPresence(@Nonnull final PatchEditOperation operation, final boolean hasData) {
407 return operation.isWithValue() == hasData;
411 * Helper class representing one patch edit.
413 private static final class PatchEdit {
415 private PatchEditOperation operation;
416 private YangInstanceIdentifier target;
417 private SchemaNode targetSchemaNode;
418 private NormalizedNode<?, ?> data;
424 void setId(final String id) {
425 this.id = Preconditions.checkNotNull(id);
428 PatchEditOperation getOperation() {
432 void setOperation(final PatchEditOperation operation) {
433 this.operation = Preconditions.checkNotNull(operation);
436 YangInstanceIdentifier getTarget() {
440 void setTarget(final YangInstanceIdentifier target) {
441 this.target = Preconditions.checkNotNull(target);
444 SchemaNode getTargetSchemaNode() {
445 return targetSchemaNode;
448 void setTargetSchemaNode(final SchemaNode targetSchemaNode) {
449 this.targetSchemaNode = Preconditions.checkNotNull(targetSchemaNode);
452 NormalizedNode<?, ?> getData() {
456 void setData(final NormalizedNode<?, ?> data) {
457 this.data = Preconditions.checkNotNull(data);
464 targetSchemaNode = null;