From: Tomas Cechvala Date: Wed, 31 May 2017 13:21:04 +0000 (+0200) Subject: Bug 8584 - missing implementation in NM for floating IPs X-Git-Tag: release/nitrogen~38 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=3c133092a8c8f29f4eaeaa3f55e26fd80e911080;p=groupbasedpolicy.git Bug 8584 - missing implementation in NM for floating IPs Updates already implemented. Added processing logic for create and delete events. Change-Id: I1340cef130b01a1797cb18e203e775dcbaec61e5 Signed-off-by: Tomas Cechvala --- diff --git a/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronFloatingIpAware.java b/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronFloatingIpAware.java index 95ff91974..cc44ecc6b 100644 --- a/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronFloatingIpAware.java +++ b/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronFloatingIpAware.java @@ -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 { @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 { 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 { 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 { @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); + } } } diff --git a/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/util/Utils.java b/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/util/Utils.java index dcc75a18f..8ed8e580c 100644 --- a/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/util/Utils.java +++ b/neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/util/Utils.java @@ -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 baseEpByPortId = NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId( - newFloatingIp.getPortId().getValue())); - Optional optional = rwTx.read(LogicalDatastoreType.OPERATIONAL, baseEpByPortId).get(); + InstanceIdentifier baseEpByPortId = + NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(newFloatingIp.getPortId().getValue())); + Optional 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 baseEpByPortId = NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId( - oldFloatingIp.getPortId().getValue())); - Optional optional = rwTx.read(LogicalDatastoreType.OPERATIONAL, baseEpByPortId).get(); + InstanceIdentifier baseEpByPortId = + NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(oldFloatingIp.getPortId().getValue())); + Optional 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 baseEpByPortId = + NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(removedFloatingIp.getPortId().getValue())); + Optional 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)); + } }