Speed up SouthboundIT through multi-threading test cases
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / NodeResource.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.northbound;
10
11 import com.fasterxml.jackson.databind.SerializationFeature;
12 import java.io.InputStream;
13 import java.util.Collections;
14 import java.util.List;
15
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.DELETE;
18 import javax.ws.rs.GET;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26
27 import org.opendaylight.controller.northbound.commons.RestMessages;
28 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
29 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
30 import org.opendaylight.ovsdb.lib.OvsdbClient;
31 import org.opendaylight.ovsdb.plugin.api.Connection;
32 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
33 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.google.common.collect.Lists;
40
41 /**
42  * Northbound Interface for OVSDB Nodes
43  */
44 public class NodeResource {
45     ObjectMapper objectMapper;
46     public NodeResource () {
47         objectMapper = new ObjectMapper();
48         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
49         objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
50     }
51
52     public static Node getOvsdbNode(String nodeId, Object bundleClassRef) {
53         OvsdbConnectionService connectionService =
54                 (OvsdbConnectionService)ServiceHelper.
55                         getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
56         if (connectionService == null) {
57             throw new ServiceUnavailableException("Ovsdb ConnectionService "
58                     + RestMessages.SERVICEUNAVAILABLE.toString());
59         }
60
61         Node node = connectionService.getNode(nodeId);
62         if (node == null) {
63             throw new ResourceNotFoundException("Node "+nodeId+" not found");
64         }
65
66         return node;
67     }
68
69     public static Connection getOvsdbConnection(String nodeId, Object bundleClassRef) {
70         OvsdbConnectionService connectionService =
71                 (OvsdbConnectionService)ServiceHelper.
72                         getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
73         if (connectionService == null) {
74             throw new ServiceUnavailableException("Ovsdb ConnectionService "
75                     + RestMessages.SERVICEUNAVAILABLE.toString());
76         }
77
78         Node node = connectionService.getNode(nodeId);
79         if (node == null) {
80             throw new ResourceNotFoundException("Node "+nodeId+" not found");
81         }
82
83         Connection connection = connectionService.getConnection(node);
84         if (connection == null) {
85             throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
86         }
87
88         return connection;
89     }
90
91     public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
92         Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
93         OvsdbClient client = connection.getClient();
94         if (client == null) {
95             throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
96         }
97         return client;
98     }
99
100     @GET
101     @Produces(MediaType.APPLICATION_JSON)
102     public Response getNodes() throws JsonProcessingException {
103         OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
104         List<Node> nodes = connectionService.getNodes();
105         if (nodes == null) {
106             return Response.noContent().build();
107         }
108
109         List<String> nodeIds = Lists.newArrayList();
110         for (Node node : nodes) {
111             nodeIds.add(node.getId().getValue());
112         }
113         Collections.sort(nodeIds);
114
115         String response = objectMapper.writeValueAsString(nodeIds);
116         return Response.status(Response.Status.OK)
117                 .entity(response)
118                 .build();
119     }
120
121     @POST
122     @Consumes(MediaType.APPLICATION_JSON)
123     @Produces(MediaType.APPLICATION_JSON)
124     public Response createNode(InputStream is){
125         return Response.noContent().build();
126     }
127
128     @GET
129     @Path("{id}")
130     @Produces(MediaType.APPLICATION_JSON)
131     public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
132         OvsdbClient client = NodeResource.getOvsdbClient(id, this);
133         String response = objectMapper.writeValueAsString(client.getConnectionInfo());
134         return Response.status(Response.Status.OK)
135                 .entity(response)
136                 .build();
137     }
138
139     @PUT
140     @Path("{id}")
141     @Consumes(MediaType.APPLICATION_JSON)
142     @Produces(MediaType.APPLICATION_JSON)
143     public Response updateNode(@PathParam("id") String id, InputStream is){
144         return Response.noContent().build();
145     }
146
147     @DELETE
148     @Path("{id}")
149     public Response deleteNode(@PathParam("id") String id){
150         return Response.noContent().build();
151     }
152
153     @Path("{id}/database")
154     public DatabaseResource getNodeDatabase(@PathParam("id") String nodeId){
155         return new DatabaseResource(nodeId);
156     }
157
158 }