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.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.io.OutputStreamWriter;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Type;
17 import java.net.URISyntaxException;
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 javax.xml.stream.XMLStreamException;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.opendaylight.netconf.sal.rest.api.Draft02;
29 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
30 import org.opendaylight.netconf.sal.rest.api.RestconfService;
31 import org.opendaylight.netconf.util.NetconfUtil;
32 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
33 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
41 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
42 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
43 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
44 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
46 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
51 import org.xml.sax.SAXException;
54 * Normalized node writer for JSON.
56 * @deprecated This class will be replaced by NormalizedNodeJsonBodyWriter from restconf-nb-rfc8040
61 Draft02.MediaTypes.API + RestconfService.JSON,
62 Draft02.MediaTypes.DATA + RestconfService.JSON,
63 Draft02.MediaTypes.OPERATION + RestconfService.JSON,
64 MediaType.APPLICATION_JSON
66 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
68 private static final int DEFAULT_INDENT_SPACES_NUM = 2;
71 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
72 final MediaType mediaType) {
73 return type.equals(NormalizedNodeContext.class);
77 public long getSize(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
78 final Annotation[] annotations, final MediaType mediaType) {
83 public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
84 final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
85 final OutputStream entityStream) throws IOException, WebApplicationException {
86 if (httpHeaders != null) {
87 for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
88 httpHeaders.add(entry.getKey(), entry.getValue());
91 NormalizedNode<?, ?> data = context.getData();
96 @SuppressWarnings("unchecked")
97 final InstanceIdentifierContext<SchemaNode> identifierCtx =
98 (InstanceIdentifierContext<SchemaNode>) context.getInstanceIdentifierContext();
100 final SchemaPath path = identifierCtx.getSchemaNode().getPath();
101 try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
102 jsonWriter.beginObject();
104 jsonWriter, path, identifierCtx, data, context.getWriterParameters().getDepth());
105 jsonWriter.endObject();
110 private static void writeNormalizedNode(final JsonWriter jsonWriter, SchemaPath path,
111 final InstanceIdentifierContext<SchemaNode> context, NormalizedNode<?, ?> data,
112 final @Nullable Integer depth) throws IOException {
113 final RestconfNormalizedNodeWriter nnWriter;
114 if (SchemaPath.ROOT.equals(path)) {
116 * Creates writer without initialNs and we write children of root data container
117 * which is not visible in restconf
119 nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
120 if (data instanceof ContainerNode) {
121 writeChildren(nnWriter,(ContainerNode) data);
122 } else if (data instanceof DOMSourceAnyxmlNode) {
124 writeChildren(nnWriter,
125 (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(
126 context.getSchemaContext(), ((DOMSourceAnyxmlNode)data).getValue()).getResult());
127 } catch (XMLStreamException | URISyntaxException | SAXException e) {
128 throw new IOException("Cannot write anyxml.", e);
131 } else if (context.getSchemaNode() instanceof RpcDefinition) {
133 * RpcDefinition is not supported as initial codec in JSONStreamWriter,
134 * so we need to emit initial output declaratation..
136 path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath();
137 nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
138 jsonWriter.name("output");
139 jsonWriter.beginObject();
140 writeChildren(nnWriter, (ContainerNode) data);
141 jsonWriter.endObject();
143 path = path.getParent();
145 if (data instanceof MapEntryNode) {
146 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild((MapEntryNode) data).build();
148 nnWriter = createNormalizedNodeWriter(context,path,jsonWriter, depth);
149 nnWriter.write(data);
154 private static void writeChildren(final RestconfNormalizedNodeWriter nnWriter, final ContainerNode data)
156 for (final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
157 nnWriter.write(child);
161 private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(
162 final InstanceIdentifierContext<SchemaNode> context, final SchemaPath path, final JsonWriter jsonWriter,
163 final @Nullable Integer depth) {
165 final SchemaNode schema = context.getSchemaNode();
166 final JSONCodecFactory codecs = getCodecFactory(context);
169 if (schema instanceof DataSchemaNode && !((DataSchemaNode)schema).isAugmenting()
170 && !(schema instanceof SchemaContext) || schema instanceof RpcDefinition) {
171 initialNs = schema.getQName().getNamespace();
175 final NormalizedNodeStreamWriter streamWriter =
176 JSONNormalizedNodeStreamWriter.createNestedWriter(codecs, path, initialNs, jsonWriter);
178 return DepthAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth);
181 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(streamWriter);
184 private static JsonWriter createJsonWriter(final OutputStream entityStream, final boolean prettyPrint) {
186 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8),
187 DEFAULT_INDENT_SPACES_NUM);
190 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
193 private static JSONCodecFactory getCodecFactory(final InstanceIdentifierContext<?> context) {
194 // TODO: Performance: Cache JSON Codec factory and schema context
195 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext());