NorthboundV3 APIs for TableResource
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / TableResource.java
1 package org.opendaylight.ovsdb.northbound;
2
3 import java.util.Set;
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.ovsdb.lib.OvsdbClient;
13 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
14 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
15 import org.opendaylight.ovsdb.lib.schema.TableSchema;
16
17 import com.fasterxml.jackson.core.JsonProcessingException;
18 import com.fasterxml.jackson.databind.DeserializationFeature;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20
21 /**
22  * Northbound interface for OVSDB tables
23  */
24 public class TableResource {
25     ObjectMapper objectMapper;
26     String databaseName;
27     String nodeId;
28
29     TableResource(String nodeId, String databaseName){
30         this.nodeId = nodeId;
31         this.databaseName = databaseName;
32         objectMapper = new ObjectMapper();
33         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
34
35     }
36
37     private DatabaseSchema getDatabaseSchema (String databaseName) {
38         OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
39         return client.getDatabaseSchema(databaseName);
40     }
41
42     @GET
43     @Produces(MediaType.APPLICATION_JSON)
44     public Response getTables() throws JsonProcessingException {
45         DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
46         if (dbSchema == null) {
47             return Response.noContent().build();
48         }
49         String response = objectMapper.writeValueAsString(dbSchema.getTables());
50         return Response.status(Response.Status.OK)
51                 .entity(response)
52                 .build();
53     }
54
55     @GET
56     @Path("{name}")
57     @Produces(MediaType.APPLICATION_JSON)
58     public Response getTableDetails(@PathParam("name") String name) throws JsonProcessingException {
59         String csTableName = this.caseSensitiveTableName(databaseName, name);
60         DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
61         if (dbSchema == null) {
62             return Response.noContent().build();
63         }
64         TableSchema<GenericTableSchema> tableSchema = dbSchema.table(csTableName, GenericTableSchema.class);
65         String response = objectMapper.writeValueAsString(tableSchema);
66         return Response.status(Response.Status.OK)
67                 .entity(response)
68                 .build();
69     }
70
71     @Path("{name}/row")
72     public RowResource getDatabaseTables(@PathParam("name") String name){
73         return new RowResource(nodeId, databaseName, name);
74     }
75
76     private String caseSensitiveTableName (String databaseName, String ciTableName) {
77         DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
78         if (dbSchema == null) {
79             return ciTableName;
80         }
81         Set<String> tables = dbSchema.getTables();
82         if (tables == null) {
83             return ciTableName;
84         }
85         for (String tableName : tables) {
86             if (tableName.equalsIgnoreCase(ciTableName)) return tableName;
87         }
88         return ciTableName;
89     }
90 }