2 * Copyright (c) 2014 Red Hat, Inc. and others. All rights reserved.
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
8 * Authors : Madhu Venugopal, Sam Hague
10 package org.opendaylight.ovsdb.integrationtest;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
18 import java.io.IOException;
19 import java.net.InetAddress;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.Properties;
24 import java.util.concurrent.Callable;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
32 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.ovsdb.lib.OvsdbClient;
35 import org.opendaylight.ovsdb.lib.OvsdbConnection;
36 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
37 import org.opendaylight.ovsdb.plugin.api.ConnectionConstants;
38 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
40 import org.junit.Rule;
41 import org.junit.rules.TestRule;
42 import org.junit.rules.TestWatcher;
43 import org.junit.runner.Description;
44 import org.osgi.framework.Bundle;
45 import org.osgi.framework.BundleContext;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public abstract class OvsdbIntegrationTestBase {
50 private static final Logger LOG = LoggerFactory.getLogger(OvsdbIntegrationTestBase.class);
51 protected final static String IDENTIFIER = "TEST";
52 protected final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
53 protected final static String SERVER_PORT = "ovsdbserver.port";
54 protected final static String CONNECTION_TYPE = "ovsdbserver.connection";
55 protected final static String CONNECTION_TYPE_ACTIVE = "active";
56 protected final static String CONNECTION_TYPE_PASSIVE = "passive";
57 protected final static int CONNECTION_INIT_TIMEOUT = 10000;
58 protected final static String DEFAULT_SERVER_PORT = "6640";
60 private static boolean bundlesReady = false;
61 public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
62 public final static String HARDWARE_VTEP = "hardware_vtep";
64 public Properties loadProperties() {
65 Properties props = new Properties(System.getProperties());
69 public Node getPluginTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
70 Properties props = loadProperties();
71 String addressStr = props.getProperty(SERVER_IPADDRESS);
72 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
73 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
76 OvsdbConnectionService
77 connection = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
78 // If the connection type is active, controller connects to the ovsdb-server
79 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
80 if (addressStr == null) {
84 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
85 params.put(ConnectionConstants.ADDRESS, addressStr);
86 params.put(ConnectionConstants.PORT, portStr);
87 node = connection.connect(IDENTIFIER, params);
88 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
89 // Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
90 Thread.sleep(CONNECTION_INIT_TIMEOUT);
91 List<Node> nodes = connection.getNodes();
93 assertTrue(nodes.size() > 0);
98 LOG.info("getPluginTestConnection: Successfully connected to {}", node);
100 fail("Connection parameter (" + CONNECTION_TYPE + ") must be active or passive");
105 public OvsdbClient getTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
106 Properties props = loadProperties();
107 String addressStr = props.getProperty(SERVER_IPADDRESS);
108 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
109 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
111 // If the connection type is active, controller connects to the ovsdb-server
112 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
113 if (addressStr == null) {
119 address = InetAddress.getByName(addressStr);
120 } catch (Exception e) {
121 System.out.println("Unable to resolve " + addressStr);
128 port = Integer.parseInt(portStr);
129 } catch (NumberFormatException e) {
130 System.out.println("Invalid port number : " + portStr);
135 OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
136 return connection.connect(address, port);
137 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
138 ExecutorService executor = Executors.newFixedThreadPool(1);
139 Future<OvsdbClient> passiveConnection = executor.submit(new PassiveListener());
140 return passiveConnection.get(60, TimeUnit.SECONDS);
142 fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
146 protected String usage() {
147 return "Integration Test needs a valid connection configuration as follows :\n" +
148 "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"+
149 "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
152 public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
153 OvsdbClient client = null;
155 public OvsdbClient call() throws Exception {
156 OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
157 connection.registerConnectionListener(this);
158 while (client == null) {
165 public void connected(OvsdbClient client) {
166 this.client = client;
170 public void disconnected(OvsdbClient client) {
171 assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());
176 public String stateToString(int state) {
180 case Bundle.INSTALLED:
182 case Bundle.RESOLVED:
184 case Bundle.UNINSTALLED:
185 return "UNINSTALLED";
187 return "Not CONVERTED";
191 public void areWeReady(BundleContext bc) throws InterruptedException {
193 LOG.info("Bundles already loaded");
197 boolean debugit = false;
198 Bundle b[] = bc.getBundles();
199 for (Bundle element : b) {
200 int state = element.getState();
201 if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
202 LOG.info("Bundle:" + element.getSymbolicName() + " state:"
203 + stateToString(state));
208 LOG.debug("Do some debugging because some bundle is unresolved");
209 Thread.sleep(600000);
212 // Assert if true, if false we are good to go!
213 assertFalse("There is a problem with loading the bundles.", debugit);
215 LOG.info("Bundles loaded");
219 * Method adds a log as each test method starts and finishes. This is useful when
220 * the test suite is used because the suites only print a final summary.
223 public TestRule watcher = new TestWatcher() {
225 protected void starting(Description description) {
226 LOG.info("TestWatcher: Starting test: {}",
227 description.getDisplayName());
231 protected void finished(Description description) {
232 LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());