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