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;
16 import java.net.URISyntaxException;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Map.Entry;
19 import javax.ws.rs.Produces;
20 import javax.ws.rs.WebApplicationException;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.ext.MessageBodyWriter;
24 import javax.ws.rs.ext.Provider;
25 import javax.xml.stream.XMLStreamException;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.netconf.sal.rest.api.Draft02;
28 import org.opendaylight.netconf.sal.rest.api.RestconfNormalizedNodeWriter;
29 import org.opendaylight.netconf.sal.rest.api.RestconfService;
30 import org.opendaylight.netconf.util.NetconfUtil;
31 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
32 import org.opendaylight.yangtools.yang.common.XMLNamespace;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
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.JSONCodecFactorySupplier;
41 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
42 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 import org.xml.sax.SAXException;
52 * Normalized node writer for JSON.
54 * @deprecated This class will be replaced by NormalizedNodeJsonBodyWriter from restconf-nb-rfc8040
59 Draft02.MediaTypes.API + RestconfService.JSON,
60 Draft02.MediaTypes.DATA + RestconfService.JSON,
61 Draft02.MediaTypes.OPERATION + RestconfService.JSON,
62 MediaType.APPLICATION_JSON
64 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
66 private static final int DEFAULT_INDENT_SPACES_NUM = 2;
69 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations,
70 final MediaType mediaType) {
71 return type.equals(NormalizedNodeContext.class);
75 public long getSize(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
76 final Annotation[] annotations, final MediaType mediaType) {
81 public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
82 final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
83 final OutputStream entityStream) throws IOException, WebApplicationException {
84 if (httpHeaders != null) {
85 for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
86 httpHeaders.add(entry.getKey(), entry.getValue());
89 NormalizedNode data = context.getData();
94 final InstanceIdentifierContext identifierCtx = context.getInstanceIdentifierContext();
96 try (JsonWriter jsonWriter = createJsonWriter(entityStream, context.getWriterParameters().isPrettyPrint())) {
97 jsonWriter.beginObject();
98 writeNormalizedNode(jsonWriter, identifierCtx, data, context.getWriterParameters().getDepth());
99 jsonWriter.endObject();
104 private static void writeNormalizedNode(final JsonWriter jsonWriter, final InstanceIdentifierContext context,
105 // Note: mutable argument
106 NormalizedNode data, final @Nullable Integer depth) throws IOException {
107 SchemaPath path = context.getSchemaNode().getPath();
108 final RestconfNormalizedNodeWriter nnWriter;
109 if (SchemaPath.ROOT.equals(path)) {
111 * Creates writer without initialNs and we write children of root data container
112 * which is not visible in restconf
114 nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
115 if (data instanceof ContainerNode) {
116 writeChildren(nnWriter,(ContainerNode) data);
117 } else if (data instanceof DOMSourceAnyxmlNode) {
119 writeChildren(nnWriter,
120 (ContainerNode) NetconfUtil.transformDOMSourceToNormalizedNode(
121 context.getSchemaContext(), ((DOMSourceAnyxmlNode)data).body()).getResult());
122 } catch (XMLStreamException | URISyntaxException | SAXException e) {
123 throw new IOException("Cannot write anyxml.", e);
126 } else if (context.getSchemaNode() instanceof RpcDefinition) {
128 * RpcDefinition is not supported as initial codec in JSONStreamWriter,
129 * so we need to emit initial output declaratation..
131 final var rpc = (RpcDefinition) context.getSchemaNode();
132 path = SchemaPath.create(true, rpc.getQName(), rpc.getOutput().getQName());
133 nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
134 jsonWriter.name("output");
135 jsonWriter.beginObject();
136 writeChildren(nnWriter, (ContainerNode) data);
137 jsonWriter.endObject();
139 path = path.getParent();
141 if (data instanceof MapEntryNode) {
142 data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType())
143 .withChild((MapEntryNode) data)
146 nnWriter = createNormalizedNodeWriter(context, path, jsonWriter, depth);
147 nnWriter.write(data);
152 private static void writeChildren(final RestconfNormalizedNodeWriter nnWriter, final ContainerNode data)
154 for (final DataContainerChild child : data.body()) {
155 nnWriter.write(child);
159 private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(
160 final InstanceIdentifierContext context, final SchemaPath path, final JsonWriter jsonWriter,
161 final @Nullable Integer depth) {
163 final SchemaNode schema = context.getSchemaNode();
164 final JSONCodecFactory codecs = getCodecFactory(context);
166 final XMLNamespace initialNs;
167 if (schema instanceof DataSchemaNode && !((DataSchemaNode)schema).isAugmenting()
168 && !(schema instanceof SchemaContext) || schema instanceof RpcDefinition) {
169 initialNs = schema.getQName().getNamespace();
173 final NormalizedNodeStreamWriter streamWriter =
174 JSONNormalizedNodeStreamWriter.createNestedWriter(codecs, path, initialNs, jsonWriter);
176 return DepthAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth);
179 return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(streamWriter);
182 private static JsonWriter createJsonWriter(final OutputStream entityStream, final boolean prettyPrint) {
184 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8),
185 DEFAULT_INDENT_SPACES_NUM);
188 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
191 private static JSONCodecFactory getCodecFactory(final InstanceIdentifierContext context) {
192 // TODO: Performance: Cache JSON Codec factory and schema context
193 return JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext());