Merge "Initial pass at netvirt model"
[netvirt.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / DatabaseResource.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.List;
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.controller.northbound.commons.exception.InternalServerErrorException;
22 import org.opendaylight.ovsdb.lib.OvsdbClient;
23 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
24
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 /**
30  * Northbound interface for OVSDB Databases
31  */
32 public class DatabaseResource {
33
34     String nodeId;
35     ObjectMapper objectMapper;
36     DatabaseResource(String id) {
37         this.nodeId = id;
38         objectMapper = new ObjectMapper();
39         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
40         objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
41     }
42
43     private DatabaseSchema getDatabaseSchema (String databaseName) {
44         String csDatabaseName = this.caseSensitiveDatabaseName(databaseName);
45         OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
46         return client.getDatabaseSchema(csDatabaseName);
47     }
48
49     @GET
50     @Produces(MediaType.APPLICATION_JSON)
51     public Response getDatabases(){
52         OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
53         try {
54             List<String> databases = client.getDatabases().get();
55             if (databases == null) {
56                 return Response.noContent().build();
57             }
58             String response = objectMapper.writeValueAsString(databases);
59             return Response.status(Response.Status.OK)
60                     .entity(response)
61                     .build();
62         } catch (Exception e) {
63             throw new InternalServerErrorException("Failed due to exception " + e.getMessage());
64         }
65     }
66
67     @GET
68     @Path("{name}")
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)
74                 .entity(response)
75                 .build();
76     }
77
78     @Path("{name}/table")
79     public TableResource getDatabaseTables(@PathParam("name") String name){
80         String csDatabaseName = this.caseSensitiveDatabaseName(name);
81         return new TableResource(nodeId, csDatabaseName);
82     }
83
84     private String caseSensitiveDatabaseName (String ciDatabaseName) {
85         OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
86         try {
87             List<String> databases = client.getDatabases().get();
88             if (databases == null) {
89                 return ciDatabaseName;
90             }
91             for (String csDatabaseName : databases) {
92                 if (csDatabaseName.equalsIgnoreCase(ciDatabaseName)) {
93                     return csDatabaseName;
94                 }
95             }
96             return ciDatabaseName;
97         } catch (Exception e) {
98             return ciDatabaseName;
99         }
100     }
101 }