Implementing DHCP proxy command for VPP
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / NeutronFloatingIpAware.java
index 95ff9197448b1693ef4d63f92c009f9b127658e5..b63aad634f5893908d76f9499cc526d29e670b9a 100644 (file)
@@ -21,12 +21,13 @@ import org.opendaylight.groupbasedpolicy.util.IidFactory;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.IpPrefixType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.IpPrefixType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.L3Context;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
 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);
+        }
     }
 }