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.nb.rfc8040.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.nio.charset.StandardCharsets;
16 import java.util.ArrayList;
17 import java.util.List;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.ext.Provider;
22 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
23 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
26 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
27 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
28 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractNormalizedNodeBodyReader;
29 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
39 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
42 import org.opendaylight.yangtools.yang.data.impl.schema.ResultAlreadySetException;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 @Consumes({ Rfc8040.MediaTypes.DATA + RestconfConstants.JSON, MediaType.APPLICATION_JSON })
52 public class JsonNormalizedNodeBodyReader extends AbstractNormalizedNodeBodyReader {
54 private static final Logger LOG = LoggerFactory.getLogger(JsonNormalizedNodeBodyReader.class);
56 @SuppressWarnings("checkstyle:IllegalCatch")
58 protected NormalizedNodeContext readBody(final InstanceIdentifierContext<?> path, final InputStream entityStream)
59 throws IOException, WebApplicationException {
61 return readFrom(path, entityStream, isPost());
62 } catch (final Exception e) {
63 propagateExceptionAs(e);
68 public static NormalizedNodeContext readFrom(
69 final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost)
71 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
72 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
74 final SchemaNode parentSchema;
76 parentSchema = path.getSchemaNode();
77 } else if (path.getSchemaNode() instanceof SchemaContext) {
78 parentSchema = path.getSchemaContext();
80 if (SchemaPath.ROOT.equals(path.getSchemaNode().getPath().getParent())) {
81 parentSchema = path.getSchemaContext();
83 parentSchema = SchemaContextUtil
84 .findDataSchemaNode(path.getSchemaContext(), path.getSchemaNode().getPath().getParent());
88 final JsonParserStream jsonParser = JsonParserStream.create(writer,
89 JSONCodecFactorySupplier.RFC7951.getShared(path.getSchemaContext()), parentSchema);
91 final JsonReader reader = new JsonReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8));
92 jsonParser.parse(reader);
94 NormalizedNode<?, ?> result = resultHolder.getResult();
95 final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
96 InstanceIdentifierContext<? extends SchemaNode> newIIContext;
98 while (result instanceof AugmentationNode || result instanceof ChoiceNode) {
99 final Object childNode = ((DataContainerNode<?>) result).getValue().iterator().next();
101 iiToDataList.add(result.getIdentifier());
103 result = (NormalizedNode<?, ?>) childNode;
107 if (result instanceof MapEntryNode) {
108 iiToDataList.add(new YangInstanceIdentifier.NodeIdentifier(result.getNodeType()));
109 iiToDataList.add(result.getIdentifier());
111 iiToDataList.add(result.getIdentifier());
114 if (result instanceof MapNode) {
115 result = Iterables.getOnlyElement(((MapNode) result).getValue());
119 final YangInstanceIdentifier fullIIToData = YangInstanceIdentifier.create(Iterables.concat(
120 path.getInstanceIdentifier().getPathArguments(), iiToDataList));
122 newIIContext = new InstanceIdentifierContext<>(fullIIToData, path.getSchemaNode(), path.getMountPoint(),
123 path.getSchemaContext());
125 return new NormalizedNodeContext(newIIContext, result);
128 private static void propagateExceptionAs(final Exception exception) throws RestconfDocumentedException {
129 if (exception instanceof RestconfDocumentedException) {
130 throw (RestconfDocumentedException)exception;
133 if (exception instanceof ResultAlreadySetException) {
134 LOG.debug("Error parsing json input:", exception);
136 throw new RestconfDocumentedException("Error parsing json input: Failed to create new parse result data. "
137 + "Are you creating multiple resources/subresources in POST request?", exception);
140 LOG.debug("Error parsing json input", exception);
142 throw new RestconfDocumentedException("Error parsing input: " + exception.getMessage(), ErrorType.PROTOCOL,
143 ErrorTag.MALFORMED_MESSAGE, exception);