Convert elanmanager-impl to use mdsal EOS APIs
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / listeners / HwvtepLogicalSwitchListener.java
index 6de0d0479f8cb9261c32ed99403ceff9ef0b2310..7ff1ed4a22490c9d9b5e4a2b90c56b873b09dd15 100644 (file)
@@ -7,15 +7,17 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.listeners;
 
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.genius.datastoreutils.hwvtep.HwvtepClusteredDataTreeChangeListener;
+import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils;
+import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.netvirt.elan.l2gw.jobs.LogicalSwitchAddedJob;
+import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayMulticastUtils;
+import org.opendaylight.netvirt.elan.l2gw.utils.ElanL2GatewayUtils;
 import org.opendaylight.netvirt.elan.l2gw.utils.L2GatewayConnectionUtils;
-import org.opendaylight.genius.datastoreutils.AsyncDataChangeListenerBase;
-import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
+import org.opendaylight.netvirt.elan.utils.ElanClusterUtils;
+import org.opendaylight.netvirt.elan.utils.ElanUtils;
 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
-import org.opendaylight.genius.utils.SystemPropertyReader;
-import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l2gateways.rev150712.l2gateway.attributes.Devices;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
@@ -31,53 +33,62 @@ import org.slf4j.LoggerFactory;
  *
  * @see LogicalSwitches
  */
-public class HwvtepLogicalSwitchListener
-        extends AsyncDataChangeListenerBase<LogicalSwitches, HwvtepLogicalSwitchListener> {
+public class HwvtepLogicalSwitchListener extends
+        HwvtepClusteredDataTreeChangeListener<LogicalSwitches, HwvtepLogicalSwitchListener> {
 
     /** The Constant LOG. */
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepLogicalSwitchListener.class);
 
     /** The node id. */
-    private NodeId nodeId;
+    private final NodeId nodeId;
 
     /** The logical switch name. */
-    private String logicalSwitchName;
+    private final String logicalSwitchName;
 
     /** The physical device. */
-    private Devices physicalDevice;
+    private final Devices physicalDevice;
 
     /** The l2 gateway device. */
-    private L2GatewayDevice l2GatewayDevice;
+    private final L2GatewayDevice l2GatewayDevice;
 
-    /** The default vlan id. */
-    private Integer defaultVlanId;
+    // The default vlan id
+    private final Integer defaultVlanId;
 
-    /** Id of L2 Gateway connection responsible for this logical switch creation */
-    private Uuid l2GwConnId;
+    // Id of L2 Gateway connection responsible for this logical switch creation
+    private final Uuid l2GwConnId;
 
-    static DataStoreJobCoordinator dataStoreJobCoordinator;
-
-    public static void setDataStoreJobCoordinator(DataStoreJobCoordinator ds) {
-        dataStoreJobCoordinator = ds;
-    }
+    private final DataBroker broker;
+    private final ElanL2GatewayUtils elanL2GatewayUtils;
+    private final EntityOwnershipUtils entityOwnershipUtils;
+    private final ElanUtils elanUtils;
+    private final ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils;
 
     /**
      * Instantiates a new hardware vtep logical switch listener.
      *
-     * @param l2GatewayDevice
-     *            the l2 gateway device
-     * @param logicalSwitchName
-     *            the logical switch name
-     * @param physicalDevice
-     *            the physical device
-     * @param defaultVlanId
-     *            the default vlan id
-     * @param l2GwConnId
-     *            the l2 gateway connection id
+     * @param dataBroker DataBroker
+     * @param elanL2GatewayUtils l2 gateway utils
+     * @param entityOwnershipUtils the entity ownership utils
+     * @param elanUtils the ELAN utilities
+     * @param elanL2GatewayMulticastUtils l2 gateway multicast utils
+     * @param l2GatewayDevice the l2 gateway device
+     * @param logicalSwitchName the logical switch name
+     * @param physicalDevice the physical device
+     * @param defaultVlanId the default vlan id
+     * @param l2GwConnId l2 gateway connection id
      */
-    public HwvtepLogicalSwitchListener(L2GatewayDevice l2GatewayDevice, String logicalSwitchName,
-            Devices physicalDevice, Integer defaultVlanId, Uuid l2GwConnId) {
+    public HwvtepLogicalSwitchListener(DataBroker dataBroker, ElanL2GatewayUtils elanL2GatewayUtils,
+                                       EntityOwnershipUtils entityOwnershipUtils, ElanUtils elanUtils,
+                                       ElanL2GatewayMulticastUtils elanL2GatewayMulticastUtils,
+                                       L2GatewayDevice l2GatewayDevice,
+                                       String logicalSwitchName,
+                                       Devices physicalDevice, Integer defaultVlanId, Uuid l2GwConnId) {
         super(LogicalSwitches.class, HwvtepLogicalSwitchListener.class);
+        this.broker = dataBroker;
+        this.elanL2GatewayUtils = elanL2GatewayUtils;
+        this.entityOwnershipUtils = entityOwnershipUtils;
+        this.elanUtils = elanUtils;
+        this.elanL2GatewayMulticastUtils = elanL2GatewayMulticastUtils;
         this.nodeId = new NodeId(l2GatewayDevice.getHwvtepNodeId());
         this.logicalSwitchName = logicalSwitchName;
         this.physicalDevice = physicalDevice;
@@ -90,49 +101,30 @@ public class HwvtepLogicalSwitchListener
      * (non-Javadoc)
      *
      * @see
-     * org.opendaylight.genius.datastoreutils.AsyncDataChangeListenerBase#
+     * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
      * getWildCardPath()
      */
     @Override
-       public InstanceIdentifier<LogicalSwitches> getWildCardPath() {
+    public InstanceIdentifier<LogicalSwitches> getWildCardPath() {
         return HwvtepSouthboundUtils.createLogicalSwitchesInstanceIdentifier(nodeId,
                 new HwvtepNodeName(logicalSwitchName));
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see
-     * org.opendaylight.genius.datastoreutils.AsyncDataChangeListenerBase#
-     * getDataChangeListener()
-     */
-    @Override
-    protected DataChangeListener getDataChangeListener() {
-        return HwvtepLogicalSwitchListener.this;
-    }
-
-    /*
-     * (non-Javadoc)
-     *
-     * @see
-     * org.opendaylight.genius.datastoreutils.AsyncDataChangeListenerBase#
-     * getDataChangeScope()
-     */
     @Override
-    protected AsyncDataBroker.DataChangeScope getDataChangeScope() {
-        return AsyncDataBroker.DataChangeScope.BASE;
+    protected HwvtepLogicalSwitchListener getDataTreeChangeListener() {
+        return this;
     }
 
     /*
      * (non-Javadoc)
      *
      * @see
-     * org.opendaylight.genius.datastoreutils.AsyncDataChangeListenerBase#
+     * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
      * remove(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
      * org.opendaylight.yangtools.yang.binding.DataObject)
      */
     @Override
-    protected void remove(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches deletedLogicalSwitch) {
+    protected void removed(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches deletedLogicalSwitch) {
         LOG.trace("Received Remove DataChange Notification for identifier: {}, LogicalSwitches: {}", identifier,
                 deletedLogicalSwitch);
     }
@@ -141,52 +133,46 @@ public class HwvtepLogicalSwitchListener
      * (non-Javadoc)
      *
      * @see
-     * org.opendaylight.genius.datastoreutils.AsyncDataChangeListenerBase#
+     * org.opendaylight.genius.datastoreutils.AsyncClusteredDataChangeListenerBase#
      * update(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
      * org.opendaylight.yangtools.yang.binding.DataObject,
      * org.opendaylight.yangtools.yang.binding.DataObject)
      */
     @Override
-    protected void update(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchOld,
+    protected void updated(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchOld,
             LogicalSwitches logicalSwitchNew) {
         LOG.trace("Received Update DataChange Notification for identifier: {}, LogicalSwitches old: {}, new: {}."
                 + "No Action Performed.", identifier, logicalSwitchOld, logicalSwitchNew);
     }
 
-    /*
-     * (non-Javadoc)
-     *
-     * @see
-     * org.opendaylight.genius.datastoreutils.AsyncDataChangeListenerBase#
-     * add(org.opendaylight.yangtools.yang.binding.InstanceIdentifier,
-     * org.opendaylight.yangtools.yang.binding.DataObject)
-     */
     @Override
-    protected void add(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchNew) {
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    protected void added(InstanceIdentifier<LogicalSwitches> identifier, LogicalSwitches logicalSwitchNew) {
         LOG.debug("Received Add DataChange Notification for identifier: {}, LogicalSwitches: {}", identifier,
                 logicalSwitchNew);
         try {
-            L2GatewayDevice elanDevice = L2GatewayConnectionUtils.addL2DeviceToElanL2GwCache(
-                    logicalSwitchNew.getHwvtepNodeName().getValue(), l2GatewayDevice, l2GwConnId);
-
-            LogicalSwitchAddedJob logicalSwitchAddedWorker = new LogicalSwitchAddedJob(
-                    logicalSwitchName, physicalDevice, elanDevice, defaultVlanId);
-            dataStoreJobCoordinator.enqueueJob(logicalSwitchAddedWorker.getJobKey(), logicalSwitchAddedWorker,
-                    SystemPropertyReader.getDataStoreJobCoordinatorMaxRetries());
-
-        } catch (Exception e) {
-            LOG.error("Failed to handle HwVTEPLogicalSwitch - add: {}", e);
+            L2GatewayDevice elanDevice = L2GatewayConnectionUtils.addL2DeviceToElanL2GwCache(broker,
+                    logicalSwitchNew.getHwvtepNodeName().getValue(), elanL2GatewayUtils,
+                    l2GatewayDevice, l2GwConnId, physicalDevice);
+
+            LogicalSwitchAddedJob logicalSwitchAddedWorker = new LogicalSwitchAddedJob(elanL2GatewayUtils,
+                    elanL2GatewayMulticastUtils, logicalSwitchName, physicalDevice, elanDevice,
+                    defaultVlanId);
+            ElanClusterUtils.runOnlyInOwnerNode(entityOwnershipUtils, logicalSwitchAddedWorker.getJobKey(),
+                    "create vlan mappings and mcast configurations", logicalSwitchAddedWorker);
+        } catch (RuntimeException e) {
+            LOG.error("Failed to handle HwVTEPLogicalSwitch - add for: {}", identifier, e);
         } finally {
             try {
                 // This listener is specific to handle a specific logical
                 // switch, hence closing it.
                 LOG.trace("Closing LogicalSwitches listener for node: {}, logicalSwitch: {}", nodeId.getValue(),
                         logicalSwitchName);
+                // TODO use https://git.opendaylight.org/gerrit/#/c/44145/ when merged, and remove @SuppressWarnings
                 close();
             } catch (Exception e) {
-                LOG.warn("Failed to close HwVTEPLogicalSwitchListener: {}", e);
+                LOG.warn("Failed to close HwVTEPLogicalSwitchListener", e);
             }
         }
     }
-
-}
\ No newline at end of file
+}