NorthboundV3 insert Row using the newly added insertTree plugin functionality 20/10720/2
authorMadhu Venugopal <mavenugo@gmail.com>
Wed, 3 Sep 2014 12:41:32 +0000 (05:41 -0700)
committerMadhu Venugopal <mavenugo@gmail.com>
Wed, 3 Sep 2014 19:58:46 +0000 (12:58 -0700)
Change-Id: I7a32798510b0f835ff9aed8e8a0da301188f7895
Signed-off-by: Madhu Venugopal <mavenugo@gmail.com>
northbound/src/main/java/org/opendaylight/ovsdb/northbound/OvsdbRow.java
northbound/src/main/java/org/opendaylight/ovsdb/northbound/RowResource.java

index ca4a56b9f259ff2eaad277ee0d9fd44906231d87..fc4b827767a98a496e16cad94b7b629a990e3bed 100644 (file)
@@ -13,8 +13,6 @@ import java.io.IOException;
 import java.util.Iterator;
 import java.util.concurrent.ExecutionException;
 
-import javax.xml.bind.annotation.XmlElement;
-
 import org.opendaylight.ovsdb.lib.OvsdbClient;
 import org.opendaylight.ovsdb.lib.notation.Row;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
@@ -24,18 +22,17 @@ import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 
-@Deprecated
 public class OvsdbRow {
     private static final String PARENTUUID = "parent_uuid";
+    private static final String PARENTTABLE = "parent_table";
+    private static final String PARENTCOLUMN = "parent_column";
     private static final String ROW = "row";
 
-    @XmlElement(name=PARENTUUID)
-    String parentUuid;
-
-    String tableName;
-
-    @XmlElement(name=ROW)
-    Row<GenericTableSchema> row;
+    private String parentUuid;
+    private String parentTable;
+    private String parentColumn;
+    private String tableName;
+    private Row<GenericTableSchema> row;
 
     public OvsdbRow() {
     }
@@ -46,11 +43,27 @@ public class OvsdbRow {
         this.row = row;
     }
 
+    public OvsdbRow(String parentTable, String parentUuid, String parentColumn, String tableName, Row<GenericTableSchema> row) {
+        this.parentTable = parentTable;
+        this.parentColumn = parentColumn;
+        this.parentUuid = parentUuid;
+        this.tableName = tableName;
+        this.row = row;
+    }
+
     public static OvsdbRow fromJsonNode(OvsdbClient client, String dbName, JsonNode json) {
         JsonNode parentUuidNode = json.get(PARENTUUID);
         String parentUuid = null;
         if (parentUuidNode != null) parentUuid = parentUuidNode.asText();
 
+        JsonNode parentTableNode = json.get(PARENTTABLE);
+        String parentTable = null;
+        if (parentTableNode != null) parentTable = parentTableNode.asText();
+
+        JsonNode parentColumnNode = json.get(PARENTCOLUMN);
+        String parentColumn = null;
+        if (parentColumnNode != null) parentColumn = parentColumnNode.asText();
+
         JsonNode rowNode = json.get(ROW);
         if (rowNode == null) return null;
         for(Iterator<String> fieldNames = rowNode.fieldNames(); fieldNames.hasNext();) {
@@ -62,7 +75,7 @@ public class OvsdbRow {
                 e.printStackTrace();
                 return null;
             }
-            return new OvsdbRow(parentUuid, tableName, row);
+            return new OvsdbRow(parentTable, parentUuid, parentColumn, tableName, row);
         }
         return null;
     }
@@ -73,10 +86,18 @@ public class OvsdbRow {
         return schema.createRow((ObjectNode)rowJson);
     }
 
+    public String getParentTable() {
+        return parentTable;
+    }
+
     public String getParentUuid() {
         return parentUuid;
     }
 
+    public String getParentColumn() {
+        return parentColumn;
+    }
+
     public String getTableName() {
         return tableName;
     }
@@ -84,10 +105,4 @@ public class OvsdbRow {
     public Row<GenericTableSchema> getRow() {
         return row;
     }
-
-    @Override
-    public String toString() {
-        return "OVSDBRow [parentUuid=" + parentUuid + ", tableName="
-                + tableName + ", row=" + row + "]";
-    }
 }
index f61b06c373499cb403ff97609cc9009b492ab3f0..1a256473749c664a2495f35a5127213b4ae584b9 100644 (file)
@@ -1,6 +1,9 @@
 package org.opendaylight.ovsdb.northbound;
 
+import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
@@ -13,6 +16,21 @@ import javax.ws.rs.Produces;
 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.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.plugin.api.OvsVswitchdSchemaConstants;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
+
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 /**
  * Northbound interface for OVSDB Rows
  */
@@ -20,12 +38,12 @@ public class RowResource {
 
     String nodeId;
     String databaseName;
-    String rowName;
+    String tableName;
 
-    public RowResource(String nodeId, String databaseName, String rowName) {
+    public RowResource(String nodeId, String databaseName, String tableName) {
         this.nodeId = nodeId;
         this.databaseName = databaseName;
-        this.rowName = rowName;
+        this.tableName = tableName;
     }
 
     @GET
@@ -34,11 +52,49 @@ public class RowResource {
         return Response.noContent().build();
     }
 
+    private OvsdbRow getOvsdbRow (InputStream stream) throws IOException {
+        StringBuilder rowNodeStrBuilder = new StringBuilder();
+        BufferedReader in = new BufferedReader(new InputStreamReader(stream));
+        String line = null;
+        while ((line = in.readLine()) != null) {
+            rowNodeStrBuilder.append(line);
+        }
+
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode jsonNode = mapper.readTree(rowNodeStrBuilder.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);
+    }
+
     @POST
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
-    public Response createRow(InputStream stream) {
-        return Response.noContent().build();
+    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();
+        }
+        OvsdbConfigurationService
+        ovsdbTable = (OvsdbConfigurationService)ServiceHelper.getGlobalInstance(OvsdbConfigurationService.class,
+                                                                                    this);
+        if (ovsdbTable == null) {
+            throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+
+        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)
+                .build();
     }
 
     @GET