2 * Copyright © 2015 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
9 package org.opendaylight.ovsdb.lib.it;
11 import java.io.IOException;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.Objects;
15 import java.util.Properties;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
23 import org.opendaylight.ovsdb.lib.OvsdbClient;
24 import org.opendaylight.ovsdb.lib.OvsdbConnection;
25 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
26 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Utilities for OVSDB integration tests.
33 public final class LibraryIntegrationTestUtils {
34 private static final Logger LOG = LoggerFactory.getLogger(LibraryIntegrationTestUtils.class);
35 public static final String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
36 public static final String SERVER_PORT = "ovsdbserver.port";
37 public static final String CONNECTION_TYPE = "ovsdbserver.connection";
38 public static final String OPEN_VSWITCH = "Open_vSwitch";
39 public static final String HARDWARE_VTEP = "hardware_vtep";
40 private static final String CONNECTION_TYPE_ACTIVE = "active";
41 private static final String CONNECTION_TYPE_PASSIVE = "passive";
42 private static final String DEFAULT_SERVER_PORT = "6640";
45 * Prevent instantiation of a utility class.
47 private LibraryIntegrationTestUtils() {
51 public static OvsdbClient getTestConnection(Object provider) throws IOException,
52 InterruptedException, ExecutionException, TimeoutException {
53 Properties props = System.getProperties();
54 String addressStr = props.getProperty(SERVER_IPADDRESS);
55 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
56 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
58 // If the connection type is active, controller connects to the ovsdb-server
59 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
60 if (addressStr == null) {
61 throw new IllegalArgumentException(usage());
66 address = InetAddress.getByName(addressStr);
67 } catch (UnknownHostException e) {
68 LOG.warn("Unable to resolve {}", addressStr, e);
74 port = Integer.parseInt(portStr);
75 } catch (NumberFormatException e) {
76 LOG.warn("Invalid port number: {}", portStr, e);
80 OvsdbConnection connection =
81 (OvsdbConnection) ServiceHelper.getGlobalInstance(OvsdbConnection.class, provider);
82 return connection.connect(address, port);
83 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
84 ExecutorService executor = Executors.newFixedThreadPool(1);
85 Future<OvsdbClient> passiveConnection = executor.submit(new PassiveListener());
86 return passiveConnection.get(60, TimeUnit.SECONDS);
88 throw new IllegalArgumentException("Connection parameter (" + CONNECTION_TYPE
89 + ") must be either active or passive");
92 private static String usage() {
93 return "Integration Test needs a valid connection configuration as follows :\n"
94 + "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x "
95 + " -Dovsdbserver.port=yyyy verify\n"
96 + "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
99 private static final class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
100 OvsdbClient client = null;
103 public OvsdbClient call() throws Exception {
104 OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
105 connection.registerConnectionListener(this);
106 while (client == null) {
113 public void connected(OvsdbClient newClient) {
114 this.client = newClient;
118 public void disconnected(OvsdbClient newClient) {
119 if (!Objects.equals(this.client.getConnectionInfo(), newClient.getConnectionInfo())) {
120 throw new IllegalStateException("disconnected unexpected client");