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.RpcDefinition;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
46 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
47 Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
48 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
51 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
52 return type.equals(NormalizedNodeContext.class);
56 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
61 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations,
62 final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
63 throws IOException, WebApplicationException {
64 NormalizedNode<?, ?> data = t.getData();
69 final InstanceIdentifierContext<SchemaNode> context = (InstanceIdentifierContext<SchemaNode>) t.getInstanceIdentifierContext();
71 SchemaPath path = context.getSchemaNode().getPath();
72 boolean isDataRoot = false;
73 if (SchemaPath.ROOT.equals(path)) {
75 } else if (context.getSchemaNode() instanceof RpcDefinition) {
77 path = ((RpcDefinition) context.getSchemaNode()).getOutput().getPath();
79 path = path.getParent();
80 // FIXME: Add proper handling of reading root.
83 final JsonWriter jsonWriter = createJsonWriter(entityStream);
84 final NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
86 jsonWriter.beginObject();
88 writeDataRoot(nnWriter,(ContainerNode) data);
90 if(data instanceof MapEntryNode) {
91 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
97 jsonWriter.endObject();
101 private NormalizedNodeWriter createNormalizedNodeWriter(final InstanceIdentifierContext<SchemaNode> context,
102 final SchemaPath path, final JsonWriter jsonWriter) {
104 final SchemaNode schema = context.getSchemaNode();
105 final JSONCodecFactory codecs = getCodecFactory(context);
107 URI initialNs = null;
108 if ( ! (schema instanceof RpcDefinition) && (!((DataSchemaNode)schema).isAugmenting() && !(schema instanceof SchemaContext))) {
109 initialNs = schema.getQName().getNamespace();
111 final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
112 return NormalizedNodeWriter.forStreamWriter(streamWriter);
115 private JsonWriter createJsonWriter(final OutputStream entityStream) {
116 // FIXME BUG-2153: Add pretty print support
117 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
121 private JSONCodecFactory getCodecFactory(final InstanceIdentifierContext context) {
122 // TODO: Performance: Cache JSON Codec factory and schema context
123 return JSONCodecFactory.create(context.getSchemaContext());
126 private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
127 for(final DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
128 nnWriter.write(child);