NorthboundV3 APIs for DatabaseResource. 22/10722/2
authorMadhu Venugopal <mavenugo@gmail.com>
Wed, 3 Sep 2014 18:31:29 +0000 (11:31 -0700)
committerMadhu Venugopal <mavenugo@gmail.com>
Wed, 3 Sep 2014 19:58:46 +0000 (12:58 -0700)
Change-Id: I8d3cb65ac101b8f9898c621c8a7b897f17e58d3b
Signed-off-by: Madhu Venugopal <mavenugo@gmail.com>
northbound/src/main/java/org/opendaylight/ovsdb/northbound/DatabaseResource.java

index d49d882e73ac23ae7620fb8c440e42fc470d7029..caf03d2f07da58c9746eba8f7b2372c538a44e70 100644 (file)
@@ -1,5 +1,7 @@
 package org.opendaylight.ovsdb.northbound;
 
+import java.util.List;
+
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
@@ -7,33 +9,85 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
+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.schema.DatabaseSchema;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 /**
  * Northbound interface for OVSDB Databases
  */
 public class DatabaseResource {
 
     String nodeId;
-
+    ObjectMapper objectMapper;
     DatabaseResource(String id) {
         this.nodeId = id;
+        objectMapper = new ObjectMapper();
+        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+    }
+
+    private DatabaseSchema getDatabaseSchema (String databaseName) {
+        String csDatabaseName = this.caseSensitiveDatabaseName(databaseName);
+        OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+        return client.getDatabaseSchema(csDatabaseName);
     }
 
     @GET
     @Produces(MediaType.APPLICATION_JSON)
     public Response getDatabases(){
-        return Response.noContent().build();
+        OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+        try {
+            List<String> databases = client.getDatabases().get();
+            if (databases == null) {
+                return Response.noContent().build();
+            }
+            String response = objectMapper.writeValueAsString(databases);
+            return Response.status(Response.Status.OK)
+                    .entity(response)
+                    .build();
+        } catch (Exception e) {
+            throw new InternalServerErrorException("Failed due to exception " + e.getMessage());
+        }
     }
 
     @GET
     @Path("{name}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response getDatabases(@PathParam("name") String name){
-        return Response.noContent().build();
+    public Response getDatabases(@PathParam("name") String name) throws JsonProcessingException {
+        DatabaseSchema dbSchema = this.getDatabaseSchema(name);
+        String response = objectMapper.writeValueAsString(dbSchema);
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @Path("{name}/table")
     public TableResource getDatabaseTables(@PathParam("name") String name){
-        return new TableResource(nodeId, name);
+        String csDatabaseName = this.caseSensitiveDatabaseName(name);
+        return new TableResource(nodeId, csDatabaseName);
     }
 
+    private String caseSensitiveDatabaseName (String ciDatabaseName) {
+        Node node = Node.fromString(nodeId);
+        OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+        OvsdbClient client = connectionService.getConnection(node).getClient();
+
+        try {
+            List<String> databases = client.getDatabases().get();
+            if (databases == null) return ciDatabaseName;
+            for (String csDatabaseName : databases) {
+                if (csDatabaseName.equalsIgnoreCase(ciDatabaseName)) return csDatabaseName;
+            }
+            return ciDatabaseName;
+        } catch (Exception e) {
+            return ciDatabaseName;
+        }
+    }
 }