Refactor of the OVSDB Plugin
[netvirt.git] / plugin / src / test / java / org / opendaylight / ovsdb / plugin / PluginTestBase.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.util.HashMap;
14 import java.util.Map;
15 import java.util.Properties;
16
17 import junit.framework.Assert;
18
19 import org.opendaylight.controller.sal.connection.ConnectionConstants;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
23 import org.opendaylight.ovsdb.plugin.impl.ConfigurationServiceImpl;
24 import org.opendaylight.ovsdb.plugin.impl.ConnectionServiceImpl;
25 import org.opendaylight.ovsdb.plugin.impl.InventoryServiceImpl;
26
27 public abstract class PluginTestBase {
28     private final static String identifier = "TEST";
29     protected final static String BRIDGE_NAME = "JUNIT_TEST_BRIDGE";
30     protected final static String PORT_NAME = "test0";
31     protected final static String TAGGED_PORT_NAME = "test1";
32     protected final static String TUNNEL_PORT_NAME = "vxlan0";
33     protected final static String FAKE_IP = "192.168.254.254";
34     private final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
35     private final static String SERVER_PORT = "ovsdbserver.port";
36     private final static String DEFAULT_SERVER_PORT = "6640";
37
38     public Properties loadProperties() throws IOException {
39         Properties props = new Properties(System.getProperties());
40         return props;
41     }
42
43     public class TestObjects {
44         public final ConnectionServiceImpl connectionService;
45         public final InventoryServiceImpl inventoryService;
46         public final ConfigurationServiceImpl configurationService;
47         public final Node node;
48
49         public TestObjects(ConnectionServiceImpl connectionService, Node node, InventoryServiceImpl inventoryService, ConfigurationServiceImpl configurationService) {
50             this.connectionService = connectionService;
51             this.inventoryService = inventoryService;
52             this.configurationService = configurationService;
53             this.node = node;
54         }
55     }
56
57     public TestObjects getTestConnection() throws IOException {
58         if (OvsdbPluginTestSuiteIT.getTestObjects() != null) {
59             return OvsdbPluginTestSuiteIT.getTestObjects();
60         }
61         Properties props = loadProperties();
62         String address = props.getProperty(SERVER_IPADDRESS);
63         String port = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
64
65         if (address == null) {
66             Assert.fail("Usage : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify");
67         }
68
69         Node.NodeIDType.registerIDType("OVS", String.class);
70         NodeConnector.NodeConnectorIDType.registerIDType("OVS", String.class,
71                 "OVS");
72         InventoryServiceImpl inventoryService = new InventoryServiceImpl();
73         inventoryService.init();
74
75         ConnectionServiceImpl connectionService = new ConnectionServiceImpl();
76         connectionService.init();
77         InventoryServiceImpl inventory = new InventoryServiceImpl();
78         inventory.init();
79         connectionService.setOvsdbInventoryService(inventory);
80         connectionService.setOvsdbConnection(OvsdbConnectionService.getService());
81         ConfigurationServiceImpl configurationService = new ConfigurationServiceImpl();
82         configurationService.setConnectionServiceInternal(connectionService);
83         configurationService.setOvsdbInventoryService(inventory);
84         inventory.setOvsdbConfigurationService(configurationService);
85
86         Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
87
88         params.put(ConnectionConstants.ADDRESS, address);
89         params.put(ConnectionConstants.PORT, port);
90
91         Node node = connectionService.connect(identifier, params);
92         if (node == null) {
93             throw new IOException("Failed to connect to the ovsdb server");
94         }
95
96         try {
97             Thread.sleep(2000);
98         } catch (InterruptedException e) {
99             // TODO Auto-generated catch block
100             e.printStackTrace();
101         }
102         TestObjects testObject = new TestObjects(connectionService, node, inventory, configurationService);
103         OvsdbPluginTestSuiteIT.setTestObjects(testObject);
104         return testObject;
105     }
106
107 }