MD-SAL API integration
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-it / src / test / java / org / opendaylight / ovsdb / hwvtepsouthbound / it / HwvtepSouthboundIT.java
index 8136e593ef689c6fe42213cb39303e28aec89781..7ed4c6dd466a12cced906ff5bc58722877134d17 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright © 2015, 2017 Ericsson India Global Services Pvt Ltd. 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,
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.ovsdb.hwvtepsouthbound.it;
 
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.ops4j.pax.exam.CoreOptions.composite;
 import static org.ops4j.pax.exam.CoreOptions.maven;
@@ -23,30 +24,27 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Properties;
 import java.util.Set;
-
-import javax.annotation.Nullable;
 import javax.inject.Inject;
-
+import org.eclipse.jdt.annotation.Nullable;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.mdsal.it.base.AbstractMdsalTestBase;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
-import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundProvider;
 import org.opendaylight.ovsdb.utils.hwvtepsouthbound.utils.HwvtepSouthboundUtils;
-import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
+import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerMdsalUtils;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
@@ -58,6 +56,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hw
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.Tunnels;
 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.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;
@@ -70,6 +69,7 @@ import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
 import org.ops4j.pax.exam.options.MavenUrlReference;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.exam.util.Filter;
 import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -81,7 +81,6 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
 
     //Constants
 
-    public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
     public static final String CUSTOM_PROPERTIES = "etc/custom.properties";
     public static final String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
     public static final String DEFAULT_SERVER_IPADDRESS = "127.0.0.1";
@@ -97,21 +96,22 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
     private static final int OVSDB_UPDATE_TIMEOUT = 1000;
     private static final int OVSDB_ROUNDTRIP_TIMEOUT = 10000;
 
-    private static MdsalUtils mdsalUtils = null;
+    private static ControllerMdsalUtils mdsalUtils = null;
     private static boolean setup = false;
     private static int testMethodsRemaining;
     private static String addressStr;
     private static int portNumber;
     private static String connectionType;
     private static Node hwvtepNode;
-
+    @Inject @Filter(timeout = 60000)
+    private static DataBroker dataBroker = null;
     @Inject
     private BundleContext bundleContext;
 
     private static final NotifyingDataChangeListener OPERATIONAL_LISTENER =
             new NotifyingDataChangeListener(LogicalDatastoreType.OPERATIONAL);
 
-    private static class NotifyingDataChangeListener implements DataTreeChangeListener<Node> {
+    private static final class NotifyingDataChangeListener implements DataTreeChangeListener<Node> {
         private final LogicalDatastoreType type;
         private final Set<InstanceIdentifier<Node>> createdNodes = new HashSet<>();
         private final Set<InstanceIdentifier<Node>> removedNodes = new HashSet<>();
@@ -126,7 +126,7 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
             for (DataTreeModification<Node> change : changes) {
                 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
                 final DataObjectModification<Node> mod = change.getRootNode();
-                    switch (mod.getModificationType()) {
+                switch (mod.getModificationType()) {
                     case DELETE:
                         removedNodes.add(key);
                         break;
@@ -143,7 +143,7 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
                         break;
                     default:
                         throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
-                    }
+                }
             }
         }
 
@@ -160,6 +160,7 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
         }
     }
 
+    @Override
     @Configuration
     public Option[] config() {
         Option[] options = super.config();
@@ -181,13 +182,13 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
     }
 
     @Override
-    public String getModuleName() {
-        return "hwvtepsouthbound";
-    }
-
-    @Override
-    public String getInstanceName() {
-        return "hwvtepsouthbound-default";
+    public String getKarafDistro() {
+        return maven()
+                .groupId("org.opendaylight.ovsdb")
+                .artifactId("hwvtepsouthbound-karaf")
+                .versionAsInProject()
+                .type("zip")
+                .getURL();
     }
 
     @Override
@@ -205,65 +206,49 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
         return "odl-ovsdb-hwvtepsouthbound-test";
     }
 
+    protected String usage() {
+        return "Integration Test needs a valid connection configuration as follows :\n"
+                + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
+                + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
+    }
+
     @Override
     public Option getLoggingOption() {
         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
-                logConfiguration(HwvtepSouthboundIT.class),
-                LogLevel.INFO.name());
+                "log4j2.logger.hwvtepsouthbound-it.name",
+                HwvtepSouthboundIT.class.getPackage().getName());
+        option = composite(option, editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
+                "log4j2.logger.hwvtepsouthbound-it.level",
+                LogLevel.INFO.name()));
         option = composite(option, super.getLoggingOption());
         return option;
     }
 
-    @Override
-    public String getKarafDistro() {
-        return maven()
-                .groupId("org.opendaylight.ovsdb")
-                .artifactId("hwvtepsouthbound-karaf")
-                .versionAsInProject()
-                .type("zip")
-                .getURL();
-    }
-
-    protected String usage() {
-        return "Integration Test needs a valid connection configuration as follows :\n"
-                + "active connection : mvn -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify\n"
-                + "passive connection : mvn -Dovsdbserver.connection=passive verify\n";
-    }
-
     private Option[] getPropertiesOptions() {
         Properties props = new Properties(System.getProperties());
-        String addressStr = props.getProperty(SERVER_IPADDRESS, DEFAULT_SERVER_IPADDRESS);
+        String ipAddressStr = props.getProperty(SERVER_IPADDRESS, DEFAULT_SERVER_IPADDRESS);
         String portStr = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
-        String connectionType = props.getProperty(CONNECTION_TYPE, CONNECTION_TYPE_ACTIVE);
+        String connectionTypeStr = props.getProperty(CONNECTION_TYPE, CONNECTION_TYPE_ACTIVE);
 
         LOG.info("getPropertiesOptions: Using the following properties: mode= {}, ip:port= {}:{}",
-                connectionType, addressStr, portStr);
+                connectionTypeStr, ipAddressStr, portStr);
 
         return new Option[] {
-                editConfigurationFilePut(CUSTOM_PROPERTIES, SERVER_IPADDRESS, addressStr),
+                editConfigurationFilePut(CUSTOM_PROPERTIES, SERVER_IPADDRESS, ipAddressStr),
                 editConfigurationFilePut(CUSTOM_PROPERTIES, SERVER_PORT, portStr),
-                editConfigurationFilePut(CUSTOM_PROPERTIES, CONNECTION_TYPE, connectionType),
+                editConfigurationFilePut(CUSTOM_PROPERTIES, CONNECTION_TYPE, connectionTypeStr),
         };
     }
 
     @Before
     @Override
-    public void setup() throws InterruptedException {
+    public void setup() throws Exception {
         if (setup) {
             LOG.info("Skipping setup, already initialized");
             return;
         }
 
-        try {
-            super.setup();
-        } catch (Exception e) {
-            LOG.warn("Failed to setup test", e);
-            fail("Failed to setup test: " + e);
-        }
-        //dataBroker = getSession().getSALService(DataBroker.class);
-        Thread.sleep(3000);
-        DataBroker dataBroker = HwvtepSouthboundProvider.getDb();
-        Assert.assertNotNull("db should not be null", dataBroker);
+        super.setup();
 
         addressStr = bundleContext.getProperty(SERVER_IPADDRESS);
         String portStr = bundleContext.getProperty(SERVER_PORT);
@@ -283,11 +268,11 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
             }
         }
 
-        mdsalUtils = new MdsalUtils(dataBroker);
+        mdsalUtils = new ControllerMdsalUtils(dataBroker);
+        assertTrue("Did not find " + HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue(), getHwvtepTopology());
         final ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portNumber);
         final InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(connectionInfo);
-        final DataTreeIdentifier<Node> treeId =
-                        new DataTreeIdentifier<Node>(LogicalDatastoreType.OPERATIONAL, iid);
+        final DataTreeIdentifier<Node> treeId = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, iid);
 
         dataBroker.registerDataTreeChangeListener(treeId, OPERATIONAL_LISTENER);
 
@@ -314,6 +299,31 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
         setup = true;
     }
 
+    private Boolean getHwvtepTopology() {
+        LOG.info("getHwvtepTopology: looking for {}...", HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue());
+        Boolean found = false;
+        final TopologyId topologyId = HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID;
+        InstanceIdentifier<Topology> path =
+                InstanceIdentifier.create(NetworkTopology.class).child(Topology.class, new TopologyKey(topologyId));
+        for (int i = 0; i < 60; i++) {
+            Topology topology = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, path);
+            if (topology != null) {
+                LOG.info("getHwvtepTopology: found {}...", HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue());
+                found = true;
+                break;
+            } else {
+                LOG.info("getHwvtepTopology: still looking ({})...", i);
+                try {
+                    Thread.sleep(1000);
+                } catch (InterruptedException e) {
+                    LOG.warn("Interrupted while waiting for {}",
+                            HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID.getValue(), e);
+                }
+            }
+        }
+        return found;
+    }
+
     private Node connectHwvtepNode(ConnectionInfo connectionInfo) throws InterruptedException {
         final InstanceIdentifier<Node> iid = HwvtepSouthboundUtils.createInstanceIdentifier(connectionInfo);
         Assert.assertTrue(mdsalUtils.put(LogicalDatastoreType.CONFIGURATION,
@@ -336,38 +346,38 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
 
     private void waitForOperationalCreation(InstanceIdentifier<Node> iid) throws InterruptedException {
         synchronized (OPERATIONAL_LISTENER) {
-            long _start = System.currentTimeMillis();
+            long start = System.currentTimeMillis();
             LOG.info("Waiting for OPERATIONAL DataChanged creation on {}", iid);
             while (!OPERATIONAL_LISTENER.isCreated(
-                    iid) && (System.currentTimeMillis() - _start) < OVSDB_ROUNDTRIP_TIMEOUT) {
+                    iid) && System.currentTimeMillis() - start < OVSDB_ROUNDTRIP_TIMEOUT) {
                 OPERATIONAL_LISTENER.wait(OVSDB_UPDATE_TIMEOUT);
             }
-            LOG.info("Woke up, waited {} for creation of {}", (System.currentTimeMillis() - _start), iid);
+            LOG.info("Woke up, waited {} for creation of {}", System.currentTimeMillis() - start, iid);
         }
     }
 
     private static void waitForOperationalDeletion(InstanceIdentifier<Node> iid) throws InterruptedException {
         synchronized (OPERATIONAL_LISTENER) {
-            long _start = System.currentTimeMillis();
+            long start = System.currentTimeMillis();
             LOG.info("Waiting for OPERATIONAL DataChanged deletion on {}", iid);
             while (!OPERATIONAL_LISTENER.isRemoved(
-                    iid) && (System.currentTimeMillis() - _start) < OVSDB_ROUNDTRIP_TIMEOUT) {
+                    iid) && System.currentTimeMillis() - start < OVSDB_ROUNDTRIP_TIMEOUT) {
                 OPERATIONAL_LISTENER.wait(OVSDB_UPDATE_TIMEOUT);
             }
-            LOG.info("Woke up, waited {} for deletion of {}", (System.currentTimeMillis() - _start), iid);
+            LOG.info("Woke up, waited {} for deletion of {}", System.currentTimeMillis() - start, iid);
         }
     }
 
-    private ConnectionInfo getConnectionInfo(String addressStr, int portNumber) {
+    private ConnectionInfo getConnectionInfo(String ipAddressStr, int portNum) {
         InetAddress inetAddress = null;
         try {
-            inetAddress = InetAddress.getByName(addressStr);
+            inetAddress = InetAddress.getByName(ipAddressStr);
         } catch (UnknownHostException e) {
-            fail("Could not resolve " + addressStr + ": " + e);
+            fail("Could not resolve " + ipAddressStr + ": " + e);
         }
 
         IpAddress address = HwvtepSouthboundMapper.createIpAddress(inetAddress);
-        PortNumber port = new PortNumber(portNumber);
+        PortNumber port = new PortNumber(portNum);
 
         final ConnectionInfo connectionInfo = new ConnectionInfoBuilder()
                 .setRemoteIp(address)
@@ -382,11 +392,11 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
         private final String psName;
 
 
-        public TestPhysicalSwitch(final ConnectionInfo connectionInfo, String psName) {
+        TestPhysicalSwitch(final ConnectionInfo connectionInfo, String psName) {
             this(connectionInfo, psName, null, null, null, true, null, null, null);
         }
 
-        public TestPhysicalSwitch (final ConnectionInfo connectionInfo, final String name,
+        TestPhysicalSwitch(final ConnectionInfo connectionInfo, final String name,
                         @Nullable InstanceIdentifier<Node> psIid, @Nullable NodeId psNodeId,
                         @Nullable final String description, final boolean setManagedBy,
                         @Nullable final List<ManagementIps> managementIps,
@@ -395,19 +405,19 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
             this.connectionInfo = connectionInfo;
             this.psName = name;
             NodeBuilder psNodeBuilder = new NodeBuilder();
-            if(psIid == null) {
+            if (psIid == null) {
                 psIid = HwvtepSouthboundUtils.createInstanceIdentifier(connectionInfo, new HwvtepNodeName(psName));
             }
-            if(psNodeId == null) {
+            if (psNodeId == null) {
                 psNodeId = HwvtepSouthboundMapper.createManagedNodeId(psIid);
             }
             psNodeBuilder.setNodeId(psNodeId);
             PhysicalSwitchAugmentationBuilder psAugBuilder = new PhysicalSwitchAugmentationBuilder();
             psAugBuilder.setHwvtepNodeName(new HwvtepNodeName(psName));
-            if(description != null) {
+            if (description != null) {
                 psAugBuilder.setHwvtepNodeDescription(description);
             }
-            if(setManagedBy) {
+            if (setManagedBy) {
                 InstanceIdentifier<Node> nodePath = HwvtepSouthboundUtils.createInstanceIdentifier(connectionInfo);
                 psAugBuilder.setManagedBy(new HwvtepGlobalRef(nodePath));
             }
@@ -415,14 +425,14 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
             psAugBuilder.setTunnelIps(tunnelIps);
             psAugBuilder.setTunnels(tunnels);
             psNodeBuilder.addAugmentation(PhysicalSwitchAugmentation.class, psAugBuilder.build());
-            LOG.debug("Built with intent to store PhysicalSwitch data {}", psAugBuilder.toString());
+            LOG.debug("Built with intent to store PhysicalSwitch data {}", psAugBuilder);
             Assert.assertTrue(
-                            mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, psIid, psNodeBuilder.build()));
-                    try {
-                        Thread.sleep(OVSDB_UPDATE_TIMEOUT);
-                    } catch (InterruptedException e) {
-                        LOG.warn("Sleep interrupted while waiting for bridge creation (bridge {})", psName, e);
-                    }
+                    mdsalUtils.merge(LogicalDatastoreType.CONFIGURATION, psIid, psNodeBuilder.build()));
+            try {
+                Thread.sleep(OVSDB_UPDATE_TIMEOUT);
+            } catch (InterruptedException e) {
+                LOG.warn("Sleep interrupted while waiting for bridge creation (bridge {})", psName, e);
+            }
         }
 
         @Override
@@ -491,9 +501,9 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
         ConnectionInfo connectionInfo = getConnectionInfo(addressStr, portNumber);
 
         try (TestPhysicalSwitch testPSwitch = new TestPhysicalSwitch(connectionInfo, PS_NAME)) {
-            PhysicalSwitchAugmentation pSwitch = getPhysicalSwitch(connectionInfo);
-            Assert.assertNotNull(pSwitch);
-            LOG.info("PhysicalSwitch: {}", pSwitch);
+            PhysicalSwitchAugmentation phySwitch = getPhysicalSwitch(connectionInfo);
+            Assert.assertNotNull(phySwitch);
+            LOG.info("PhysicalSwitch: {}", phySwitch);
         }
     }
 
@@ -509,7 +519,7 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
                     LogicalDatastoreType dataStore) {
         Node psNode = getPhysicalSwitchNode(connectionInfo, psName, dataStore);
         Assert.assertNotNull(psNode);
-        PhysicalSwitchAugmentation psAugmentation = psNode.getAugmentation(PhysicalSwitchAugmentation.class);
+        PhysicalSwitchAugmentation psAugmentation = psNode.augmentation(PhysicalSwitchAugmentation.class);
         Assert.assertNotNull(psAugmentation);
         return psAugmentation;
     }
@@ -517,7 +527,6 @@ public class HwvtepSouthboundIT extends AbstractMdsalTestBase {
     private Node getPhysicalSwitchNode(ConnectionInfo connectionInfo, String psName, LogicalDatastoreType dataStore) {
         InstanceIdentifier<Node> psIid =
                         HwvtepSouthboundUtils.createInstanceIdentifier(connectionInfo, new HwvtepNodeName(psName));
-                return mdsalUtils.read(dataStore, psIid);
+        return mdsalUtils.read(dataStore, psIid);
     }
-
 }