Merge "BUG-2314 Expose inventory mountpoint on proper path"
[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.MapNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 @Provider
44 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
45         MediaType.APPLICATION_JSON })
46 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
47
48     private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
49
50     @Override
51     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
52             final MediaType mediaType) {
53         return true;
54     }
55
56     @Override
57     public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
58             final Annotation[] annotations, final MediaType mediaType,
59             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
60             WebApplicationException {
61         try {
62             final InstanceIdentifierContext<?> path = getIdentifierWithSchema().get();
63             if (entityStream.available() < 1) {
64                 return new NormalizedNodeContext(path, null);
65             }
66             final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
67             final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
68
69             final SchemaNode parentSchema;
70             if(isPost()) {
71                 // FIXME: We need dispatch for RPC.
72                 parentSchema = path.getSchemaNode();
73             } else if(path.getSchemaNode() instanceof SchemaContext) {
74                 parentSchema = path.getSchemaContext();
75             } else {
76                 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
77                     parentSchema = path.getSchemaContext();
78                 } else {
79                     parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
80                 }
81             }
82
83             final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
84             final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
85             jsonParser.parse(reader);
86
87             final NormalizedNode<?, ?> partialResult = resultHolder.getResult();
88             final NormalizedNode<?, ?> result;
89             if(partialResult instanceof MapNode) {
90                 result = Iterables.getOnlyElement(((MapNode) partialResult).getValue());
91             } else {
92                 result = partialResult;
93             }
94             return new NormalizedNodeContext(path,result);
95         } catch (final Exception e) {
96             LOG.debug("Error parsing json input", e);
97
98             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
99                     ErrorTag.MALFORMED_MESSAGE);
100         }
101     }
102 }
103