Speed up SouthboundIT through multi-threading test cases
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / NodeResource.java
index c6491d89d6fc168f18b783ec9d4fdc622fb75543..5ccdf4f41f3cda1f641c2e7077416ef95713f5d1 100644 (file)
@@ -1,6 +1,17 @@
+/*
+ * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
 package org.opendaylight.ovsdb.northbound;
 
+import com.fasterxml.jackson.databind.SerializationFeature;
 import java.io.InputStream;
+import java.util.Collections;
+import java.util.List;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
@@ -13,15 +24,98 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import org.opendaylight.controller.northbound.commons.RestMessages;
+import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
+import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
+import org.opendaylight.ovsdb.lib.OvsdbClient;
+import org.opendaylight.ovsdb.plugin.api.Connection;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.Lists;
+
 /**
  * Northbound Interface for OVSDB Nodes
  */
 public class NodeResource {
+    ObjectMapper objectMapper;
+    public NodeResource () {
+        objectMapper = new ObjectMapper();
+        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+    }
+
+    public static Node getOvsdbNode(String nodeId, Object bundleClassRef) {
+        OvsdbConnectionService connectionService =
+                (OvsdbConnectionService)ServiceHelper.
+                        getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
+        if (connectionService == null) {
+            throw new ServiceUnavailableException("Ovsdb ConnectionService "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+
+        Node node = connectionService.getNode(nodeId);
+        if (node == null) {
+            throw new ResourceNotFoundException("Node "+nodeId+" not found");
+        }
+
+        return node;
+    }
+
+    public static Connection getOvsdbConnection(String nodeId, Object bundleClassRef) {
+        OvsdbConnectionService connectionService =
+                (OvsdbConnectionService)ServiceHelper.
+                        getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
+        if (connectionService == null) {
+            throw new ServiceUnavailableException("Ovsdb ConnectionService "
+                    + RestMessages.SERVICEUNAVAILABLE.toString());
+        }
+
+        Node node = connectionService.getNode(nodeId);
+        if (node == null) {
+            throw new ResourceNotFoundException("Node "+nodeId+" not found");
+        }
+
+        Connection connection = connectionService.getConnection(node);
+        if (connection == null) {
+            throw new ResourceNotFoundException("Connection for "+nodeId+" not available");
+        }
+
+        return connection;
+    }
+
+    public static OvsdbClient getOvsdbClient(String nodeId, Object bundleClassRef) {
+        Connection connection = NodeResource.getOvsdbConnection(nodeId, bundleClassRef);
+        OvsdbClient client = connection.getClient();
+        if (client == null) {
+            throw new ResourceNotFoundException("No Ovsdb Client to handle Node "+nodeId);
+        }
+        return client;
+    }
 
     @GET
     @Produces(MediaType.APPLICATION_JSON)
-    public Response getNodes(){
-        return Response.noContent().build();
+    public Response getNodes() throws JsonProcessingException {
+        OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
+        List<Node> nodes = connectionService.getNodes();
+        if (nodes == null) {
+            return Response.noContent().build();
+        }
+
+        List<String> nodeIds = Lists.newArrayList();
+        for (Node node : nodes) {
+            nodeIds.add(node.getId().getValue());
+        }
+        Collections.sort(nodeIds);
+
+        String response = objectMapper.writeValueAsString(nodeIds);
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @POST
@@ -34,8 +128,12 @@ public class NodeResource {
     @GET
     @Path("{id}")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response getNode(@PathParam("id") String id){
-        return Response.noContent().build();
+    public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
+        OvsdbClient client = NodeResource.getOvsdbClient(id, this);
+        String response = objectMapper.writeValueAsString(client.getConnectionInfo());
+        return Response.status(Response.Status.OK)
+                .entity(response)
+                .build();
     }
 
     @PUT