1 package org.opendaylight.ovsdb.northbound;
3 import com.fasterxml.jackson.databind.SerializationFeature;
4 import java.io.InputStream;
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;
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;
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;
33 * Northbound Interface for OVSDB Nodes
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);
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());
52 Node node = connectionService.getNode(nodeId);
54 throw new ResourceNotFoundException("Node "+nodeId+" not found");
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());
69 Node node = connectionService.getNode(nodeId);
71 throw new ResourceNotFoundException("Node "+nodeId+" not found");
74 Connection connection = connectionService.getConnection(node);
75 if (connection == null) {
76 throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
82 public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
83 Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
84 OvsdbClient client = connection.getClient();
86 throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
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();
98 List<String> nodeIds = Lists.newArrayList();
99 for (Node node : nodes) {
100 nodeIds.add(node.toString());
103 String response = objectMapper.writeValueAsString(nodeIds);
104 return Response.status(Response.Status.OK)
110 @Consumes(MediaType.APPLICATION_JSON)
111 @Produces(MediaType.APPLICATION_JSON)
112 public Response createNode(InputStream is){
113 return Response.noContent().build();
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)
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();
137 public Response deleteNode(@PathParam("id") String id){
138 return Response.noContent().build();
141 @Path("{id}/database")
142 public DatabaseResource getNodeDatabase(@PathParam("id") String nodeId){
143 return new DatabaseResource(nodeId);