Code ReOrganization and Re-Architecture changes
[ovsdb.git] / plugin / src / test / java / org / opendaylight / ovsdb / plugin / OvsdbTestBase.java
1 /*
2  * [[ Authors will Fill in the Copyright header ]]
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 : Brent Salisbury, Hugo Trippaers
9  */
10 package org.opendaylight.ovsdb.plugin;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Properties;
17
18 import org.opendaylight.controller.sal.connection.ConnectionConstants;
19 import org.opendaylight.controller.sal.core.Node;
20 import org.opendaylight.controller.sal.core.NodeConnector;
21
22 public abstract class OvsdbTestBase {
23     private final static String identifier = "TEST";
24
25     public Properties loadProperties() throws IOException {
26         InputStream is = this
27                 .getClass()
28                 .getClassLoader()
29                 .getResourceAsStream(
30                         "org/opendaylight/ovsdb/lib/message/integration-test.properties");
31         if (is == null) {
32             throw new IOException("Unable to load integration-test.properties");
33         }
34         Properties props = new Properties();
35         props.load(is);
36
37         return props;
38     }
39
40     public class TestObjects {
41         public final ConnectionService connectionService;
42         public final Node node;
43
44         public TestObjects(ConnectionService connectionService, Node node) {
45             this.connectionService = connectionService;
46             this.node = node;
47         }
48     }
49
50     public TestObjects getTestConnection() throws IOException {
51         Node.NodeIDType.registerIDType("OVS", String.class);
52         NodeConnector.NodeConnectorIDType.registerIDType("OVS", String.class,
53                 "OVS");
54         InventoryService inventoryService = new InventoryService();
55         inventoryService.init();
56
57         ConnectionService connectionService = new ConnectionService();
58         connectionService.init();
59
60         connectionService.setInventoryServiceInternal(inventoryService);
61         Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
62         Properties props = loadProperties();
63         params.put(ConnectionConstants.ADDRESS,
64                 props.getProperty("ovsdbserver.ipaddress"));
65         params.put(ConnectionConstants.PORT,
66                 props.getProperty("ovsdbserver.port", "6640"));
67
68         Node node = connectionService.connect(identifier, params);
69         if (node == null) {
70             throw new IOException("Failed to connecto to ovsdb server");
71         }
72         return new TestObjects(connectionService, node);
73     }
74
75 }