Remove DS operation part of Yang Consumer Thread 43/76743/16
authorshaik <shaik.b@altencalsoftlabs.com>
Mon, 8 Oct 2018 11:21:25 +0000 (16:51 +0530)
committerShaik Zakir Basha <shaik.b@altencalsoftlabs.com>
Wed, 16 Jan 2019 05:18:57 +0000 (05:18 +0000)
Description:
1. Changes are done to remove move the logic of
performing DS read/write and install
installation/removal to perform in the worker
thread instead of performing the same in yang
consumer thread.

2. In the private BGPVPN association case, if the
association is created first and the router-gateway-set
is done later, it is noticed that the default NAT
flow is instaled with external VPN metadata rather then
private BGPVPN metadat. Changes are done to correct it

JIRA : NETVIRT-1392

Change-Id: Ic296b2d3c1c22350518c5008ceb58df1ef17a76c
Signed-off-by: shaik <shaik.b@altencalsoftlabs.com>
natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/NAPTEntryEvent.java
natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/NaptEventHandler.java
natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/NaptFlowRemovedEventHandler.java
natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/NatUtil.java
natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/NatVpnMapsChangeListener.java [moved from natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/RouterToVpnListener.java with 72% similarity]
natservice/impl/src/main/resources/OSGI-INF/blueprint/natservice.xml
neutronvpn/api/src/main/yang/neutronvpn.yang
neutronvpn/impl/src/main/java/org/opendaylight/netvirt/neutronvpn/NeutronvpnManager.java

index 63dde2a28411791ab55f5bd8404bf835a5b0405b..aec92363150b6ef6976f1e240a6a96cb74307c14 100644 (file)
@@ -36,6 +36,18 @@ public class NAPTEntryEvent {
         this.objectCreationTime = System.currentTimeMillis();
     }
 
+    NAPTEntryEvent(String ipAddress, int portNumber, Long routerId, Operation op, Protocol protocol) {
+        this.op = op;
+        this.ipAddress = ipAddress;
+        this.portNumber = portNumber;
+        this.routerId = routerId;
+        this.protocol = protocol;
+        this.packetReceived = null;
+        this.pktProcessed = false;
+        this.state = null;
+        this.objectCreationTime = System.currentTimeMillis();
+    }
+
     public PacketReceived getPacketReceived() {
         return packetReceived;
     }
index 13153bd5e666780548e6fabc42b15e7ff58565bc..df13e84e9d3a395a16fe2427568c890dab4d33bf 100644 (file)
@@ -100,6 +100,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternal;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -186,7 +187,7 @@ public class NaptEventHandler {
                 bgpVpnId = routerId;
                 LOG.debug("handleEvent : BGP VPN ID {}", bgpVpnId);
                 String vpnName = NatUtil.getRouterName(dataBroker, bgpVpnId);
-                String routerName = NatUtil.getRouterIdfromVpnInstance(dataBroker, vpnName);
+                String routerName = NatUtil.getRouterIdfromVpnInstance(dataBroker, vpnName, internalIpAddress);
                 if (routerName == null) {
                     NaptPacketInHandler.removeIncomingPacketMap(sourceIPPortKey);
                     LOG.error("handleEvent: Unable to find router for VpnName {}. Droping packet for SNAT ({})"
@@ -320,8 +321,7 @@ public class NaptEventHandler {
                         naptEntryEvent.isPktProcessed());
             } else {
                 LOG.debug("handleEvent : Inside delete Operation of NaptEventHandler");
-                removeNatFlows(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId, naptEntryEvent.getIpAddress(),
-                    naptEntryEvent.getPortNumber());
+                handleFlowRemoved(naptEntryEvent, routerId, sourceIPPortKey, dpnId);
                 LOG.info("handleEvent : exited for removeEvent for IP {}, port {}, routerID : {}",
                         naptEntryEvent.getIpAddress(), naptEntryEvent.getPortNumber(), routerId);
             }
@@ -643,7 +643,7 @@ public class NaptEventHandler {
         String switchFlowRef = NatUtil.getNaptFlowRef(dpnId, tableId, String.valueOf(segmentId), ip, port);
         FlowEntity snatFlowEntity = NatUtil.buildFlowEntity(dpnId, tableId, switchFlowRef);
         LOG.debug("removeNatFlows : Remove the flow in the table {} for the switch with the DPN ID {}",
-            NwConstants.INBOUND_NAPT_TABLE, dpnId);
+                tableId, dpnId);
         long startTime = System.currentTimeMillis();
         mdsalManager.removeFlow(snatFlowEntity);
         LOG.trace("removeNatFlows : Time Elapsed for removing table-{} flow from switch with DPN ID:{} "
@@ -719,4 +719,32 @@ public class NaptEventHandler {
 
         return NatConstants.INVALID_ID;
     }
+
+    public void handleFlowRemoved(NAPTEntryEvent naptEntryEvent, Long routerId, String sourceIPPortKey,
+                                  BigInteger dpnId) {
+        String internalIpv4HostAddress = naptEntryEvent.getIpAddress();
+        Integer internalPortNumber = naptEntryEvent.getPortNumber();
+        NAPTEntryEvent.Protocol protocol = naptEntryEvent.getProtocol();
+        //Get the external IP address and the port from the model
+        IpPortExternal ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId, internalIpv4HostAddress,
+                internalPortNumber.toString(), protocol);
+        if (ipPortExternal == null) {
+            LOG.error("handleFlowRemoved : IpPortExternal is null while queried from the model for routerId {}",
+                    routerId);
+            return;
+        }
+        String externalIpAddress = ipPortExternal.getIpAddress();
+        int externalPortNumber = ipPortExternal.getPortNum();
+
+        removeNatFlows(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId, externalIpAddress, externalPortNumber);
+
+        removeNatFlows(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId, internalIpv4HostAddress, internalPortNumber);
+
+        //Remove the SourceIP:Port key from the Napt packet handler map.
+        NaptPacketInHandler.removeIncomingPacketMap(sourceIPPortKey);
+
+        //Remove the mapping of internal fixed ip/port to external ip/port from the datastore.
+        SessionAddress internalSessionAddress = new SessionAddress(internalIpv4HostAddress, internalPortNumber);
+        naptManager.releaseIpExtPortMapping(routerId, internalSessionAddress, protocol);
+    }
 }
index 5a83b0c560a7d239534c2876931575d321dd0c51..ac3b64f66b64428aaf3c9ae58ae30d8c6f97a4a8 100644 (file)
@@ -11,7 +11,6 @@ import java.math.BigInteger;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.genius.mdsalutil.FlowEntity;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
@@ -24,7 +23,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.Node
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.RemovedFlowReason;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.TcpMatchFields;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.UdpMatchFields;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
@@ -32,8 +30,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternal;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,13 +60,6 @@ public class NaptFlowRemovedEventHandler implements SalFlowListener {
 
     }
 
-    private BigInteger getDpnId(String node) {
-        //openflow:1]
-        String[] temp = node.split(":");
-        return new BigInteger(temp[1]);
-
-    }
-
     @Override
     public void onFlowAdded(FlowAdded arg0) {
         // TODO Auto-generated method stub
@@ -145,63 +134,9 @@ public class NaptFlowRemovedEventHandler implements SalFlowListener {
                 LOG.error("onFlowRemoved : Null exception while retrieving routerId");
                 return;
             }
-            final String internalIpPortKey = routerId + NatConstants.COLON_SEPARATOR
-                    + internalIpv4HostAddress + NatConstants.COLON_SEPARATOR + internalPortNumber;
-            //Get the external IP address and the port from the model
-            IpPortExternal ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId,
-                internalIpv4HostAddress, internalPortNumber.toString(), protocol);
-            if (ipPortExternal == null) {
-                LOG.error("onFlowRemoved : IpPortExternal not found, BGP vpn might be "
-                    + "associated with router");
-                //router must be associated with BGP vpn ID
-                long bgpVpnId = routerId;
-                LOG.debug("onFlowRemoved : BGP VPN ID {}", bgpVpnId);
-                String vpnName = NatUtil.getRouterName(dataBroker, bgpVpnId);
-                String routerName = NatUtil.getRouterIdfromVpnInstance(dataBroker, vpnName);
-                if (routerName == null) {
-                    LOG.error("onFlowRemoved : Unable to find router for VpnName {}", vpnName);
-                    return;
-                }
-                routerId = NatUtil.getVpnId(dataBroker, routerName);
-                LOG.debug("onFlowRemoved : Router ID {}", routerId);
-                ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId,
-                    internalIpv4HostAddress, internalPortNumber.toString(), protocol);
-                if (ipPortExternal == null) {
-                    LOG.error("onFlowRemoved : IpPortExternal is null while queried from the "
-                        + "model for routerId {}", routerId);
-                    return;
-                }
-            }
-            String externalIpAddress = ipPortExternal.getIpAddress();
-            int externalPortNumber = ipPortExternal.getPortNum();
-
-            //Create an NAPT event and place it in the queue.
-            NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(externalIpAddress, externalPortNumber,
-                routerId, NAPTEntryEvent.Operation.DELETE, protocol, null, false, null);
+            NAPTEntryEvent naptEntryEvent = new NAPTEntryEvent(internalIpv4HostAddress, internalPortNumber, routerId,
+                    NAPTEntryEvent.Operation.DELETE, protocol);
             naptEventdispatcher.addFlowRemovedNaptEvent(naptEntryEvent);
-
-            //Get the DPN ID from the Node
-            InstanceIdentifier<Node> nodeRef = flowRemoved.getNode().getValue().firstIdentifierOf(Node.class);
-            String dpn = nodeRef.firstKeyOf(Node.class).getId().getValue();
-            BigInteger dpnId = getDpnId(dpn);
-            String switchFlowRef = NatUtil.getNaptFlowRef(dpnId, tableId, String.valueOf(routerId),
-                internalIpv4HostAddress, internalPortNumber);
-
-            //Inform the MDSAL manager to inform about the flow removal.
-            LOG.debug("onFlowRemoved : DPN ID {}, Metadata {}, SwitchFlowRef {}, "
-                + "internalIpv4HostAddress{}", dpnId, routerId, switchFlowRef, internalIpv4AddressAsString);
-            FlowEntity snatFlowEntity = NatUtil.buildFlowEntity(dpnId, tableId, switchFlowRef);
-            long startTime = System.currentTimeMillis();
-            mdsalManager.removeFlow(snatFlowEntity);
-            LOG.debug("onFlowRemoved : Elapsed time fo deleting table-{} flow for snat ({}) session:{}ms",
-                    tableId, internalIpPortKey, (System.currentTimeMillis() - startTime));
-            //Remove the SourceIP:Port key from the Napt packet handler map.
-            naptPacketInHandler.removeIncomingPacketMap(internalIpPortKey);
-
-            //Remove the mapping of internal fixed ip/port to external ip/port from the datastore.
-            SessionAddress internalSessionAddress = new SessionAddress(internalIpv4HostAddress, internalPortNumber);
-            naptManager.releaseIpExtPortMapping(routerId, internalSessionAddress, protocol);
-            LOG.info("onFlowRemoved : exit");
         } else {
             LOG.debug("onFlowRemoved : Received flow removed notification due to flowdelete from switch for flowref");
         }
index d9708410cb36eaa96a0588892ca177b2ac5cbed5..7a892907b553db530078a423e0753fe71e91f662 100644 (file)
@@ -36,6 +36,7 @@ import java.util.concurrent.Future;
 import java.util.stream.Collectors;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+import org.apache.commons.net.util.SubnetUtils;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -243,7 +244,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev15060
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMap;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMapKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.RouterKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
@@ -679,8 +679,9 @@ public final class NatUtil {
         return new BigInteger(getDpnFromNodeConnectorId(nodeConnectorId));
     }
 
+
     @Nullable
-    public static String getRouterIdfromVpnInstance(DataBroker broker, String vpnName) {
+    public static String getRouterIdfromVpnInstance(DataBroker broker, String vpnName, String ipAddress) {
         // returns only router, attached to IPv4 networks
         InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
             .child(VpnMap.class, new VpnMapKey(new Uuid(vpnName))).build();
@@ -694,20 +695,18 @@ public final class NatUtil {
         List<Uuid> routerIdsList = NeutronUtils.getVpnMapRouterIdsListUuid(optionalVpnMap.get().getRouterIds());
         if (routerIdsList != null && !routerIdsList.isEmpty()) {
             for (Uuid routerUuid : routerIdsList) {
-                InstanceIdentifier<Router> routerIdentifier = buildNeutronRouterIdentifier(routerUuid);
-                Optional<Router> optRouter = SingleTransactionDataBroker
-                    .syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(broker,
-                            LogicalDatastoreType.CONFIGURATION, routerIdentifier);
-                if (!optRouter.isPresent()) {
-                    continue;
-                }
-                List<Routes> routes = optRouter.get().getRoutes();
-                if (routes == null || routes.isEmpty()) {
-                    continue;
-                }
-                for (Routes r : routes) {
-                    if (r.getDestination().getIpv4Prefix() != null) {
-                        return routerUuid.getValue();
+                InstanceIdentifier<Routers> id = buildRouterIdentifier(routerUuid.getValue());
+                Optional<Routers> routerData =
+                        SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(broker,
+                                LogicalDatastoreType.CONFIGURATION, id);
+                if (routerData.isPresent()) {
+                    List<Uuid> subnetIdsList = routerData.get().getSubnetIds();
+                    for (Uuid subnetUuid : subnetIdsList) {
+                        String subnetIp = getSubnetIp(broker, subnetUuid);
+                        SubnetUtils subnet = new SubnetUtils(subnetIp);
+                        if (subnet.getInfo().isInRange(ipAddress)) {
+                            return routerUuid.getValue();
+                        }
                     }
                 }
             }
@@ -731,7 +730,7 @@ public final class NatUtil {
                 }
                 List<Uuid> routerIdsList = NeutronUtils.getVpnMapRouterIdsListUuid(vpnMap.getRouterIds());
                 if (routerIdsList.isEmpty()) {
-                    return null;
+                    continue;
                 }
                 // Skip router vpnId fetching from internet BGP-VPN
                 if (vpnMap.getNetworkIds() != null && !vpnMap.getNetworkIds().isEmpty()) {
similarity index 72%
rename from natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/RouterToVpnListener.java
rename to natservice/impl/src/main/java/org/opendaylight/netvirt/natservice/internal/NatVpnMapsChangeListener.java
index b51c238713b1092663184beed82030ba7aab67f0..994c16f37db6e1100ab0dce60b0819272d6b85e1 100644 (file)
@@ -13,11 +13,13 @@ import static org.opendaylight.netvirt.natservice.internal.NatUtil.requireNonNul
 import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.util.Collections;
+import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
@@ -27,16 +29,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev16011
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.RouterPorts;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.Ports;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.ports.InternalToExternalPortMap;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NeutronvpnListener;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpn;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpn;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMap;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class RouterToVpnListener implements NeutronvpnListener {
-    private static final Logger LOG = LoggerFactory.getLogger(RouterToVpnListener.class);
+public class NatVpnMapsChangeListener extends AsyncDataTreeChangeListenerBase<VpnMap, NatVpnMapsChangeListener> {
+    private static final Logger LOG = LoggerFactory.getLogger(NatVpnMapsChangeListener.class);
     private final DataBroker dataBroker;
     private final ManagedNewTransactionRunner txRunner;
     private final FloatingIPListener floatingIpListener;
@@ -44,10 +45,11 @@ public class RouterToVpnListener implements NeutronvpnListener {
     private final ExternalRoutersListener externalRoutersListener;
 
     @Inject
-    public RouterToVpnListener(final DataBroker dataBroker,
+    public NatVpnMapsChangeListener(final DataBroker dataBroker,
                                final FloatingIPListener floatingIpListener,
                                final OdlInterfaceRpcService interfaceManager,
                                final ExternalRoutersListener externalRoutersListener) {
+        super(VpnMap.class, NatVpnMapsChangeListener.class);
         this.dataBroker = dataBroker;
         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
         this.floatingIpListener = floatingIpListener;
@@ -55,13 +57,75 @@ public class RouterToVpnListener implements NeutronvpnListener {
         this.externalRoutersListener = externalRoutersListener;
     }
 
-    /**
-     * router association to vpn.
-     */
+    public void init() {
+        LOG.info("{} init", getClass().getSimpleName());
+        registerListener(dataBroker);
+    }
+
+    @Override
+    protected InstanceIdentifier<VpnMap> getWildCardPath() {
+        return InstanceIdentifier.create(VpnMaps.class).child(VpnMap.class);
+    }
+
+    private void registerListener(final DataBroker db) {
+        registerListener(LogicalDatastoreType.CONFIGURATION, db);
+    }
+
+    @Override
+    protected void add(InstanceIdentifier<VpnMap> identifier, VpnMap vpnMap) {
+        Uuid vpnUuid = vpnMap.getVpnId();
+        String vpnName = vpnUuid.getValue();
+        vpnMap.getRouterIds().stream()
+                .filter(router -> !(Objects.equals(router.getRouterId(), vpnUuid)))
+                .forEach(router -> {
+                    String routerName = router.getRouterId().getValue();
+                    LOG.info("REMOVE: Router {} is disassociated from Vpn {}", routerName, vpnName);
+                    onRouterAssociatedToVpn(vpnName, routerName);
+                });
+    }
+
+    @Override
+    protected void remove(InstanceIdentifier<VpnMap> identifier, VpnMap vpnMap) {
+        Uuid vpnUuid = vpnMap.getVpnId();
+        String vpnName = vpnUuid.getValue();
+        vpnMap.getRouterIds().stream()
+                .filter(router -> !(Objects.equals(router.getRouterId(), vpnUuid)))
+                .forEach(router -> {
+                    String routerName = router.getRouterId().getValue();
+                    LOG.info("REMOVE: Router {} is disassociated from Vpn {}", routerName, vpnName);
+                    onRouterDisassociatedFromVpn(vpnName, routerName);
+                });
+    }
+
     @Override
-    public void onRouterAssociatedToVpn(RouterAssociatedToVpn notification) {
-        String routerName = notification.getRouterId().getValue();
-        String vpnName = notification.getVpnId().getValue();
+    protected void update(InstanceIdentifier<VpnMap> identifier, VpnMap original, VpnMap updated) {
+        Uuid vpnUuid = updated.getVpnId();
+        String vpnName = vpnUuid.getValue();
+
+        updated.getRouterIds().stream()
+                .filter(router -> ! original.getRouterIds().contains(router))
+                .filter(router -> !(Objects.equals(router.getRouterId(), updated.getVpnId())))
+                .forEach(router -> {
+                    String routerName = router.getRouterId().getValue();
+                    onRouterAssociatedToVpn(vpnName, routerName);
+                });
+
+        original.getRouterIds().stream()
+                .filter(router -> ! updated.getRouterIds().contains(router))
+                .filter(router -> !(Objects.equals(router.getRouterId(), original.getVpnId())))
+                .forEach(router -> {
+                    String routerName = router.getRouterId().getValue();
+                    onRouterDisassociatedFromVpn(vpnName, routerName);
+                });
+    }
+
+    @Override
+    protected NatVpnMapsChangeListener getDataTreeChangeListener() {
+        return this;
+    }
+
+    public void onRouterAssociatedToVpn(String vpnName, String routerName) {
+
         //check router is associated to external network
         String extNetwork = NatUtil.getAssociatedExternalNetwork(dataBroker, routerName);
         if (extNetwork != null) {
@@ -71,11 +135,11 @@ public class RouterToVpnListener implements NeutronvpnListener {
                 Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
                 if (extNetworkUuid == null) {
                     LOG.error("onRouterAssociatedToVpn : Unable to retrieve external network Uuid for router {}",
-                        routerName);
+                            routerName);
                     return;
                 }
                 ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerName,
-                    extNetworkUuid);
+                        extNetworkUuid);
                 if (extNwProvType == null) {
                     LOG.error("onRouterAssociatedToVpn : External Network Provider Type missing");
                     return;
@@ -83,7 +147,7 @@ public class RouterToVpnListener implements NeutronvpnListener {
                 long routerId = NatUtil.getVpnId(dataBroker, routerName);
                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
                     tx -> externalRoutersListener.changeLocalVpnIdToBgpVpnId(routerName, routerId, vpnName, tx,
-                            extNwProvType)).get();
+                                extNwProvType)).get();
             } catch (InterruptedException | ExecutionException e) {
                 LOG.error("Error changling local VPN identifier to BGP VPN identifier", e);
             }
@@ -96,16 +160,15 @@ public class RouterToVpnListener implements NeutronvpnListener {
     /**
      * router disassociation from vpn.
      */
-    @Override
-    public void onRouterDisassociatedFromVpn(RouterDisassociatedFromVpn notification) {
-        String routerName = notification.getRouterId().getValue();
-        String vpnName = notification.getVpnId().getValue();
+
+    public void onRouterDisassociatedFromVpn(String vpnName, String routerName) {
+
         //check router is associated to external network
         String extNetwork = NatUtil.getAssociatedExternalNetwork(dataBroker, routerName);
         if (extNetwork != null) {
             try {
                 LOG.debug("onRouterDisassociatedFromVpn : Router {} is associated with ext nw {}", routerName,
-                    extNetwork);
+                        extNetwork);
                 handleDNATConfigurationForRouterDisassociation(routerName, vpnName, extNetwork);
                 Uuid extNetworkUuid = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
                 if (extNetworkUuid == null) {
@@ -122,7 +185,7 @@ public class RouterToVpnListener implements NeutronvpnListener {
                 long routerId = NatUtil.getVpnId(dataBroker, routerName);
                 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
                     tx -> externalRoutersListener.changeBgpVpnIdToLocalVpnId(routerName, routerId, vpnName, tx,
-                            extNwProvType)).get();
+                                extNwProvType)).get();
             } catch (InterruptedException | ExecutionException e) {
                 LOG.error("Error changing BGP VPN identifier to local VPN identifier", e);
             }
@@ -136,7 +199,7 @@ public class RouterToVpnListener implements NeutronvpnListener {
             throws ExecutionException, InterruptedException {
         InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerName);
         Optional<RouterPorts> optRouterPorts =
-            MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
+                MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
         if (!optRouterPorts.isPresent()) {
             LOG.debug("handleDNATConfigurationForRouterAssociation : Could not read Router Ports data "
                     + "object with id: {} to handle associate vpn {}", routerName, vpnName);
@@ -169,7 +232,7 @@ public class RouterToVpnListener implements NeutronvpnListener {
             throws ExecutionException, InterruptedException {
         InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerName);
         Optional<RouterPorts> optRouterPorts =
-            MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
+                MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
         if (!optRouterPorts.isPresent()) {
             LOG.error("handleDNATConfigurationForRouterDisassociation : Could not read Router Ports "
                     + "data object with id: {} to handle disassociate vpn {}", routerName, vpnName);
index 7e279da90cefd206c834b387573911834ccb001c..64bf40c4a46390aa93e3bc2edc7460027810423d 100644 (file)
@@ -59,7 +59,6 @@
 
   <odl:notification-listener ref="naptPacketInHandler" />
   <odl:notification-listener ref="naptFlowRemovedEventHandler" />
-  <odl:notification-listener ref="routerToVpnListener" />
 
   <service ref="vpnFloatingIpHandler"
            interface="org.opendaylight.netvirt.natservice.internal.FloatingIPHandler" />
index 76c55e0d5ef39309d6602ac690182c156b5df08d..a23213005b6d1002b47152afc7a3036e88ac5193 100644 (file)
@@ -460,25 +460,4 @@ module neutronvpn {
             }
         }
     }
-
-    notification router-associated-to-vpn {
-            description "router association to vpn";
-            leaf router-id{
-                type yang:uuid;
-            }
-            leaf vpn-id{
-                type yang:uuid;
-            }
-    }
-
-    notification router-disassociated-from-vpn {
-            description "router disassociation from vpn";
-            leaf router-id{
-                type yang:uuid;
-            }
-            leaf vpn-id{
-                type yang:uuid;
-            }
-    }
-
 }
index c0098e2fff6f33c443c2526ab60100b579631fa4..dd956c40bc16c5873666e91e381a03d806d89221 100644 (file)
@@ -133,10 +133,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev15060
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNOutputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NetworkAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NeutronvpnService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpn;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpnBuilder;
-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.Subnetmaps;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
@@ -2315,15 +2311,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         for (Subnetmap sn : subMapList) {
             updateVpnForSubnet(routerId, vpnId, sn.getId(), true);
         }
-
-        try {
-            checkAndPublishRouterAssociatedtoVpnNotification(routerId, vpnId);
-            LOG.debug("associateRouterToVpn: notification upon association of router {} to VPN {} published",
-                    routerId.getValue(), vpnId.getValue());
-        } catch (Exception e) {
-            LOG.error("associateRouterToVpn: publishing of notification upon association of router {} to VPN {}"
-                    + " failed : ", routerId.getValue(), vpnId.getValue(), e);
-        }
     }
 
     protected void associateRouterToInternalVpn(Uuid vpnId, Uuid routerId) {
@@ -2363,14 +2350,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
             neutronvpnUtils.updateVpnInstanceWithIpFamily(vpnId.getValue(), ipVersion,
                     false);
         }
-        try {
-            checkAndPublishRouterDisassociatedFromVpnNotification(routerId, vpnId);
-            LOG.debug("dissociateRouterFromVpn: notification upon disassociation of router {} from VPN {} published",
-                    routerId.getValue(), vpnId.getValue());
-        } catch (Exception e) {
-            LOG.error("dissociateRouterFromVpn: publishing of notification upon disassociation of router {}"
-                    + " from VPN {} failed : ", routerId.getValue(), vpnId.getValue(), e);
-        }
     }
 
     /**
@@ -3247,22 +3226,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         return help.toString();
     }
 
-    private void checkAndPublishRouterAssociatedtoVpnNotification(Uuid routerId, Uuid vpnId) throws
-            InterruptedException {
-        RouterAssociatedToVpn routerAssociatedToVpn = new RouterAssociatedToVpnBuilder().setRouterId(routerId)
-                .setVpnId(vpnId).build();
-        LOG.info("publishing notification upon association of router to VPN");
-        notificationPublishService.putNotification(routerAssociatedToVpn);
-    }
-
-    private void checkAndPublishRouterDisassociatedFromVpnNotification(Uuid routerId, Uuid vpnId) throws
-            InterruptedException {
-        RouterDisassociatedFromVpn routerDisassociatedFromVpn =
-            new RouterDisassociatedFromVpnBuilder().setRouterId(routerId).setVpnId(vpnId).build();
-        LOG.info("publishing notification upon disassociation of router from VPN");
-        notificationPublishService.putNotification(routerDisassociatedFromVpn);
-    }
-
     protected void dissociatefixedIPFromFloatingIP(String fixedNeutronPortName) {
         floatingIpMapListener.dissociatefixedIPFromFloatingIP(fixedNeutronPortName);
     }