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.util.ArrayList;
16 import java.util.List;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.ext.Provider;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
24 import org.opendaylight.restconf.Rfc8040;
25 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.restconf.utils.RestconfConstants;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
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.data.impl.schema.ResultAlreadySetException;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 @Consumes({ Rfc8040.MediaTypes.DATA + RestconfConstants.JSON, MediaType.APPLICATION_JSON })
49 public class JsonNormalizedNodeBodyReader extends AbstractNormalizedNodeBodyReader {
51 private static final Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
53 @SuppressWarnings("checkstyle:IllegalCatch")
55 protected NormalizedNodeContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
56 throws IOException, WebApplicationException {
58 return readFrom(path, entityStream, isPost());
59 } catch (final Exception e) {
60 propagateExceptionAs(e);
65 public static NormalizedNodeContext readFrom(
66 final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost)
68 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
69 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
71 final SchemaNode parentSchema;
73 parentSchema = path.getSchemaNode();
74 } else if (path.getSchemaNode() instanceof SchemaContext) {
75 parentSchema = path.getSchemaContext();
77 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
78 parentSchema = path.getSchemaContext();
80 parentSchema = SchemaContextUtil
81 .findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
85 final JsonParserStream jsonParser = JsonParserStream.create(writer, path.getSchemaContext(), parentSchema);
86 final JsonReader reader = new JsonReader(new InputStreamReader(entityStream));
87 jsonParser.parse(reader);
89 NormalizedNode<?, ?> result = resultHolder.getResult();
90 final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
91 InstanceIdentifierContext<? extends SchemaNode> newIIContext;
93 while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
94 final Object childNode = ((DataContainerNode<?>) result).getValue().iterator().next();
96 iiToDataList.add(result.getIdentifier());
98 result = (NormalizedNode<?, ?>) childNode;
102 if (result instanceof MapEntryNode) {
103 iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getNodeType()));
104 iiToDataList.add(result.getIdentifier());
106 iiToDataList.add(result.getIdentifier());
109 if (result instanceof MapNode) {
110 result = Iterables.getOnlyElement(((MapNode) result).getValue());
114 final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
115 path.getInstanceIdentifier().getPathArguments(), iiToDataList));
117 newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(),
118 path.getSchemaContext());
120 return new NormalizedNodeContext(newIIContext, result);
123 private static void propagateExceptionAs(final Exception exception) throws RestconfDocumentedException {
124 if (exception instanceof RestconfDocumentedException) {
125 throw (RestconfDocumentedException)exception;
128 if (exception instanceof ResultAlreadySetException) {
129 LOG.debug("Error parsing json input:", exception);
131 throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. "
132 + "Are you creating multiple resources/subresources in POST request?", exception);
135 LOG.debug("Error parsing json input", exception);
137 throw new RestconfDocumentedException("Error parsing input: " + exception.getMessage(), ErrorType.PROTOCOL,
138 ErrorTag.MALFORMED_MESSAGE, exception);