Sort reported nodes
[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.Collections;
6 import java.util.List;
7
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;
18
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;
27
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;
32
33 /**
34  * Northbound Interface for OVSDB Nodes
35  */
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);
42     }
43
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());
51         }
52
53         Node node = connectionService.getNode(nodeId);
54         if (node == null) {
55             throw new ResourceNotFoundException("Node "+nodeId+" not found");
56         }
57
58         return node;
59     }
60
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());
68         }
69
70         Node node = connectionService.getNode(nodeId);
71         if (node == null) {
72             throw new ResourceNotFoundException("Node "+nodeId+" not found");
73         }
74
75         Connection connection = connectionService.getConnection(node);
76         if (connection == null) {
77             throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
78         }
79
80         return connection;
81     }
82
83     public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
84         Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
85         OvsdbClient client = connection.getClient();
86         if (client == null) {
87             throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
88         }
89         return client;
90     }
91
92     @GET
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();
98
99         List<String> nodeIds = Lists.newArrayList();
100         for (Node node : nodes) {
101             nodeIds.add(node.getId().getValue());
102         }
103         Collections.sort(nodeIds);
104
105         String response = objectMapper.writeValueAsString(nodeIds);
106         return Response.status(Response.Status.OK)
107                 .entity(response)
108                 .build();
109     }
110
111     @POST
112     @Consumes(MediaType.APPLICATION_JSON)
113     @Produces(MediaType.APPLICATION_JSON)
114     public Response createNode(InputStream is){
115         return Response.noContent().build();
116     }
117
118     @GET
119     @Path("{id}")
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)
125                 .entity(response)
126                 .build();
127     }
128
129     @PUT
130     @Path("{id}")
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();
135     }
136
137     @DELETE
138     @Path("{id}")
139     public Response deleteNode(@PathParam("id") String id){
140         return Response.noContent().build();
141     }
142
143     @Path("{id}/database")
144     public DatabaseResource getNodeDatabase(@PathParam("id") String nodeId){
145         return new DatabaseResource(nodeId);
146     }
147
148 }