Switch to JDT annotations for Nullable and NonNull
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronSubnetGwMacResolver.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. 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.netvirt.neutronvpn;
10
11 import com.google.common.util.concurrent.JdkFutureAdapters;
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20 import java.util.concurrent.ScheduledExecutorService;
21 import java.util.concurrent.TimeUnit;
22 import javax.annotation.PostConstruct;
23 import javax.annotation.PreDestroy;
24 import javax.inject.Inject;
25 import javax.inject.Singleton;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.genius.arputil.api.ArpConstants;
29 import org.opendaylight.genius.mdsalutil.NWUtil;
30 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
31 import org.opendaylight.netvirt.elanmanager.api.IElanService;
32 import org.opendaylight.netvirt.vpnmanager.api.ICentralizedSwitchProvider;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.OdlArputilService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpRequestInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.SendArpRequestInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.interfaces.InterfaceAddressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.Ipv6NdUtilService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.SendNeighborSolicitationInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.SendNeighborSolicitationInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6.nd.util.rev170210.SendNeighborSolicitationOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfo;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 @Singleton
57 public class NeutronSubnetGwMacResolver {
58     private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetGwMacResolver.class);
59     private static final long L3_INSTALL_DELAY_MILLIS = 5000;
60
61     private final DataBroker broker;
62     private final OdlArputilService arpUtilService;
63     private final IElanService elanService;
64     private final ICentralizedSwitchProvider cswitchProvider;
65     private final NeutronvpnUtils neutronvpnUtils;
66     private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(
67             new ThreadFactoryBuilder().setNameFormat("Gw-Mac-Res").build());
68     private final Ipv6NdUtilService ipv6NdUtilService;
69
70     @Inject
71     public NeutronSubnetGwMacResolver(final DataBroker broker,
72             final OdlArputilService arputilService, final IElanService elanService,
73             final ICentralizedSwitchProvider cswitchProvider, final NeutronvpnUtils neutronvpnUtils,
74             final Ipv6NdUtilService ipv6NdUtilService) {
75         this.broker = broker;
76         this.arpUtilService = arputilService;
77         this.elanService = elanService;
78         this.cswitchProvider = cswitchProvider;
79         this.neutronvpnUtils = neutronvpnUtils;
80         this.ipv6NdUtilService = ipv6NdUtilService;
81     }
82
83     // TODO Clean up the exception handling
84     @SuppressWarnings("checkstyle:IllegalCatch")
85     @PostConstruct
86     public void init() {
87         LOG.info("{} init", getClass().getSimpleName());
88
89         executorService.scheduleAtFixedRate(() -> {
90             try {
91                 sendArpRequestsToExtGateways();
92             } catch (Exception e) {
93                 LOG.warn("Failed to send ARP request to GW ips", e);
94             }
95         }, 0, ArpConstants.ARP_CACHE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
96
97     }
98
99     @PreDestroy
100     public void close() {
101         executorService.shutdownNow();
102     }
103
104     public void sendArpRequestsToExtGateways(Router router) {
105         // Let the FIB flows a chance to be installed
106         // otherwise the ARP response will be routed straight to L2
107         // and bypasses L3 arp cache
108         executorService.schedule(() -> sendArpRequestsToExtGatewayTask(router), L3_INSTALL_DELAY_MILLIS,
109                 TimeUnit.MILLISECONDS);
110     }
111
112     public void sendArpRequestsToExtGateways() {
113         LOG.trace("Sending ARP requests to external gateways");
114         for (Router router : neutronvpnUtils.getAllRouters()) {
115             sendArpRequestsToExtGateways(router);
116         }
117     }
118
119     private void sendArpRequestsToExtGatewayTask(Router router) {
120         LOG.trace("Send ARP requests to external GW for router {}", router);
121         Port extPort = getRouterExtGatewayPort(router);
122         if (extPort == null) {
123             LOG.trace("External GW port for router {} is missing", router.getUuid().getValue());
124             return;
125         }
126
127         String extInterface = getExternalInterface(router);
128         if (extInterface == null) {
129             LOG.trace("No external interface defined for router {}", router.getUuid().getValue());
130             return;
131         }
132
133         List<FixedIps> fixedIps = extPort.getFixedIps();
134         if (fixedIps == null || fixedIps.isEmpty()) {
135             LOG.trace("External GW port {} for router {} has no fixed IPs", extPort.getUuid().getValue(),
136                     router.getUuid().getValue());
137             return;
138         }
139
140         MacAddress macAddress = extPort.getMacAddress();
141         if (macAddress == null) {
142             LOG.trace("External GW port {} for router {} has no mac address", extPort.getUuid().getValue(),
143                     router.getUuid().getValue());
144             return;
145         }
146
147         for (FixedIps fixIp : fixedIps) {
148             Uuid subnetId = fixIp.getSubnetId();
149             IpAddress srcIpAddress = fixIp.getIpAddress();
150             IpAddress dstIpAddress = getExternalGwIpAddress(subnetId);
151             String srcIpAddressString = srcIpAddress.stringValue();
152             String dstIpAddressString = dstIpAddress.stringValue();
153             if (NWUtil.isIpv4Address(srcIpAddressString)) {
154                 sendArpRequest(srcIpAddress, dstIpAddress, macAddress, extInterface);
155             } else {
156                 sendNeighborSolication(new Ipv6Address(srcIpAddressString),macAddress,
157                         new Ipv6Address(dstIpAddressString), extInterface);
158             }
159         }
160
161     }
162
163     // TODO Clean up the exception handling
164     @SuppressWarnings("checkstyle:IllegalCatch")
165     private void sendArpRequest(IpAddress srcIpAddress, IpAddress dstIpAddress, MacAddress srcMacAddress,
166             String interfaceName) {
167         if (srcIpAddress == null || dstIpAddress == null) {
168             LOG.trace("Skip sending ARP to external GW srcIp {} dstIp {}", srcIpAddress, dstIpAddress);
169             return;
170         }
171
172         PhysAddress srcMacPhysAddress = new PhysAddress(srcMacAddress.getValue());
173         try {
174             InterfaceAddress interfaceAddress = new InterfaceAddressBuilder().setInterface(interfaceName)
175                     .setIpAddress(srcIpAddress).setMacaddress(srcMacPhysAddress).build();
176
177             SendArpRequestInput sendArpRequestInput = new SendArpRequestInputBuilder().setIpaddress(dstIpAddress)
178                     .setInterfaceAddress(Collections.singletonList(interfaceAddress)).build();
179
180             ListenableFutures.addErrorLogging(JdkFutureAdapters.listenInPoolThread(
181                     arpUtilService.sendArpRequest(sendArpRequestInput)), LOG, "Send ARP request");
182         } catch (Exception e) {
183             LOG.error("Failed to send ARP request to external GW {} from interface {}",
184                     dstIpAddress.getIpv4Address().getValue(), interfaceName, e);
185         }
186     }
187
188     private void sendNeighborSolication(Ipv6Address srcIpv6Address,
189             MacAddress srcMac, Ipv6Address dstIpv6Address, String interfaceName) {
190         List<org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6
191             .nd.util.rev170210.interfaces.InterfaceAddress> interfaceAddresses = new ArrayList<>();
192         interfaceAddresses.add(new org.opendaylight.yang.gen.v1.urn.opendaylight.genius.ipv6
193                 .nd.util.rev170210.interfaces.InterfaceAddressBuilder()
194             .setInterface(interfaceName)
195             .setSrcIpAddress(srcIpv6Address)
196             .setSrcMacAddress(new PhysAddress(srcMac.getValue())).build());
197         SendNeighborSolicitationInput input = new SendNeighborSolicitationInputBuilder()
198                 .setInterfaceAddress(interfaceAddresses).setTargetIpAddress(dstIpv6Address)
199                 .build();
200         try {
201             Future<RpcResult<SendNeighborSolicitationOutput>> result = ipv6NdUtilService
202                     .sendNeighborSolicitation(input);
203             RpcResult<SendNeighborSolicitationOutput> rpcResult = result.get();
204             if (!rpcResult.isSuccessful()) {
205                 LOG.error("sendNeighborSolicitationToOfGroup: RPC Call failed for input={} and Errors={}", input,
206                         rpcResult.getErrors());
207             }
208         } catch (InterruptedException | ExecutionException e) {
209             LOG.error("Failed to send NS packet to ELAN group, input={}", input, e);
210         }
211     }
212
213     @Nullable
214     private Port getRouterExtGatewayPort(Router router) {
215         if (router == null) {
216             LOG.trace("Router is null");
217             return null;
218         }
219
220         Uuid extPortId = router.getGatewayPortId();
221         if (extPortId == null) {
222             LOG.trace("Router {} is not associated with any external GW port", router.getUuid().getValue());
223             return null;
224         }
225
226         return neutronvpnUtils.getNeutronPort(extPortId);
227     }
228
229     @Nullable
230     private String getExternalInterface(Router router) {
231         ExternalGatewayInfo extGatewayInfo = router.getExternalGatewayInfo();
232         String routerName = router.getUuid().getValue();
233         if (extGatewayInfo == null) {
234             LOG.warn("External GW info missing for router {}", routerName);
235             return null;
236         }
237
238         Uuid extNetworkId = extGatewayInfo.getExternalNetworkId();
239         if (extNetworkId == null) {
240             LOG.warn("External network id missing for router {}", routerName);
241             return null;
242         }
243
244         BigInteger primarySwitch = cswitchProvider.getPrimarySwitchForRouter(routerName);
245         if (primarySwitch == null || BigInteger.ZERO.equals(primarySwitch)) {
246             LOG.warn("Primary switch has not been allocated for router {}", routerName);
247             return null;
248         }
249
250         return elanService.getExternalElanInterface(extNetworkId.getValue(), primarySwitch);
251     }
252
253     @Nullable
254     private IpAddress getExternalGwIpAddress(Uuid subnetId) {
255         if (subnetId == null) {
256             LOG.error("Subnet id is null");
257             return null;
258         }
259
260         Subnet subnet = neutronvpnUtils.getNeutronSubnet(subnetId);
261         return subnet != null ? subnet.getGatewayIp() : null;
262     }
263
264 }