+/*
+ * Copyright (c) 2015 Intel, Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping;
import static com.google.common.base.Preconditions.checkNotNull;
+import java.util.concurrent.ExecutionException;
+
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.groupbasedpolicy.neutron.gbp.util.NeutronGbpIidFactory;
import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
-import org.opendaylight.neutron.spi.INeutronFloatingIPAware;
-import org.opendaylight.neutron.spi.INeutronPortCRUD;
-import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
-import org.opendaylight.neutron.spi.NeutronFloatingIP;
-import org.opendaylight.neutron.spi.NeutronPort;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.UniqueId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.floating.ip.association.mappings.floating.ip.ports.by.internal.ports.FloatingIpPortByInternalPortBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.floating.ip.association.mappings.internal.ports.by.floating.ip.ports.InternalPortByFloatingIpPort;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.floating.ip.association.mappings.internal.ports.by.floating.ip.ports.InternalPortByFloatingIpPortBuilder;
+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.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;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Strings;
+import com.google.common.base.Objects;
-public class NeutronFloatingIpAware implements INeutronFloatingIPAware {
+public class NeutronFloatingIpAware implements NeutronAware<Floatingip> {
- public static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingIpAware.class);
+ private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingIpAware.class);
+ public static final InstanceIdentifier<Floatingip> FLOATING_IP_WILDCARD_IID =
+ InstanceIdentifier.builder(Neutron.class).child(Floatingips.class).child(Floatingip.class).build();
private final DataBroker dataProvider;
- private final EndpointService epService;
- public NeutronFloatingIpAware(DataBroker dataProvider, EndpointService epService) {
+ public NeutronFloatingIpAware(DataBroker dataProvider) {
this.dataProvider = checkNotNull(dataProvider);
- this.epService = checkNotNull(epService);
- }
-
- @Override
- public int canCreateFloatingIP(NeutronFloatingIP floatingIP) {
- LOG.trace("canCreateFloatingIP - {}", floatingIP);
- return StatusCode.OK;
}
@Override
- public void neutronFloatingIPCreated(NeutronFloatingIP floatingIP) {
- LOG.trace("neutronFloatingIPCreated - {}", floatingIP);
+ 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
- public int canUpdateFloatingIP(NeutronFloatingIP delta, NeutronFloatingIP original) {
- LOG.trace("canUpdateFloatingIP - delta: {} original: {}", delta, original);
- // floating IP UUID is same as device ID of a port representing floating IP
- UniqueId floatingIpPortId = NeutronPortAware.getFloatingIpPortIdByDeviceId(original.getFloatingIPUUID());
- if (floatingIpPortId == null) {
- LOG.warn("Illegal state - Port representing floating ip where floating IP uuid is {} does not exist.",
- original.getFloatingIPUUID());
- return StatusCode.INTERNAL_SERVER_ERROR;
+ public void onUpdated(Floatingip oldFloatingIp, Floatingip newFloatingIp, Neutron oldNeutron, Neutron newNeutron) {
+ LOG.trace("updated floatingIP - OLD: {}\nNEW: {}", oldFloatingIp, newFloatingIp);
+ IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
+ IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
+ if (Objects.equal(oldEpIp, newEpIp)) {
+ // floating IP was not moved from one port to the other
+ return;
}
-
ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
- String oldFixedIPAddress = Strings.nullToEmpty(original.getFixedIPAddress());
- String oldPortUUID = Strings.nullToEmpty(original.getPortUUID());
- String newFixedIPAddress = Strings.nullToEmpty(delta.getFixedIPAddress());
- String newPortUUID = Strings.nullToEmpty(delta.getPortUUID());
- if (oldFixedIPAddress.equals(newFixedIPAddress) && oldPortUUID.equals(newPortUUID)) {
- // interesting fields were not changed
- return StatusCode.OK;
- }
-
- if ((!oldFixedIPAddress.isEmpty() && newFixedIPAddress.isEmpty())
- || (!oldPortUUID.isEmpty() && newPortUUID.isEmpty())) {
- DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
- NeutronGbpIidFactory.internalPortByFloatingIpPortIid(floatingIpPortId), rwTx);
- DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
- NeutronGbpIidFactory.floatingIpPortByInternalPortIid(new UniqueId(oldPortUUID)), rwTx);
- // TODO unregister EP representing floating ip port
- } else if (!newFixedIPAddress.isEmpty() && !newPortUUID.isEmpty()) {
- // workaround for https://bugs.opendaylight.org/show_bug.cgi?id=3368
- // otherwise we will create port representing floating IP in NeutronPortAware
- Integer errorCode = registerFloatingIpPort(original.getTenantUUID(), floatingIpPortId.getValue(), rwTx);
- if (errorCode != null) {
- rwTx.cancel();
- return errorCode;
- }
-
- UniqueId internalPortId = new UniqueId(newPortUUID);
- InternalPortByFloatingIpPort internalPortByFloatingIpPort = new InternalPortByFloatingIpPortBuilder().setFloatingIpPortId(
- floatingIpPortId)
- .setFloatingIpPortIpAddress(Utils.createIpAddress(original.getFloatingIPAddress()))
- .setInternalPortId(internalPortId)
- .setInternalPortIpAddress(Utils.createIpAddress(newFixedIPAddress))
- .build();
- rwTx.put(LogicalDatastoreType.OPERATIONAL, NeutronGbpIidFactory.internalPortByFloatingIpPortIid(floatingIpPortId),
- internalPortByFloatingIpPort, true);
- rwTx.put(LogicalDatastoreType.OPERATIONAL, NeutronGbpIidFactory.floatingIpPortByInternalPortIid(internalPortId),
- new FloatingIpPortByInternalPortBuilder(internalPortByFloatingIpPort).build(), true);
- }
+ Utils.syncNat(rwTx, oldFloatingIp, newFloatingIp);
+ syncNatForEndpoint(rwTx, oldFloatingIp, newFloatingIp);
boolean isSubmitToDsSuccessful = DataStoreHelper.submitToDs(rwTx);
if (!isSubmitToDsSuccessful) {
- return StatusCode.INTERNAL_SERVER_ERROR;
+ LOG.warn("Nat address {} was not added to endpoint {}", newFloatingIp.getFloatingIpAddress(), newEpIp);
}
-
- return StatusCode.OK;
}
- private Integer registerFloatingIpPort(String tenantUUID, String floatingIpPortUUID, ReadWriteTransaction rwTx) {
- INeutronPortCRUD portInterface = NeutronCRUDInterfaces.getINeutronPortCRUD(this);
- if (portInterface == null) {
- LOG.warn("Illegal state - No provider for {}", INeutronPortCRUD.class.getName());
- return StatusCode.INTERNAL_SERVER_ERROR;
+ @Deprecated
+ private void syncNatForEndpoint(ReadWriteTransaction rwTx, Floatingip oldFloatingIp, Floatingip newFloatingIp) {
+ IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
+ IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
+ IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
+ 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);
}
- NeutronPort floatingIpPort = portInterface.getPort(floatingIpPortUUID);
- // TenantId tenantId = new TenantId(Utils.normalizeUuid());
- floatingIpPort.setTenantID(tenantUUID);
- boolean isNeutronPortCreated = NeutronPortAware.addNeutronPort(floatingIpPort, rwTx, epService);
- if (!isNeutronPortCreated) {
- rwTx.cancel();
- return StatusCode.INTERNAL_SERVER_ERROR;
+ 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,
+ routerL3ContextId, L3Context.class);
+ LOG.info("Adding NAT augmentation {} for base-endpoint {}", epNatIp, aek);
+ rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.addressEndpointIid(aek)
+ .augmentation(
+ org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddress.class),
+ new org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddressBuilder()
+ .setNatAddress(epNatIp)
+ .build(), true);
+ rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.l3EndpointIid(routerL3ContextId, newEpIp)
+ .augmentation(NatAddress.class), nat, true);
}
- return null;
- }
-
- @Override
- public void neutronFloatingIPUpdated(NeutronFloatingIP floatingIP) {
- LOG.trace("neutronFloatingIPUpdated - {}", floatingIP);
}
@Override
- public int canDeleteFloatingIP(NeutronFloatingIP floatingIP) {
- LOG.trace("canDeleteFloatingIP - {}", floatingIP);
- return StatusCode.OK;
- }
-
- @Override
- public void neutronFloatingIPDeleted(NeutronFloatingIP floatingIP) {
- LOG.trace("neutronFloatingIPDeleted - {}", floatingIP);
+ 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);
+ }
}
-
}