Merge "Use dpl portId"
[netvirt.git] / openstack / net-virt-it / src / test / java / org / opendaylight / netvirt / 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.netvirt.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.netvirt.utils.netvirt.it.utils.NetvirtItUtils;
45 import org.opendaylight.neutron.spi.INeutronPortCRUD;
46 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
47 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
48 import org.opendaylight.neutron.spi.NeutronPort;
49 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
50 import org.opendaylight.neutron.spi.NeutronSecurityRule;
51 import org.opendaylight.neutron.spi.NeutronNetwork;
52 import org.opendaylight.neutron.spi.NeutronSubnet;
53 import org.opendaylight.ovsdb.lib.notation.Version;
54 import org.opendaylight.netvirt.openstack.netvirt.NetworkHandler;
55 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
56 import org.opendaylight.netvirt.openstack.netvirt.providers.NetvirtProvidersProvider;
57 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
58 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
59 import org.opendaylight.ovsdb.utils.ovsdb.it.utils.OvsdbItUtils;
60 import org.opendaylight.ovsdb.utils.ovsdb.it.utils.NodeInfo;
61 import org.opendaylight.netvirt.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.node.attributes.ConnectionInfo;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
76 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
77 import org.ops4j.pax.exam.Configuration;
78 import org.ops4j.pax.exam.Option;
79 import org.ops4j.pax.exam.junit.PaxExam;
80 import org.ops4j.pax.exam.karaf.options.LogLevelOption;
81 import org.ops4j.pax.exam.options.MavenUrlReference;
82 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
83 import org.ops4j.pax.exam.spi.reactors.PerClass;
84 import org.slf4j.Logger;
85 import org.slf4j.LoggerFactory;
86
87 /**
88  * Integration tests for netvirt
89  *
90  * @author Sam Hague (shague@redhat.com)
91  */
92 @RunWith(PaxExam.class)
93 @ExamReactorStrategy(PerClass.class)
94 public class NetvirtIT extends AbstractMdsalTestBase {
95     private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
96     private static DataBroker dataBroker = null;
97     private static OvsdbItUtils itUtils;
98     private static NetvirtItUtils nvItUtils;
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.netvirt")
125                 .artifactId("features-netvirt")
126                 .classifier("features")
127                 .type("xml")
128                 .versionAsInProject();
129     }
130
131     @Override
132     public String getFeatureName() {
133         return "odl-ovsdb-openstack-it";
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.netvirt", "utils.mdsal-openflow")
154                                 .version(asInProject())
155                                 .type("jar")),
156                 wrappedBundle(
157                         mavenBundle("org.opendaylight.netvirt", "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 = NetvirtItUtils.getDatabroker(getProviderContext());
246         itUtils = new OvsdbItUtils(dataBroker);
247         nvItUtils = new NetvirtItUtils(dataBroker);
248         mdsalUtils = new MdsalUtils(dataBroker);
249         assertNotNull("mdsalUtils should not be null", mdsalUtils);
250         assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
251         southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
252         assertNotNull("southbound should not be null", southbound);
253         southboundUtils = new SouthboundUtils(mdsalUtils);
254         pipelineOrchestrator =
255                 (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
256         assertNotNull("pipelineOrchestrator should not be null", pipelineOrchestrator);
257         setup.set(true);
258     }
259
260     private BindingAwareBroker.ProviderContext getProviderContext() {
261         BindingAwareBroker.ProviderContext providerContext = null;
262         for (int i=0; i < 60; i++) {
263             providerContext = getSession();
264             if (providerContext != null) {
265                 break;
266             } else {
267                 try {
268                     Thread.sleep(1000);
269                 } catch (InterruptedException e) {
270                     LOG.warn("Interrupted while waiting for provider context", e);
271                 }
272             }
273         }
274         assertNotNull("providercontext should not be null", providerContext);
275         /* One more second to let the provider finish initialization */
276         try {
277             Thread.sleep(1000);
278         } catch (InterruptedException e) {
279             LOG.warn("Interrupted while waiting for other provider", e);
280         }
281         return providerContext;
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                     LOG.warn("Interrupted while waiting for {}", NETVIRT_TOPOLOGY_ID, e);
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", itUtils.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", itUtils.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             nvItUtils.verifyFlow(datapathId, flowId, pipelineOrchestrator.getTable(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         nvItUtils.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     @Ignore
406     @Test
407     public void testOpenVSwitchOtherConfig() throws InterruptedException {
408         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
409         Node ovsdbNode = connectOvsdbNode(connectionInfo);
410         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
411         Assert.assertNotNull(ovsdbNodeAugmentation);
412         List<OpenvswitchOtherConfigs> otherConfigsList = ovsdbNodeAugmentation.getOpenvswitchOtherConfigs();
413         if (otherConfigsList != null) {
414             for (OpenvswitchOtherConfigs otherConfig : otherConfigsList) {
415                 if (otherConfig.getOtherConfigKey().equals("local_ip")) {
416                     LOG.info("local_ip: {}", otherConfig.getOtherConfigValue());
417                     break;
418                 } else {
419                     LOG.info("other_config {}:{}", otherConfig.getOtherConfigKey(), otherConfig.getOtherConfigValue());
420                 }
421             }
422         } else {
423             LOG.info("other_config is not present");
424         }
425         Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
426     }
427
428     /**
429      * Test for basic southbound events to netvirt.
430      * <pre>The test will:
431      * - connect to an OVSDB node and verify it is added to operational
432      * - then verify that br-int was created on the node and stored in operational
433      * - a port is then added to the bridge to verify that it is ignored by netvirt
434      * - remove the bridge
435      * - remove the node and verify it is not in operational
436      * </pre>
437      * @throws InterruptedException
438      */
439     @Test
440     public void testNetVirt() throws InterruptedException {
441         LOG.info("testNetVirt: starting test");
442         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
443         NodeInfo nodeInfo = itUtils.createNodeInfo(connectionInfo, null);
444         nodeInfo.connect();
445         LOG.info("testNetVirt: should be connected: {}", nodeInfo.ovsdbNode.getNodeId());
446
447         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
448         List<Service> staticPipelineFound = Lists.newArrayList();
449         for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
450             if (staticPipeline.contains(service)) {
451                 staticPipelineFound.add(service);
452             }
453             String flowId = "DEFAULT_PIPELINE_FLOW_" + pipelineOrchestrator.getTable(service);
454             nvItUtils.verifyFlow(nodeInfo.datapathId, flowId, pipelineOrchestrator.getTable(service));
455         }
456         assertEquals("did not find all expected flows in static pipeline",
457                 staticPipeline.size(), staticPipelineFound.size());
458
459         southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, NetvirtITConstants.PORT_NAME, "internal", null, null, 0L);
460         Thread.sleep(1000);
461         OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
462                 southbound.getTerminationPointOfBridge(nodeInfo.bridgeNode, NetvirtITConstants.PORT_NAME);
463         Assert.assertNotNull("Did not find " + NetvirtITConstants.PORT_NAME, ovsdbTerminationPointAugmentation);
464
465         nodeInfo.disconnect();
466     }
467
468     @Test
469     public void testNetVirtFixedSG() throws InterruptedException {
470         final Version minSGOvsVersion = Version.fromString("1.10.2");
471         final String portName = "sg1";
472         final String networkId = "521e29d6-67b8-4b3c-8633-027d21195111";
473         final String tenantId = "521e29d6-67b8-4b3c-8633-027d21195100";
474         final String subnetId = "521e29d6-67b8-4b3c-8633-027d21195112";
475         final String portId = "521e29d6-67b8-4b3c-8633-027d21195113";
476         final String dhcpPortId ="521e29d6-67b8-4b3c-8633-027d21195115";
477
478         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
479         NodeInfo nodeInfo = itUtils.createNodeInfo(connectionInfo, null);
480         nodeInfo.connect();
481         LOG.info("testNetVirtFixedSG: should be connected: {}", nodeInfo.ovsdbNode.getNodeId());
482
483         // Verify the minimum version required for this test
484         OvsdbNodeAugmentation ovsdbNodeAugmentation = nodeInfo.ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
485         Assert.assertNotNull(ovsdbNodeAugmentation);
486         assertNotNull(ovsdbNodeAugmentation.getOvsVersion());
487         String ovsVersion = ovsdbNodeAugmentation.getOvsVersion();
488         Version version = Version.fromString(ovsVersion);
489         if (version.compareTo(minSGOvsVersion) < 0) {
490             LOG.warn("{} minimum version is required", minSGOvsVersion);
491             Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo,
492                     NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
493             Thread.sleep(1000);
494             Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
495             return;
496         }
497
498         NeutronNetwork nn = neutronUtils.createNeutronNetwork(networkId, tenantId,
499                 NetworkHandler.NETWORK_TYPE_VXLAN, "100");
500         NeutronSubnet ns = neutronUtils.createNeutronSubnet(subnetId, tenantId, networkId, "10.0.0.0/24");
501         NeutronPort nport = neutronUtils.createNeutronPort(networkId, subnetId, portId,
502                 "compute", "10.0.0.10", "f6:00:00:0f:00:01");
503         NeutronPort dhcp = neutronUtils.createNeutronPort(networkId, subnetId, dhcpPortId,
504                 "dhcp", "10.0.0.1", "f6:00:00:0f:00:02");
505
506         Thread.sleep(1000);
507         Map<String, String> externalIds = Maps.newHashMap();
508         externalIds.put("attached-mac", "f6:00:00:0f:00:01");
509         externalIds.put("iface-id", portId);
510         southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, portName, "internal", null, externalIds, 3L);
511         southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, "vm1", "internal", null, null, 0L);
512         southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, "vm2", "internal", null, null, 0L);
513         Map<String, String> options = Maps.newHashMap();
514         options.put("key", "flow");
515         options.put("remote_ip", "192.168.120.32");
516         southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, "vx", "vxlan", options, null, 4L);
517         Thread.sleep(1000);
518
519         String flowId = "Egress_DHCP_Client"  + "_Permit_";
520         nvItUtils.verifyFlow(nodeInfo.datapathId, flowId, pipelineOrchestrator.getTable(Service.EGRESS_ACL));
521
522         testDefaultSG(nport, nodeInfo.datapathId, nn, tenantId, portId);
523         Thread.sleep(1000);
524
525         nodeInfo.disconnect();
526     }
527
528     private void testDefaultSG(NeutronPort nport, long datapathId, NeutronNetwork nn, String tenantId, String portId)
529             throws InterruptedException {
530         INeutronSecurityGroupCRUD ineutronSecurityGroupCRUD =
531                 (INeutronSecurityGroupCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityGroupCRUD.class, this);
532         assertNotNull("Could not find ineutronSecurityGroupCRUD Service", ineutronSecurityGroupCRUD);
533         INeutronSecurityRuleCRUD ineutronSecurityRuleCRUD =
534                 (INeutronSecurityRuleCRUD) ServiceHelper.getGlobalInstance(INeutronSecurityRuleCRUD.class, this);
535         assertNotNull("Could not find ineutronSecurityRuleCRUD Service", ineutronSecurityRuleCRUD);
536
537         NeutronSecurityGroup neutronSG = new NeutronSecurityGroup();
538         neutronSG.setSecurityGroupDescription("testig defaultSG-IT");
539         neutronSG.setSecurityGroupName("DefaultSG");
540         neutronSG.setSecurityGroupUUID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
541         neutronSG.setTenantID(tenantId);
542
543         List<NeutronSecurityRule> nsrs = new ArrayList<>();
544         NeutronSecurityRule nsrIN = new NeutronSecurityRule();
545         nsrIN.setSecurityRemoteGroupID(null);
546         nsrIN.setSecurityRuleDirection("ingress");
547         nsrIN.setSecurityRuleEthertype("IPv4");
548         nsrIN.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
549         nsrIN.setSecurityRuleProtocol("TCP");
550         nsrIN.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
551         nsrIN.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e6");
552         nsrIN.setTenantID(tenantId);
553
554         NeutronSecurityRule nsrEG = new NeutronSecurityRule();
555         nsrEG.setSecurityRemoteGroupID(null);
556         nsrEG.setSecurityRuleDirection("egress");
557         nsrEG.setSecurityRuleEthertype("IPv4");
558         nsrEG.setSecurityRuleGroupID("d3329053-bae5-4bf4-a2d1-7330f11ba5db");
559         nsrEG.setSecurityRuleProtocol("TCP");
560         nsrEG.setSecurityRuleRemoteIpPrefix("10.0.0.0/24");
561         nsrEG.setSecurityRuleUUID("823faaf7-175d-4f01-a271-0bf56fb1e7e1");
562         nsrEG.setTenantID(tenantId);
563
564         nsrs.add(nsrIN);
565         nsrs.add(nsrEG);
566
567         neutronSG.setSecurityRules(nsrs);
568         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrIN);
569         ineutronSecurityRuleCRUD.addNeutronSecurityRule(nsrEG);
570         ineutronSecurityGroupCRUD.add(neutronSG);
571
572         List<NeutronSecurityGroup> sgs = new ArrayList<>();
573         sgs.add(neutronSG);
574         nport.setSecurityGroups(sgs);
575
576         INeutronPortCRUD iNeutronPortCRUD =
577                 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
578         iNeutronPortCRUD.update(portId, nport);
579
580         LOG.info("Neutron ports have been added");
581         Thread.sleep(10000);
582         String flowId = "Egress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
583         nvItUtils.verifyFlow(datapathId, flowId, pipelineOrchestrator.getTable(Service.EGRESS_ACL));
584
585         flowId = "Ingress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
586         nvItUtils.verifyFlow(datapathId, flowId, pipelineOrchestrator.getTable(Service.INGRESS_ACL));
587     }
588
589     private Flow getFlow (
590             FlowBuilder flowBuilder,
591             org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder nodeBuilder,
592             LogicalDatastoreType store) throws InterruptedException {
593
594         Flow flow = null;
595         for (int i = 0; i < 10; i++) {
596             LOG.info("getFlow try {} from {}: looking for flow: {}, node: {}",
597                     i, store, flowBuilder.build(), nodeBuilder.build());
598             flow = FlowUtils.getFlow(flowBuilder, nodeBuilder, dataBroker.newReadOnlyTransaction(), store);
599             if (flow != null) {
600                 LOG.info("getFlow try {} from {}: found flow: {}", i, store, flow);
601                 break;
602             }
603             Thread.sleep(1000);
604         }
605         return flow;
606     }
607 }