NorthboundV3 APIs for DatabaseResource.
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / DatabaseResource.java
1 package org.opendaylight.ovsdb.northbound;
2
3 import java.util.List;
4
5 import javax.ws.rs.GET;
6 import javax.ws.rs.Path;
7 import javax.ws.rs.PathParam;
8 import javax.ws.rs.Produces;
9 import javax.ws.rs.core.MediaType;
10 import javax.ws.rs.core.Response;
11
12 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
13 import org.opendaylight.controller.sal.core.Node;
14 import org.opendaylight.controller.sal.utils.ServiceHelper;
15 import org.opendaylight.ovsdb.lib.OvsdbClient;
16 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
17 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
18
19 import com.fasterxml.jackson.core.JsonProcessingException;
20 import com.fasterxml.jackson.databind.DeserializationFeature;
21 import com.fasterxml.jackson.databind.ObjectMapper;
22
23 /**
24  * Northbound interface for OVSDB Databases
25  */
26 public class DatabaseResource {
27
28     String nodeId;
29     ObjectMapper objectMapper;
30     DatabaseResource(String id) {
31         this.nodeId = id;
32         objectMapper = new ObjectMapper();
33         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
34     }
35
36     private DatabaseSchema getDatabaseSchema (String databaseName) {
37         String csDatabaseName = this.caseSensitiveDatabaseName(databaseName);
38         OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
39         return client.getDatabaseSchema(csDatabaseName);
40     }
41
42     @GET
43     @Produces(MediaType.APPLICATION_JSON)
44     public Response getDatabases(){
45         OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
46         try {
47             List<String> databases = client.getDatabases().get();
48             if (databases == null) {
49                 return Response.noContent().build();
50             }
51             String response = objectMapper.writeValueAsString(databases);
52             return Response.status(Response.Status.OK)
53                     .entity(response)
54                     .build();
55         } catch (Exception e) {
56             throw new InternalServerErrorException("Failed due to exception " + e.getMessage());
57         }
58     }
59
60     @GET
61     @Path("{name}")
62     @Produces(MediaType.APPLICATION_JSON)
63     public Response getDatabases(@PathParam("name") String name) throws JsonProcessingException {
64         DatabaseSchema dbSchema = this.getDatabaseSchema(name);
65         String response = objectMapper.writeValueAsString(dbSchema);
66         return Response.status(Response.Status.OK)
67                 .entity(response)
68                 .build();
69     }
70
71     @Path("{name}/table")
72     public TableResource getDatabaseTables(@PathParam("name") String name){
73         String csDatabaseName = this.caseSensitiveDatabaseName(name);
74         return new TableResource(nodeId, csDatabaseName);
75     }
76
77     private String caseSensitiveDatabaseName (String ciDatabaseName) {
78         Node node = Node.fromString(nodeId);
79         OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
80         OvsdbClient client = connectionService.getConnection(node).getClient();
81
82         try {
83             List<String> databases = client.getDatabases().get();
84             if (databases == null) return ciDatabaseName;
85             for (String csDatabaseName : databases) {
86                 if (csDatabaseName.equalsIgnoreCase(ciDatabaseName)) return csDatabaseName;
87             }
88             return ciDatabaseName;
89         } catch (Exception e) {
90             return ciDatabaseName;
91         }
92     }
93 }