Merge "Initial pass at netvirt model"
[netvirt.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / TableResource.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.northbound;
10
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import java.util.Set;
13
14 import javax.ws.rs.GET;
15 import javax.ws.rs.Path;
16 import javax.ws.rs.PathParam;
17 import javax.ws.rs.Produces;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.Response;
20
21 import org.opendaylight.ovsdb.lib.OvsdbClient;
22 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
23 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
24 import org.opendaylight.ovsdb.lib.schema.TableSchema;
25
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29
30 /**
31  * Northbound interface for OVSDB tables
32  */
33 public class TableResource {
34     ObjectMapper objectMapper;
35     String databaseName;
36     String nodeId;
37
38     TableResource(String nodeId, String databaseName){
39         this.nodeId = nodeId;
40         this.databaseName = databaseName;
41         objectMapper = new ObjectMapper();
42         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
43         objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
44     }
45
46     private DatabaseSchema getDatabaseSchema (String databaseName) {
47         OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
48         return client.getDatabaseSchema(databaseName);
49     }
50
51     @GET
52     @Produces(MediaType.APPLICATION_JSON)
53     public Response getTables() throws JsonProcessingException {
54         DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
55         if (dbSchema == null) {
56             return Response.noContent().build();
57         }
58         String response = objectMapper.writeValueAsString(dbSchema.getTables());
59         return Response.status(Response.Status.OK)
60                 .entity(response)
61                 .build();
62     }
63
64     @GET
65     @Path("{name}")
66     @Produces(MediaType.APPLICATION_JSON)
67     public Response getTableDetails(@PathParam("name") String name) throws JsonProcessingException {
68         String csTableName = this.caseSensitiveTableName(databaseName, name);
69         DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
70         if (dbSchema == null) {
71             return Response.noContent().build();
72         }
73         TableSchema<GenericTableSchema> tableSchema = dbSchema.table(csTableName, GenericTableSchema.class);
74         String response = objectMapper.writeValueAsString(tableSchema);
75         return Response.status(Response.Status.OK)
76                 .entity(response)
77                 .build();
78     }
79
80     @Path("{name}/row")
81     public RowResource getDatabaseTables(@PathParam("name") String name){
82         return new RowResource(nodeId, databaseName, name);
83     }
84
85     private String caseSensitiveTableName (String databaseName, String ciTableName) {
86         DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
87         if (dbSchema == null) {
88             return ciTableName;
89         }
90         Set<String> tables = dbSchema.getTables();
91         if (tables == null) {
92             return ciTableName;
93         }
94         for (String tableName : tables) {
95             if (tableName.equalsIgnoreCase(ciTableName)) {
96                 return tableName;
97             }
98         }
99         return ciTableName;
100     }
101 }