Merge "Enable PowerMock instrumentation with JaCoCo"
[netvirt.git] / openstack / net-virt-it / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / it / NetvirtIT.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.ovsdb.openstack.netvirt.it;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.ops4j.pax.exam.CoreOptions.composite;
16 import static org.ops4j.pax.exam.CoreOptions.maven;
17 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
18 import static org.ops4j.pax.exam.CoreOptions.propagateSystemProperties;
19 import static org.ops4j.pax.exam.CoreOptions.vmOption;
20 import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
21 import static org.ops4j.pax.exam.MavenUtils.asInProject;
22 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
23 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
24 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
25
26 import com.google.common.collect.Lists;
27 import com.google.common.collect.Maps;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Properties;
33 import java.util.concurrent.atomic.AtomicBoolean;
34
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
43 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
44 import org.opendaylight.neutron.spi.INeutronPortCRUD;
45 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
46 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
47 import org.opendaylight.neutron.spi.NeutronPort;
48 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
49 import org.opendaylight.neutron.spi.NeutronSecurityRule;
50 import org.opendaylight.neutron.spi.NeutronNetwork;
51 import org.opendaylight.neutron.spi.NeutronSubnet;
52 import org.opendaylight.ovsdb.lib.notation.Version;
53 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
54 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
55 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
56 import org.opendaylight.ovsdb.openstack.netvirt.providers.NetvirtProvidersProvider;
57 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
58 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
59 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
60 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
61 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
62 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.ops4j.pax.exam.Configuration;
77 import org.ops4j.pax.exam.Option;
78 import org.ops4j.pax.exam.junit.PaxExam;
79 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
80 import org.ops4j.pax.exam.options.MavenUrlReference;
81 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
82 import org.ops4j.pax.exam.spi.reactors.PerClass;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85
86 /**
87  * Integration tests for netvirt
88  *
89  * @author Sam Hague (shague@redhat.com)
90  */
91 @RunWith(PaxExam.class)
92 @ExamReactorStrategy(PerClass.class)
93 public class NetvirtIT extends AbstractMdsalTestBase {
94     private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
95     private static DataBroker dataBroker = null;
96     private static String addressStr;
97     private static String portStr;
98     private static String connectionType;
99     private static String controllerStr;
100     private static AtomicBoolean setup = new AtomicBoolean(false);
101     private static MdsalUtils mdsalUtils = null;
102     private static Southbound southbound = null;
103     private static PipelineOrchestrator pipelineOrchestrator = null;
104     private static SouthboundUtils southboundUtils;
105     private static NeutronUtils neutronUtils = new NeutronUtils();
106     private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
107
108     @Override
109     public String getModuleName() {
110         return "netvirt-providers-impl";
111     }
112
113     @Override
114     public String getInstanceName() {
115         return "netvirt-providers-default";
116     }
117
118     @Override
119     public MavenUrlReference getFeatureRepo() {
120         return maven()
121                 .groupId("org.opendaylight.ovsdb")
122                 .artifactId("features-ovsdb")
123                 .classifier("features")
124                 .type("xml")
125                 .versionAsInProject();
126     }
127
128     @Override
129     public String getFeatureName() {
130         return "odl-ovsdb-openstack";
131     }
132
133     @Configuration
134     @Override
135     public Option[] config() {
136         Option[] parentOptions = super.config();
137         Option[] propertiesOptions = getPropertiesOptions();
138         Option[] otherOptions = getOtherOptions();
139         Option[] options = new Option[parentOptions.length + propertiesOptions.length + otherOptions.length];
140         System.arraycopy(parentOptions, 0, options, 0, parentOptions.length);
141         System.arraycopy(propertiesOptions, 0, options, parentOptions.length, propertiesOptions.length);
142         System.arraycopy(otherOptions, 0, options, parentOptions.length + propertiesOptions.length,
143                 otherOptions.length);
144         return options;
145     }
146
147     private Option[] getOtherOptions() {
148         return new Option[] {
149                 wrappedBundle(
150                         mavenBundle("org.opendaylight.ovsdb", "utils.mdsal-openflow")
151                                 .version(asInProject())
152                                 .type("jar")),
153                 wrappedBundle(
154                         mavenBundle("org.opendaylight.ovsdb", "utils.config")
155                                 .version(asInProject())
156                                 .type("jar")),
157                 configureConsole().startLocalConsole(),
158                 vmOption("-javaagent:../jars/org.jacoco.agent.jar=destfile=../../jacoco-it.exec"),
159                 keepRuntimeFolder()
160         };
161     }
162
163     public Option[] getPropertiesOptions() {
164         return new Option[] {
165                 propagateSystemProperties(NetvirtITConstants.SERVER_IPADDRESS,
166                         NetvirtITConstants.SERVER_PORT, NetvirtITConstants.CONNECTION_TYPE,
167                         NetvirtITConstants.CONTROLLER_IPADDRESS,
168                         NetvirtITConstants.USERSPACE_ENABLED)
169         };
170     }
171
172     @Override
173     public Option getLoggingOption() {
174         return composite(
175                 //editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
176                 //        "log4j.logger.org.opendaylight.controller",
177                 //        LogLevelOption.LogLevel.TRACE.name()),
178                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
179                         "log4j.logger.org.opendaylight.ovsdb",
180                         LogLevelOption.LogLevel.TRACE.name()),
181                 editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
182                         logConfiguration(NetvirtIT.class),
183                         LogLevelOption.LogLevel.INFO.name()),
184                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
185                         "log4j.logger.org.opendaylight.ovsdb.lib",
186                         LogLevelOption.LogLevel.INFO.name()),
187                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
188                         "log4j.logger.org.opendaylight.openflowjava",
189                         LogLevelOption.LogLevel.INFO.name()),
190                 editConfigurationFilePut(NetvirtITConstants.ORG_OPS4J_PAX_LOGGING_CFG,
191                         "log4j.logger.org.opendaylight.openflowplugin",
192                         LogLevelOption.LogLevel.INFO.name()),
193                 super.getLoggingOption());
194     }
195
196     protected String usage() {
197         return "Integration Test needs a valid connection configuration as follows :\n"
198                 + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
199                 + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
200     }
201
202     private void getProperties() {
203         Properties props = System.getProperties();
204         addressStr = props.getProperty(NetvirtITConstants.SERVER_IPADDRESS);
205         portStr = props.getProperty(NetvirtITConstants.SERVER_PORT, NetvirtITConstants.DEFAULT_SERVER_PORT);
206         connectionType = props.getProperty(NetvirtITConstants.CONNECTION_TYPE, "active");
207         controllerStr = props.getProperty(NetvirtITConstants.CONTROLLER_IPADDRESS, "0.0.0.0");
208         String userSpaceEnabled = props.getProperty(NetvirtITConstants.USERSPACE_ENABLED, "no");
209         LOG.info("setUp: Using the following properties: mode= {}, ip:port= {}:{}, controller ip: {}, " +
210                 "userspace.enabled: {}",
211                 connectionType, addressStr, portStr, controllerStr, userSpaceEnabled);
212         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
213             if (addressStr == null) {
214                 fail(usage());
215             }
216         }
217     }
218
219     @Before
220     @Override
221     public void setup() throws InterruptedException {
222         if (setup.get()) {
223             LOG.info("Skipping setUp, already initialized");
224             return;
225         }
226
227         try {
228             super.setup();
229         } catch (Exception e) {
230             e.printStackTrace();
231         }
232
233         getProperties();
234
235         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_ACTIVE)) {
236             if (addressStr == null) {
237                 fail(usage());
238             }
239         }
240
241         dataBroker = getDatabroker(getProviderContext());
242         mdsalUtils = new MdsalUtils(dataBroker);
243         assertNotNull("mdsalUtils should not be null", mdsalUtils);
244         assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
245         southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
246         assertNotNull("southbound should not be null", southbound);
247         southboundUtils = new SouthboundUtils(mdsalUtils);
248         pipelineOrchestrator =
249                 (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
250         assertNotNull("pipelineOrchestrator should not be null", pipelineOrchestrator);
251         setup.set(true);
252     }
253
254     private BindingAwareBroker.ProviderContext getProviderContext() {
255         BindingAwareBroker.ProviderContext providerContext = null;
256         for (int i=0; i < 60; i++) {
257             providerContext = getSession();
258             if (providerContext != null) {
259                 break;
260             } else {
261                 try {
262                     Thread.sleep(1000);
263                 } catch (InterruptedException e) {
264                     e.printStackTrace();
265                 }
266             }
267         }
268         assertNotNull("providercontext should not be null", providerContext);
269         /* One more second to let the provider finish initialization */
270         try {
271             Thread.sleep(1000);
272         } catch (InterruptedException e) {
273             e.printStackTrace();
274         }
275         return providerContext;
276     }
277
278     private DataBroker getDatabroker(BindingAwareBroker.ProviderContext providerContext) {
279         DataBroker dataBroker = providerContext.getSALService(DataBroker.class);
280         assertNotNull("dataBroker should not be null", dataBroker);
281         return dataBroker;
282     }
283
284     private Boolean getNetvirtTopology() {
285         LOG.info("getNetvirtTopology: looking for {}...", NETVIRT_TOPOLOGY_ID);
286         Boolean found = false;
287         final TopologyId topologyId = new TopologyId(new Uri(NETVIRT_TOPOLOGY_ID));
288         InstanceIdentifier<Topology> path =
289                 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
290         for (int i = 0; i < 60; i++) {
291             Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
292             if (topology != null) {
293                 LOG.info("getNetvirtTopology: found {}...", NETVIRT_TOPOLOGY_ID);
294                 found = true;
295                 break;
296             } else {
297                 LOG.info("getNetvirtTopology: still looking ({})...", i);
298                 try {
299                     Thread.sleep(1000);
300                 } catch (InterruptedException e) {
301                     e.printStackTrace();
302                 }
303             }
304         }
305         return found;
306     }
307
308     /**
309      * Test passive connection mode. The southbound starts in a listening mode waiting for connections on port
310      * 6640. This test will wait for incoming connections for {@link NetvirtITConstants#CONNECTION_INIT_TIMEOUT} ms.
311      *
312      * @throws InterruptedException
313      */
314     @Ignore
315     @Test
316     public void testPassiveNode() throws InterruptedException {
317         if (connectionType.equalsIgnoreCase(NetvirtITConstants.CONNECTION_TYPE_PASSIVE)) {
318             //Wait for CONNECTION_INIT_TIMEOUT for the Passive connection to be initiated by the ovsdb-server.
319             Thread.sleep(NetvirtITConstants.CONNECTION_INIT_TIMEOUT);
320         }
321     }
322
323     private Node connectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
324         LOG.info("connectOvsdbNode enter");
325         Assert.assertTrue(southboundUtils.addOvsdbNode(connectionInfo));
326         Node node = southboundUtils.getOvsdbNode(connectionInfo);
327         Assert.assertNotNull("Should find OVSDB node after connect", node);
328         LOG.info("Connected to {}", SouthboundUtils.connectionInfoToString(connectionInfo));
329         return node;
330     }
331
332     private boolean disconnectOvsdbNode(final ConnectionInfo connectionInfo) throws InterruptedException {
333         LOG.info("disconnectOvsdbNode enter");
334         Assert.assertTrue(southboundUtils.deleteOvsdbNode(connectionInfo));
335         Node node = southboundUtils.getOvsdbNode(connectionInfo);
336         Assert.assertNull("Should not find OVSDB node after disconnect", node);
337         LOG.info("Disconnected from {}", SouthboundUtils.connectionInfoToString(connectionInfo));
338         return true;
339     }
340
341     // This is an extra test for local testing and testNetVirt covers this is more detail
342     @Ignore
343     @Test
344     public void testAddDeleteOvsdbNode() throws InterruptedException {
345         LOG.info("testAddDeleteOvsdbNode enter");
346         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
347         Node ovsdbNode = connectOvsdbNode(connectionInfo);
348         assertNotNull("connection failed", ovsdbNode);
349         LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
350
351         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
352                 + " is not connected", isControllerConnected(connectionInfo));
353
354         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
355         Thread.sleep(1000);
356         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
357         LOG.info("testAddDeleteOvsdbNode exit");
358     }
359
360     // TODO add tests for when L3 is enabled and check for br-ex
361
362     // This is an extra test for local testing and testNetVirt covers this is more detail
363     @Ignore
364     @Test
365     public void testAddDeleteOvsdbNodeWithTableOffset() throws InterruptedException {
366         LOG.info("testAddDeleteOvsdbNodeWithTableOffset enter");
367         NetvirtProvidersProvider.setTableOffset((short)1);
368         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
369         Node ovsdbNode = connectOvsdbNode(connectionInfo);
370         assertNotNull("connection failed", ovsdbNode);
371         LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
372
373         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
374                 + " is not connected", isControllerConnected(connectionInfo));
375
376         // Verify the pipeline flows were installed
377         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
378         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
379         long datapathId = southbound.getDataPathId(bridgeNode);
380         String datapathIdString = southbound.getDatapathId(bridgeNode);
381         LOG.info("testNetVirt: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
382         assertNotEquals("datapathId was not found", datapathId, 0);
383
384         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
385         List<Service> staticPipelineFound = Lists.newArrayList();
386         for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
387             if (staticPipeline.contains(service)) {
388                 staticPipelineFound.add(service);
389             }
390             String flowId = "DEFAULT_PIPELINE_FLOW_" + pipelineOrchestrator.getTable(service);
391             verifyFlow(datapathId, flowId, service);
392         }
393         assertEquals("did not find all expected flows in static pipeline",
394                 staticPipeline.size(), staticPipelineFound.size());
395
396         String flowId = "TableOffset_" + pipelineOrchestrator.getTable(Service.CLASSIFIER);
397         verifyFlow(datapathId, flowId, Service.CLASSIFIER.getTable());
398
399         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
400         Thread.sleep(1000);
401         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
402         LOG.info("testAddDeleteOvsdbNodeWithTableOffset exit");
403     }
404
405     private boolean isControllerConnected(ConnectionInfo connectionInfo) throws InterruptedException {
406         LOG.info("isControllerConnected enter");
407         Boolean connected = false;
408         ControllerEntry controllerEntry;
409         Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
410         assertNotNull("ovsdb node not found", ovsdbNode);
411
412         BridgeConfigurationManager bridgeConfigurationManager =
413                 (BridgeConfigurationManager) ServiceHelper.getGlobalInstance(BridgeConfigurationManager.class, this);
414         assertNotNull("Could not find BridgeConfigurationManager Service", bridgeConfigurationManager);
415         String controllerTarget = bridgeConfigurationManager.getControllersFromOvsdbNode(ovsdbNode).get(0);
416         Assert.assertNotNull("Failed to get controller target", controllerTarget);
417
418         for (int i = 0; i < 10; i++) {
419             LOG.info("isControllerConnected try {}: looking for controller: {}", i, controllerTarget);
420             OvsdbBridgeAugmentation bridge =
421                     southboundUtils.getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
422             Assert.assertNotNull(bridge);
423             Assert.assertNotNull(bridge.getControllerEntry());
424             controllerEntry = bridge.getControllerEntry().iterator().next();
425             Assert.assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
426             if (controllerEntry.isIsConnected()) {
427                 Assert.assertTrue("Controller is not connected", controllerEntry.isIsConnected());
428                 connected = true;
429                 break;
430             }
431             Thread.sleep(1000);
432         }
433         LOG.info("isControllerConnected exit: {} - {}", connected, controllerTarget);
434         return connected;
435     }
436
437     @Ignore
438     @Test
439     public void testOpenVSwitchOtherConfig() throws InterruptedException {
440         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
441         Node ovsdbNode = connectOvsdbNode(connectionInfo);
442         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
443         Assert.assertNotNull(ovsdbNodeAugmentation);
444         List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeAugmentation.getOpenvswitchOtherConfigs();
445         if (otherConfigsList != null) {
446             for (OpenvswitchOtherConfigs otherConfig : otherConfigsList) {
447                 if (otherConfig.getOtherConfigKey().equals("local_ip")) {
448                     LOG.info("local_ip: {}", otherConfig.getOtherConfigValue());
449                     break;
450                 } else {
451                     LOG.info("other_config {}:{}", otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
452                 }
453             }
454         } else {
455             LOG.info("other_config is not present");
456         }
457         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
458     }
459
460     /**
461      * Test for basic southbound events to netvirt.
462      * <pre>The test will:
463      * - connect to an OVSDB node and verify it is added to operational
464      * - then verify that br-int was created on the node and stored in operational
465      * - a port is then added to the bridge to verify that it is ignored by netvirt
466      * - remove the bridge
467      * - remove the node and verify it is not in operational
468      * </pre>
469      * @throws InterruptedException
470      */
471     @Test
472     public void testNetVirt() throws InterruptedException {
473         LOG.info("testNetVirt: starting test");
474         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
475         Node ovsdbNode = connectOvsdbNode(connectionInfo);
476         assertNotNull("connection failed", ovsdbNode);
477         LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
478
479         //TODO use controller value rather that ovsdb connectionInfo or change log
480         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
481                 + " is not connected", isControllerConnected(connectionInfo));
482
483         // Verify the pipeline flows were installed
484         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
485         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
486         long datapathId = southbound.getDataPathId(bridgeNode);
487         String datapathIdString = southbound.getDatapathId(bridgeNode);
488         LOG.info("testNetVirt: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
489         assertNotEquals("datapathId was not found", datapathId, 0);
490
491         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
492         List<Service> staticPipelineFound = Lists.newArrayList();
493         for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
494             if (staticPipeline.contains(service)) {
495                 staticPipelineFound.add(service);
496             }
497             String flowId = "DEFAULT_PIPELINE_FLOW_" + pipelineOrchestrator.getTable(service);
498             verifyFlow(datapathId, flowId, service);
499         }
500         assertEquals("did not find all expected flows in static pipeline",
501                 staticPipeline.size(), staticPipelineFound.size());
502
503         southboundUtils.addTerminationPoint(bridgeNode, NetvirtITConstants.PORT_NAME, "internal", null, null, 0L);
504         Thread.sleep(1000);
505         OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
506                 southbound.getTerminationPointOfBridge(bridgeNode, NetvirtITConstants.PORT_NAME);
507         Assert.assertNotNull("Did not find " + NetvirtITConstants.PORT_NAME, ovsdbTerminationPointAugmentation);
508         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
509         Thread.sleep(1000);
510         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
511     }
512
513     @Test
514     public void testNetVirtFixedSG() throws InterruptedException {
515         final Version minSGOvsVersion = Version.fromString("1.10.2");
516         final String portName = "sg1";
517         final String networkId = "521e29d6-67b8-4b3c-8633-027d21195111";
518         final String tenantId = "521e29d6-67b8-4b3c-8633-027d21195100";
519         final String subnetId = "521e29d6-67b8-4b3c-8633-027d21195112";
520         final String portId = "521e29d6-67b8-4b3c-8633-027d21195113";
521         final String dhcpPortId ="521e29d6-67b8-4b3c-8633-027d21195115";
522
523         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
524         Node ovsdbNode = connectOvsdbNode(connectionInfo);
525         assertNotNull("connection failed", ovsdbNode);
526         LOG.info("testNetVirtFixedSG: should be connected: {}", ovsdbNode.getNodeId());
527
528         // Verify the minimum version required for this test
529         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
530         Assert.assertNotNull(ovsdbNodeAugmentation);
531         assertNotNull(ovsdbNodeAugmentation.getOvsVersion());
532         String ovsVersion = ovsdbNodeAugmentation.getOvsVersion();
533         Version version = Version.fromString(ovsVersion);
534         if (version.compareTo(minSGOvsVersion) < 0) {
535             LOG.warn("{} minimum version is required", minSGOvsVersion);
536             Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo,
537                     NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
538             Thread.sleep(1000);
539             Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
540             return;
541         }
542
543         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
544                 + " is not connected", isControllerConnected(connectionInfo));
545
546         Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
547         assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
548         long datapathId = southbound.getDataPathId(bridgeNode);
549         String datapathIdString = southbound.getDatapathId(bridgeNode);
550         LOG.info("testNetVirtFixedSG: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
551         assertNotEquals("datapathId was not found", datapathId, 0);
552
553         NeutronNetwork nn = neutronUtils.createNeutronNetwork(networkId, tenantId,
554                 NetworkHandler.NETWORK_TYPE_VXLAN, "100");
555         NeutronSubnet ns = neutronUtils.createNeutronSubnet(subnetId, tenantId, networkId, "10.0.0.0/24");
556         NeutronPort nport = neutronUtils.createNeutronPort(networkId, subnetId, portId,
557                 "compute", "10.0.0.10", "f6:00:00:0f:00:01");
558         NeutronPort dhcp = neutronUtils.createNeutronPort(networkId, subnetId, dhcpPortId,
559                 "dhcp", "10.0.0.1", "f6:00:00:0f:00:02");
560
561         Thread.sleep(1000);
562         Map<String, String> externalIds = Maps.newHashMap();
563         externalIds.put("attached-mac", "f6:00:00:0f:00:01");
564         externalIds.put("iface-id", portId);
565         southboundUtils.addTerminationPoint(bridgeNode, portName, "internal", null, externalIds, 3L);
566         southboundUtils.addTerminationPoint(bridgeNode, "vm1", "internal", null, null, 0L);
567         southboundUtils.addTerminationPoint(bridgeNode, "vm2", "internal", null, null, 0L);
568         Map<String, String> options = Maps.newHashMap();
569         options.put("key", "flow");
570         options.put("remote_ip", "192.168.120.32");
571         southboundUtils.addTerminationPoint(bridgeNode, "vx", "vxlan", options, null, 4L);
572         Thread.sleep(1000);
573
574         String flowId = "Egress_DHCP_Client"  + "_Permit_";
575         verifyFlow(datapathId, flowId, Service.EGRESS_ACL);
576
577         testDefaultSG(nport, datapathId, nn, tenantId, portId);
578         Thread.sleep(1000);
579         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
580         Thread.sleep(1000);
581         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
582     }
583
584     private void testDefaultSG(NeutronPort nport, long datapathId, NeutronNetwork nn, String tenantId, String portId)
585             throws InterruptedException {
586         INeutronSecurityGroupCRUD ineutronSecurityGroupCRUD =
587                 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
588         assertNotNull("Could not find ineutronSecurityGroupCRUD Service", ineutronSecurityGroupCRUD);
589         INeutronSecurityRuleCRUD ineutronSecurityRuleCRUD =
590                 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
591         assertNotNull("Could not find ineutronSecurityRuleCRUD Service", ineutronSecurityRuleCRUD);
592
593         NeutronSecurityGroup neutronSG = new NeutronSecurityGroup();
594         neutronSG.setSecurityGroupDescription("testig defaultSG-IT");
595         neutronSG.setSecurityGroupName("DefaultSG");
596         neutronSG.setSecurityGroupUUID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
597         neutronSG.setTenantID(tenantId);
598
599         List<NeutronSecurityRule> nsrs = new ArrayList<>();
600         NeutronSecurityRule nsrIN = new NeutronSecurityRule();
601         nsrIN.setSecurityRemoteGroupID(null);
602         nsrIN.setSecurityRuleDirection("ingress");
603         nsrIN.setSecurityRuleEthertype("IPv4");
604         nsrIN.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
605         nsrIN.setSecurityRuleProtocol("TCP");
606         nsrIN.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
607         nsrIN.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e6");
608         nsrIN.setTenantID(tenantId);
609
610         NeutronSecurityRule nsrEG = new NeutronSecurityRule();
611         nsrEG.setSecurityRemoteGroupID(null);
612         nsrEG.setSecurityRuleDirection("egress");
613         nsrEG.setSecurityRuleEthertype("IPv4");
614         nsrEG.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
615         nsrEG.setSecurityRuleProtocol("TCP");
616         nsrEG.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
617         nsrEG.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e1");
618         nsrEG.setTenantID(tenantId);
619
620         nsrs.add(nsrIN);
621         nsrs.add(nsrEG);
622
623         neutronSG.setSecurityRules(nsrs);
624         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrIN);
625         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrEG);
626         ineutronSecurityGroupCRUD.add(neutronSG);
627
628         List<NeutronSecurityGroup> sgs = new ArrayList<>();
629         sgs.add(neutronSG);
630         nport.setSecurityGroups(sgs);
631
632         INeutronPortCRUD iNeutronPortCRUD =
633                 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
634         iNeutronPortCRUD.update(portId, nport);
635
636         Thread.sleep(10000);
637         String flowId = "Egress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
638         verifyFlow(datapathId, flowId, Service.EGRESS_ACL);
639
640         flowId = "Ingress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
641         verifyFlow(datapathId, flowId, Service.INGRESS_ACL);
642     }
643
644     private Flow getFlow (
645             FlowBuilder flowBuilder,
646             org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder,
647             LogicalDatastoreType store) throws InterruptedException {
648
649         Flow flow = null;
650         for (int i = 0; i < 10; i++) {
651             LOG.info("getFlow try {} from {}: looking for flow: {}, node: {}",
652                     i, store, flowBuilder.build(), nodeBuilder.build());
653             flow = FlowUtils.getFlow(flowBuilder, nodeBuilder, dataBroker.newReadOnlyTransaction(), store);
654             if (flow != null) {
655                 LOG.info("getFlow try {} from {}: found flow: {}", i, store, flow);
656                 break;
657             }
658             Thread.sleep(1000);
659         }
660         return flow;
661     }
662
663     private void verifyFlow(long datapathId, String flowId, short table) throws InterruptedException {
664         org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder =
665                 FlowUtils.createNodeBuilder(datapathId);
666         FlowBuilder flowBuilder =
667                 FlowUtils.initFlowBuilder(new FlowBuilder(), flowId, table);
668         Flow flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
669         assertNotNull("Could not find flow in config: " + flowBuilder.build() + "--" + nodeBuilder.build(), flow);
670         flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
671         assertNotNull("Could not find flow in operational: " + flowBuilder.build() + "--" + nodeBuilder.build(),
672                 flow);
673     }
674
675     private void verifyFlow(long datapathId, String flowId, Service service) throws InterruptedException {
676         verifyFlow(datapathId, flowId, pipelineOrchestrator.getTable(service));
677     }
678 }