Plugin migration to use the new Schema independent Library.
[ovsdb.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / plugin / OvsdbPluginIT.java
1 /*
2  * Copyright (c) 2014 Red Hat, Inc. and others.  All rights reserved.
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 package org.opendaylight.ovsdb.integrationtest.plugin;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertNull;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
19 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
20 import static org.ops4j.pax.exam.CoreOptions.options;
21 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperty;
22 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
23
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.concurrent.ConcurrentMap;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeoutException;
32
33 import javax.inject.Inject;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.opendaylight.controller.sal.connection.ConnectionConstants;
39 import org.opendaylight.controller.sal.core.Node;
40 import org.opendaylight.controller.sal.utils.ServiceHelper;
41 import org.opendaylight.controller.sal.utils.Status;
42 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
43 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
44 import org.opendaylight.ovsdb.lib.OvsdbClient;
45 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
46 import org.opendaylight.ovsdb.lib.notation.Row;
47 import org.opendaylight.ovsdb.plugin.Connection;
48 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
49 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
50 import org.opendaylight.ovsdb.plugin.StatusWithUuid;
51 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
52 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
53 import org.ops4j.pax.exam.Configuration;
54 import org.ops4j.pax.exam.Option;
55 import org.ops4j.pax.exam.junit.PaxExam;
56 import org.ops4j.pax.exam.util.PathUtils;
57 import org.osgi.framework.Bundle;
58 import org.osgi.framework.BundleContext;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 import com.google.common.collect.ImmutableMap;
63 import com.google.common.collect.Sets;
64
65 @RunWith(PaxExam.class)
66 public class OvsdbPluginIT extends OvsdbIntegrationTestBase {
67     private Logger log = LoggerFactory.getLogger(OvsdbPluginIT.class);
68     @Inject
69     private BundleContext bc;
70     private OVSDBConfigService ovsdbConfigService = null;
71     private Node node = null;
72     private OvsdbClient client = null;
73
74     // Configure the OSGi container
75     @Configuration
76     public Option[] config() {
77         return options(
78             //
79             systemProperty("logback.configurationFile").value(
80                     "file:" + PathUtils.getBaseDir()
81                     + "/src/test/resources/logback.xml"
82             ),
83             // To start OSGi console for inspection remotely
84             systemProperty("osgi.console").value("2401"),
85
86             propagateSystemProperty("ovsdbserver.ipaddress"),
87             propagateSystemProperty("ovsdbserver.port"),
88
89             ConfigurationBundles.controllerBundles(),
90             ConfigurationBundles.ovsdbLibraryBundles(),
91             ConfigurationBundles.ovsdbDefaultSchemaBundles(),
92             mavenBundle("org.opendaylight.ovsdb", "ovsdb_plugin").versionAsInProject(),
93             junitBundles()
94         );
95     }
96
97     private String stateToString(int state) {
98         switch (state) {
99         case Bundle.ACTIVE:
100             return "ACTIVE";
101         case Bundle.INSTALLED:
102             return "INSTALLED";
103         case Bundle.RESOLVED:
104             return "RESOLVED";
105         case Bundle.UNINSTALLED:
106             return "UNINSTALLED";
107         default:
108             return "Not CONVERTED";
109         }
110     }
111
112     public Node getPluginTestConnection() throws IOException, InterruptedException, ExecutionException, TimeoutException {
113         Properties props = loadProperties();
114         String addressStr = props.getProperty(SERVER_IPADDRESS);
115         String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
116         String connectionType = props.getProperty(CONNECTION_TYPE, "active");
117
118         IConnectionServiceInternal connection = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
119         // If the connection type is active, controller connects to the ovsdb-server
120         if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_ACTIVE)) {
121             if (addressStr == null) {
122                 fail(usage());
123             }
124
125             Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
126             params.put(ConnectionConstants.ADDRESS, addressStr);
127             params.put(ConnectionConstants.PORT, portStr);
128             return connection.connect(IDENTIFIER, params);
129         }  else if (connectionType.equalsIgnoreCase(CONNECTION_TYPE_PASSIVE)) {
130             // Wait for 10 seconds for the Passive connection to be initiated by the ovsdb-server.
131             Thread.sleep(10000);
132             List<Node> nodes = connection.getNodes();
133             assertNotNull(nodes);
134             assertTrue(nodes.size() > 0);
135             return nodes.get(0);
136         }
137         fail("Connection parameter ("+CONNECTION_TYPE+") must be active or passive");
138         return null;
139     }
140
141     @Before
142     public void areWeReady() throws InterruptedException {
143         assertNotNull(bc);
144         boolean debugit = false;
145         Bundle b[] = bc.getBundles();
146         for (Bundle element : b) {
147             int state = element.getState();
148             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
149                 log.info("Bundle:" + element.getSymbolicName() + " state:"
150                           + stateToString(state));
151                 debugit = true;
152             }
153         }
154         if (debugit) {
155             log.debug("Do some debugging because some bundle is unresolved");
156         }
157
158         assertFalse(debugit);
159         try {
160             node = getPluginTestConnection();
161         } catch (Exception e) {
162             fail("Exception : "+e.getMessage());
163         }
164         this.ovsdbConfigService = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
165     }
166
167     @Test
168     public void apiTests() throws Exception {
169         Thread.sleep(5000);
170         IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
171
172         // Check for the ovsdb Connection as seen by the Plugin layer
173         assertNotNull(connectionService.getNodes());
174         assertTrue(connectionService.getNodes().size() > 0);
175         Node node = connectionService.getNodes().get(0);
176         Connection connection = connectionService.getConnection(node);
177         OvsdbConnectionInfo connectionInfo = connection.getClient().getConnectionInfo();
178         String identifier = IDENTIFIER;
179         if (connectionInfo.getType().equals(OvsdbConnectionInfo.ConnectionType.PASSIVE)) {
180             identifier = connectionInfo.getRemoteAddress().getHostAddress()+":"+connectionInfo.getRemotePort();
181         }
182         assertEquals(Node.fromString("OVS|"+identifier), connectionService.getNodes().get(0));
183         System.out.println("Nodes = "+ connectionService.getNodes());
184         /*
185          * Test sequence :
186          * 1. Print Cache and Assert to make sure the bridge is not created yet.
187          * 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
188          * 3. Assert to make sure the bridge is created with a valid Uuid.
189          * 4. Delete the bridge & Assert to make sure the return status is success.
190          * 5. Assert to make sure the bridge is deleted
191          */
192
193         this.endToEndApiTest(connection, getOpenVSwitchTableUUID(connection));
194
195         /*
196          * Repeat all of the above tests without the parent_uuid
197          */
198
199         this.endToEndApiTest(connection, null);
200     }
201
202     public void endToEndApiTest(Connection connection, String parentUuid) throws Exception {
203         // 1. Print Cache and Assert to make sure the bridge is not created yet.
204         printCache();
205
206         // 2. Create a bridge with a valid parent_uuid & Assert to make sure the return status is success.
207         StatusWithUuid status = insertBridge(connection, parentUuid);
208         assertTrue(status.isSuccess());
209
210         Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
211
212         // 3. Assert to make sure the bridge is created with a valid Uuid.
213         printCache();
214         Bridge bridge = connection.getClient().getTypedRowWrapper(Bridge.class, null);
215         Row bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
216         assertNotNull(bridgeRow);
217         bridge = connection.getClient().getTypedRowWrapper(Bridge.class, bridgeRow);
218         assertEquals(bridge.getUuid(), status.getUuid());
219
220         // 4. Delete the bridge & Assert to make sure the return status is success.
221         Status delStatus = ovsdbConfigService.deleteRow(node, bridge.getSchema().getName(), status.getUuid().toString());
222         assertTrue(delStatus.isSuccess());
223         Thread.sleep(2000); // TODO : Remove this Sleep once the Select operation is resolved.
224
225         // 5. Assert to make sure the bridge is deleted
226         bridgeRow = ovsdbConfigService.getRow(node, bridge.getSchema().getName(), status.getUuid().toString());
227         assertNull(bridgeRow);
228     }
229
230     public StatusWithUuid insertBridge(Connection connection, String parentUuid) throws Exception {
231         Bridge bridge = connection.getClient().createTypedRowWrapper(Bridge.class);
232         bridge.setName("br_test1");
233         bridge.setStatus(ImmutableMap.of("key", "value"));
234         bridge.setFloodVlans(Sets.newHashSet(34));
235         return ovsdbConfigService.insertRow(node, bridge.getSchema().getName(), parentUuid, bridge.getRow());
236     }
237
238     public String getOpenVSwitchTableUUID(Connection connection) throws Exception {
239         OpenVSwitch openVSwitch = connection.getClient().getTypedRowWrapper(OpenVSwitch.class, null);
240         ConcurrentMap<String, Row> row = ovsdbConfigService.getRows(node, openVSwitch.getSchema().getName());
241         if (row == null || row.size() == 0) return null;
242         return (String)row.keySet().toArray()[0];
243     }
244
245     public void printCache() throws Exception {
246         List<String> tables = ovsdbConfigService.getTables(node);
247         System.out.println("Tables = "+tables);
248         assertNotNull(tables);
249         for (String table : tables) {
250             System.out.println("Table "+table);
251             ConcurrentMap<String,Row> row = ovsdbConfigService.getRows(node, table);
252             System.out.println(row);
253         }
254     }
255
256 }