Sonar clean-up: braces for control statements
[ovsdb.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) {
98             return Response.noContent().build();
99         }
100
101         List<String> nodeIds = Lists.newArrayList();
102         for (Node node : nodes) {
103             nodeIds.add(node.getId().getValue());
104         }
105         Collections.sort(nodeIds);
106
107         String response = objectMapper.writeValueAsString(nodeIds);
108         return Response.status(Response.Status.OK)
109                 .entity(response)
110                 .build();
111     }
112
113     @POST
114     @Consumes(MediaType.APPLICATION_JSON)
115     @Produces(MediaType.APPLICATION_JSON)
116     public Response createNode(InputStream is){
117         return Response.noContent().build();
118     }
119
120     @GET
121     @Path("{id}")
122     @Produces(MediaType.APPLICATION_JSON)
123     public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
124         OvsdbClient client = NodeResource.getOvsdbClient(id, this);
125         String response = objectMapper.writeValueAsString(client.getConnectionInfo());
126         return Response.status(Response.Status.OK)
127                 .entity(response)
128                 .build();
129     }
130
131     @PUT
132     @Path("{id}")
133     @Consumes(MediaType.APPLICATION_JSON)
134     @Produces(MediaType.APPLICATION_JSON)
135     public Response updateNode(@PathParam("id") String id, InputStream is){
136         return Response.noContent().build();
137     }
138
139     @DELETE
140     @Path("{id}")
141     public Response deleteNode(@PathParam("id") String id){
142         return Response.noContent().build();
143     }
144
145     @Path("{id}/database")
146     public DatabaseResource getNodeDatabase(@PathParam("id") String nodeId){
147         return new DatabaseResource(nodeId);
148     }
149
150 }