it-utils should be only included for IT
[netvirt.git] / openstack / net-virt-it / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / it / NetvirtIT.java
index 2bef4e5279ad4908b3ee27f83c5e8a43d9152d26..d6f60f3cec226cfd5b370f5bc2cd7b1188dc93fb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Red Hat, Inc. and others.  All rights reserved.
+ * Copyright (c) 2015 - 2016 Red Hat, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -26,11 +26,7 @@ import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRunti
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
-import java.net.InetAddress;
-import java.net.NetworkInterface;
-import java.net.UnknownHostException;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
@@ -56,12 +52,12 @@ import org.opendaylight.neutron.spi.NeutronSubnet;
 import org.opendaylight.ovsdb.lib.notation.Version;
 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
-import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
+import org.opendaylight.ovsdb.openstack.netvirt.providers.NetvirtProvidersProvider;
 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
-import org.opendaylight.ovsdb.southbound.SouthboundMapper;
-import org.opendaylight.ovsdb.utils.config.ConfigProperties;
+import org.opendaylight.ovsdb.utils.it.ItUtils;
+import org.opendaylight.ovsdb.utils.it.NodeInfo;
 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
@@ -72,19 +68,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.InterfaceTypeEntryBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchOtherConfigs;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.ops4j.pax.exam.Configuration;
 import org.ops4j.pax.exam.Option;
@@ -105,8 +94,8 @@ import org.slf4j.LoggerFactory;
 @ExamReactorStrategy(PerClass.class)
 public class NetvirtIT extends AbstractMdsalTestBase {
     private static final Logger LOG = LoggerFactory.getLogger(NetvirtIT.class);
-    private static final int OVSDB_UPDATE_TIMEOUT = 1000;
     private static DataBroker dataBroker = null;
+    private static ItUtils itUtils;
     private static String addressStr;
     private static String portStr;
     private static String connectionType;
@@ -114,6 +103,7 @@ public class NetvirtIT extends AbstractMdsalTestBase {
     private static AtomicBoolean setup = new AtomicBoolean(false);
     private static MdsalUtils mdsalUtils = null;
     private static Southbound southbound = null;
+    private static PipelineOrchestrator pipelineOrchestrator = null;
     private static SouthboundUtils southboundUtils;
     private static NeutronUtils neutronUtils = new NeutronUtils();
     private static final String NETVIRT_TOPOLOGY_ID = "netvirt:1";
@@ -140,7 +130,7 @@ public class NetvirtIT extends AbstractMdsalTestBase {
 
     @Override
     public String getFeatureName() {
-        return "odl-ovsdb-openstack";
+        return "odl-ovsdb-openstack-it";
     }
 
     @Configuration
@@ -240,7 +230,8 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         try {
             super.setup();
         } catch (Exception e) {
-            e.printStackTrace();
+            LOG.warn("Failed to setup test", e);
+            fail("Failed to setup test: " + e);
         }
 
         getProperties();
@@ -252,12 +243,16 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         }
 
         dataBroker = getDatabroker(getProviderContext());
+        itUtils = new ItUtils(dataBroker);
         mdsalUtils = new MdsalUtils(dataBroker);
         assertNotNull("mdsalUtils should not be null", mdsalUtils);
         assertTrue("Did not find " + NETVIRT_TOPOLOGY_ID, getNetvirtTopology());
         southbound = (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
         assertNotNull("southbound should not be null", southbound);
         southboundUtils = new SouthboundUtils(mdsalUtils);
+        pipelineOrchestrator =
+                (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
+        assertNotNull("pipelineOrchestrator should not be null", pipelineOrchestrator);
         setup.set(true);
     }
 
@@ -271,7 +266,7 @@ public class NetvirtIT extends AbstractMdsalTestBase {
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
-                    e.printStackTrace();
+                    LOG.warn("Interrupted while waiting for provider context", e);
                 }
             }
         }
@@ -280,7 +275,7 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         try {
             Thread.sleep(1000);
         } catch (InterruptedException e) {
-            e.printStackTrace();
+            LOG.warn("Interrupted while waiting for other provider", e);
         }
         return providerContext;
     }
@@ -308,7 +303,7 @@ public class NetvirtIT extends AbstractMdsalTestBase {
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
-                    e.printStackTrace();
+                    LOG.warn("Interrupted while waiting for {}", NETVIRT_TOPOLOGY_ID, e);
                 }
             }
         }
@@ -348,14 +343,18 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         return true;
     }
 
+    // This is an extra test for local testing and testNetVirt covers this is more detail
+    @Ignore
     @Test
     public void testAddDeleteOvsdbNode() throws InterruptedException {
         LOG.info("testAddDeleteOvsdbNode enter");
         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
-        connectOvsdbNode(connectionInfo);
+        Node ovsdbNode = connectOvsdbNode(connectionInfo);
+        assertNotNull("connection failed", ovsdbNode);
+        LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
 
         assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
-                + " is not connected", isControllerConnected(connectionInfo));
+                + " is not connected", itUtils.isControllerConnected(connectionInfo));
 
         Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
         Thread.sleep(1000);
@@ -363,36 +362,49 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         LOG.info("testAddDeleteOvsdbNode exit");
     }
 
-    private boolean isControllerConnected(ConnectionInfo connectionInfo) throws InterruptedException {
-        LOG.info("isControllerConnected enter");
-        Boolean connected = false;
-        ControllerEntry controllerEntry;
-        Node ovsdbNode = southboundUtils.getOvsdbNode(connectionInfo);
-        assertNotNull("ovsdb node not found", ovsdbNode);
+    // TODO add tests for when L3 is enabled and check for br-ex
 
-        BridgeConfigurationManager bridgeConfigurationManager =
-                (BridgeConfigurationManager) ServiceHelper.getGlobalInstance(BridgeConfigurationManager.class, this);
-        assertNotNull("Could not find PipelineOrchestrator Service", bridgeConfigurationManager);
-        String controllerTarget = bridgeConfigurationManager.getControllersFromOvsdbNode(ovsdbNode).get(0);
-        Assert.assertNotNull("Failed to get controller target", controllerTarget);
+    // This is an extra test for local testing and testNetVirt covers this is more detail
+    @Ignore
+    @Test
+    public void testAddDeleteOvsdbNodeWithTableOffset() throws InterruptedException {
+        LOG.info("testAddDeleteOvsdbNodeWithTableOffset enter");
+        NetvirtProvidersProvider.setTableOffset((short)1);
+        ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
+        Node ovsdbNode = connectOvsdbNode(connectionInfo);
+        assertNotNull("connection failed", ovsdbNode);
+        LOG.info("testNetVirt: should be connected: {}", ovsdbNode.getNodeId());
 
-        for (int i = 0; i < 10; i++) {
-            LOG.info("isControllerConnected try {}: looking for controller: {}", i, controllerTarget);
-            OvsdbBridgeAugmentation bridge =
-                    southboundUtils.getBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
-            Assert.assertNotNull(bridge);
-            Assert.assertNotNull(bridge.getControllerEntry());
-            controllerEntry = bridge.getControllerEntry().iterator().next();
-            Assert.assertEquals(controllerTarget, controllerEntry.getTarget().getValue());
-            if (controllerEntry.isIsConnected()) {
-                Assert.assertTrue("Controller is not connected", controllerEntry.isIsConnected());
-                connected = true;
-                break;
+        assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
+                + " is not connected", itUtils.isControllerConnected(connectionInfo));
+
+        // Verify the pipeline flows were installed
+        Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
+        assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
+        long datapathId = southbound.getDataPathId(bridgeNode);
+        String datapathIdString = southbound.getDatapathId(bridgeNode);
+        LOG.info("testNetVirt: bridgeNode: {}, datapathId: {} - {}", bridgeNode, datapathIdString, datapathId);
+        assertNotEquals("datapathId was not found", datapathId, 0);
+
+        List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
+        List<Service> staticPipelineFound = Lists.newArrayList();
+        for (Service service : pipelineOrchestrator.getServiceRegistry().keySet()) {
+            if (staticPipeline.contains(service)) {
+                staticPipelineFound.add(service);
             }
-            Thread.sleep(1000);
+            String flowId = "DEFAULT_PIPELINE_FLOW_" + pipelineOrchestrator.getTable(service);
+            verifyFlow(datapathId, flowId, service);
         }
-        LOG.info("isControllerConnected exit: {} - {}", connected, controllerTarget);
-        return connected;
+        assertEquals("did not find all expected flows in static pipeline",
+                staticPipeline.size(), staticPipelineFound.size());
+
+        String flowId = "TableOffset_" + pipelineOrchestrator.getTable(Service.CLASSIFIER);
+        verifyFlow(datapathId, flowId, Service.CLASSIFIER.getTable());
+
+        Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
+        Thread.sleep(1000);
+        Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
+        LOG.info("testAddDeleteOvsdbNodeWithTableOffset exit");
     }
 
     @Ignore
@@ -433,22 +445,9 @@ public class NetvirtIT extends AbstractMdsalTestBase {
     public void testNetVirt() throws InterruptedException {
         LOG.info("testNetVirt: starting test");
         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
-        Node ovsdbNode = connectOvsdbNode(connectionInfo);
-        LOG.info("testNetVirt: should be connected");
-
-        //TODO use controller value rather that ovsdb connectionInfo or change log
-        assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
-                + " is not connected", isControllerConnected(connectionInfo));
-
-        // Verify the pipeline flows were installed
-        PipelineOrchestrator pipelineOrchestrator =
-                (PipelineOrchestrator) ServiceHelper.getGlobalInstance(PipelineOrchestrator.class, this);
-        assertNotNull("Could not find PipelineOrchestrator Service", pipelineOrchestrator);
-        Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
-        assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
-        LOG.info("testNetVirt: bridgeNode: {}", bridgeNode);
-        long datapathId = southbound.getDataPathId(bridgeNode);
-        assertNotEquals("datapathId was not found", datapathId, 0);
+        NodeInfo nodeInfo = itUtils.createNodeInfo(connectionInfo, null);
+        nodeInfo.connect();
+        LOG.info("testNetVirt: should be connected: {}", nodeInfo.ovsdbNode.getNodeId());
 
         List<Service> staticPipeline = pipelineOrchestrator.getStaticPipeline();
         List<Service> staticPipelineFound = Lists.newArrayList();
@@ -456,21 +455,19 @@ public class NetvirtIT extends AbstractMdsalTestBase {
             if (staticPipeline.contains(service)) {
                 staticPipelineFound.add(service);
             }
-            String flowId = "DEFAULT_PIPELINE_FLOW_" + service.getTable();
-            verifyFlow(datapathId, flowId, service.getTable());
+            String flowId = "DEFAULT_PIPELINE_FLOW_" + pipelineOrchestrator.getTable(service);
+            verifyFlow(nodeInfo.datapathId, flowId, service);
         }
         assertEquals("did not find all expected flows in static pipeline",
                 staticPipeline.size(), staticPipelineFound.size());
 
-        southboundUtils.addTerminationPoint(bridgeNode, NetvirtITConstants.PORT_NAME, "internal", null, null, 0L);
+        southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, NetvirtITConstants.PORT_NAME, "internal", null, null, 0L);
         Thread.sleep(1000);
         OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
-                southbound.getTerminationPointOfBridge(bridgeNode, NetvirtITConstants.PORT_NAME);
+                southbound.getTerminationPointOfBridge(nodeInfo.bridgeNode, NetvirtITConstants.PORT_NAME);
         Assert.assertNotNull("Did not find " + NetvirtITConstants.PORT_NAME, ovsdbTerminationPointAugmentation);
-        Thread.sleep(1000);
-        Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
-        Thread.sleep(1000);
-        Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
+
+        nodeInfo.disconnect();
     }
 
     @Test
@@ -484,12 +481,12 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         final String dhcpPortId ="521e29d6-67b8-4b3c-8633-027d21195115";
 
         ConnectionInfo connectionInfo = SouthboundUtils.getConnectionInfo(addressStr, portStr);
-        assertNotNull("connection failed", southboundUtils.connectOvsdbNode(connectionInfo));
-        Node ovsdbNode = connectOvsdbNode(connectionInfo);
-        assertNotNull("node is not connected", ovsdbNode);
+        NodeInfo nodeInfo = itUtils.createNodeInfo(connectionInfo, null);
+        nodeInfo.connect();
+        LOG.info("testNetVirtFixedSG: should be connected: {}", nodeInfo.ovsdbNode.getNodeId());
 
         // Verify the minimum version required for this test
-        OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
+        OvsdbNodeAugmentation ovsdbNodeAugmentation = nodeInfo.ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
         Assert.assertNotNull(ovsdbNodeAugmentation);
         assertNotNull(ovsdbNodeAugmentation.getOvsVersion());
         String ovsVersion = ovsdbNodeAugmentation.getOvsVersion();
@@ -503,14 +500,6 @@ public class NetvirtIT extends AbstractMdsalTestBase {
             return;
         }
 
-        assertTrue("Controller " + SouthboundUtils.connectionInfoToString(connectionInfo)
-                + " is not connected", isControllerConnected(connectionInfo));
-
-        Node bridgeNode = southbound.getBridgeNode(ovsdbNode, NetvirtITConstants.INTEGRATION_BRIDGE_NAME);
-        assertNotNull("bridge " + NetvirtITConstants.INTEGRATION_BRIDGE_NAME + " was not found", bridgeNode);
-        long datapathId = southbound.getDataPathId(bridgeNode);
-        assertNotEquals("datapathId was not found", datapathId, 0);
-
         NeutronNetwork nn = neutronUtils.createNeutronNetwork(networkId, tenantId,
                 NetworkHandler.NETWORK_TYPE_VXLAN, "100");
         NeutronSubnet ns = neutronUtils.createNeutronSubnet(subnetId, tenantId, networkId, "10.0.0.0/24");
@@ -523,23 +512,22 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         Map<String, String> externalIds = Maps.newHashMap();
         externalIds.put("attached-mac", "f6:00:00:0f:00:01");
         externalIds.put("iface-id", portId);
-        southboundUtils.addTerminationPoint(bridgeNode, portName, "internal", null, externalIds, 3L);
-        southboundUtils.addTerminationPoint(bridgeNode, "vm1", "internal", null, null, 0L);
-        southboundUtils.addTerminationPoint(bridgeNode, "vm2", "internal", null, null, 0L);
+        southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, portName, "internal", null, externalIds, 3L);
+        southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, "vm1", "internal", null, null, 0L);
+        southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, "vm2", "internal", null, null, 0L);
         Map<String, String> options = Maps.newHashMap();
         options.put("key", "flow");
         options.put("remote_ip", "192.168.120.32");
-        southboundUtils.addTerminationPoint(bridgeNode, "vx", "vxlan", options, null, 4L);
+        southboundUtils.addTerminationPoint(nodeInfo.bridgeNode, "vx", "vxlan", options, null, 4L);
         Thread.sleep(1000);
 
         String flowId = "Egress_DHCP_Client"  + "_Permit_";
-        verifyFlow(datapathId, flowId, Service.EGRESS_ACL.getTable());
+        verifyFlow(nodeInfo.datapathId, flowId, Service.EGRESS_ACL);
 
-        testDefaultSG(nport, datapathId, nn, tenantId, portId);
-        Thread.sleep(1000);
-        Assert.assertTrue(southboundUtils.deleteBridge(connectionInfo, NetvirtITConstants.INTEGRATION_BRIDGE_NAME));
+        testDefaultSG(nport, nodeInfo.datapathId, nn, tenantId, portId);
         Thread.sleep(1000);
-        Assert.assertTrue(disconnectOvsdbNode(connectionInfo));
+
+        nodeInfo.disconnect();
     }
 
     private void testDefaultSG(NeutronPort nport, long datapathId, NeutronNetwork nn, String tenantId, String portId)
@@ -594,12 +582,13 @@ public class NetvirtIT extends AbstractMdsalTestBase {
                 (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
         iNeutronPortCRUD.update(portId, nport);
 
+        LOG.info("Neutron ports have been added");
         Thread.sleep(10000);
         String flowId = "Egress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
-        verifyFlow(datapathId, flowId, Service.EGRESS_ACL.getTable());
+        verifyFlow(datapathId, flowId, Service.EGRESS_ACL);
 
         flowId = "Ingress_IP" + nn.getProviderSegmentationID() + "_" + nport.getMacAddress() + "_Permit_";
-        verifyFlow(datapathId, flowId, Service.INGRESS_ACL.getTable());
+        verifyFlow(datapathId, flowId, Service.INGRESS_ACL);
     }
 
     private Flow getFlow (
@@ -629,6 +618,11 @@ public class NetvirtIT extends AbstractMdsalTestBase {
         Flow flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.CONFIGURATION);
         assertNotNull("Could not find flow in config: " + flowBuilder.build() + "--" + nodeBuilder.build(), flow);
         flow = getFlow(flowBuilder, nodeBuilder, LogicalDatastoreType.OPERATIONAL);
-        assertNotNull("Could not find flow in operational: " + flowBuilder.build() + "--" + nodeBuilder.build(), flow);
+        assertNotNull("Could not find flow in operational: " + flowBuilder.build() + "--" + nodeBuilder.build(),
+                flow);
+    }
+
+    private void verifyFlow(long datapathId, String flowId, Service service) throws InterruptedException {
+        verifyFlow(datapathId, flowId, pipelineOrchestrator.getTable(service));
     }
 }