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.netconf.sal.rest.impl;
10 import com.google.common.base.Optional;
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.Map.Entry;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.WebApplicationException;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.MultivaluedMap;
24 import javax.ws.rs.ext.MessageBodyWriter;
25 import javax.ws.rs.ext.Provider;
26 import org.opendaylight.netconf.sal.rest.api.Draft02;
27 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
28 import org.opendaylight.netconf.sal.rest.api.RestconfService;
29 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
30 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
38 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
39 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
40 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
41 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
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;
49 * Normalized node writer for JSON.
51 * @deprecated This class will be replaced by NormalizedNodeJsonBodyWriter from restconf-nb-rfc8040
55 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
56 Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
57 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
59 private static final int DEFAULT_INDENT_SPACES_NUM = 2;
62 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
63 final MediaType mediaType) {
64 return type.equals(NormalizedNodeContext.class);
68 public long getSize(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
69 final Annotation[] annotations, final MediaType mediaType) {
74 public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
75 final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
76 final OutputStream entityStream) throws IOException, WebApplicationException {
77 if (httpHeaders != null) {
78 for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
79 httpHeaders.add(entry.getKey(), entry.getValue());
82 final NormalizedNode<?, ?> data = context.getData();
87 @SuppressWarnings("unchecked")
88 final InstanceIdentifierContext<SchemaNode> identifierCtx =
89 (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
91 final SchemaPath path = identifierCtx.getSchemaNode().getPath();
92 try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
93 jsonWriter.beginObject();
95 jsonWriter, path, identifierCtx, data,
96 Optional.fromNullable(context.getWriterParameters().getDepth()));
97 jsonWriter.endObject();
102 private static void writeNormalizedNode(final JsonWriter jsonWriter, SchemaPath path,
103 final InstanceIdentifierContext<SchemaNode> context, NormalizedNode<?, ?> data,
104 final Optional<Integer> depth) throws IOException {
105 final RestconfNormalizedNodeWriter nnWriter;
106 if (SchemaPath.ROOT.equals(path)) {
108 * Creates writer without initialNs and we write children of root data container
109 * which is not visible in restconf
111 nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
112 writeChildren(nnWriter,(ContainerNode) data);
113 } else if (context.getSchemaNode() instanceof RpcDefinition) {
115 * RpcDefinition is not supported as initial codec in JSONStreamWriter,
116 * so we need to emit initial output declaratation..
118 path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath();
119 nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
120 jsonWriter.name("output");
121 jsonWriter.beginObject();
122 writeChildren(nnWriter, (ContainerNode) data);
123 jsonWriter.endObject();
125 path = path.getParent();
127 if (data instanceof MapEntryNode) {
128 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild((MapEntryNode) data).build();
130 nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
131 nnWriter.write(data);
136 private static void writeChildren(final RestconfNormalizedNodeWriter nnWriter, final ContainerNode data)
138 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
139 nnWriter.write(child);
143 private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(
144 final InstanceIdentifierContext<SchemaNode> context, final SchemaPath path, final JsonWriter jsonWriter,
145 final Optional<Integer> depth) {
147 final SchemaNode schema = context.getSchemaNode();
148 final JSONCodecFactory codecs = getCodecFactory(context);
151 if (schema instanceof DataSchemaNode && !((DataSchemaNode)schema).isAugmenting()
152 && !(schema instanceof SchemaContext)) {
153 initialNs = schema.getQName().getNamespace();
154 } else if (schema instanceof RpcDefinition) {
155 initialNs = schema.getQName().getNamespace();
159 final NormalizedNodeStreamWriter streamWriter =
160 JSONNormalizedNodeStreamWriter.createNestedWriter(codecs, path, initialNs, jsonWriter);
161 if (depth.isPresent()) {
162 return DepthAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth.get());
165 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(streamWriter);
168 private static JsonWriter createJsonWriter(final OutputStream entityStream, final boolean prettyPrint) {
170 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8),
171 DEFAULT_INDENT_SPACES_NUM);
174 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
177 private static JSONCodecFactory getCodecFactory(final InstanceIdentifierContext<?> context) {
178 // TODO: Performance: Cache JSON Codec factory and schema context
179 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext());