2 * Copyright (c) 2015 Intel, Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
19 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
20 import org.opendaylight.groupbasedpolicy.util.IidFactory;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.IpPrefixType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L3Context;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddressBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.google.common.base.Objects;
38 public class NeutronFloatingIpAware implements NeutronAware<Floatingip> {
40 private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingIpAware.class);
41 public static final InstanceIdentifier<Floatingip> FLOATING_IP_WILDCARD_IID =
42 InstanceIdentifier.builder(Neutron.class).child(Floatingips.class).child(Floatingip.class).build();
43 private final DataBroker dataProvider;
45 public NeutronFloatingIpAware(DataBroker dataProvider) {
46 this.dataProvider = checkNotNull(dataProvider);
50 public void onCreated(Floatingip floatingIP, Neutron neutron) {
51 LOG.trace("created floatingIp - {}", floatingIP);
52 // TODO implement onCreate properly and replace tmp workaround
53 if (floatingIP.getFixedIpAddress() != null && floatingIP.getPortId() != null
54 && floatingIP.getRouterId() != null) {
55 Floatingip unassociatedFloatingIp =
56 new FloatingipBuilder(floatingIP).setFixedIpAddress(null).setPortId(null).setRouterId(null).build();
57 onUpdated(unassociatedFloatingIp, floatingIP, neutron, neutron);
62 public void onUpdated(Floatingip oldFloatingIp, Floatingip newFloatingIp, Neutron oldNeutron, Neutron newNeutron) {
63 LOG.trace("updated floatingIP - OLD: {}\nNEW: {}", oldFloatingIp, newFloatingIp);
64 IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
65 IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
66 if (Objects.equal(oldEpIp, newEpIp)) {
67 // floating IP was not moved from one port to the other
70 ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
71 Utils.syncNat(rwTx, oldFloatingIp, newFloatingIp);
72 syncNatForEndpoint(rwTx, oldFloatingIp, newFloatingIp);
73 boolean isSubmitToDsSuccessful = DataStoreHelper.submitToDs(rwTx);
74 if (!isSubmitToDsSuccessful) {
75 LOG.warn("Nat address {} was not added to endpoint {}", newFloatingIp.getFloatingIpAddress(), newEpIp);
80 private void syncNatForEndpoint(ReadWriteTransaction rwTx, Floatingip oldFloatingIp, Floatingip newFloatingIp) {
81 IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
82 IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
83 IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
84 if (oldEpIp != null && oldFloatingIp.getRouterId() != null) {
85 L3ContextId routerL3ContextId = new L3ContextId(oldFloatingIp.getRouterId().getValue());
86 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
87 IidFactory.l3EndpointIid(routerL3ContextId, oldEpIp).augmentation(NatAddress.class), rwTx);
89 if (epNatIp != null && newEpIp != null && newFloatingIp.getRouterId() != null) {
90 L3ContextId routerL3ContextId = new L3ContextId(newFloatingIp.getRouterId().getValue());
91 NatAddress nat = new NatAddressBuilder().setNatAddress(epNatIp).build();
92 AddressEndpointKey aek =
93 new AddressEndpointKey(newEpIp.getIpv4Address().getValue() + "/32", IpPrefixType.class,
94 routerL3ContextId, L3Context.class);
95 LOG.info("Adding NAT augmentation {} for base-endpoint {}", epNatIp, aek);
96 rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.addressEndpointIid(aek)
98 org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddress.class),
99 new org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddressBuilder()
100 .setNatAddress(epNatIp)
102 rwTx.put(LogicalDatastoreType.OPERATIONAL, IidFactory.l3EndpointIid(routerL3ContextId, newEpIp)
103 .augmentation(NatAddress.class), nat, true);
108 public void onDeleted(Floatingip floatingIP, Neutron oldNeutron, Neutron newNeutron) {
109 LOG.trace("deleted floatingIP - {}", floatingIP);
110 ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
111 Utils.removeNat(rwTx, floatingIP);
114 } catch (InterruptedException | ExecutionException e) {
115 LOG.error("Failed to remove floating IP {}. {}", floatingIP, e);