Merge branch 'master' into topic/schema
[ovsdb.git] / schemas / Open_vSwitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / 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.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;
22
23 import junit.framework.Assert;
24
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;
30
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";
37
38     private final static String DEFAULT_SERVER_PORT = "6640";
39
40     /**
41      * Represents the Open Vswitch Schema
42      */
43     public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
44
45     public Properties loadProperties() {
46         Properties props = new Properties(System.getProperties());
47         return props;
48     }
49
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");
55
56         // If the connection type is active, controller connects to the ovsdb-server
57         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
58             if (addressStr == null) {
59                 Assert.fail(usage());
60             }
61
62             InetAddress address;
63             try {
64                 address = InetAddress.getByName(addressStr);
65             } catch (Exception e) {
66                 System.out.println("Unable to resolve " + addressStr);
67                 e.printStackTrace();
68                 return null;
69             }
70
71             Integer port;
72             try {
73                 port = Integer.parseInt(portStr);
74             } catch (NumberFormatException e) {
75                 System.out.println("Invalid port number : " + portStr);
76                 e.printStackTrace();
77                 return null;
78             }
79
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);
86         }
87         Assert.fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
88         return null;
89     }
90
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";
95     }
96
97     public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
98         OvsdbClient client = null;
99         @Override
100         public OvsdbClient call() throws Exception {
101             OvsdbConnection connection = OvsdbConnectionService.getService();
102             connection.registerForPassiveConnection(this);
103             while (client == null) {
104                 Thread.sleep(500);
105             }
106             return client;
107         }
108
109         @Override
110         public void connected(OvsdbClient client) {
111             this.client = client;
112         }
113
114         @Override
115         public void disconnected(OvsdbClient client) {
116             Assert.assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());
117             this.client = null;
118         }
119     }
120 }