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>
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) {
}
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;
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;
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);
}
@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();
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);
}
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);
@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();
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());
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));
@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();
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)
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) {
}
private DatabaseSchema getDatabaseSchema (String databaseName) {
- OvsdbClient client = NodeResource.getOvsdbConnection(nodeId, this);
+ OvsdbClient client = NodeResource.getOvsdbClient(nodeId, this);
return client.getDatabaseSchema(databaseName);
}
--- /dev/null
+/*
+ * 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);
+ }
+}