Fix license header violations in northbound
[netvirt.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / RowResource.java
index 1a256473749c664a2495f35a5127213b4ae584b9..e6564285af1e05327c46264cab1cf334eb66594a 100644 (file)
@@ -1,9 +1,19 @@
+/*
+ * Copyright (c) 2014, 2015 Red Hat, 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.ovsdb.northbound;
 
+import com.fasterxml.jackson.databind.SerializationFeature;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.util.Map;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
@@ -17,16 +27,18 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
 import org.opendaylight.controller.northbound.commons.RestMessages;
+import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
-import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.ovsdb.lib.OvsdbClient;
 import org.opendaylight.ovsdb.lib.notation.Row;
 import org.opendaylight.ovsdb.lib.notation.UUID;
+import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.plugin.api.OvsVswitchdSchemaConstants;
 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
-import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 
+import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.DeserializationFeature;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -39,17 +51,15 @@ public class RowResource {
     String nodeId;
     String databaseName;
     String tableName;
+    ObjectMapper objectMapper;
 
     public RowResource(String nodeId, String databaseName, String tableName) {
         this.nodeId = nodeId;
         this.databaseName = databaseName;
         this.tableName = tableName;
-    }
-
-    @GET
-    @Produces(MediaType.APPLICATION_JSON)
-    public Response getRows(){
-        return Response.noContent().build();
+        objectMapper = new ObjectMapper();
+        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
     }
 
     private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
@@ -59,22 +69,38 @@ public class RowResource {
         while ((line = in.readLine()) != null) {
             rowNodeStrBuilder.append(line);
         }
+        JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
+        OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
+        return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
+    }
 
-        ObjectMapper mapper = new ObjectMapper();
-        JsonNode jsonNode = mapper.readTree(rowNodeStrBuilder.toString());
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getRows() throws JsonProcessingException {
+        OvsdbConfigurationService
+                ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
+                                                                                            this);
+        if (ovsdbTable == null) {
+            throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
 
-        Node node = Node.fromString(nodeId);
-        OvsdbConnectionService
-        connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
-        OvsdbClient client = connectionService.getConnection(node).getClient();
-        return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
+        Map<UUID,Row<GenericTableSchema>> rows = null;
+        try {
+            rows = ovsdbTable.getRows(node, databaseName, tableName);
+        } catch (Exception e) {
+            throw new BadRequestException(e.getMessage());
+        }
+        String response = objectMapper.writeValueAsString(rows);
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @POST
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
     public Response createRow(InputStream stream) throws IOException {
-        Node node = Node.fromString(nodeId);
         OvsdbRow localRow = this.getOvsdbRow(stream);
         if (localRow == null) {
             return Response.status(Response.Status.BAD_REQUEST).build();
@@ -86,11 +112,10 @@ public class RowResource {
             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
         Row row = ovsdbTable.insertTree(node, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName,
                 localRow.getParentTable(), new UUID(localRow.getParentUuid()), localRow.getParentColumn(),
                 localRow.getRow());
-        ObjectMapper objectMapper = new ObjectMapper();
-        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         String response = objectMapper.writeValueAsString(row);
         return Response.status(Response.Status.CREATED)
                 .entity(response)
@@ -100,24 +125,69 @@ public class RowResource {
     @GET
     @Path("{id}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response getRowDetails(@PathParam("id") String id){
-        return Response.noContent().build();
+    public Response getRowDetails(@PathParam("id") String id) throws JsonProcessingException {
+        OvsdbConfigurationService
+        ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
+                                                                                    this);
+        if (ovsdbTable == null) {
+            throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
+        Row<GenericTableSchema> row = null;
+        try {
+            row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
+        } catch (Exception e) {
+            throw new BadRequestException(e.getMessage());
+        }
+        String response = objectMapper.writeValueAsString(row);
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @PUT
     @Path("{id}")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
-    public Response updateRow(@PathParam("id") String id){
-        return Response.noContent().build();
+    public Response updateRow(@PathParam("id") String id, InputStream stream) throws IOException{
+        OvsdbRow localRow = this.getOvsdbRow(stream);
+        if (localRow == null) {
+            return Response.status(Response.Status.BAD_REQUEST).build();
+        }
+        OvsdbConfigurationService ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
+                                                                                    this);
+        if (ovsdbTable == null) {
+            throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
+        Row<GenericTableSchema> row = ovsdbTable.updateRow(node, databaseName, tableName, new UUID(id), localRow.getRow(), true);
+        String response = objectMapper.writeValueAsString(row);
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @DELETE
     @Path("{id}")
     @Consumes(MediaType.APPLICATION_JSON)
     public Response deleteRow(@PathParam("id") String id){
-        return Response.noContent().build();
-    }
+        OvsdbConfigurationService
+        ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
+                                                                                    this);
+        if (ovsdbTable == null) {
+            throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
 
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
+        try {
+            ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
+        } catch (Exception e) {
+            throw new BadRequestException(e.getMessage());
+        }
+        return Response.status(Response.Status.OK)
+                .build();
 
+    }
 }