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;
@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
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) {
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,
@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);
+ }
}
}
}
//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));
+ }
}