Normalize Bundle Naming
[ovsdb.git] / schemas / hardwarevtep / src / test / java / org / opendaylight / ovsdb / schema / hardwarevtep / OvsdbTestBase.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal
9  */
10
11 package org.opendaylight.ovsdb.schema.hardwarevtep;
12
13 import java.io.IOException;
14 import java.net.InetAddress;
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
24 import junit.framework.Assert;
25
26 import org.opendaylight.ovsdb.lib.OvsdbClient;
27 import org.opendaylight.ovsdb.lib.OvsdbConnection;
28 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
29 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
30 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
31
32 public abstract class OvsdbTestBase implements OvsdbRPC.Callback{
33     private final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
34     private final static String SERVER_PORT = "ovsdbserver.port";
35     private final static String DEFAULT_SERVER_PORT = "6640";
36     private final static String CONNECTION_TYPE = "ovsdbserver.connection";
37     private final static String CONNECTION_TYPE_ACTIVE = "active";
38     private final static String CONNECTION_TYPE_PASSIVE = "passive";
39
40     /**
41      * Represents the Hardware VTEP Schema
42      */
43     public final static String HARDWARE_VTEP_SCHEMA = "hardware_vtep";
44
45     public Properties loadProperties() {
46         Properties props = new Properties(System.getProperties());
47         return props;
48     }
49
50
51     public OvsdbClient getTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
52         Properties props = loadProperties();
53         String addressStr = props.getProperty(SERVER_IPADDRESS);
54         String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
55         String connectionType = props.getProperty(CONNECTION_TYPE, "active");
56
57         // If the connection type is active, controller connects to the ovsdb-server
58         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
59             if (addressStr == null) {
60                 Assert.fail(usage());
61             }
62
63             InetAddress address;
64             try {
65                 address = InetAddress.getByName(addressStr);
66             } catch (Exception e) {
67                 System.out.println("Unable to resolve " + addressStr);
68                 e.printStackTrace();
69                 return null;
70             }
71
72             Integer port;
73             try {
74                 port = Integer.parseInt(portStr);
75             } catch (NumberFormatException e) {
76                 System.out.println("Invalid port number : " + portStr);
77                 e.printStackTrace();
78                 return null;
79             }
80
81             OvsdbConnection connection = OvsdbConnectionService.getService();
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);
87         }
88         Assert.fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
89         return null;
90     }
91
92     private 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 -Dovsdbserver.port=yyyy verify\n"+
95                "passive connection : mvn -Pintegrationtest -Dovsdbserver.connection=passive verify\n";
96     }
97
98     public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
99         OvsdbClient client = null;
100         @Override
101         public OvsdbClient call() throws Exception {
102             OvsdbConnection connection = OvsdbConnectionService.getService();
103             connection.registerForPassiveConnection(this);
104             while (client == null) {
105                 Thread.sleep(500);
106             }
107             return client;
108         }
109
110         @Override
111         public void connected(OvsdbClient client) {
112             this.client = client;
113         }
114
115         @Override
116         public void disconnected(OvsdbClient client) {
117             Assert.assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());
118             this.client = null;
119         }
120     }
121 }