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.controller.sal.rest.impl;
10 import com.google.common.base.Charsets;
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 javax.ws.rs.Produces;
19 import javax.ws.rs.WebApplicationException;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.ext.MessageBodyWriter;
23 import javax.ws.rs.ext.Provider;
24 import org.opendaylight.controller.sal.rest.api.Draft02;
25 import org.opendaylight.controller.sal.rest.api.RestconfService;
26 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
27 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
35 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
36 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
44 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
45 Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
46 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
49 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
50 return type.equals(NormalizedNodeContext.class);
54 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
59 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations,
60 final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
61 throws IOException, WebApplicationException {
62 NormalizedNode<?, ?> data = t.getData();
67 final InstanceIdentifierContext<DataSchemaNode> context = (InstanceIdentifierContext<DataSchemaNode>) t.getInstanceIdentifierContext();
69 SchemaPath path = context.getSchemaNode().getPath();
70 boolean isDataRoot = false;
71 if (SchemaPath.ROOT.equals(path)) {
74 path = path.getParent();
75 // FIXME: Add proper handling of reading root.
78 final JsonWriter jsonWriter = createJsonWriter(entityStream);
79 final NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
81 jsonWriter.beginObject();
83 writeDataRoot(nnWriter,(ContainerNode) data);
85 if(data instanceof MapEntryNode) {
86 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
92 jsonWriter.endObject();
96 private NormalizedNodeWriter createNormalizedNodeWriter(final InstanceIdentifierContext<DataSchemaNode> context,
97 final SchemaPath path, final JsonWriter jsonWriter) {
99 final DataSchemaNode schema = context.getSchemaNode();
100 final JSONCodecFactory codecs = getCodecFactory(context);
102 URI initialNs = null;
103 if(!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
104 initialNs = schema.getQName().getNamespace();
106 final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
107 return NormalizedNodeWriter.forStreamWriter(streamWriter);
110 private JsonWriter createJsonWriter(final OutputStream entityStream) {
111 // FIXME BUG-2153: Add pretty print support
112 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
116 private JSONCodecFactory getCodecFactory(final InstanceIdentifierContext context) {
117 // TODO: Performance: Cache JSON Codec factory and schema context
118 return JSONCodecFactory.create(context.getSchemaContext());
121 private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
122 for(final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
123 nnWriter.write(child);