Bug 8584 - missing implementation in NM for floating IPs 73/58173/2
authorTomas Cechvala <tcechval@cisco.com>
Wed, 31 May 2017 13:21:04 +0000 (15:21 +0200)
committerTomas Cechvala <tcechval@cisco.com>
Mon, 5 Jun 2017 07:58:12 +0000 (07:58 +0000)
Updates already implemented.
Added processing logic for create and delete events.

Change-Id: I1340cef130b01a1797cb18e203e775dcbaec61e5
Signed-off-by: Tomas Cechvala <tcechval@cisco.com>
neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronFloatingIpAware.java
neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/util/Utils.java

index 95ff9197448b1693ef4d63f92c009f9b127658e5..cc44ecc6b4279a7748ad535e7fcda0428fb4a1c9 100644 (file)
@@ -27,6 +27,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -48,6 +49,13 @@ public class NeutronFloatingIpAware implements NeutronAware<Floatingip> {
     @Override
     public void onCreated(Floatingip floatingIP, Neutron neutron) {
         LOG.trace("created floatingIp - {}", floatingIP);
+        // TODO implement onCreate properly and replace tmp workaround
+        if (floatingIP.getFixedIpAddress() != null && floatingIP.getPortId() != null
+                && floatingIP.getRouterId() != null) {
+            Floatingip unassociatedFloatingIp =
+                    new FloatingipBuilder(floatingIP).setFixedIpAddress(null).setPortId(null).setRouterId(null).build();
+            onUpdated(unassociatedFloatingIp, floatingIP, neutron, neutron);
+        }
     }
 
     @Override
@@ -60,11 +68,7 @@ public class NeutronFloatingIpAware implements NeutronAware<Floatingip> {
             return;
         }
         ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
-        try {
-            Utils.syncNat(rwTx, oldFloatingIp, newFloatingIp);
-        } catch (InterruptedException | ExecutionException e) {
-            e.printStackTrace();
-        }
+        Utils.syncNat(rwTx, oldFloatingIp, newFloatingIp);
         syncNatForEndpoint(rwTx, oldFloatingIp, newFloatingIp);
         boolean isSubmitToDsSuccessful = DataStoreHelper.submitToDs(rwTx);
         if (!isSubmitToDsSuccessful) {
@@ -77,14 +81,13 @@ public class NeutronFloatingIpAware implements NeutronAware<Floatingip> {
         IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
         IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
         IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
-        L3ContextId routerL3ContextId = new L3ContextId(newFloatingIp.getRouterId().getValue());
-
-        if (oldEpIp != null) {
+        if (oldEpIp != null && oldFloatingIp.getRouterId() != null) {
+            L3ContextId routerL3ContextId = new L3ContextId(oldFloatingIp.getRouterId().getValue());
             DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
                 IidFactory.l3EndpointIid(routerL3ContextId, oldEpIp).augmentation(NatAddress.class), rwTx);
         }
-
-        if (epNatIp != null && newEpIp != null) {
+        if (epNatIp != null && newEpIp != null && newFloatingIp.getRouterId() != null) {
+            L3ContextId routerL3ContextId = new L3ContextId(newFloatingIp.getRouterId().getValue());
             NatAddress nat = new NatAddressBuilder().setNatAddress(epNatIp).build();
             AddressEndpointKey aek =
                 new AddressEndpointKey(newEpIp.getIpv4Address().getValue() + "/32", IpPrefixType.class,
@@ -104,5 +107,12 @@ public class NeutronFloatingIpAware implements NeutronAware<Floatingip> {
     @Override
     public void onDeleted(Floatingip floatingIP, Neutron oldNeutron, Neutron newNeutron) {
         LOG.trace("deleted floatingIP - {}", floatingIP);
+        ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
+        Utils.removeNat(rwTx, floatingIP);
+        try {
+            rwTx.submit().get();
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Failed to remove floating IP {}. {}", floatingIP, e);
+        }
     }
 }
index dcc75a18fe4b4d9aa5facfba29335abf61739699..8ed8e580ce6dd44dacb99e54f592b27469ab7007 100644 (file)
@@ -102,35 +102,54 @@ public class Utils {
     }
 
     //TODO move to FloatingIpAware when deprecated API is removed
-    public static void syncNat(ReadWriteTransaction rwTx, Floatingip oldFloatingIp, Floatingip newFloatingIp)
-            throws InterruptedException, ExecutionException {
+    public static void syncNat(ReadWriteTransaction rwTx, Floatingip oldFloatingIp, Floatingip newFloatingIp) {
         IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
         IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
         IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
         if (epNatIp != null && newEpIp != null) {
-            InstanceIdentifier<BaseEndpointByPort> baseEpByPortId = NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(
-                    newFloatingIp.getPortId().getValue()));
-            Optional<BaseEndpointByPort> optional = rwTx.read(LogicalDatastoreType.OPERATIONAL, baseEpByPortId).get();
+            InstanceIdentifier<BaseEndpointByPort> baseEpByPortId =
+                    NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(newFloatingIp.getPortId().getValue()));
+            Optional<BaseEndpointByPort> optional =
+                    DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, baseEpByPortId, rwTx);
             if (!optional.isPresent()) {
                 return;
             }
             NatAddress nat = new NatAddressBuilder().setNatAddress(epNatIp).build();
-            AddressEndpointKey addrEpKey = new AddressEndpointKey(optional.get().getAddress(), optional.get()
-                .getAddressType(), optional.get().getContextId(), optional.get().getContextType());
+            AddressEndpointKey addrEpKey = new AddressEndpointKey(optional.get().getAddress(),
+                    optional.get().getAddressType(), optional.get().getContextId(), optional.get().getContextType());
             rwTx.put(LogicalDatastoreType.OPERATIONAL,
                     IidFactory.addressEndpointIid(addrEpKey).augmentation(NatAddress.class), nat, true);
         }
         if (oldEpIp != null) {
-            InstanceIdentifier<BaseEndpointByPort> baseEpByPortId = NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(
-                    oldFloatingIp.getPortId().getValue()));
-            Optional<BaseEndpointByPort> optional = rwTx.read(LogicalDatastoreType.OPERATIONAL, baseEpByPortId).get();
+            InstanceIdentifier<BaseEndpointByPort> baseEpByPortId =
+                    NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(oldFloatingIp.getPortId().getValue()));
+            Optional<BaseEndpointByPort> optional =
+                    DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, baseEpByPortId, rwTx);
             if (!optional.isPresent()) {
                 return;
             }
-            AddressEndpointKey addrEpKey = new AddressEndpointKey(optional.get().getAddress(), optional.get()
-                .getAddressType(), optional.get().getContextId(), optional.get().getContextType());
-            DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL, IidFactory.addressEndpointIid(addrEpKey)
-                .augmentation(NatAddress.class), rwTx);
+            AddressEndpointKey addrEpKey = new AddressEndpointKey(optional.get().getAddress(),
+                    optional.get().getAddressType(), optional.get().getContextId(), optional.get().getContextType());
+            DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
+                    IidFactory.addressEndpointIid(addrEpKey).augmentation(NatAddress.class), rwTx);
         }
     }
+
+    public static void removeNat(ReadWriteTransaction rwTx, Floatingip removedFloatingIp) {
+        if (removedFloatingIp.getFixedIpAddress() == null) {
+            // NAT augmentation should have been already removed
+            return;
+        }
+        InstanceIdentifier<BaseEndpointByPort> baseEpByPortId =
+                NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(removedFloatingIp.getPortId().getValue()));
+        Optional<BaseEndpointByPort> optional =
+                DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, baseEpByPortId, rwTx);
+        if (!optional.isPresent()) {
+            return;
+        }
+        AddressEndpointKey addrEpKey = new AddressEndpointKey(optional.get().getAddress(),
+                optional.get().getAddressType(), optional.get().getContextId(), optional.get().getContextType());
+        rwTx.delete(LogicalDatastoreType.OPERATIONAL,
+                IidFactory.addressEndpointIid(addrEpKey).augmentation(NatAddress.class));
+    }
 }