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, Matt Oswalt
11 package org.opendaylight.ovsdb.schema.hardwarevtep;
13 import java.io.IOException;
14 import java.net.InetAddress;
15 import java.util.List;
17 import java.util.Properties;
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.ExecutorService;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.Future;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
26 import junit.framework.Assert;
28 import org.junit.Before;
29 import org.opendaylight.ovsdb.lib.OvsdbClient;
30 import org.opendaylight.ovsdb.lib.OvsdbConnection;
31 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
32 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
33 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
34 import org.opendaylight.ovsdb.lib.notation.Row;
35 import org.opendaylight.ovsdb.lib.notation.UUID;
36 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
38 import com.google.common.util.concurrent.ListenableFuture;
40 public abstract class HardwareVtepSchemaTestBase implements OvsdbRPC.Callback{
41 private final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
42 private final static String SERVER_PORT = "ovsdbserver.port";
43 private final static String DEFAULT_SERVER_PORT = "6640";
44 private final static String CONNECTION_TYPE = "ovsdbserver.connection";
45 private final static String CONNECTION_TYPE_ACTIVE = "active";
46 private final static String CONNECTION_TYPE_PASSIVE = "passive";
47 private final static String TEST_MANAGER_TARGET = "ptcp:10.12.0.15:6640"; //TODO: for future use
50 * Represents the Hardware VTEP Schema
52 public final static String HARDWARE_VTEP_SCHEMA = "hardware_vtep";
53 protected OvsdbClient ovs = null;
54 Boolean supportsHardwareVtepSchema;
56 DatabaseSchema dbSchema = null;
58 public Properties loadProperties() {
59 Properties props = new Properties(System.getProperties());
64 public void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException {
66 this.ovs = HardwareVtepSchemaSuiteIT.getOvsdbClient();
68 if (this.ovs == null) {
69 this.ovs = getTestConnection();
70 HardwareVtepSchemaSuiteIT.setOvsdbClient(this.ovs);
72 //retrieve list of databases from OVSDB server
73 ListenableFuture<List<String>> databases = ovs.getDatabases();
74 List<String> dbNames = databases.get();
75 Assert.assertNotNull(dbNames);
77 if (supportsHardwareVtepSchema == null) {
78 supportsHardwareVtepSchema = false;
80 //verify that HW VTEP schema is in the list of supported databases
81 for (String dbName : dbNames) {
82 if (dbName.equals(HARDWARE_VTEP_SCHEMA)) {
83 supportsHardwareVtepSchema = true;
89 if (supportsHardwareVtepSchema) {
90 //desired schema exists, retrieve contents of specified database
91 dbSchema = this.ovs.getSchema(HARDWARE_VTEP_SCHEMA).get();
96 public boolean checkSchema() {
97 return supportsHardwareVtepSchema;
100 public OvsdbClient getTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
101 Properties props = loadProperties();
102 String addressStr = props.getProperty(SERVER_IPADDRESS);
103 String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
104 String connectionType = props.getProperty(CONNECTION_TYPE, "active");
106 // If the connection type is active, controller connects to the ovsdb-server
107 if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
108 if (addressStr == null) {
109 Assert.fail(usage());
114 address = InetAddress.getByName(addressStr);
115 } catch (Exception e) {
116 System.out.println("Unable to resolve " + addressStr);
123 port = Integer.parseInt(portStr);
124 } catch (NumberFormatException e) {
125 System.out.println("Invalid port number : " + portStr);
130 OvsdbConnection connection = OvsdbConnectionService.getService();
131 return connection.connect(address, port);
132 } else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
133 ExecutorService executor = Executors.newFixedThreadPool(1);
134 Future<OvsdbClient> passiveConnection = executor.submit(new PassiveListener());
135 return passiveConnection.get(60, TimeUnit.SECONDS);
137 Assert.fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
141 public UUID getGlobalTableUuid(OvsdbClient ovs, Map<String, Map<UUID, Row>> tableCache) {
142 Global glbl = ovs.getTypedRowWrapper(Global.class, null);
143 Map<UUID, Row> glblTbl = tableCache.get(glbl.getSchema().getName());
144 if (glblTbl != null) {
145 if (glblTbl.keySet().size() >= 1) {
146 return (UUID)glblTbl.keySet().toArray()[0];
152 private String usage() {
153 return "Integration Test needs a valid connection configuration as follows :\n" +
154 "active connection : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"+
155 "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
158 public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
159 OvsdbClient client = null;
161 public OvsdbClient call() throws Exception {
162 OvsdbConnection connection = OvsdbConnectionService.getService();
163 connection.registerConnectionListener(this);
164 while (client == null) {
171 public void connected(OvsdbClient client) {
172 this.client = client;
176 public void disconnected(OvsdbClient client) {
177 Assert.assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());