Merge "Include JMX Counters and resetTransactionCounters"
[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.base.Optional;
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.stream.NormalizedNodeStreamWriter;
31 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
33 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Provider
38 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
39         MediaType.APPLICATION_JSON })
40 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
41
42     private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
43
44     @Override
45     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
46             final MediaType mediaType) {
47         return true;
48     }
49
50     @Override
51     public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
52             final Annotation[] annotations, final MediaType mediaType,
53             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
54             WebApplicationException {
55         try {
56             Optional<InstanceIdentifierContext> path = getIdentifierWithSchema();
57             NormalizedNodeResult resultHolder = new NormalizedNodeResult();
58             NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
59             JsonParserStream jsonParser = JsonParserStream.create(writer, path.get().getSchemaContext());
60             JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
61             jsonParser.parse(reader);
62             return new NormalizedNodeContext(path.get(),resultHolder.getResult());
63         } catch (Exception e) {
64             LOG.debug("Error parsing json input", e);
65
66             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
67                     ErrorTag.MALFORMED_MESSAGE);
68         }
69     }
70 }
71