OVSDB V3 northbound failed to find node 70/15870/2
authorSam Hague <shague@redhat.com>
Sun, 1 Mar 2015 22:56:43 +0000 (17:56 -0500)
committerSam Hague <shague@redhat.com>
Sun, 1 Mar 2015 23:56:47 +0000 (18:56 -0500)
The Node.fromString(id) no longer works with the latest adsal deprecation changes.

A new helper method getNode() was previously created as a workaround but the v3 code was not using it. This commit fixes that.

Also added unit test for the v3 northbound.

Change-Id: I3bdffe52b8ba63b6bd52da482b0a3b1a162b6db8
Signed-off-by: Sam Hague <shague@redhat.com>
northbound/src/main/java/org/opendaylight/ovsdb/northbound/DatabaseResource.java
northbound/src/main/java/org/opendaylight/ovsdb/northbound/NodeResource.java
northbound/src/main/java/org/opendaylight/ovsdb/northbound/RowResource.java
northbound/src/main/java/org/opendaylight/ovsdb/northbound/TableResource.java
northbound/src/test/java/org/opendaylight/ovsdb/northbound/NodeResourceTest.java [new file with mode: 0644]

index caf03d2f07da58c9746eba8f7b2372c538a44e70..70bde89d730cb61bac359d493afab7d2fcc6781e 100644 (file)
@@ -35,14 +35,14 @@ public class DatabaseResource {
 
     private DatabaseSchema getDatabaseSchema (String databaseName) {
         String csDatabaseName = this.caseSensitiveDatabaseName(databaseName);
-        OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+        OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
         return client.getDatabaseSchema(csDatabaseName);
     }
 
     @GET
     @Produces(MediaType.APPLICATION_JSON)
     public Response getDatabases(){
-        OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+        OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
         try {
             List<String> databases = client.getDatabases().get();
             if (databases == null) {
@@ -75,10 +75,7 @@ public class DatabaseResource {
     }
 
     private String caseSensitiveDatabaseName (String ciDatabaseName) {
-        Node node = Node.fromString(nodeId);
-        OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
-        OvsdbClient client = connectionService.getConnection(node).getClient();
-
+        OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
         try {
             List<String> databases = client.getDatabases().get();
             if (databases == null) return ciDatabaseName;
index d80658b456e8e14b70a32a60270cea0cc8b2d2c7..e3ed0d2fb6bfdc42254b4708002c935989851af6 100644 (file)
@@ -14,11 +14,12 @@ 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.controller.sal.core.Node;
 import org.opendaylight.controller.sal.utils.ServiceHelper;
 import org.opendaylight.ovsdb.lib.OvsdbClient;
-import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
 import org.opendaylight.ovsdb.plugin.api.Connection;
 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
 
@@ -37,17 +38,48 @@ public class NodeResource {
         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
     }
 
-    public static OvsdbClient getOvsdbConnection(String nodeId, Object bundleClassRef) {
-        Node node = Node.fromString(nodeId);
+    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");
         }
-        OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, bundleClassRef);
+
+        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");
         }
-        OvsdbClient client = connectionService.getConnection(node).getClient();
+
+        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);
         }
@@ -83,13 +115,8 @@ public class NodeResource {
     @Path("{id}")
     @Produces(MediaType.APPLICATION_JSON)
     public Response getNode(@PathParam("id") String id) throws JsonProcessingException {
-        OvsdbConnectionService connectionService = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
-        OvsdbClient client = NodeResource.getOvsdbConnection(id, this);
-        if (client == null) {
-            throw new ResourceNotFoundException("Node "+id+" not found");
-        }
-        OvsdbConnectionInfo connectionInfo = client.getConnectionInfo();
-        String response = objectMapper.writeValueAsString(connectionInfo);
+        OvsdbClient client = NodeResource.getOvsdbClient(id, this);
+        String response = objectMapper.writeValueAsString(client.getConnectionInfo());
         return Response.status(Response.Status.OK)
                 .entity(response)
                 .build();
index 148d90eb7605842e061ea140960f49b615105b96..b1f3435fd0d1bc07c185f65ad265457fefaf664a 100644 (file)
@@ -61,7 +61,7 @@ public class RowResource {
             rowNodeStrBuilder.append(line);
         }
         JsonNode jsonNode = objectMapper.readTree(rowNodeStrBuilder.toString());
-        OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+        OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
         return OvsdbRow.fromJsonNode(client, OvsVswitchdSchemaConstants.DATABASE_NAME, jsonNode);
     }
 
@@ -75,7 +75,7 @@ public class RowResource {
             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeId);
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
         Map<UUID,Row<GenericTableSchema>> rows = null;
         try {
             rows = ovsdbTable.getRows(node, databaseName, tableName);
@@ -92,7 +92,6 @@ public class RowResource {
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
     public Response createRow(InputStream stream) throws IOException {
-        Node node = Node.fromString(nodeId);
         OvsdbRow localRow = this.getOvsdbRow(stream);
         if (localRow == null) {
             return Response.status(Response.Status.BAD_REQUEST).build();
@@ -104,6 +103,7 @@ public class RowResource {
             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
         Row row = ovsdbTable.insertTree(node, OvsVswitchdSchemaConstants.DATABASE_NAME, tableName,
                 localRow.getParentTable(), new UUID(localRow.getParentUuid()), localRow.getParentColumn(),
                 localRow.getRow());
@@ -124,7 +124,7 @@ public class RowResource {
             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeId);
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
         Row<GenericTableSchema> row = null;
         try {
             row = ovsdbTable.getRow(node, databaseName, tableName, new UUID(id));
@@ -142,7 +142,6 @@ public class RowResource {
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
     public Response updateRow(@PathParam("id") String id, InputStream stream) throws IOException{
-        Node node = Node.fromString(nodeId);
         OvsdbRow localRow = this.getOvsdbRow(stream);
         if (localRow == null) {
             return Response.status(Response.Status.BAD_REQUEST).build();
@@ -153,6 +152,7 @@ public class RowResource {
             throw new ServiceUnavailableException("OVS Configuration Service " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
         Row<GenericTableSchema> row = ovsdbTable.updateRow(node, databaseName, tableName, new UUID(id), localRow.getRow(), true);
         String response = objectMapper.writeValueAsString(row);
         return Response.status(Response.Status.OK)
@@ -171,7 +171,7 @@ public class RowResource {
             throw new ServiceUnavailableException("Ovsdb ConfigurationService " + RestMessages.SERVICEUNAVAILABLE.toString());
         }
 
-        Node node = Node.fromString(nodeId);
+        Node node = NodeResource.getOvsdbNode(nodeId, this);
         try {
             ovsdbTable.deleteRow(node, databaseName, tableName, new UUID(id));
         } catch (Exception e) {
index c5501b3a7d52758b265e5d15cc1fabc2d7bd8e17..61bf7f9953565b2228f4707c7f231cb795efceef 100644 (file)
@@ -35,7 +35,7 @@ public class TableResource {
     }
 
     private DatabaseSchema getDatabaseSchema (String databaseName) {
-        OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+        OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
         return client.getDatabaseSchema(databaseName);
     }
 
diff --git a/northbound/src/test/java/org/opendaylight/ovsdb/northbound/NodeResourceTest.java b/northbound/src/test/java/org/opendaylight/ovsdb/northbound/NodeResourceTest.java
new file mode 100644 (file)
index 0000000..159c9d2
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ *  Copyright (C) 2015 Red Hat, Inc.
+ *
+ *  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
+ *
+ *  Authors : Sam Hague
+ */
+package org.opendaylight.ovsdb.northbound;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.when;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
+import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
+import org.opendaylight.controller.sal.core.Node;
+import org.opendaylight.controller.sal.core.NodeConnector;
+import org.opendaylight.controller.sal.utils.ServiceHelper;
+import org.opendaylight.ovsdb.plugin.api.Connection;
+import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
+import org.opendaylight.ovsdb.plugin.impl.ConnectionServiceImpl;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ServiceHelper.class)
+public class NodeResourceTest {
+    private static final String OVS = "OVS";
+    private static final String IDENTIFIER = "192.168.120.31:45001";
+    private static final String OVS_IDENTIFIER = OVS + "|" + IDENTIFIER;
+    private static final String BAD_IDENTIFIER = "BAD" + "|" + IDENTIFIER;
+
+    @Test
+    public void testGetOvsdbNode () {
+        Node.NodeIDType.registerIDType(OVS, String.class);
+        NodeConnector.NodeConnectorIDType.registerIDType(OVS, String.class, OVS);
+        ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
+        Connection connection = new Connection(IDENTIFIER, null);
+        connectionService.putOvsdbConnection(IDENTIFIER, connection);
+
+        PowerMockito.mockStatic(ServiceHelper.class);
+        when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
+                .thenReturn(null)
+                .thenReturn(connectionService)
+                .thenReturn(connectionService);
+
+        Node node = null;
+        try {
+            node = NodeResource.getOvsdbNode(IDENTIFIER, this);
+            fail("Expected an ServiceUnavailableException to be thrown");
+        } catch (ServiceUnavailableException e) {
+            assertSame(ServiceUnavailableException.class, e.getClass());
+        }
+
+        try {
+            node = NodeResource.getOvsdbNode(BAD_IDENTIFIER, this);
+            fail("Expected an ResourceNotFoundException to be thrown");
+        } catch (ResourceNotFoundException e) {
+            assertSame(ResourceNotFoundException.class, e.getClass());
+        }
+
+        node = NodeResource.getOvsdbNode(OVS_IDENTIFIER, this);
+        assertNotNull("Node " + OVS_IDENTIFIER + " is null", node);
+    }
+
+    @Test
+    public void testGetOvsdbConnection () {
+        Node.NodeIDType.registerIDType(OVS, String.class);
+        NodeConnector.NodeConnectorIDType.registerIDType(OVS, String.class, OVS);
+        ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
+        Connection connection = new Connection(IDENTIFIER, null);
+        connectionService.putOvsdbConnection(IDENTIFIER, connection);
+
+        PowerMockito.mockStatic(ServiceHelper.class);
+        when(ServiceHelper.getGlobalInstance(eq(OvsdbConnectionService.class), anyObject()))
+                .thenReturn(null)
+                .thenReturn(connectionService)
+                .thenReturn(connectionService);
+
+        Connection testConnection = null;
+        try {
+            testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
+            fail("Expected an ServiceUnavailableException to be thrown");
+        } catch (ServiceUnavailableException e) {
+            assertSame(ServiceUnavailableException.class, e.getClass());
+        }
+
+        try {
+            testConnection = NodeResource.getOvsdbConnection(BAD_IDENTIFIER, this);
+            fail("Expected an ResourceNotFoundException to be thrown");
+        } catch (ResourceNotFoundException e) {
+            assertSame(ResourceNotFoundException.class, e.getClass());
+        }
+
+        testConnection = NodeResource.getOvsdbConnection(IDENTIFIER, this);
+        assertNotNull("Connection " + OVS_IDENTIFIER + " is null", testConnection);
+    }
+}