subnet-op-data and port-op-data is empty after cluster reboot 10/48310/7
authorSuraj Ranjan <suraj.ranjan@ericsson.com>
Mon, 14 Nov 2016 10:53:10 +0000 (16:23 +0530)
committerAlon Kochba <alonko@hpe.com>
Mon, 2 Jan 2017 16:14:49 +0000 (16:14 +0000)
Change-Id: If7e0b5d3109c53eb58505e69a389d15d2f7a6d1e
Signed-off-by: Suraj Ranjan <suraj.ranjan@ericsson.com>
vpnservice/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/netvirt/neutronvpn/NeutronPortChangeListener.java
vpnservice/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/netvirt/neutronvpn/NeutronvpnManager.java
vpnservice/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/netvirt/neutronvpn/SubnetmapListener.java [new file with mode: 0644]
vpnservice/neutronvpn/neutronvpn-impl/src/main/resources/org/opendaylight/blueprint/neutronvpn.xml

index f077aa1144930a230cf687a5e9cf0c208e58349c..4345f8fb3143d638b6591ffe4242c0d255c85568 100644 (file)
@@ -386,26 +386,6 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                     // create vpn-interface on this neutron port
                     LOG.debug("Adding VPN Interface for port {}", portName);
                     nvpnManager.createVpnInterface(vpnId, routerId, port, wrtConfigTxn);
-                    // send port added to subnet notification
-                    // only sent when the port is part of a VPN
-                    String elanInstanceName = port.getNetworkId().getValue();
-                    InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                            .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-                    try {
-                        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                                .CONFIGURATION, elanIdentifierId);
-                        if (elanInstance.isPresent()) {
-                            long elanTag = elanInstance.get().getElanTag();
-                            checkAndPublishPortAddNotification(subnetMap.getSubnetIp(), subnetId, port.getUuid(), elanTag);
-
-                            LOG.debug("Port added to subnet notification sent for port {}", portName);
-                        } else {
-                            LOG.error("Port added to subnet notification failed for port {} because of failure in " +
-                                    "reading ELANInstance {}", portName, elanInstanceName);
-                        }
-                    } catch (Exception e) {
-                        LOG.error("Port added to subnet notification failed for port {}", portName, e);
-                    }
                 }
                 futures.add(wrtConfigTxn.submit());
                 return futures;
@@ -438,27 +418,6 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                     // remove vpn-interface for this neutron port
                     LOG.debug("removing VPN Interface for port {}", portName);
                     nvpnManager.deleteVpnInterface(vpnId, routerId, port, wrtConfigTxn);
-                    // send port removed from subnet notification
-                    // only sent when the port was part of a VPN
-                    String elanInstanceName = port.getNetworkId().getValue();
-                    InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                            .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-                    try {
-                        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                                .CONFIGURATION, elanIdentifierId);
-                        if (elanInstance.isPresent()) {
-                            long elanTag = elanInstance.get().getElanTag();
-                            checkAndPublishPortRemoveNotification(subnetMap.getSubnetIp(), subnetId, port.getUuid(),
-                                    elanTag);
-
-                            LOG.debug("Port removed from subnet notification sent for port {}", portName);
-                        } else {
-                            LOG.error("Port removed from subnet notification failed for port {} because of failure in" +
-                                    "reading ELANInstance {}", portName, elanInstanceName);
-                        }
-                    } catch (Exception e) {
-                        LOG.error("Port removed from subnet notification failed for port {}", portName, e);
-                    }
                 }
                 // Remove of-port interface for this neutron port
                 // ELAN interface is also implicitly deleted as part of this operation
@@ -502,65 +461,9 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                             Subnetmap subnetMapOld = nvpnManager.removePortsFromSubnetmapNode(subnetIdOr, portoriginal
                                     .getUuid(), null);
                             Uuid vpnIdOld = (subnetMapOld != null) ? subnetMapOld.getVpnId() : null;
-                            if (vpnIdOld != null) {
-                                // send port removed from subnet notification
-                                // only sent when the port was part of a VPN
-                                String portOriginalName = portoriginal.getUuid().getValue();
-                                String elanInstanceName = portoriginal.getNetworkId().getValue();
-                                InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder
-                                        (ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey
-                                        (elanInstanceName)).build();
-                                try {
-                                    Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker,
-                                            LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
-                                    if (elanInstance.isPresent()) {
-                                        long elanTag = elanInstance.get().getElanTag();
-                                        checkAndPublishPortRemoveNotification(subnetMapOld.getSubnetIp(), subnetIdOr,
-                                                portoriginal.getUuid(), elanTag);
-
-                                        LOG.debug("Port removed from subnet notification sent for port {}",
-                                                portOriginalName);
-                                    } else {
-                                        LOG.error("Port removed from subnet notification failed for port {} because "
-                                                + "of failure in" + "reading ELANInstance {}", portOriginalName,
-                                                elanInstanceName);
-                                    }
-                                } catch (Exception e) {
-                                    LOG.error("Port removed from subnet notification failed for port {}",
-                                            portOriginalName, e);
-                                }
-                            }
                             Subnetmap subnetMapNew = nvpnManager.updateSubnetmapNodeWithPorts(subnetIdUp, portupdate
                                             .getUuid(), null);
                             vpnIdNew = (subnetMapNew != null) ? subnetMapNew.getVpnId() : null;
-                            if (vpnIdNew != null) {
-                                // send port added to subnet notification
-                                // only sent when the port is part of a VPN
-                                String portUpdatedName = portoriginal.getUuid().getValue();
-                                String elanInstanceName = portupdate.getNetworkId().getValue();
-                                InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder
-                                        (ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey
-                                        (elanInstanceName)).build();
-                                try {
-                                    Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker,
-                                            LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
-                                    if (elanInstance.isPresent()) {
-                                        long elanTag = elanInstance.get().getElanTag();
-                                        checkAndPublishPortAddNotification(subnetMapNew.getSubnetIp(), subnetIdUp,
-                                                portupdate.getUuid(), elanTag);
-
-                                        LOG.debug("Port added to subnet notification sent for port {}",
-                                                portUpdatedName);
-                                    } else {
-                                        LOG.error("Port added to subnet notification failed for port {} because of " +
-                                                "failure in " + "reading ELANInstance {}", portUpdatedName,
-                                                elanInstanceName);
-                                    }
-                                } catch (Exception e) {
-                                    LOG.error("Port added to subnet notification failed for port {}",
-                                            portUpdatedName, e);
-                                }
-                            }
                         }
                         if (!subnetUpdated) {
                             Subnetmap subnetmap = NeutronvpnUtils.getSubnetmap(dataBroker, subnetIdUp);
@@ -717,28 +620,4 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                     .getValue(), e);
         }
     }
-
-    private void checkAndPublishPortAddNotification(String subnetIp, Uuid subnetId, Uuid portId, Long elanTag) throws
-            InterruptedException {
-        PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
-        LOG.info("publish notification called");
-        builder.setSubnetIp(subnetIp);
-        builder.setSubnetId(subnetId);
-        builder.setPortId(portId);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
-
-    private void checkAndPublishPortRemoveNotification(String subnetIp, Uuid subnetId, Uuid portId, Long elanTag)
-            throws InterruptedException {
-        PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
-        LOG.info("publish notification called");
-        builder.setPortId(portId);
-        builder.setSubnetIp(subnetIp);
-        builder.setSubnetId(subnetId);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
 }
index bd1e44a3d5703a5af288c6132edb5fce8fedda78..c9433753c625d955a5b570d758b7a8e806039069 100644 (file)
@@ -44,9 +44,6 @@ import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev14081
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
 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.yang.types.rev130715.Uuid;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.Adjacencies;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.Adjacency;
@@ -81,9 +78,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev15060
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpn;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpnBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterInterfacesMap;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetAddedToVpnBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetDeletedFromVpnBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetUpdatedInVpnBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.createl3vpn.input.L3vpn;
@@ -1094,28 +1088,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         LOG.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
         Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId);
         final Uuid routerId = NeutronvpnUtils.getVpnMap(dataBroker, vpnId).getRouterId();
-        // send subnet added to vpn notification
-        isExternalVpn = vpnId.equals(routerId) ? false : true;
-        String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        try {
-            Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                    .CONFIGURATION, elanIdentifierId);
-            if (elanInstance.isPresent()) {
-                long elanTag = elanInstance.get().getElanTag();
-                checkAndPublishSubnetAddNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn,
-                        elanTag);
-                LOG.debug("Subnet added to VPN notification sent for subnet {} on VPN {}", subnet.getValue(),
-                        vpnId.getValue());
-            } else {
-                LOG.error("Subnet added to VPN notification failed for subnet {}  on VPN {} because of failure in " +
-                        "reading ELANInstance {}", subnet.getValue(), vpnId.getValue(), elanInstanceName);
-            }
-        } catch (Exception e) {
-            LOG.error("Subnet added to VPN notification failed for subnet {} on VPN {}", subnet.getValue(), vpnId
-                    .getValue(), e);
-        }
         // Check if there are ports on this subnet and add corresponding
         // vpn-interfaces
         List<Uuid> portList = sn.getPortList();
@@ -1159,27 +1131,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
             }
         }
         sn = updateSubnetNode(subnet, null, null, null, null, vpnId);
-        // send vpn updated for subnet notification
-        String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        try {
-            Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                    .CONFIGURATION, elanIdentifierId);
-            if (elanInstance.isPresent()) {
-                long elanTag = elanInstance.get().getElanTag();
-                checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated,
-                        elanTag);
-                LOG.debug("VPN updated for subnet notification sent for subnet {} on VPN {}", subnet.getValue(),
-                        vpnId.getValue());
-            } else {
-                LOG.error("VPN updated for subnet notification failed for subnet {} on VPN {} because of failure " +
-                        "in reading ELANInstance {}", subnet.getValue(), vpnId.getValue(), elanInstanceName);
-            }
-        } catch (Exception e) {
-            LOG.error("VPN updated for subnet notification failed for subnet {} on VPN {}", subnet.getValue(),
-                    vpnId.getValue(), e);
-        }
         // Check for ports on this subnet and update association of
         // corresponding vpn-interfaces to external vpn
         List<Uuid> portList = sn.getPortList();
@@ -1493,28 +1444,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         LOG.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
         final Uuid routerId = NeutronvpnUtils.getVpnMap(dataBroker, vpnId).getRouterId();
         Subnetmap sn = NeutronvpnUtils.getSubnetmap(dataBroker, subnet);
-        // send subnet removed from vpn notification
-        isExternalVpn = vpnId.equals(routerId) ? false : true;
-        String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        try {
-            Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                    .CONFIGURATION, elanIdentifierId);
-            if (elanInstance.isPresent()) {
-                long elanTag = elanInstance.get().getElanTag();
-                checkAndPublishSubnetDelNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn,
-                        elanTag);
-                LOG.debug("Subnet removed from VPN notification sent for subnet {} on VPN {}", subnet.getValue(),
-                        vpnId.getValue());
-            } else {
-                LOG.error("Subnet removed from VPN notification failed for subnet {} on VPN {} because of failure " +
-                        "in reading ELANInstance {}", subnet.getValue(), vpnId.getValue(), elanInstanceName);
-            }
-        } catch (Exception e) {
-            LOG.error("Subnet removed from VPN notification failed for subnet {} on VPN {}", subnet.getValue(),
-                    vpnId.getValue(), e);
-        }
         if (sn != null) {
             // Check if there are ports on this subnet; remove corresponding vpn-interfaces
             List<Uuid> portList = sn.getPortList();
@@ -2195,51 +2124,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         return help.toString();
     }
 
-    private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName,
-                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
-        SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder();
-
-        LOG.info("publish notification called");
-
-        builder.setSubnetId(subnetId);
-        builder.setSubnetIp(subnetIp);
-        builder.setVpnName(vpnName);
-        builder.setExternalVpn(isExternalvpn);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
-
-    private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName,
-                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
-        SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder();
-
-        LOG.info("publish notification called");
-
-        builder.setSubnetId(subnetId);
-        builder.setSubnetIp(subnetIp);
-        builder.setVpnName(vpnName);
-        builder.setExternalVpn(isExternalvpn);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
-
-    private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName,
-                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
-        SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder();
-
-        LOG.info("publish notification called");
-
-        builder.setSubnetId(subnetId);
-        builder.setSubnetIp(subnetIp);
-        builder.setVpnName(vpnName);
-        builder.setExternalVpn(isExternalvpn);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
-
     private void checkAndPublishRouterAssociatedtoVpnNotification(Uuid routerId, Uuid vpnId) throws
             InterruptedException {
         RouterAssociatedToVpn routerAssociatedToVpn = new RouterAssociatedToVpnBuilder().setRouterId(routerId)
diff --git a/vpnservice/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/netvirt/neutronvpn/SubnetmapListener.java b/vpnservice/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/netvirt/neutronvpn/SubnetmapListener.java
new file mode 100644 (file)
index 0000000..1fa9fb4
--- /dev/null
@@ -0,0 +1,261 @@
+/*
+ * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.netvirt.neutronvpn;
+
+import com.google.common.base.Optional;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
+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.netvirt.elan.rev150602.ElanInstances;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortAddedToSubnetBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetAddedToVpnBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetDeletedFromVpnBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetUpdatedInVpnBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class SubnetmapListener extends AsyncDataTreeChangeListenerBase<Subnetmap, SubnetmapListener> {
+    private static final Logger LOG = LoggerFactory.getLogger(SubnetmapListener.class);
+    private final DataBroker dataBroker;
+    private NotificationPublishService notificationPublishService;
+
+    public SubnetmapListener(final DataBroker dataBroker, final NotificationPublishService notiPublishService) {
+        super(Subnetmap.class, SubnetmapListener.class);
+        this.dataBroker = dataBroker;
+        this.notificationPublishService = notiPublishService;
+    }
+
+    public void start() {
+        LOG.info("{} start", getClass().getSimpleName());
+        registerListener(dataBroker);
+    }
+
+    @Override
+    protected InstanceIdentifier<Subnetmap> getWildCardPath() {
+        return InstanceIdentifier.create(Subnetmaps.class).child(Subnetmap.class);
+    }
+
+    private void registerListener(final DataBroker db) {
+        try {
+            registerListener(LogicalDatastoreType.CONFIGURATION, db);
+        } catch (final Exception e) {
+            LOG.error("NeutronVpn subnetMap config DataChange listener registration fail!", e);
+            throw new IllegalStateException("NeutronVpn subnetMap config DataChange listener registration failed.", e);
+        }
+    }
+
+    @Override
+    protected void add(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
+        LOG.trace("SubnetmapListener add subnetmap method - key: " + identifier + ", value=" + subnetmap);
+    }
+
+    @Override
+    protected void remove(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
+        LOG.trace("SubnetmapListener remove subnetmap method - key: " + identifier + ", value" + subnetmap);
+    }
+
+    @Override
+    protected void update(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmapOriginal, Subnetmap subnetmapUpdate) {
+        LOG.trace("SubnetmapListener update subnetmap method - key: " + identifier + ", original=" + subnetmapOriginal + ", update=" + subnetmapUpdate);
+        Uuid vpnIdNew = subnetmapUpdate.getVpnId();
+        Uuid vpnIdOld = subnetmapOriginal.getVpnId();
+        Uuid subnetId = subnetmapUpdate.getId();
+        String elanInstanceName = subnetmapUpdate.getNetworkId().getValue();
+        String subnetIp = subnetmapUpdate.getSubnetIp();
+        Long elanTag = getElanTag(elanInstanceName);
+        if (elanTag.equals(0L)) {
+            LOG.debug("Unable to fetch elantag from ElanInstance {} and hence not proceeding with subnetmapListener update" +
+                    " for subnet {}", elanInstanceName, subnetId);
+            return;
+        }
+        //////////////////////////SubnetAddedToVpn////////////////////////////////////////////////////
+        if (vpnIdNew != null && vpnIdOld == null) {
+            boolean isExternalVpn = vpnIdNew.equals(subnetmapUpdate.getRouterId()) ? false : true;
+            try {
+                checkAndPublishSubnetAddedToVpnNotification(subnetId, subnetIp, vpnIdNew.getValue(),
+                        isExternalVpn, elanTag);
+                LOG.debug("Subnet added to VPN notification sent for subnet {} on VPN {}", subnetId.getValue(),
+                        vpnIdNew.getValue());
+            } catch (Exception e) {
+                LOG.error("Subnet added to VPN notification failed for subnet {} on VPN {}", subnetId.getValue(),
+                        vpnIdNew.getValue(), e);
+            }
+            return;
+        }
+        ////////////////////////////SubnetRemovedFromVpn//////////////////////////////////////////////
+        if (vpnIdOld != null && vpnIdNew == null) {
+            Boolean isExternalVpn = vpnIdOld.equals(subnetmapOriginal.getRouterId()) ? false : true;
+            try {
+                checkAndPublishSubnetDeletedFromVpnNotification(subnetId, subnetIp,
+                        vpnIdOld.getValue(), isExternalVpn, elanTag);
+                LOG.debug("Subnet removed from VPN notification sent for subnet {} on VPN {}", subnetId.getValue(),
+                        vpnIdOld.getValue());
+            } catch (Exception e){
+                LOG.error("Subnet removed from VPN notification failed for subnet {} on VPN {}", subnetId.getValue(),
+                        vpnIdOld.getValue(), e);
+            }
+            return;
+        }
+        ///////////////////////////SubnetUpdatedInVpn///////////////////////////////////////////////
+        if (vpnIdOld != null && vpnIdNew != null && (!vpnIdNew.equals(vpnIdOld))) {
+            boolean isBeingAssociated = subnetmapUpdate.getVpnId().equals(subnetmapUpdate.getRouterId()) ? false : true;
+            try {
+                checkAndPublishSubnetUpdatedInVpnNotification(subnetId, subnetIp,
+                        subnetmapUpdate.getVpnId().getValue(), isBeingAssociated, elanTag);
+                LOG.debug("VPN updated for subnet notification sent for subnet {} on VPN {}", subnetId.getValue(),
+                        vpnIdNew.getValue());
+            } catch (Exception e){
+                LOG.error("VPN updated for subnet notification failed for subnet {} on VPN {}", subnetId.getValue(),
+                        vpnIdNew.getValue(), e);
+            }
+            return;
+        }
+        ///////////////////////////PortAdded/RemovedFromSubnet///////////////////////////////////////////////
+        List<Uuid> oldPortList;
+        List<Uuid> newPortList;
+        newPortList = subnetmapUpdate.getPortList() != null ? subnetmapUpdate.getPortList() : new ArrayList<>();
+        oldPortList = subnetmapOriginal.getPortList() != null ? subnetmapOriginal.getPortList() : new ArrayList<>();
+        if (newPortList.size() == oldPortList.size()) {
+            return;
+        }
+        if (newPortList.size() > oldPortList.size()) {
+            for (Uuid port : newPortList) {
+                if (! oldPortList.contains(port)) {
+                    try {
+                        checkAndPublishPortAddedToSubnetNotification(subnetIp, subnetId, port, elanTag);
+                        LOG.debug("Port added to subnet notification sent for port {} in subnet {}", port.getValue(),
+                                subnetId.getValue());
+                    }catch (Exception e){
+                        LOG.error("Port added to subnet notification failed for port {} in subnet {}", port.getValue(),
+                                subnetId.getValue(), e);
+                    }
+                    return;
+                }
+            }
+        } else {
+            for (Uuid port : oldPortList) {
+                if (! newPortList.contains(port)) {
+                    try {
+                        checkAndPublishPortRemovedFromSubnetNotification(subnetIp, subnetId, port, elanTag);
+                        LOG.debug("Port removed from subnet notification sent for port {} in subnet {}", port.getValue(),
+                                subnetId.getValue());
+                    }catch (Exception e){
+                        LOG.error("Port removed from subnet notification failed for port {} in subnet {}", port.getValue(),
+                                subnetId.getValue(), e);
+                    }
+                    return;
+                }
+            }
+        }
+    }
+
+    @Override
+    protected SubnetmapListener getDataTreeChangeListener() {
+        return this;
+    }
+
+    protected long getElanTag (String elanInstanceName) {
+        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
+                .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
+        long elanTag = 0L;
+        try {
+            Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
+                    .CONFIGURATION, elanIdentifierId);
+            if (elanInstance.isPresent()) {
+                elanTag = elanInstance.get().getElanTag();
+            } else {
+                LOG.error("Notification failed because of failure in reading ELANInstance {}", elanInstanceName);
+            }
+        } catch (Exception e) {
+            LOG.error("Notification failed because of failure in fetching elanTag from ElanInstance {} config DS", elanInstanceName, e);
+        }
+        return elanTag;
+    }
+
+    private void checkAndPublishSubnetAddedToVpnNotification(Uuid subnetId, String subnetIp, String vpnName,
+                                                             Boolean isExternalvpn, Long elanTag) throws InterruptedException {
+        SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder();
+
+        LOG.trace("publish notification called from SubnetAddedToVpnNotification");
+
+        builder.setSubnetId(subnetId);
+        builder.setSubnetIp(subnetIp);
+        builder.setVpnName(vpnName);
+        builder.setExternalVpn(isExternalvpn);
+        builder.setElanTag(elanTag);
+
+        notificationPublishService.putNotification(builder.build());
+    }
+
+    private void checkAndPublishSubnetDeletedFromVpnNotification(Uuid subnetId, String subnetIp, String vpnName,
+                                                                 Boolean isExternalvpn, Long elanTag) throws InterruptedException {
+        SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder();
+
+        LOG.trace("publish notification called SubnetDeletedFromVpnNotification");
+
+        builder.setSubnetId(subnetId);
+        builder.setSubnetIp(subnetIp);
+        builder.setVpnName(vpnName);
+        builder.setExternalVpn(isExternalvpn);
+        builder.setElanTag(elanTag);
+
+        notificationPublishService.putNotification(builder.build());
+    }
+
+    private void checkAndPublishSubnetUpdatedInVpnNotification(Uuid subnetId, String subnetIp, String vpnName,
+                                                               Boolean isExternalvpn, Long elanTag) throws InterruptedException {
+        SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder();
+
+        LOG.trace("publish notification called SubnetUpdatedInVpnNotification");
+
+        builder.setSubnetId(subnetId);
+        builder.setSubnetIp(subnetIp);
+        builder.setVpnName(vpnName);
+        builder.setExternalVpn(isExternalvpn);
+        builder.setElanTag(elanTag);
+
+        notificationPublishService.putNotification(builder.build());
+    }
+
+    private void checkAndPublishPortAddedToSubnetNotification(String subnetIp, Uuid subnetId, Uuid portId, Long elanTag) throws
+            InterruptedException {
+        PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
+        LOG.trace("publish notification called PortAddedToSubnetNotification");
+        builder.setSubnetIp(subnetIp);
+        builder.setSubnetId(subnetId);
+        builder.setPortId(portId);
+        builder.setElanTag(elanTag);
+
+        notificationPublishService.putNotification(builder.build());
+    }
+
+    private void checkAndPublishPortRemovedFromSubnetNotification(String subnetIp, Uuid subnetId, Uuid portId, Long elanTag)
+            throws InterruptedException {
+        PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
+        LOG.trace("publish notification called PortRemovedFromSubnetNotification");
+        builder.setPortId(portId);
+        builder.setSubnetIp(subnetIp);
+        builder.setSubnetId(subnetId);
+        builder.setElanTag(elanTag);
+
+        notificationPublishService.putNotification(builder.build());
+    }
+}
index 648e5e3d4a0eacccb09cc57dbf5cdf6fdb7361b2..768f4fecea8291fe222c3f710b30e040ea2dfb09 100644 (file)
     <argument ref="elanService" />
   </bean>
 
+  <bean id="neutronvpnSubnetmapListener"
+        class="org.opendaylight.netvirt.neutronvpn.SubnetmapListener"
+        init-method="start" destroy-method="close">
+    <argument ref="dataBroker" />
+    <argument ref="notificationPublishService" />
+  </bean>
+
   <bean id="neutronSubnetGwMacResolver"
         class="org.opendaylight.netvirt.neutronvpn.NeutronSubnetGwMacResolver"
         init-method="start" destroy-method="close">