3be247a3bbdc71a911570d431dc679c86e1222d4
[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.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 @Provider
47 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
48         MediaType.APPLICATION_JSON })
49 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
50
51     private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
52
53     @Override
54     public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
55             final MediaType mediaType) {
56         return true;
57     }
58
59     @Override
60     public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
61             final Annotation[] annotations, final MediaType mediaType,
62             final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
63             WebApplicationException {
64         try {
65             final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
66             if (entityStream.available() < 1) {
67                 return new NormalizedNodeContext(path, null);
68             }
69             final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
70             final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
71
72             final SchemaNode parentSchema;
73             if(isPost()) {
74                 // FIXME: We need dispatch for RPC.
75                 parentSchema = path.getSchemaNode();
76             } else if(path.getSchemaNode() instanceof SchemaContext) {
77                 parentSchema = path.getSchemaContext();
78             } else {
79                 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
80                     parentSchema = path.getSchemaContext();
81                 } else {
82                     parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
83                 }
84             }
85
86             final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
87             final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
88             jsonParser.parse(reader);
89
90             NormalizedNode<?, ?> partialResult = resultHolder.getResult();
91             final NormalizedNode<?, ?> result;
92
93             // FIXME: Also II should be updated unwrap result from augmentation and choice nodes on PUT
94             if (!isPost()) {
95                 while (partialResult instanceof AugmentationNode || partialResult instanceof ChoiceNode) {
96                     final Object childNode = ((DataContainerNode) partialResult).getValue().iterator().next();
97                     partialResult = (NormalizedNode<?, ?>) childNode;
98                 }
99             }
100
101             if (partialResult instanceof MapNode && !isPost()) {
102                 result = Iterables.getOnlyElement(((MapNode) partialResult).getValue());
103             } else {
104                 result = partialResult;
105             }
106             return new NormalizedNodeContext(path,result);
107         } catch (final RestconfDocumentedException e) {
108             throw e;
109         } catch (final Exception e) {
110             LOG.debug("Error parsing json input", e);
111
112             throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
113                     ErrorTag.MALFORMED_MESSAGE);
114         }
115     }
116 }
117

©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.