2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.northbound;
11 import com.fasterxml.jackson.databind.SerializationFeature;
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;
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;
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.DeserializationFeature;
28 import com.fasterxml.jackson.databind.ObjectMapper;
31 * Northbound interface for OVSDB tables
33 public class TableResource {
34 ObjectMapper objectMapper;
38 TableResource(String nodeId, String databaseName){
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);
46 private DatabaseSchema getDatabaseSchema (String databaseName) {
47 OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
48 return client.getDatabaseSchema(databaseName);
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();
58 String response = objectMapper.writeValueAsString(dbSchema.getTables());
59 return Response.status(Response.Status.OK)
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();
73 TableSchema<GenericTableSchema> tableSchema = dbSchema.table(csTableName, GenericTableSchema.class);
74 String response = objectMapper.writeValueAsString(tableSchema);
75 return Response.status(Response.Status.OK)
81 public RowResource getDatabaseTables(@PathParam("name") String name){
82 return new RowResource(nodeId, databaseName, name);
85 private String caseSensitiveTableName (String databaseName, String ciTableName) {
86 DatabaseSchema dbSchema = this.getDatabaseSchema(databaseName);
87 if (dbSchema == null) {
90 Set<String> tables = dbSchema.getTables();
94 for (String tableName : tables) {
95 if (tableName.equalsIgnoreCase(ciTableName)) {