2 * Copyright (c) 2016 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.restconf.jersey.providers;
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.core.Request;
24 import javax.ws.rs.core.UriInfo;
25 import javax.ws.rs.ext.MessageBodyReader;
26 import javax.ws.rs.ext.Provider;
27 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
28 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
32 import org.opendaylight.restconf.Draft17;
33 import org.opendaylight.restconf.utils.RestconfConstants;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
44 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 @Consumes({ Draft17.MediaTypes.DATA + RestconfConstants.JSON, MediaType.APPLICATION_JSON })
55 public class JsonNormalizedNodeBodyReader extends AbstractIdentifierAwareJaxRsProvider implements MessageBodyReader<NormalizedNodeContext> {
57 private final static Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
60 public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations,
61 final MediaType mediaType) {
66 public NormalizedNodeContext readFrom(final Class<NormalizedNodeContext> type, final Type genericType,
67 final Annotation[] annotations, final MediaType mediaType,
68 final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException,
69 WebApplicationException {
71 return readFrom(getInstanceIdentifierContext(), entityStream, isPost());
72 } catch (final Exception e) {
73 propagateExceptionAs(e);
78 private static void propagateExceptionAs(final Exception e) throws RestconfDocumentedException {
79 if(e instanceof RestconfDocumentedException) {
80 throw (RestconfDocumentedException)e;
83 if(e instanceof ResultAlreadySetException) {
84 LOG.debug("Error parsing json input:", e);
86 throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. " +
87 "Are you creating multiple resources/subresources in POST request?");
90 LOG.debug("Error parsing json input", e);
92 throw new RestconfDocumentedException("Error parsing input: " + e.getMessage(), ErrorType.PROTOCOL,
93 ErrorTag.MALFORMED_MESSAGE, e);
96 private static NormalizedNodeContext readFrom(final InstanceIdentifierContext<?> path, final InputStream entityStream,
97 final boolean isPost) throws IOException {
98 if (entityStream.available() < 1) {
99 return new NormalizedNodeContext(path, null);
101 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
102 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
104 final SchemaNode parentSchema;
106 parentSchema = path.getSchemaNode();
107 } else if(path.getSchemaNode() instanceof SchemaContext) {
108 parentSchema = path.getSchemaContext();
110 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
111 parentSchema = path.getSchemaContext();
113 parentSchema = SchemaContextUtil.findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
117 final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
118 final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
119 jsonParser.parse(reader);
121 NormalizedNode<?, ?> result = resultHolder.getResult();
122 final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
123 InstanceIdentifierContext<? extends SchemaNode> newIIContext;
125 while ((result instanceof AugmentationNode) || (result instanceof ChoiceNode)) {
126 final Object childNode = ((DataContainerNode) result).getValue().iterator().next();
128 iiToDataList.add(result.getIdentifier());
130 result = (NormalizedNode<?, ?>) childNode;
134 if (result instanceof MapEntryNode) {
135 iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getNodeType()));
136 iiToDataList.add(result.getIdentifier());
138 iiToDataList.add(result.getIdentifier());
141 if (result instanceof MapNode) {
142 result = Iterables.getOnlyElement(((MapNode) result).getValue());
146 final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
147 path.getInstanceIdentifier().getPathArguments(), iiToDataList));
149 newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(),
150 path.getSchemaContext());
152 return new NormalizedNodeContext(newIIContext, result);
155 public void injectParams(final UriInfo uriInfo, final Request request) {