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