Refactor of the OVSDB Plugin
[ovsdb.git] / schemas / hardwarevtep / src / test / java / org / opendaylight / ovsdb / schema / hardwarevtep / HardwareVtepSchemaTestBase.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, Matt Oswalt
9  */
10
11 package org.opendaylight.ovsdb.schema.hardwarevtep;
12
13 import java.io.IOException;
14 import java.net.InetAddress;
15 import java.util.List;
16 import java.util.Map;
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;
25
26 import junit.framework.Assert;
27
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;
37
38 import com.google.common.util.concurrent.ListenableFuture;
39
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
48
49     /**
50      * Represents the Hardware VTEP Schema
51      */
52     public final static String HARDWARE_VTEP_SCHEMA = "hardware_vtep";
53     protected OvsdbClient ovs = null;
54     Boolean supportsHardwareVtepSchema;
55
56     DatabaseSchema dbSchema = null;
57
58     public Properties loadProperties() {
59         Properties props = new Properties(System.getProperties());
60         return props;
61     }
62
63     @Before
64     public  void setUp() throws IOException, ExecutionException, InterruptedException, TimeoutException {
65
66         this.ovs = HardwareVtepSchemaSuiteIT.getOvsdbClient();
67
68         if (this.ovs == null) {
69             this.ovs = getTestConnection();
70             HardwareVtepSchemaSuiteIT.setOvsdbClient(this.ovs);
71         }
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);
76
77         if (supportsHardwareVtepSchema == null) {
78             supportsHardwareVtepSchema = false;
79
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;
84                     break;
85                 }
86             }
87         }
88
89         if (supportsHardwareVtepSchema) {
90             //desired schema exists, retrieve contents of specified database
91             dbSchema = this.ovs.getSchema(HARDWARE_VTEP_SCHEMA).get();
92         }
93
94     }
95
96     public boolean checkSchema() {
97         return supportsHardwareVtepSchema;
98     }
99
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");
105
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());
110             }
111
112             InetAddress address;
113             try {
114                 address = InetAddress.getByName(addressStr);
115             } catch (Exception e) {
116                 System.out.println("Unable to resolve " + addressStr);
117                 e.printStackTrace();
118                 return null;
119             }
120
121             Integer port;
122             try {
123                 port = Integer.parseInt(portStr);
124             } catch (NumberFormatException e) {
125                 System.out.println("Invalid port number : " + portStr);
126                 e.printStackTrace();
127                 return null;
128             }
129
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);
136         }
137         Assert.fail("Connection parameter ("+CONNECTION_TYPE+") must be either active or passive");
138         return null;
139     }
140
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];
147             }
148         }
149         return null;
150     }
151
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";
156     }
157
158     public class PassiveListener implements Callable<OvsdbClient>, OvsdbConnectionListener {
159         OvsdbClient client = null;
160         @Override
161         public OvsdbClient call() throws Exception {
162             OvsdbConnection connection = OvsdbConnectionService.getService();
163             connection.registerConnectionListener(this);
164             while (client == null) {
165                 Thread.sleep(500);
166             }
167             return client;
168         }
169
170         @Override
171         public void connected(OvsdbClient client) {
172             this.client = client;
173         }
174
175         @Override
176         public void disconnected(OvsdbClient client) {
177             Assert.assertEquals(this.client.getConnectionInfo(), client.getConnectionInfo());
178             this.client = null;
179         }
180     }
181 }