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