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 java.util.ArrayList;
18 import java.util.List;
19 import javax.ws.rs.Consumes;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.MessageBodyReader;
24 import javax.ws.rs.ext.Provider;
25 import org.opendaylight.controller.sal.rest.api.Draft02;
26 import org.opendaylight.controller.sal.rest.api.RestconfService;
27 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
28 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
40 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 @Consumes({ Draft02.MediaTypes.DATA + RestconfService.JSON, Draft02.MediaTypes.OPERATION + RestconfService.JSON,
53 MediaType.APPLICATION_JSON })
54 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
56 private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
59 public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
60 final MediaType mediaType) {
65 public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
66 final Annotation[] annotations, final MediaType mediaType,
67 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
68 WebApplicationException {
70 final InstanceIdentifierContext<?> path = getInstanceIdentifierContext();
71 if (entityStream.available() < 1) {
72 return new NormalizedNodeContext(path, null);
74 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
75 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
77 final SchemaNode parentSchema;
79 // FIXME: We need dispatch for RPC.
80 parentSchema = path.getSchemaNode();
81 } else if(path.getSchemaNode() instanceof SchemaContext) {
82 parentSchema = path.getSchemaContext();
84 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
85 parentSchema = path.getSchemaContext();
87 parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
91 final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
92 final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
93 jsonParser.parse(reader);
95 NormalizedNode<?, ?> result = resultHolder.getResult();
96 final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
97 InstanceIdentifierContext<? extends SchemaNode> newIIContext;
100 while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
101 final Object childNode = ((DataContainerNode) result).getValue().iterator().next();
102 iiToDataList.add(result.getIdentifier());
103 result = (NormalizedNode<?, ?>) childNode;
105 if (result instanceof MapEntryNode) {
106 iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getNodeType()));
107 iiToDataList.add(result.getIdentifier());
109 iiToDataList.add(result.getIdentifier());
112 if (result instanceof MapNode) {
113 result = Iterables.getOnlyElement(((MapNode) result).getValue());
117 final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
118 path.getInstanceIdentifier().getPathArguments(), iiToDataList));
120 newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(),
121 path.getSchemaContext());
123 return new NormalizedNodeContext(newIIContext, result);
124 } catch (final RestconfDocumentedException e) {
126 } catch (final ResultAlreadySetException e) {
127 LOG.debug("Error parsing json input:", e);
129 throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. " +
130 "Are you creating multiple resources/subresources in POST request?");
131 } catch (final Exception e) {
132 LOG.debug("Error parsing json input", e);
134 throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
135 ErrorTag.MALFORMED_MESSAGE);