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.core.Response;
23 import javax.ws.rs.ext.MessageBodyWriter;
24 import javax.ws.rs.ext.Provider;
25 import org.opendaylight.controller.sal.rest.api.Draft02;
26 import org.opendaylight.controller.sal.rest.api.RestconfService;
27 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
28 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
36 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
37 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
38 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
39 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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();
66 throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
69 final InstanceIdentifierContext context = t.getInstanceIdentifierContext();
71 SchemaPath path = context.getSchemaNode().getPath();
72 boolean isDataRoot = false;
73 if (SchemaPath.ROOT.equals(path)) {
76 path = path.getParent();
77 // FIXME: Add proper handling of reading root.
80 JsonWriter jsonWriter = createJsonWriter(entityStream);
81 NormalizedNodeWriter nnWriter = createNormalizedNodeWriter(context,path,jsonWriter);
83 jsonWriter.beginObject();
85 writeDataRoot(nnWriter,(ContainerNode) data);
87 if(data instanceof MapEntryNode) {
88 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
94 jsonWriter.endObject();
98 private NormalizedNodeWriter createNormalizedNodeWriter(InstanceIdentifierContext context, SchemaPath path, JsonWriter jsonWriter) {
100 final DataSchemaNode schema = context.getSchemaNode();
101 final JSONCodecFactory codecs = getCodecFactory(context);
103 URI initialNs = null;
104 if(!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
105 initialNs = schema.getQName().getNamespace();
107 final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs,path,initialNs,jsonWriter);
108 return NormalizedNodeWriter.forStreamWriter(streamWriter);
111 private JsonWriter createJsonWriter(OutputStream entityStream) {
112 // FIXME BUG-2153: Add pretty print support
113 return JsonWriterFactory.createJsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8));
117 private JSONCodecFactory getCodecFactory(InstanceIdentifierContext context) {
118 // TODO: Performance: Cache JSON Codec factory and schema context
119 return JSONCodecFactory.create(context.getSchemaContext());
122 private void writeDataRoot(final NormalizedNodeWriter nnWriter, final ContainerNode data) throws IOException {
123 for(DataContainerChild<? extends PathArgument, ?> child : data.getValue()) {
124 nnWriter.write(child);