1 package org.opendaylight.ovsdb.northbound;
3 import com.fasterxml.jackson.databind.SerializationFeature;
4 import java.io.InputStream;
5 import java.util.Collections;
8 import javax.ws.rs.Consumes;
9 import javax.ws.rs.DELETE;
10 import javax.ws.rs.GET;
11 import javax.ws.rs.POST;
12 import javax.ws.rs.PUT;
13 import javax.ws.rs.Path;
14 import javax.ws.rs.PathParam;
15 import javax.ws.rs.Produces;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.Response;
19 import org.opendaylight.controller.northbound.commons.RestMessages;
20 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
21 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
22 import org.opendaylight.ovsdb.lib.OvsdbClient;
23 import org.opendaylight.ovsdb.plugin.api.Connection;
24 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
25 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.DeserializationFeature;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.google.common.collect.Lists;
34 * Northbound Interface for OVSDB Nodes
36 public class NodeResource {
37 ObjectMapper objectMapper;
38 public NodeResource () {
39 objectMapper = new ObjectMapper();
40 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
41 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
44 public static Node getOvsdbNode(String nodeId, Object bundleClassRef) {
45 OvsdbConnectionService connectionService =
46 (OvsdbConnectionService)ServiceHelper.
47 getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
48 if (connectionService == null) {
49 throw new ServiceUnavailableException("Ovsdb ConnectionService "
50 + RestMessages.SERVICEUNAVAILABLE.toString());
53 Node node = connectionService.getNode(nodeId);
55 throw new ResourceNotFoundException("Node "+nodeId+" not found");
61 public static Connection getOvsdbConnection(String nodeId, Object bundleClassRef) {
62 OvsdbConnectionService connectionService =
63 (OvsdbConnectionService)ServiceHelper.
64 getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
65 if (connectionService == null) {
66 throw new ServiceUnavailableException("Ovsdb ConnectionService "
67 + RestMessages.SERVICEUNAVAILABLE.toString());
70 Node node = connectionService.getNode(nodeId);
72 throw new ResourceNotFoundException("Node "+nodeId+" not found");
75 Connection connection = connectionService.getConnection(node);
76 if (connection == null) {
77 throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
83 public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
84 Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
85 OvsdbClient client = connection.getClient();
87 throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
93 @Produces(MediaType.APPLICATION_JSON)
94 public Response getNodes() throws JsonProcessingException {
95 OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
96 List<Node> nodes = connectionService.getNodes();
97 if (nodes == null) return Response.noContent().build();
99 List<String> nodeIds = Lists.newArrayList();
100 for (Node node : nodes) {
101 nodeIds.add(node.getId().getValue());
103 Collections.sort(nodeIds);
105 String response = objectMapper.writeValueAsString(nodeIds);
106 return Response.status(Response.Status.OK)
112 @Consumes(MediaType.APPLICATION_JSON)
113 @Produces(MediaType.APPLICATION_JSON)
114 public Response createNode(InputStream is){
115 return Response.noContent().build();
120 @Produces(MediaType.APPLICATION_JSON)
121 public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
122 OvsdbClient client = NodeResource.getOvsdbClient(id, this);
123 String response = objectMapper.writeValueAsString(client.getConnectionInfo());
124 return Response.status(Response.Status.OK)
131 @Consumes(MediaType.APPLICATION_JSON)
132 @Produces(MediaType.APPLICATION_JSON)
133 public Response updateNode(@PathParam("id") String id, InputStream is){
134 return Response.noContent().build();
139 public Response deleteNode(@PathParam("id") String id){
140 return Response.noContent().build();
143 @Path("{id}/database")
144 public DatabaseResource getNodeDatabase(@PathParam("id") String nodeId){
145 return new DatabaseResource(nodeId);