Bug 5730 - Delete subset of list items using PATCH?
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / rest / impl / JsonToPATCHBodyReader.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.sal.rest.impl;
10
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.lang.annotation.Annotation;
19 import java.lang.reflect.Type;
20 import java.util.ArrayList;
21 import java.util.List;
22 import javax.annotation.Nonnull;
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.WebApplicationException;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.MultivaluedMap;
27 import javax.ws.rs.ext.MessageBodyReader;
28 import javax.ws.rs.ext.Provider;
29 import org.opendaylight.netconf.sal.rest.api.Draft02;
30 import org.opendaylight.netconf.sal.rest.api.RestconfService;
31 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
33 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
34 import org.opendaylight.netconf.sal.restconf.impl.PATCHEditOperation;
35 import org.opendaylight.netconf.sal.restconf.impl.PATCHEntity;
36 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
37 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
38 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
44 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
46 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
47 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 @Provider
52 @Consumes({Draft02.MediaTypes.PATCH + RestconfService.JSON})
53 public class JsonToPATCHBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<PATCHContext> {
54
55     private final static Logger LOG = LoggerFactory.getLogger(JsonToPATCHBodyReader.class);
56     private String patchId;
57
58     @Override
59     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
60         return true;
61     }
62
63     @Override
64     public PATCHContext readFrom(Class<PATCHContext> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
65         try {
66             return readFrom(getInstanceIdentifierContext(), entityStream);
67         } catch (final Exception e) {
68             throw propagateExceptionAs(e);
69         }
70     }
71
72     private static RuntimeException propagateExceptionAs(Exception e) throws RestconfDocumentedException {
73         if(e instanceof RestconfDocumentedException) {
74             throw (RestconfDocumentedException)e;
75         }
76
77         if(e instanceof ResultAlreadySetException) {
78             LOG.debug("Error parsing json input:", e);
79
80             throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. ");
81         }
82
83         throw new RestconfDocumentedException("Error parsing json input: " + e.getMessage(), ErrorType.PROTOCOL,
84                 ErrorTag.MALFORMED_MESSAGE, e);
85     }
86
87     public PATCHContext readFrom(final String uriPath, final InputStream entityStream) throws
88             RestconfDocumentedException {
89         try {
90             return readFrom(ControllerContext.getInstance().toInstanceIdentifier(uriPath), entityStream);
91         } catch (final Exception e) {
92             propagateExceptionAs(e);
93             return null; // no-op
94         }
95     }
96
97     private PATCHContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream) throws IOException {
98         if (entityStream.available() < 1) {
99             return new PATCHContext(path, null, null);
100         }
101
102         final JsonReader jsonReader = new JsonReader(new InputStreamReader(entityStream));
103         final List<PATCHEntity> resultList = read(jsonReader, path);
104         jsonReader.close();
105
106         return new PATCHContext(path, resultList, patchId);
107     }
108
109     private List<PATCHEntity> read(final JsonReader in, InstanceIdentifierContext path) throws IOException {
110         List<PATCHEntity> resultCollection = new ArrayList<>();
111         StringModuleInstanceIdentifierCodec codec = new StringModuleInstanceIdentifierCodec(path.getSchemaContext());
112         JsonToPATCHBodyReader.PatchEdit edit = new JsonToPATCHBodyReader.PatchEdit();
113
114         while (in.hasNext()) {
115             switch (in.peek()) {
116                 case STRING:
117                 case NUMBER:
118                     in.nextString();
119                     break;
120                 case BOOLEAN:
121                     Boolean.toString(in.nextBoolean());
122                     break;
123                 case NULL:
124                     in.nextNull();
125                     break;
126                 case BEGIN_ARRAY:
127                     in.beginArray();
128                     break;
129                 case BEGIN_OBJECT:
130                     in.beginObject();
131                     break;
132                 case END_DOCUMENT:
133                     break;
134                 case NAME:
135                     parseByName(in.nextName(), edit, in, path, codec, resultCollection);
136                     break;
137                 case END_OBJECT:
138                     in.endObject();
139                     break;
140                 case END_ARRAY:
141                     in.endArray();
142                     break;
143
144                 default:
145                     break;
146             }
147         }
148
149         return ImmutableList.copyOf(resultCollection);
150     }
151
152     /**
153      * Switch value of parsed JsonToken.NAME and read edit definition or patch id
154      * @param name value of token
155      * @param edit PatchEdit instance
156      * @param in JsonReader reader
157      * @param path InstanceIdentifierContext context
158      * @param codec StringModuleInstanceIdentifierCodec codec
159      * @param resultCollection collection of parsed edits
160      * @throws IOException
161      */
162     private void parseByName(@Nonnull final String name, @Nonnull final PatchEdit edit,
163                              @Nonnull final JsonReader in, @Nonnull final InstanceIdentifierContext path,
164                              @Nonnull final StringModuleInstanceIdentifierCodec codec,
165                              @Nonnull final List<PATCHEntity> resultCollection) throws IOException {
166         switch (name) {
167             case "edit" :
168                 if (in.peek() == JsonToken.BEGIN_ARRAY) {
169                     in.beginArray();
170
171                     while (in.hasNext()) {
172                         readEditDefinition(edit, in, path, codec);
173                         resultCollection.add(prepareEditOperation(edit));
174                         edit.clear();
175                     }
176
177                     in.endArray();
178                 } else {
179                     readEditDefinition(edit, in, path, codec);
180                     resultCollection.add(prepareEditOperation(edit));
181                     edit.clear();
182                 }
183
184                 break;
185             case "patch-id" :
186                 this.patchId = in.nextString();
187                 break;
188             default:
189                 break;
190         }
191     }
192
193     /**
194      * Read one patch edit object from Json input
195      * @param edit PatchEdit instance to be filled with read data
196      * @param in JsonReader reader
197      * @param path InstanceIdentifierContext path context
198      * @param codec StringModuleInstanceIdentifierCodec codec
199      * @throws IOException
200      */
201     private void readEditDefinition(@Nonnull final PatchEdit edit, @Nonnull final JsonReader in,
202                                     @Nonnull final InstanceIdentifierContext path,
203                                     @Nonnull final StringModuleInstanceIdentifierCodec codec) throws IOException {
204         StringBuffer value = new StringBuffer();
205         in.beginObject();
206
207         while (in.hasNext()) {
208             final String editDefinition = in.nextName();
209             switch (editDefinition) {
210                 case "edit-id" :
211                     edit.setId(in.nextString());
212                     break;
213                 case "operation" :
214                     edit.setOperation(in.nextString());
215                     break;
216                 case "target" :
217                     edit.setTarget(codec.deserialize(codec.serialize(path.getInstanceIdentifier()) + in.nextString()));
218                     edit.setTargetSchemaNode(SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(),
219                             codec.getDataContextTree().getChild(edit.getTarget()).getDataSchemaNode().getPath()
220                                     .getParent()));
221                     break;
222                 case "value" :
223                     // save data defined in value node for next (later) processing, because target needs to be read
224                     // always first and there is no ordering in Json input
225                     readValueNode(value, in);
226                     break;
227                 default:
228                     break;
229             }
230         }
231
232         in.endObject();
233
234         // read saved data to normalized node when target schema is already known
235         edit.setData(readEditData(new JsonReader(new StringReader(value.toString())), edit.getTargetSchemaNode(), path));
236     }
237
238     /**
239      * Parse data defined in value node and saves it to buffer
240      * @param value Buffer to read value node
241      * @param in JsonReader reader
242      * @throws IOException
243      */
244     private void readValueNode(@Nonnull final StringBuffer value, @Nonnull final JsonReader in) throws IOException {
245         in.beginObject();
246         value.append("{");
247
248         value.append("\"" + in.nextName() + "\"" + ":");
249
250         if (in.peek() == JsonToken.BEGIN_ARRAY) {
251             in.beginArray();
252             value.append("[");
253
254             while (in.hasNext()) {
255                 readValueObject(value, in);
256                 if (in.peek() != JsonToken.END_ARRAY) {
257                     value.append(",");
258                 }
259             }
260
261             in.endArray();
262             value.append("]");
263         } else {
264             readValueObject(value, in);
265         }
266
267         in.endObject();
268         value.append("}");
269     }
270
271     /**
272      * Parse one value object of data and saves it to buffer
273      * @param value Buffer to read value object
274      * @param in JsonReader reader
275      * @throws IOException
276      */
277     private void readValueObject(@Nonnull final StringBuffer value, @Nonnull final JsonReader in) throws IOException {
278         in.beginObject();
279         value.append("{");
280
281         while (in.hasNext()) {
282             value.append("\"" + in.nextName() + "\"");
283             value.append(":");
284             value.append("\"" + in.nextString() + "\"");
285             if (in.peek() != JsonToken.END_OBJECT) {
286                 value.append(",");
287             }
288         }
289
290         in.endObject();
291         value.append("}");
292     }
293
294     /**
295      * Read patch edit data defined in value node to NormalizedNode
296      * @param in reader JsonReader reader
297      * @return NormalizedNode representing data
298      */
299     private NormalizedNode readEditData(@Nonnull final JsonReader in, @Nonnull SchemaNode targetSchemaNode,
300                                         @Nonnull InstanceIdentifierContext path) {
301         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
302         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
303         JsonParserStream.create(writer, path.getSchemaContext(), targetSchemaNode).parse(in);
304
305         return resultHolder.getResult();
306     }
307
308     /**
309      * Prepare PATCHEntity from PatchEdit instance when it satisfies conditions, otherwise throws exception
310      * @param edit Instance of PatchEdit
311      * @return PATCHEntity
312      */
313     private PATCHEntity prepareEditOperation(@Nonnull final PatchEdit edit) {
314         if (edit.getOperation() != null && edit.getTargetSchemaNode() != null
315                 && checkDataPresence(edit.getOperation(), (edit.getData() != null))) {
316             if (isPatchOperationWithValue(edit.getOperation())) {
317                 return new PATCHEntity(edit.getId(), edit.getOperation(), edit.getTarget().getParent(), edit.getData());
318             } else {
319                 return new PATCHEntity(edit.getId(), edit.getOperation(), edit.getTarget());
320             }
321         }
322
323         throw new RestconfDocumentedException("Error parsing input", ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE);
324     }
325
326     /**
327      * Check if data is present when operation requires it and not present when operation data is not allowed
328      * @param operation Name of operation
329      * @param hasData Data in edit are present/not present
330      * @return true if data is present when operation requires it or if there are no data when operation does not
331      * allow it, false otherwise
332      */
333     private boolean checkDataPresence(@Nonnull final String operation, final boolean hasData) {
334         if (isPatchOperationWithValue(operation)) {
335             if (hasData) {
336                 return true;
337             } else {
338                 return false;
339             }
340         } else  {
341             if (!hasData) {
342                 return true;
343             } else {
344                 return false;
345             }
346         }
347     }
348
349     /**
350      * Check if operation requires data to be specified
351      * @param operation Name of the operation to be checked
352      * @return true if operation requires data, false otherwise
353      */
354     private boolean isPatchOperationWithValue(@Nonnull final String operation) {
355         switch (PATCHEditOperation.valueOf(operation.toUpperCase())) {
356             case CREATE:
357             case MERGE:
358             case REPLACE:
359             case INSERT:
360                 return true;
361             default:
362                 return false;
363         }
364     }
365
366     /**
367      * Helper class representing one patch edit
368      */
369     private static final class PatchEdit {
370         private String id;
371         private String operation;
372         private YangInstanceIdentifier target;
373         private SchemaNode targetSchemaNode;
374         private NormalizedNode data;
375
376         public String getId() {
377             return id;
378         }
379
380         public void setId(String id) {
381             this.id = id;
382         }
383
384         public String getOperation() {
385             return operation;
386         }
387
388         public void setOperation(String operation) {
389             this.operation = operation;
390         }
391
392         public YangInstanceIdentifier getTarget() {
393             return target;
394         }
395
396         public void setTarget(YangInstanceIdentifier target) {
397             this.target = target;
398         }
399
400         public SchemaNode getTargetSchemaNode() {
401             return targetSchemaNode;
402         }
403
404         public void setTargetSchemaNode(SchemaNode targetSchemaNode) {
405             this.targetSchemaNode = targetSchemaNode;
406         }
407
408         public NormalizedNode getData() {
409             return data;
410         }
411
412         public void setData(NormalizedNode data) {
413             this.data = data;
414         }
415
416         public void clear() {
417             id = null;
418             operation = null;
419             target = null;
420             targetSchemaNode = null;
421             data = null;
422         }
423     }
424 }