NorthboundV3 APIs for TableResource 23/10723/2
authorMadhu Venugopal <mavenugo@gmail.com>
Wed, 3 Sep 2014 18:34:42 +0000 (11:34 -0700)
committerMadhu Venugopal <mavenugo@gmail.com>
Wed, 3 Sep 2014 19:58:46 +0000 (12:58 -0700)
Change-Id: I59187c4eb737523f15f47402f1bb3d757223f894
Signed-off-by: Madhu Venugopal <mavenugo@gmail.com>
northbound/src/main/java/org/opendaylight/ovsdb/northbound/TableResource.java

index 8620516a907fc463639cf33eb0efa8a55eeb365d..c5501b3a7d52758b265e5d15cc1fabc2d7bd8e17 100644 (file)
@@ -1,5 +1,7 @@
 package org.opendaylight.ovsdb.northbound;
 
+import java.util.Set;
+
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
@@ -7,30 +9,63 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import org.opendaylight.ovsdb.lib.OvsdbClient;
+import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
+import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
+import org.opendaylight.ovsdb.lib.schema.TableSchema;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 /**
  * Northbound interface for OVSDB tables
  */
 public class TableResource {
-
+    ObjectMapper objectMapper;
     String databaseName;
     String nodeId;
 
     TableResource(String nodeId, String databaseName){
         this.nodeId = nodeId;
         this.databaseName = databaseName;
+        objectMapper = new ObjectMapper();
+        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+
+    }
+
+    private DatabaseSchema getDatabaseSchema (String databaseName) {
+        OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+        return client.getDatabaseSchema(databaseName);
     }
 
     @GET
     @Produces(MediaType.APPLICATION_JSON)
-    public Response getTables(){
-        return Response.noContent().build();
+    public Response getTables() throws JsonProcessingException {
+        DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
+        if (dbSchema == null) {
+            return Response.noContent().build();
+        }
+        String response = objectMapper.writeValueAsString(dbSchema.getTables());
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @GET
     @Path("{name}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response getTableDetails(@PathParam("name") String name){
-        return Response.noContent().build();
+    public Response getTableDetails(@PathParam("name") String name) throws JsonProcessingException {
+        String csTableName = this.caseSensitiveTableName(databaseName, name);
+        DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
+        if (dbSchema == null) {
+            return Response.noContent().build();
+        }
+        TableSchema<GenericTableSchema> tableSchema = dbSchema.table(csTableName, GenericTableSchema.class);
+        String response = objectMapper.writeValueAsString(tableSchema);
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @Path("{name}/row")
@@ -38,4 +73,18 @@ public class TableResource {
         return new RowResource(nodeId, databaseName, name);
     }
 
+    private String caseSensitiveTableName (String databaseName, String ciTableName) {
+        DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
+        if (dbSchema == null) {
+            return ciTableName;
+        }
+        Set<String> tables = dbSchema.getTables();
+        if (tables == null) {
+            return ciTableName;
+        }
+        for (String tableName : tables) {
+            if (tableName.equalsIgnoreCase(ciTableName)) return tableName;
+        }
+        return ciTableName;
+    }
 }