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
10 package org.opendaylight.ovsdb.integrationtest;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
17 import java.io.IOException;
18 import java.net.InetAddress;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.Properties;
23 import java.util.concurrent.Callable;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
31 import org.opendaylight.controller.sal.connection.ConnectionConstants;
32 import org.opendaylight.controller.sal.core.Node;
33 import org.opendaylight.controller.sal.utils.ServiceHelper;
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.OvsdbConnectionService;
39 public abstract class OvsdbIntegrationTestBase {
40 protected final static String IDENTIFIER = "TEST";
41 protected final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
42 protected final static String SERVER_PORT = "ovsdbserver.port";
43 protected final static String CONNECTION_TYPE = "ovsdbserver.connection";
44 protected final static String CONNECTION_TYPE_ACTIVE = "active";
45 protected final static String CONNECTION_TYPE_PASSIVE = "passive";
46 protected final static int CONNECTION_INIT_TIMEOUT = 10000;
48 protected final static String DEFAULT_SERVER_PORT = "6640";
51 * Represents the Open Vswitch Schema
53 public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
54 public final static String HARDWARE_VTEP = "hardware_vtep";
56 public Properties loadProperties() {
57 Properties props = new Properties(System.getProperties());
61 public Node getPluginTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
62 Properties props = loadProperties();
63 String addressStr = props.getProperty(SERVER_IPADDRESS);
64 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
65 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
67 OvsdbConnectionService
68 connection = (OvsdbConnectionService)ServiceHelper.getGlobalInstance(OvsdbConnectionService.class, this);
69 // If the connection type is active, controller connects to the ovsdb-server
70 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
71 if (addressStr == null) {
75 Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
76 params.put(ConnectionConstants.ADDRESS, addressStr);
77 params.put(ConnectionConstants.PORT, portStr);
78 return connection.connect(IDENTIFIER, params);
79 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
80 // Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
81 Thread.sleep(CONNECTION_INIT_TIMEOUT);
82 List<Node> nodes = connection.getNodes();
84 assertTrue(nodes.size() > 0);
87 fail("Connection parameter ("+CONNECTION_TYPE+") must be active or passive");
91 public OvsdbClient getTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
92 Properties props = loadProperties();
93 String addressStr = props.getProperty(SERVER_IPADDRESS);
94 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
95 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
97 // If the connection type is active, controller connects to the ovsdb-server
98 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
99 if (addressStr == null) {
105 address = InetAddress.getByName(addressStr);
106 } catch (Exception e) {
107 System.out.println("Unable to resolve " + addressStr);
114 port = Integer.parseInt(portStr);
115 } catch (NumberFormatException e) {
116 System.out.println("Invalid port number : " + portStr);
121 OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
122 return connection.connect(address, port);
123 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
124 ExecutorService executor = Executors.newFixedThreadPool(1);
125 Future<OvsdbClient> passiveConnection = executor.submit(new PassiveListener());
126 return passiveConnection.get(60, TimeUnit.SECONDS);
128 fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
132 protected String usage() {
133 return "Integration Test needs a valid connection configuration as follows :\n" +
134 "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"+
135 "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
138 public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
139 OvsdbClient client = null;
141 public OvsdbClient call() throws Exception {
142 OvsdbConnection connection = (OvsdbConnection)ServiceHelper.getGlobalInstance(OvsdbConnection.class, this);
143 connection.registerConnectionListener(this);
144 while (client == null) {
151 public void connected(OvsdbClient client) {
152 this.client = client;
156 public void disconnected(OvsdbClient client) {
157 assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());