Bug 8584 - missing implementation in NM for floating IPs
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / util / Utils.java
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));
+    }
 }