MRI version bumpup for Aluminium
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronFloatingToFixedIpMappingChangeListener.java
index 79767de0b5050a1270e083dae927988ffbc7cdd7..8741e629b2b5145a81bd1acfe2aac693b26834c1 100644 (file)
@@ -9,23 +9,26 @@ package org.opendaylight.netvirt.neutronvpn;
 
 import static org.opendaylight.netvirt.neutronvpn.NeutronvpnUtils.buildfloatingIpIdToPortMappingIdentifier;
 
-import com.google.common.base.Optional;
 import edu.umd.cs.findbugs.annotations.CheckReturnValue;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
-import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
 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.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
 import org.opendaylight.infrautils.utils.concurrent.NamedLocks;
 import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock.AcquireResult;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
 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.natservice.rev160111.FloatingIpInfo;
@@ -48,8 +51,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @Singleton
-public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTreeChangeListenerBase<Floatingip,
-        NeutronFloatingToFixedIpMappingChangeListener> {
+public class NeutronFloatingToFixedIpMappingChangeListener extends AbstractAsyncDataTreeChangeListener<Floatingip> {
     private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingToFixedIpMappingChangeListener.class);
     private static final long LOCK_WAIT_TIME = 10L;
 
@@ -58,29 +60,25 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
 
     @Inject
     public NeutronFloatingToFixedIpMappingChangeListener(final DataBroker dataBroker) {
-        super(Floatingip.class, NeutronFloatingToFixedIpMappingChangeListener.class);
+        super(dataBroker, LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Neutron.class)
+                        .child(Floatingips.class).child(Floatingip.class),
+                Executors.newSingleThreadExecutor("NeutronFloatingToFixedIpMappingChangeListener", LOG));
         this.dataBroker = dataBroker;
     }
 
-    @Override
-    @PostConstruct
     public void init() {
         LOG.info("{} init", getClass().getSimpleName());
-        registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
     }
 
     @Override
-    protected InstanceIdentifier<Floatingip> getWildCardPath() {
-        return InstanceIdentifier.create(Neutron.class).child(Floatingips.class).child(Floatingip.class);
+    @PreDestroy
+    public void close() {
+        super.close();
+        Executors.shutdownAndAwaitTermination(getExecutorService());
     }
 
     @Override
-    protected NeutronFloatingToFixedIpMappingChangeListener getDataTreeChangeListener() {
-        return NeutronFloatingToFixedIpMappingChangeListener.this;
-    }
-
-    @Override
-    protected void add(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
+    public void add(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
         LOG.trace("Neutron Floating IP created: key: {}, value={}", identifier, input);
         IpAddress fixedIp = input.getFixedIpAddress();
         String floatingIp = input.getFloatingIpAddress().getIpv4Address().getValue();
@@ -91,7 +89,7 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
     }
 
     @Override
-    protected void remove(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
+    public void remove(InstanceIdentifier<Floatingip> identifier, Floatingip input) {
         LOG.trace("Neutron Floating IP deleted : key: {}, value={}", identifier, input);
         IpAddress fixedIp = input.getFixedIpAddress();
         if (fixedIp != null) {
@@ -108,7 +106,7 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
 
     // populate the floating to fixed ip map upon association/dissociation from fixed ip
     @Override
-    protected void update(InstanceIdentifier<Floatingip> identifier, Floatingip original, Floatingip update) {
+    public void update(InstanceIdentifier<Floatingip> identifier, Floatingip original, Floatingip update) {
         LOG.trace("Handling FloatingIptoFixedIp mapping : key: {}, original value={}, update value={}", identifier,
                 original, update);
         IpAddress oldFixedIp = original.getFixedIpAddress();
@@ -151,7 +149,7 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
             }
             if (fixedNeutronPortName != null) {
                 List<Ports> portsList = routerPortsBuilder.getPorts() != null
-                        ? new ArrayList<>(routerPortsBuilder.getPorts()) : new ArrayList<>();
+                        ? new ArrayList<Ports>(routerPortsBuilder.getPorts().values()) : new ArrayList<Ports>();
                 PortsBuilder fixedNeutronPortBuilder = null;
                 for (Ports neutronPort : portsList) {
                     if (neutronPort.getPortName().equals(fixedNeutronPortName)) {
@@ -164,8 +162,9 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
                             .setPortName(fixedNeutronPortName);
                 }
                 if (fixedIpAddress != null) {
-                    List<InternalToExternalPortMap> intExtPortMapList = fixedNeutronPortBuilder
-                            .getInternalToExternalPortMap();
+                    List<InternalToExternalPortMap> intExtPortMapList
+                            = new ArrayList<InternalToExternalPortMap>(fixedNeutronPortBuilder
+                            .getInternalToExternalPortMap().values());
                     if (intExtPortMapList == null) {
                         intExtPortMapList = new ArrayList<>();
                     }
@@ -191,7 +190,7 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
                     routerPortsBuilder.build());
                 LOG.debug("FloatingIpInfo DS updated for floating IP {} ", floatingIpAddress);
             }
-        } catch (ReadFailedException | RuntimeException e) {
+        } catch (RuntimeException | ExecutionException | InterruptedException e) {
             LOG.error("addToFloatingIpInfo failed for floating IP: {} ", floatingIpAddress, e);
         }
     }
@@ -207,19 +206,20 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
                             routerPortsIdentifierBuilder.build());
             if (optionalRouterPorts.isPresent()) {
                 RouterPorts routerPorts = optionalRouterPorts.get();
-                List<Ports> portsList = routerPorts.nonnullPorts();
-                List<InternalToExternalPortMap> intExtPortMap = new ArrayList<>();
-                for (Ports ports : portsList) {
+                Map<PortsKey, Ports> keyPortsMap = routerPorts.nonnullPorts();
+                Map<InternalToExternalPortMapKey, InternalToExternalPortMap> keyInternalToExternalPortMapMap
+                        = new HashMap<InternalToExternalPortMapKey, InternalToExternalPortMap>();
+                for (Ports ports : keyPortsMap.values()) {
                     if (Objects.equals(ports.getPortName(), fixedNeutronPortName)) {
-                        intExtPortMap = ports.nonnullInternalToExternalPortMap();
+                        keyInternalToExternalPortMapMap = ports.nonnullInternalToExternalPortMap();
                         break;
                     }
                 }
-                if (intExtPortMap.size() == 1) {
-                    removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder, portsList,
-                            fixedNeutronPortName);
+                if (keyInternalToExternalPortMapMap.size() == 1) {
+                    removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder,
+                            new ArrayList<Ports>(keyPortsMap.values()), fixedNeutronPortName);
                 } else {
-                    for (InternalToExternalPortMap intToExtMap : intExtPortMap) {
+                    for (InternalToExternalPortMap intToExtMap : keyInternalToExternalPortMapMap.values()) {
                         if (Objects.equals(intToExtMap.getInternalIp(), fixedIpAddress)) {
                             InstanceIdentifier<InternalToExternalPortMap> intExtPortMapIdentifier =
                                     routerPortsIdentifierBuilder.child(Ports
@@ -232,13 +232,14 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
                                 }
 
                                 // remove particular internal-to-external-port-map
-                                LOG.debug("removing particular internal-to-external-port-map {}", intExtPortMap);
+                                LOG.debug("removing particular internal-to-external-port-map {}",
+                                        keyInternalToExternalPortMapMap);
                                 try {
                                     MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION,
                                         intExtPortMapIdentifier);
                                 } catch (Exception e) {
-                                    LOG.error("Failure in deletion of internal-to-external-port-map {}", intExtPortMap,
-                                        e);
+                                    LOG.error("Failure in deletion of internal-to-external-port-map {}",
+                                            keyInternalToExternalPortMapMap, e);
                                 }
                             }
                         }
@@ -248,7 +249,7 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
             } else {
                 LOG.warn("routerPorts for router {} - fixedIp {} not found", routerName, fixedIpAddress);
             }
-        } catch (RuntimeException | ReadFailedException e) {
+        } catch (RuntimeException | ExecutionException | InterruptedException e) {
             LOG.error("Failed to delete internal-to-external-port-map from FloatingIpInfo DS for fixed Ip {}",
                     fixedIpAddress, e);
         }
@@ -262,19 +263,19 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
                     SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.CONFIGURATION,
                             floatingIpInfoIdentifierBuilder.build());
             if (optionalFloatingIPInfo.isPresent()) {
-                List<RouterPorts> routerPortsList = optionalFloatingIPInfo.get().getRouterPorts();
-                if (routerPortsList != null && !routerPortsList.isEmpty()) {
-                    for (RouterPorts routerPorts : routerPortsList) {
-                        List<Ports> portsList = routerPorts.getPorts();
-                        if (portsList != null && !portsList.isEmpty()) {
-                            for (Ports ports : portsList) {
+                Map<RouterPortsKey, RouterPorts> keyRouterPortsMap = optionalFloatingIPInfo.get().getRouterPorts();
+                if (keyRouterPortsMap != null && !keyRouterPortsMap.isEmpty()) {
+                    for (RouterPorts routerPorts : keyRouterPortsMap.values()) {
+                        Map<PortsKey, Ports> keyPortsMap = routerPorts.getPorts();
+                        if (keyPortsMap != null && !keyPortsMap.isEmpty()) {
+                            for (Ports ports : keyPortsMap.values()) {
                                 if (Objects.equals(ports.getPortName(), fixedNeutronPortName)) {
                                     String routerName = routerPorts.getRouterId();
                                     InstanceIdentifier.InstanceIdentifierBuilder<RouterPorts>
                                         routerPortsIdentifierBuilder = floatingIpInfoIdentifierBuilder
                                         .child(RouterPorts.class, new RouterPortsKey(routerName));
-                                    removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder, portsList,
-                                            fixedNeutronPortName);
+                                    removeRouterPortsOrPortsNode(routerName, routerPortsIdentifierBuilder,
+                                            new ArrayList<Ports>(keyPortsMap.values()), fixedNeutronPortName);
                                     LOG.debug("Deletion from FloatingIpInfo DS successful for fixedIP neutron port {} ",
                                             fixedNeutronPortName);
                                     break;
@@ -289,7 +290,7 @@ public class NeutronFloatingToFixedIpMappingChangeListener extends AsyncDataTree
                 LOG.debug("FloatingIPInfo DS empty. Hence, no router present containing fixed to floating IP "
                     + "association(s)");
             }
-        } catch (ReadFailedException e) {
+        } catch (ExecutionException | InterruptedException e) {
             LOG.error("Failed to dissociate fixedIP from FloatingIpInfo DS for neutron port {}",
                     fixedNeutronPortName, e);
         }