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 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 javax.ws.rs.Produces;
18 import javax.ws.rs.WebApplicationException;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.Response;
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.controller.sal.restconf.impl.RestconfDocumentedException;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
33 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 @Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON,
41 Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON })
42 public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<NormalizedNodeContext> {
45 public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
46 return type.equals(NormalizedNodeContext.class);
50 public long getSize(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
55 public void writeTo(final NormalizedNodeContext t, final Class<?> type, final Type genericType, final Annotation[] annotations,
56 final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream)
57 throws IOException, WebApplicationException {
58 NormalizedNode<?, ?> data = t.getData();
59 InstanceIdentifierContext context = t.getInstanceIdentifierContext();
60 DataSchemaNode schema = context.getSchemaNode();
61 SchemaPath path = context.getSchemaNode().getPath();
62 OutputStreamWriter outputWriter = new OutputStreamWriter(entityStream, Charsets.UTF_8);
64 throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
69 outputWriter.write('{');
70 if (!SchemaPath.ROOT.equals(path)) {
71 path = path.getParent();
72 // FIXME: Add proper handling of reading root.
74 if(data instanceof MapEntryNode) {
75 data = ImmutableNodes.mapNodeBuilder(data.getNodeType()).withChild(((MapEntryNode) data)).build();
77 if(!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
78 initialNs = schema.getQName().getNamespace();
80 NormalizedNodeStreamWriter jsonWriter = JSONNormalizedNodeStreamWriter.create(context.getSchemaContext(),path,initialNs,outputWriter);
81 NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(jsonWriter);
85 outputWriter.write('}');