Neutron-mapper uses only DTOs from neutron.yang
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / NeutronFloatingIpAware.java
index 987ac79664d8f8b590d6339d6cddcf4483dd022a..5288af85b1df289b22f09461cc4cd8fe35d8a13d 100644 (file)
@@ -13,23 +13,26 @@ import static com.google.common.base.Preconditions.checkNotNull;
 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.mapper.util.Utils;
 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
 import org.opendaylight.groupbasedpolicy.util.IidFactory;
-import org.opendaylight.neutron.spi.NeutronFloatingIP;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
 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.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 MappingProcessor<Floatingip, NeutronFloatingIP> {
+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;
 
     public NeutronFloatingIpAware(DataBroker dataProvider) {
@@ -37,90 +40,43 @@ public class NeutronFloatingIpAware implements MappingProcessor<Floatingip, Neut
     }
 
     @Override
-    public NeutronFloatingIP convertToNeutron(Floatingip fip) {
-        NeutronFloatingIP result = new NeutronFloatingIP();
-        result.setID(fip.getUuid().getValue());
-        if (fip.getFloatingNetworkId() != null) {
-            result.setFloatingNetworkUUID(fip.getFloatingNetworkId().getValue());
-        }
-        if (fip.getPortId() != null) {
-            result.setPortUUID(fip.getPortId().getValue());
-        }
-        if (fip.getFixedIpAddress() != null ) {
-            result.setFixedIPAddress(String.valueOf(fip.getFixedIpAddress().getValue()));
-        }
-        if (fip.getFloatingIpAddress() != null) {
-            result.setFloatingIPAddress(String.valueOf(fip.getFloatingIpAddress().getValue()));
-        }
-        if (fip.getTenantId() != null) {
-            result.setTenantID(fip.getTenantId());
-        }
-        if (fip.getRouterId() != null) {
-            result.setRouterUUID(fip.getRouterId().getValue());
-        }
-        result.setStatus(fip.getStatus());
-        return result;
-    }
-
-    @Override
-    public int canCreate(NeutronFloatingIP floatingIP) {
-        LOG.trace("canCreate floatingIP - {}", floatingIP);
-        return StatusCode.OK;
-    }
-
-    @Override
-    public void created(NeutronFloatingIP floatingIP) {
+    public void onCreated(Floatingip floatingIP, Neutron neutron) {
         LOG.trace("created floatinIp - {}", floatingIP);
     }
 
     @Override
-    public int canUpdate(NeutronFloatingIP delta, NeutronFloatingIP original) {
-        LOG.trace("canUpdate floatingIP - delta: {} original: {}", delta, original);
-        String oldFixedIPAddress = Strings.nullToEmpty(original.getFixedIPAddress());
-        String newFixedIPAddress = Strings.nullToEmpty(delta.getFixedIPAddress());
-        if (oldFixedIPAddress.equals(newFixedIPAddress)) {
-            // interesting fields were not changed
-            return StatusCode.OK;
+    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();
-        L3ContextId routerL3Context = new L3ContextId(delta.getRouterUUID());
-        String newFloatingIp = Strings.nullToEmpty(delta.getFloatingIPAddress());
-        if (!newFixedIPAddress.isEmpty() && !newFloatingIp.isEmpty()) {
-            IpAddress epIp = Utils.createIpAddress(newFixedIPAddress);
-            IpAddress epNatIp = Utils.createIpAddress(newFloatingIp);
+        IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
+        L3ContextId routerL3ContextId = new L3ContextId(newFloatingIp.getRouterId().getValue());
+        if (epNatIp != null && newEpIp != null) {
             NatAddress nat = new NatAddressBuilder().setNatAddress(epNatIp).build();
             rwTx.put(LogicalDatastoreType.OPERATIONAL,
-                    IidFactory.l3EndpointIid(routerL3Context, epIp).augmentation(NatAddress.class), nat, true);
+                    IidFactory.l3EndpointIid(routerL3ContextId, newEpIp).augmentation(NatAddress.class),
+                    nat, true);
         }
-        if (!oldFixedIPAddress.isEmpty()) {
+        if (oldEpIp != null) {
             DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
-                    IidFactory.l3EndpointIid(routerL3Context, Utils.createIpAddress(oldFixedIPAddress))
-                        .augmentation(NatAddress.class),
+                    IidFactory.l3EndpointIid(routerL3ContextId, oldEpIp).augmentation(NatAddress.class),
                     rwTx);
         }
 
         boolean isSubmitToDsSuccessful = DataStoreHelper.submitToDs(rwTx);
         if (!isSubmitToDsSuccessful) {
-            return StatusCode.INTERNAL_SERVER_ERROR;
+            LOG.warn("Nat address {} was not added to endpoint {}", epNatIp, newEpIp);
         }
-
-        return StatusCode.OK;
-    }
-
-    @Override
-    public void updated(NeutronFloatingIP floatingIP) {
-        LOG.trace("updated floatingIP - {}", floatingIP);
-    }
-
-    @Override
-    public int canDelete(NeutronFloatingIP floatingIP) {
-        LOG.trace("canDelete floatingIP - {}", floatingIP);
-        return StatusCode.OK;
     }
 
     @Override
-    public void deleted(NeutronFloatingIP floatingIP) {
+    public void onDeleted(Floatingip floatingIP, Neutron oldNeutron, Neutron newNeutron) {
         LOG.trace("deleted floatingIP - {}", floatingIP);
     }
 }