2 * Copyright (C) 2014 Red Hat, Inc.
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
11 package org.opendaylight.ovsdb.schema.openvswitch;
12 import java.io.IOException;
13 import java.net.InetAddress;
14 import java.util.Properties;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
23 import junit.framework.Assert;
25 import org.opendaylight.ovsdb.lib.OvsdbClient;
26 import org.opendaylight.ovsdb.lib.OvsdbConnection;
27 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
28 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
29 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
31 public abstract class OvsdbTestBase implements OvsdbRPC.Callback{
32 private final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
33 private final static String SERVER_PORT = "ovsdbserver.port";
34 private final static String CONNECTION_TYPE = "ovsdbserver.connection";
35 private final static String CONNECTION_TYPE_ACTIVE = "active";
36 private final static String CONNECTION_TYPE_PASSIVE = "passive";
38 private final static String DEFAULT_SERVER_PORT = "6640";
41 * Represents the Open Vswitch Schema
43 public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
45 public Properties loadProperties() {
46 Properties props = new Properties(System.getProperties());
50 public OvsdbClient getTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
51 Properties props = loadProperties();
52 String addressStr = props.getProperty(SERVER_IPADDRESS);
53 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
54 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
56 // If the connection type is active, controller connects to the ovsdb-server
57 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
58 if (addressStr == null) {
64 address = InetAddress.getByName(addressStr);
65 } catch (Exception e) {
66 System.out.println("Unable to resolve " + addressStr);
73 port = Integer.parseInt(portStr);
74 } catch (NumberFormatException e) {
75 System.out.println("Invalid port number : " + portStr);
80 OvsdbConnection connection = OvsdbConnectionService.getService();
81 return connection.connect(address, port);
82 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
83 ExecutorService executor = Executors.newFixedThreadPool(1);
84 Future<OvsdbClient> passiveConnection = executor.submit(new PassiveListener());
85 return passiveConnection.get(60, TimeUnit.SECONDS);
87 Assert.fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
91 private String usage() {
92 return "Integration Test needs a valid connection configuration as follows :\n" +
93 "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"+
94 "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
97 public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
98 OvsdbClient client = null;
100 public OvsdbClient call() throws Exception {
101 OvsdbConnection connection = OvsdbConnectionService.getService();
102 connection.registerForPassiveConnection(this);
103 while (client == null) {
110 public void connected(OvsdbClient client) {
111 this.client = client;
115 public void disconnected(OvsdbClient client) {
116 Assert.assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());