apply checkstyle check during build for neutron-mapper
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / NeutronFloatingIpAware.java
1 /*
2  * Copyright (c) 2015 Intel, Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import com.google.common.base.Objects;
14
15 import java.util.concurrent.ExecutionException;
16
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.Utils;
21 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
22 import org.opendaylight.groupbasedpolicy.util.IidFactory;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.l3endpoint.rev151217.NatAddressBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class NeutronFloatingIpAware implements NeutronAware<Floatingip> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(NeutronFloatingIpAware.class);
38     public static final InstanceIdentifier<Floatingip> FLOATING_IP_WILDCARD_IID =
39             InstanceIdentifier.builder(Neutron.class).child(Floatingips.class).child(Floatingip.class).build();
40     private final DataBroker dataProvider;
41
42     public NeutronFloatingIpAware(DataBroker dataProvider) {
43         this.dataProvider = checkNotNull(dataProvider);
44     }
45
46     @Override
47     public void onCreated(Floatingip floatingIP, Neutron neutron) {
48         LOG.trace("created floatingIp - {}", floatingIP);
49         // TODO implement onCreate properly and replace tmp workaround
50         if (floatingIP.getFixedIpAddress() != null && floatingIP.getPortId() != null
51                 && floatingIP.getRouterId() != null) {
52             Floatingip unassociatedFloatingIp =
53                     new FloatingipBuilder(floatingIP).setFixedIpAddress(null).setPortId(null).setRouterId(null).build();
54             onUpdated(unassociatedFloatingIp, floatingIP, neutron, neutron);
55         }
56     }
57
58     @Override
59     public void onUpdated(Floatingip oldFloatingIp, Floatingip newFloatingIp, Neutron oldNeutron, Neutron newNeutron) {
60         LOG.trace("updated floatingIP - OLD: {}\nNEW: {}", oldFloatingIp, newFloatingIp);
61         IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
62         IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
63         if (Objects.equal(oldEpIp, newEpIp)) {
64             // floating IP was not moved from one port to the other
65             return;
66         }
67         ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
68         Utils.syncNat(rwTx, oldFloatingIp, newFloatingIp);
69         syncNatForEndpoint(rwTx, oldFloatingIp, newFloatingIp);
70         boolean isSubmitToDsSuccessful = DataStoreHelper.submitToDs(rwTx);
71         if (!isSubmitToDsSuccessful) {
72             LOG.warn("Nat address {} was not added to endpoint {}", newFloatingIp.getFloatingIpAddress(), newEpIp);
73         }
74     }
75
76     @Deprecated
77     private void syncNatForEndpoint(ReadWriteTransaction rwTx, Floatingip oldFloatingIp, Floatingip newFloatingIp) {
78         IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
79         IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
80         IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
81         if (oldEpIp != null && oldFloatingIp.getRouterId() != null) {
82             L3ContextId routerL3ContextId = new L3ContextId(oldFloatingIp.getRouterId().getValue());
83             DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL,
84                     IidFactory.l3EndpointIid(routerL3ContextId, oldEpIp).augmentation(NatAddress.class), rwTx);
85         }
86         if (epNatIp != null && newEpIp != null && newFloatingIp.getRouterId() != null) {
87             L3ContextId routerL3ContextId = new L3ContextId(newFloatingIp.getRouterId().getValue());
88             NatAddress nat = new NatAddressBuilder().setNatAddress(epNatIp).build();
89             LOG.info("Adding NAT augmentation {} for endpoint (deperecated model) {}", epNatIp, newEpIp.getValue());
90             rwTx.put(LogicalDatastoreType.OPERATIONAL,
91                     IidFactory.l3EndpointIid(routerL3ContextId, newEpIp).augmentation(NatAddress.class), nat, true);
92         }
93     }
94
95     @Override
96     public void onDeleted(Floatingip floatingIP, Neutron oldNeutron, Neutron newNeutron) {
97         LOG.trace("deleted floatingIP - {}", floatingIP);
98         ReadWriteTransaction rwTx = dataProvider.newReadWriteTransaction();
99         Utils.removeNat(rwTx, floatingIP);
100         try {
101             rwTx.submit().get();
102         } catch (InterruptedException | ExecutionException e) {
103             LOG.error("Failed to remove floating IP {}. {}", floatingIP, e);
104         }
105     }
106 }