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
9 package org.opendaylight.restconf.jersey.providers;
11 import com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.OutputStreamWriter;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Type;
18 import java.nio.charset.StandardCharsets;
19 import java.util.List;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.WebApplicationException;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.MultivaluedMap;
25 import javax.ws.rs.ext.MessageBodyWriter;
26 import javax.ws.rs.ext.Provider;
27 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
28 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.restconf.Rfc8040;
31 import org.opendaylight.restconf.utils.RestconfConstants;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
40 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
42 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
43 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 @Produces({ Rfc8040.MediaTypes.DATA + RestconfConstants.JSON, MediaType.APPLICATION_JSON })
51 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
53 private static final int DEFAULT_INDENT_SPACES_NUM = 2;
56 public boolean isWriteable(final Class<?> type,
57 final Type genericType,
58 final Annotation[] annotations,
59 final MediaType mediaType) {
60 return type.equals(NormalizedNodeContext.class);
64 public long getSize(final NormalizedNodeContext context,
66 final Type genericType,
67 final Annotation[] annotations,
68 final MediaType mediaType) {
73 public void writeTo(final NormalizedNodeContext context,
75 final Type genericType,
76 final Annotation[] annotations,
77 final MediaType mediaType,
78 final MultivaluedMap<String, Object> httpHeaders,
79 final OutputStream entityStream) throws IOException, WebApplicationException {
80 final NormalizedNode<?, ?> data = context.getData();
85 @SuppressWarnings("unchecked")
86 final InstanceIdentifierContext<SchemaNode> identifierCtx =
87 (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
88 final SchemaPath path = identifierCtx.getSchemaNode().getPath();
89 final JsonWriter jsonWriter = createJsonWriter(entityStream,
90 context.getWriterParameters().isPrettyPrint());
92 jsonWriter.beginObject();
93 writeNormalizedNode(jsonWriter, path, identifierCtx, data,
94 context.getWriterParameters().getDepth(), context.getWriterParameters().getFields());
95 jsonWriter.endObject();
99 private static void writeNormalizedNode(final JsonWriter jsonWriter,
100 final SchemaPath path, final InstanceIdentifierContext<SchemaNode> context, final NormalizedNode<?, ?> data,
101 final Integer depth, final List<Set<QName>> fields) throws IOException {
102 final RestconfNormalizedNodeWriter nnWriter;
104 if (context.getSchemaNode() instanceof RpcDefinition) {
106 * RpcDefinition is not supported as initial codec in JSONStreamWriter,
107 * so we need to emit initial output declaration..
109 nnWriter = createNormalizedNodeWriter(
111 ((RpcDefinition) context.getSchemaNode()).getOutput().getPath(),
115 jsonWriter.name("output");
116 jsonWriter.beginObject();
117 writeChildren(nnWriter, (ContainerNode) data);
118 jsonWriter.endObject();
120 if (SchemaPath.ROOT.equals(path)) {
121 nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth, fields);
123 nnWriter = createNormalizedNodeWriter(context, path.getParent(), jsonWriter, depth, fields);
126 if (data instanceof MapEntryNode) {
127 // Restconf allows returning one list item. We need to wrap it
128 // in map node in order to serialize it properly
130 ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild((MapEntryNode) data).build());
132 nnWriter.write(data);
139 private static void writeChildren(final RestconfNormalizedNodeWriter nnWriter,
140 final ContainerNode data) throws IOException {
141 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
142 nnWriter.write(child);
146 private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(
147 final InstanceIdentifierContext<SchemaNode> context, final SchemaPath path, final JsonWriter jsonWriter,
148 final Integer depth, final List<Set<QName>> fields) {
150 final SchemaNode schema = context.getSchemaNode();
151 final JSONCodecFactory codecs = getCodecFactory(context);
154 if (schema instanceof DataSchemaNode
155 && !((DataSchemaNode)schema).isAugmenting()
156 && !(schema instanceof SchemaContext)) {
157 initialNs = schema.getQName().getNamespace();
158 } else if (schema instanceof RpcDefinition) {
159 initialNs = schema.getQName().getNamespace();
163 final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(
164 codecs, path, initialNs, jsonWriter);
165 return ParameterAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth, fields);
168 private static JsonWriter createJsonWriter(final OutputStream entityStream, final boolean prettyPrint) {
170 return JsonWriterFactory.createJsonWriter(
171 new OutputStreamWriter(entityStream, StandardCharsets.UTF_8), DEFAULT_INDENT_SPACES_NUM);
173 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
176 private static JSONCodecFactory getCodecFactory(final InstanceIdentifierContext<?> context) {
177 // TODO: Performance: Cache JSON Codec factory and schema context
178 return JSONCodecFactory.getShared(context.getSchemaContext());