Fix TransactionBuilder to take OvsdbClient rather than OvsdbClientImpl
[netvirt.git] / integrationtest / src / test / java / org / opendaylight / ovsdb / integrationtest / neutron / NeutronIT.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 : Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.integrationtest.neutron;
12
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.fail;
16 import static org.ops4j.pax.exam.CoreOptions.junitBundles;
17 import static org.ops4j.pax.exam.CoreOptions.options;
18 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperty;
19 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
20
21 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
22 import org.opendaylight.controller.sal.core.Node;
23 import org.opendaylight.controller.sal.utils.Status;
24 import org.opendaylight.ovsdb.integrationtest.ConfigurationBundles;
25 import org.opendaylight.ovsdb.integrationtest.OvsdbIntegrationTestBase;
26 import org.opendaylight.ovsdb.lib.notation.Row;
27 import org.opendaylight.ovsdb.lib.notation.UUID;
28 import org.opendaylight.ovsdb.lib.notation.Version;
29 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
30 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
31 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
32 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
33 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
34 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
35 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
36 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
37
38 import com.google.common.collect.ImmutableMap;
39 import org.apache.commons.lang3.tuple.ImmutablePair;
40 import org.apache.felix.dm.Component;
41 import org.apache.felix.dm.DependencyManager;
42 import org.junit.After;
43 import org.junit.Assert;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.ops4j.pax.exam.Configuration;
48 import org.ops4j.pax.exam.Option;
49 import org.ops4j.pax.exam.junit.PaxExam;
50 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
51 import org.ops4j.pax.exam.spi.reactors.PerClass;
52 import org.ops4j.pax.exam.util.PathUtils;
53 import org.osgi.framework.Bundle;
54 import org.osgi.framework.BundleContext;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 import java.io.IOException;
59 import java.net.InetAddress;
60 import java.util.Map;
61 import java.util.Properties;
62 import java.util.concurrent.ExecutionException;
63 import java.util.concurrent.TimeoutException;
64
65 import javax.inject.Inject;
66
67
68 @RunWith(PaxExam.class)
69 @ExamReactorStrategy(PerClass.class)
70 public class NeutronIT extends OvsdbIntegrationTestBase {
71     private Logger log = LoggerFactory.getLogger(NeutronIT.class);
72     @Inject
73     private BundleContext bc;
74
75     @Inject
76     private OvsdbConfigurationService ovsdbConfigurationService;
77     private Node node = null;
78
79     Component of13Provider;
80
81     @Inject
82     BridgeConfigurationManager bridgeConfigurationManager;
83     @Inject
84     ConfigurationService netVirtConfigurationService;
85
86     Boolean tearDownBridge = false;
87     ImmutablePair<UUID, Map<String, String>> tearDownOpenVSwitchOtherConfig = null;
88
89     // Configure the OSGi container
90     @Configuration
91     public Option[] config() {
92         return options(
93                 //
94                 systemProperty("logback.configurationFile").value(
95                         "file:" + PathUtils.getBaseDir()
96                         + "/src/test/resources/logback.xml"
97                 ),
98                 // To start OSGi console for inspection remotely
99                 systemProperty("osgi.console").value("2401"),
100
101                 propagateSystemProperty("ovsdbserver.ipaddress"),
102                 propagateSystemProperty("ovsdbserver.port"),
103
104                 ConfigurationBundles.controllerBundles(),
105                 ConfigurationBundles.ovsdbLibraryBundles(),
106                 ConfigurationBundles.ovsdbDefaultSchemaBundles(),
107                 ConfigurationBundles.ovsdbPluginBundles(),
108                 ConfigurationBundles.ovsdbNeutronBundles(),
109                 junitBundles()
110         );
111     }
112
113     @Before
114     public void areWeReady() throws InterruptedException, ExecutionException, IOException, TimeoutException {
115         assertNotNull(bc);
116         boolean debugit = false;
117         Bundle b[] = bc.getBundles();
118         for (Bundle element : b) {
119             int state = element.getState();
120             if (state != Bundle.ACTIVE && state != Bundle.RESOLVED) {
121                 log.info("Bundle:" + element.getSymbolicName() + " state:"
122                          + stateToString(state));
123                 debugit = true;
124             }
125         }
126         if (debugit) {
127             log.debug("Do some debugging because some bundle is unresolved");
128         }
129
130         assertFalse(debugit);
131
132         if (node == null) {
133             try {
134                 node = getPluginTestConnection();
135             } catch (Exception e) {
136                 fail("Exception : " + e.getMessage());
137             }
138         }
139
140         //Register fake NetworkingProviders
141         Properties of13Properties = new Properties();
142         of13Properties.put(Constants.OPENFLOW_VERSION_PROPERTY, Constants.OPENFLOW13);
143
144         DependencyManager dm = new DependencyManager(bc);
145
146         of13Provider = dm.createComponent();
147         of13Provider.setInterface(NetworkingProvider.class.getName(), of13Properties);
148         of13Provider.setImplementation(new FakeOF13Provider());
149
150         dm.add(of13Provider);
151     }
152
153     @Test
154     public void testPrepareNode() throws Exception {
155         Thread.sleep(5000);
156
157         // Create the integration bridge
158         bridgeConfigurationManager.prepareNode(node);
159
160         Map<String, Row>
161                 bridgeRows =
162                 ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
163         Assert.assertEquals(1, bridgeRows.size());
164
165         Bridge bridgeRow = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRows.values().iterator().next());
166         Assert.assertEquals(netVirtConfigurationService.getIntegrationBridgeName(), bridgeRow.getName());
167
168         String uuid = bridgeConfigurationManager.getBridgeUuid(node, netVirtConfigurationService.getIntegrationBridgeName());
169         Assert.assertEquals(uuid, bridgeRow.getUuid().toString());
170
171         tearDownBridge = true;
172     }
173
174     @Test
175     public void testGetTunnelEndpoint() throws Exception {
176         Thread.sleep(5000);
177
178         final String endpointAddress = "10.10.10.10";
179
180         Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
181                                                               ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
182         OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
183                                                             OpenVSwitch.class,
184                                                             ovsRows.values().iterator().next());
185
186         Assert.assertEquals(null, netVirtConfigurationService.getTunnelEndPoint(node));
187         final UUID originalVersion = ovsRow.getVersion();
188
189         OpenVSwitch updateOvsRow = ovsdbConfigurationService.createTypedRow(node, OpenVSwitch.class);
190
191         updateOvsRow.setOtherConfig(
192                 ImmutableMap.of(netVirtConfigurationService.getTunnelEndpointKey(), endpointAddress));
193
194         ovsdbConfigurationService.updateRow(node,
195                                             ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
196                                             null,
197                                             ovsRow.getUuid().toString(),
198                                             updateOvsRow.getRow());
199
200         // Remember original value so it can be restored on tearDown
201         tearDownOpenVSwitchOtherConfig = ImmutablePair.of(ovsRow.getUuid(),
202                                                           ovsRow.getOtherConfigColumn().getData());
203
204         // Make sure tunnel end point was set
205         Assert.assertEquals(InetAddress.getByName(endpointAddress), netVirtConfigurationService.getTunnelEndPoint(node));
206
207         // Fetch rows again, and compare tunnel end point values
208         ovsRows = ovsdbConfigurationService.getRows(node,
209                                                     ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
210         ovsRow = ovsdbConfigurationService.getTypedRow(node,
211                                                        OpenVSwitch.class,
212                                                        ovsRows.values().iterator().next());
213
214         Assert.assertEquals(ovsRow.getOtherConfigColumn(), updateOvsRow.getOtherConfigColumn());
215
216         // expect version of row to be changed, due to the update
217         Assert.assertNotEquals(ovsRow.getVersion(), originalVersion);
218     }
219
220     @Test
221     public void testGetOpenflowVersion() throws Exception {
222         Thread.sleep(5000);
223
224         Version ovsVersion = this.getOvsVersion();
225         if (ovsVersion.compareTo(Constants.OPENFLOW13_SUPPORTED) >= 0) {
226             Assert.assertEquals(Constants.OPENFLOW13, netVirtConfigurationService.getOpenflowVersion(node));
227         }
228     }
229
230     @Test
231     public void testGetDefaultGatewayMacAddress() throws Exception {
232         // Thread.sleep(5000);
233         String defaultGatewayMacAddress = netVirtConfigurationService.getDefaultGatewayMacAddress(node);
234
235         if (defaultGatewayMacAddress != null) {
236             String[] splits = defaultGatewayMacAddress.split(":");
237             Assert.assertTrue("Unexpected mac format", splits.length == 6);
238         }
239         // log.info("testGetDefaultGatewayMacAddress got mac {}", defaultGatewayMacAddress);
240     }
241
242     @After
243     public void tearDown() throws InterruptedException {
244         Thread.sleep(5000);
245
246         if (tearDownBridge) {
247             try {
248                 String uuid = bridgeConfigurationManager.getBridgeUuid(node,
249                                                                        netVirtConfigurationService.getIntegrationBridgeName());
250                 ovsdbConfigurationService.deleteRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), uuid);
251             } catch (Exception e) {
252                 log.error("tearDownBridge Exception : " + e.getMessage());
253             }
254             tearDownBridge = false;
255         }
256
257         if (tearDownOpenVSwitchOtherConfig != null) {
258             try {
259                 OpenVSwitch updateOvsRow = ovsdbConfigurationService.createTypedRow(node, OpenVSwitch.class);
260                 updateOvsRow.setOtherConfig(tearDownOpenVSwitchOtherConfig.getRight());
261                 ovsdbConfigurationService.updateRow(node,
262                                                     ovsdbConfigurationService.getTableName(node, OpenVSwitch.class),
263                                                     null,
264                                                     tearDownOpenVSwitchOtherConfig.getLeft().toString(),
265                                                     updateOvsRow.getRow());
266             } catch (Exception e) {
267                 log.error("tearDownOpenVSwitchOtherConfig Exception : " + e.getMessage());
268             }
269             tearDownOpenVSwitchOtherConfig = null;
270         }
271
272         DependencyManager dm = new DependencyManager(bc);
273         dm.remove(of13Provider);
274     }
275
276     private Version getOvsVersion(){
277         Map<String, Row> ovsRows = ovsdbConfigurationService.getRows(node,
278                                                               ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
279         OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node,
280                                                             OpenVSwitch.class,
281                                                             ovsRows.values().iterator().next());
282         return Version.fromString(ovsRow.getOvsVersionColumn().getData().iterator().next());
283     }
284
285     private class FakeOF13Provider implements NetworkingProvider {
286
287         @Override
288         public String getName() {
289             return null;
290         }
291
292         @Override
293         public boolean supportsServices() {
294             return false;
295         }
296
297         @Override
298         public boolean hasPerTenantTunneling() {
299             return false;
300         }
301
302         @Override
303         public Status handleInterfaceUpdate(String tunnelType, String tunnelKey) {
304             return null;
305         }
306
307         @Override
308         public Status handleInterfaceUpdate(NeutronNetwork network, Node source, Interface intf) {
309             return null;
310         }
311
312         @Override
313         public Status handleInterfaceDelete(String tunnelType, NeutronNetwork network, Node source, Interface intf,
314                                             boolean isLastInstanceOnNode) {
315             return null;
316         }
317
318         @Override
319         public void initializeFlowRules(Node node) {
320
321         }
322
323         @Override
324         public void initializeOFFlowRules(Node openflowNode) {
325
326         }
327     }
328 }