2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.rest.impl;
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;
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> {
51 private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
54 public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
55 final MediaType mediaType) {
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 {
65 final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
66 if (entityStream.available() < 1) {
67 return new NormalizedNodeContext(path, null);
69 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
70 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
72 final SchemaNode parentSchema;
74 // FIXME: We need dispatch for RPC.
75 parentSchema = path.getSchemaNode();
76 } else if(path.getSchemaNode() instanceof SchemaContext) {
77 parentSchema = path.getSchemaContext();
79 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
80 parentSchema = path.getSchemaContext();
82 parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
86 final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
87 final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
88 jsonParser.parse(reader);
90 NormalizedNode<?, ?> partialResult = resultHolder.getResult();
91 final NormalizedNode<?, ?> result;
93 // FIXME: Also II should be updated unwrap result from augmentation and choice nodes on PUT
95 while (partialResult instanceof AugmentationNode || partialResult instanceof ChoiceNode) {
96 final Object childNode = ((DataContainerNode) partialResult).getValue().iterator().next();
97 partialResult = (NormalizedNode<?, ?>) childNode;
101 if (partialResult instanceof MapNode && !isPost()) {
102 result = Iterables.getOnlyElement(((MapNode) partialResult).getValue());
104 result = partialResult;
106 return new NormalizedNodeContext(path,result);
107 } catch (final Exception e) {
108 LOG.debug("Error parsing json input", e);
110 throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
111 ErrorTag.MALFORMED_MESSAGE);