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;
12 import java.util.List;
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.controller.northbound.commons.exception.InternalServerErrorException;
22 import org.opendaylight.ovsdb.lib.OvsdbClient;
23 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.ObjectMapper;
30 * Northbound interface for OVSDB Databases
32 public class DatabaseResource {
35 ObjectMapper objectMapper;
36 DatabaseResource(String id) {
38 objectMapper = new ObjectMapper();
39 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
40 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
43 private DatabaseSchema getDatabaseSchema (String databaseName) {
44 String csDatabaseName = this.caseSensitiveDatabaseName(databaseName);
45 OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
46 return client.getDatabaseSchema(csDatabaseName);
50 @Produces(MediaType.APPLICATION_JSON)
51 public Response getDatabases(){
52 OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
54 List<String> databases = client.getDatabases().get();
55 if (databases == null) {
56 return Response.noContent().build();
58 String response = objectMapper.writeValueAsString(databases);
59 return Response.status(Response.Status.OK)
62 } catch (Exception e) {
63 throw new InternalServerErrorException("Failed due to exception " + e.getMessage());
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response getDatabases(@PathParam("name") String name) throws JsonProcessingException {
71 DatabaseSchema dbSchema = this.getDatabaseSchema(name);
72 String response = objectMapper.writeValueAsString(dbSchema);
73 return Response.status(Response.Status.OK)
79 public TableResource getDatabaseTables(@PathParam("name") String name){
80 String csDatabaseName = this.caseSensitiveDatabaseName(name);
81 return new TableResource(nodeId, csDatabaseName);
84 private String caseSensitiveDatabaseName (String ciDatabaseName) {
85 OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
87 List<String> databases = client.getDatabases().get();
88 if (databases == null) {
89 return ciDatabaseName;
91 for (String csDatabaseName : databases) {
92 if (csDatabaseName.equalsIgnoreCase(ciDatabaseName)) {
93 return csDatabaseName;
96 return ciDatabaseName;
97 } catch (Exception e) {
98 return ciDatabaseName;