Bug 2983 - POST operation to list only accepts data for keys
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / JsonNormalizedNodeBodyReader.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.rest.impl;
9
10 import com.google.common.collect.Iterables;
11 import com.google.gson.stream.JsonReader;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.ext.MessageBodyReader;
22 import javax.ws.rs.ext.Provider;
23 import org.opendaylight.controller.sal.rest.api.Draft02;
24 import org.opendaylight.controller.sal.rest.api.RestconfService;
25 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
26 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
27 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
28 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
30 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
39 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 @Provider
48 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
49         MediaType.APPLICATION_JSON })
50 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
51
52     private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
53
54     @Override
55     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
56             final MediaType mediaType) {
57         return true;
58     }
59
60     @Override
61     public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
62             final Annotation[] annotations, final MediaType mediaType,
63             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
64             WebApplicationException {
65         try {
66             final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
67             if (entityStream.available() < 1) {
68                 return new NormalizedNodeContext(path, null);
69             }
70             final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
71             final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
72
73             final SchemaNode parentSchema;
74             if(isPost()) {
75                 // FIXME: We need dispatch for RPC.
76                 parentSchema = path.getSchemaNode();
77             } else if(path.getSchemaNode() instanceof SchemaContext) {
78                 parentSchema = path.getSchemaContext();
79             } else {
80                 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
81                     parentSchema = path.getSchemaContext();
82                 } else {
83                     parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
84                 }
85             }
86
87             final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
88             final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
89             jsonParser.parse(reader);
90
91             NormalizedNode<?, ?> partialResult = resultHolder.getResult();
92             final NormalizedNode<?, ?> result;
93
94             // FIXME: Also II should be updated unwrap result from augmentation and choice nodes on PUT
95             if (!isPost()) {
96                 while (partialResult instanceof AugmentationNode || partialResult instanceof ChoiceNode) {
97                     final Object childNode = ((DataContainerNode) partialResult).getValue().iterator().next();
98                     partialResult = (NormalizedNode<?, ?>) childNode;
99                 }
100             }
101
102             if (partialResult instanceof MapNode && !isPost()) {
103                 result = Iterables.getOnlyElement(((MapNode) partialResult).getValue());
104             } else {
105                 result = partialResult;
106             }
107             return new NormalizedNodeContext(path,result);
108         } catch (final RestconfDocumentedException e) {
109             throw e;
110         } catch (final ResultAlreadySetException e) {
111             LOG.debug("Error parsing json input:", e);
112
113             throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. " +
114                     "Are you creating multiple resources/subresources in POST request?");
115         } catch (final Exception e) {
116             LOG.debug("Error parsing json input", e);
117
118             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
119                     ErrorTag.MALFORMED_MESSAGE);
120         }
121     }
122 }
123

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.