X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fimpl%2FStructuredDataToJsonProvider.java;h=1c2e9c5009897f741efaa6a1320665d7ba2a4b0a;hb=03ee21b4d6ae3dd6fd3a6991253e877dc96fabf5;hp=608cdcd94341f5d8a37fc1ec520f2667b4b62028;hpb=b5daa3678322a764f9b0e2483f82781f4d39d263;p=controller.git diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java index 608cdcd943..1c2e9c5009 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/StructuredDataToJsonProvider.java @@ -1,136 +1,66 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.rest.impl; -import static org.opendaylight.controller.sal.restconf.impl.MediaTypes.API; +import com.google.common.base.Charsets; +import com.google.gson.stream.JsonWriter; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import java.util.List; -import java.util.Set; import javax.ws.rs.Produces; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; +import org.opendaylight.controller.sal.rest.api.Draft02; import org.opendaylight.controller.sal.rest.api.RestconfService; +import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException; import org.opendaylight.controller.sal.restconf.impl.StructuredData; import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.api.SimpleNode; -import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; - -import com.google.gson.stream.JsonWriter; @Provider -@Produces({ API + RestconfService.JSON }) +@Produces({ Draft02.MediaTypes.API + RestconfService.JSON, Draft02.MediaTypes.DATA + RestconfService.JSON, + Draft02.MediaTypes.OPERATION + RestconfService.JSON, MediaType.APPLICATION_JSON }) public enum StructuredDataToJsonProvider implements MessageBodyWriter { INSTANCE; - + @Override - public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { - // TODO Auto-generated method stub - return false; + public boolean isWriteable(final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { + return type.equals( StructuredData.class ); } @Override - public long getSize(StructuredData t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + public long getSize(final StructuredData t, final Class type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) { return -1; } @Override - public void writeTo(StructuredData t, Class type, Type genericType, Annotation[] annotations, - MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) - throws IOException, WebApplicationException { - JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, "UTF-8")); - writer.setIndent(" "); - writer.beginObject(); - convertNodeToJsonAccordingToSchema(writer, t.getData(), t.getSchema()); - writer.endObject(); - } - - private void convertNodeToJsonAccordingToSchema(JsonWriter writer, Node node, DataSchemaNode dataSchemaNode) throws IOException { - if (node instanceof CompositeNode) { - if (!(dataSchemaNode instanceof DataNodeContainer)) { - throw new IllegalStateException("CompositeNode should be represented as DataNodeContainer"); - } - if (dataSchemaNode instanceof ContainerSchemaNode) { - writer.name(node.getNodeType().getLocalName()); - writer.beginObject(); - String listName = ""; - for (Node n : ((CompositeNode) node).getChildren()) { - DataSchemaNode foundDataSchemaNode = findSchemaForNode(n, ((DataNodeContainer) dataSchemaNode).getChildNodes()); - if (foundDataSchemaNode instanceof ListSchemaNode) { - if (listName.equals(n.getNodeType().getLocalName())) { - continue; - } - listName = n.getNodeType().getLocalName(); - } - convertNodeToJsonAccordingToSchema(writer, n, foundDataSchemaNode); - } - writer.endObject(); - } else if (dataSchemaNode instanceof ListSchemaNode) { - writer.name(node.getNodeType().getLocalName()); - writer.beginArray(); - List> nodeSiblings = node.getParent().getChildren(); - for (Node nodeSibling : nodeSiblings) { - if (nodeSibling.getNodeType().getLocalName().equals(node.getNodeType().getLocalName())) { - DataSchemaNode schemaForNodeSibling = findSchemaForNode(nodeSibling, - ((DataNodeContainer) dataSchemaNode.getParent()).getChildNodes()); - writer.beginObject(); - for (Node child : ((CompositeNode) nodeSibling).getChildren()) { - DataSchemaNode schemaForChild = findSchemaForNode(child, - ((DataNodeContainer) schemaForNodeSibling).getChildNodes()); - convertNodeToJsonAccordingToSchema(writer, child, schemaForChild); - } - writer.endObject(); - } - } - writer.endArray(); - } - } else if (node instanceof SimpleNode) { - if (!(dataSchemaNode instanceof LeafSchemaNode)) { - throw new IllegalStateException("SimpleNode should should be represented as LeafSchemaNode"); - } - writeLeaf(writer, (LeafSchemaNode) dataSchemaNode, (SimpleNode) node); + public void writeTo(final StructuredData t, final Class type, final Type genericType, final Annotation[] annotations, + final MediaType mediaType, final MultivaluedMap httpHeaders, final OutputStream entityStream) + throws IOException, WebApplicationException { + CompositeNode data = t.getData(); + if (data == null) { + throw new RestconfDocumentedException(Response.Status.NOT_FOUND); } - } - private DataSchemaNode findSchemaForNode(Node node, Set dataSchemaNode) { - for (DataSchemaNode dsn : dataSchemaNode) { - if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) { - return dsn; - } - } - return null; - } - - private void writeLeaf(JsonWriter writer, LeafSchemaNode leafSchemaNode, SimpleNode data) throws IOException { - TypeDefinition type = leafSchemaNode.getType(); - - writer.name(data.getNodeType().getLocalName()); - - if (type instanceof DecimalTypeDefinition) { - writer.value((Double.valueOf((String) data.getValue())).doubleValue()); - } else if (type instanceof IntegerTypeDefinition) { - writer.value((Integer.valueOf((String) data.getValue())).intValue()); - } else if (type instanceof EmptyTypeDefinition) { - writer.value("[null]"); - } else { - writer.value(String.valueOf(data.getValue())); - } + JsonWriter writer = new JsonWriter(new OutputStreamWriter(entityStream, Charsets.UTF_8)); + writer.setIndent(" "); + JsonMapper jsonMapper = new JsonMapper(t.getMountPoint()); + jsonMapper.write(writer, data, (DataNodeContainer) t.getSchema()); + writer.flush(); } }