Bug 2438: No serializer found for class.. BooleanBaseType
[netvirt.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / NodeResource.java
1 package org.opendaylight.ovsdb.northbound;
2
3 import com.fasterxml.jackson.databind.SerializationFeature;
4 import java.io.InputStream;
5 import java.util.List;
6
7 import javax.ws.rs.Consumes;
8 import javax.ws.rs.DELETE;
9 import javax.ws.rs.GET;
10 import javax.ws.rs.POST;
11 import javax.ws.rs.PUT;
12 import javax.ws.rs.Path;
13 import javax.ws.rs.PathParam;
14 import javax.ws.rs.Produces;
15 import javax.ws.rs.core.MediaType;
16 import javax.ws.rs.core.Response;
17
18 import org.opendaylight.controller.northbound.commons.RestMessages;
19 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
20 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.utils.ServiceHelper;
23 import org.opendaylight.ovsdb.lib.OvsdbClient;
24 import org.opendaylight.ovsdb.plugin.api.Connection;
25 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.DeserializationFeature;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.google.common.collect.Lists;
31
32 /**
33  * Northbound Interface for OVSDB Nodes
34  */
35 public class NodeResource {
36     ObjectMapper objectMapper;
37     public NodeResource () {
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     public static Node getOvsdbNode(String nodeId, Object bundleClassRef) {
44         OvsdbConnectionService connectionService =
45                 (OvsdbConnectionService)ServiceHelper.
46                         getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
47         if (connectionService == null) {
48             throw new ServiceUnavailableException("Ovsdb ConnectionService "
49                     + RestMessages.SERVICEUNAVAILABLE.toString());
50         }
51
52         Node node = connectionService.getNode(nodeId);
53         if (node == null) {
54             throw new ResourceNotFoundException("Node "+nodeId+" not found");
55         }
56
57         return node;
58     }
59
60     public static Connection getOvsdbConnection(String nodeId, Object bundleClassRef) {
61         OvsdbConnectionService connectionService =
62                 (OvsdbConnectionService)ServiceHelper.
63                         getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
64         if (connectionService == null) {
65             throw new ServiceUnavailableException("Ovsdb ConnectionService "
66                     + RestMessages.SERVICEUNAVAILABLE.toString());
67         }
68
69         Node node = connectionService.getNode(nodeId);
70         if (node == null) {
71             throw new ResourceNotFoundException("Node "+nodeId+" not found");
72         }
73
74         Connection connection = connectionService.getConnection(node);
75         if (connection == null) {
76             throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
77         }
78
79         return connection;
80     }
81
82     public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
83         Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
84         OvsdbClient client = connection.getClient();
85         if (client == null) {
86             throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
87         }
88         return client;
89     }
90
91     @GET
92     @Produces(MediaType.APPLICATION_JSON)
93     public Response getNodes() throws JsonProcessingException {
94         OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
95         List<Node> nodes = connectionService.getNodes();
96         if (nodes == null) return Response.noContent().build();
97
98         List<String> nodeIds = Lists.newArrayList();
99         for (Node node : nodes) {
100             nodeIds.add(node.toString());
101         }
102
103         String response = objectMapper.writeValueAsString(nodeIds);
104         return Response.status(Response.Status.OK)
105                 .entity(response)
106                 .build();
107     }
108
109     @POST
110     @Consumes(MediaType.APPLICATION_JSON)
111     @Produces(MediaType.APPLICATION_JSON)
112     public Response createNode(InputStream is){
113         return Response.noContent().build();
114     }
115
116     @GET
117     @Path("{id}")
118     @Produces(MediaType.APPLICATION_JSON)
119     public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
120         OvsdbClient client = NodeResource.getOvsdbClient(id, this);
121         String response = objectMapper.writeValueAsString(client.getConnectionInfo());
122         return Response.status(Response.Status.OK)
123                 .entity(response)
124                 .build();
125     }
126
127     @PUT
128     @Path("{id}")
129     @Consumes(MediaType.APPLICATION_JSON)
130     @Produces(MediaType.APPLICATION_JSON)
131     public Response updateNode(@PathParam("id") String id, InputStream is){
132         return Response.noContent().build();
133     }
134
135     @DELETE
136     @Path("{id}")
137     public Response deleteNode(@PathParam("id") String id){
138         return Response.noContent().build();
139     }
140
141     @Path("{id}/database")
142     public DatabaseResource getNodeDatabase(@PathParam("id") String nodeId){
143         return new DatabaseResource(nodeId);
144     }
145
146 }