Squashed commit of the following:
[ovsdb.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / CompatOvsdbIntegrationTestBase.java
1 package org.opendaylight.ovsdb.integrationtest;
2
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import java.io.IOException;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Properties;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.TimeoutException;
14 import org.opendaylight.controller.sal.core.Node;
15 import org.opendaylight.controller.sal.utils.ServiceHelper;
16 import org.opendaylight.ovsdb.plugin.api.ConnectionConstants;
17 import org.opendaylight.ovsdb.compatibility.plugin.api.OvsdbConnectionService;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public abstract class CompatOvsdbIntegrationTestBase extends OvsdbIntegrationTestBase {
22     private static final Logger LOG = LoggerFactory.getLogger(OvsdbIntegrationTestBase.class);
23
24     public Node getCompatPluginTestConnection() throws IOException,
25             InterruptedException, ExecutionException, TimeoutException {
26         Properties props = loadProperties();
27         String addressStr = props.getProperty(SERVER_IPADDRESS);
28         String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
29         String connectionType = props.getProperty(CONNECTION_TYPE, "active");
30         org.opendaylight.controller.sal.core.Node node = null;
31
32         OvsdbConnectionService connection = (OvsdbConnectionService)
33                 ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
34         // If the connection type is active, controller connects to the ovsdb-server
35         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
36             if (addressStr == null) {
37                 fail(usage());
38             }
39
40             Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
41             params.put(ConnectionConstants.ADDRESS, addressStr);
42             params.put(ConnectionConstants.PORT, portStr);
43             node = connection.connect(IDENTIFIER, params);
44         }  else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
45             // Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
46             Thread.sleep(CONNECTION_INIT_TIMEOUT);
47             List<Node> nodes = connection.getNodes();
48             assertNotNull(nodes);
49             assertTrue(nodes.size() > 0);
50             node = nodes.get(0);
51         }
52
53         if (node != null) {
54             LOG.info("getPluginTestConnection: Successfully connected to {}", node);
55         } else {
56             fail("Connection parameter (" + CONNECTION_TYPE + ") must be active or passive");
57         }
58         return node;
59     }
60 }