Merge "Bug 2260: Reduce overhead of unused TransactionProxy instances"
[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.SchemaNode;
37 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @Provider
42 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
43         MediaType.APPLICATION_JSON })
44 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
45
46     private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
47
48     @Override
49     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
50             final MediaType mediaType) {
51         return true;
52     }
53
54     @Override
55     public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
56             final Annotation[] annotations, final MediaType mediaType,
57             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
58             WebApplicationException {
59         try {
60             final InstanceIdentifierContext<?> path = getIdentifierWithSchema().get();
61             final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
62             final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
63
64             final SchemaNode parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
65             final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
66             final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
67             jsonParser.parse(reader);
68
69             final NormalizedNode<?, ?> partialResult = resultHolder.getResult();
70             final NormalizedNode<?, ?> result;
71             if(partialResult instanceof MapNode) {
72
73                 result = Iterables.getOnlyElement(((MapNode) partialResult).getValue());
74             } else {
75                 result = partialResult;
76             }
77             return new NormalizedNodeContext(path,result);
78         } catch (final Exception e) {
79             LOG.debug("Error parsing json input", e);
80
81             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
82                     ErrorTag.MALFORMED_MESSAGE);
83         }
84     }
85 }
86