2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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
8 package org.opendaylight.netvirt.vpnmanager.intervpnlink;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Objects;
20 import java.util.stream.Collectors;
21 import javax.annotation.PostConstruct;
22 import javax.annotation.PreDestroy;
23 import javax.inject.Inject;
24 import javax.inject.Singleton;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
30 import org.opendaylight.genius.mdsalutil.MDSALUtil;
31 import org.opendaylight.genius.mdsalutil.NwConstants;
32 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
33 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
34 import org.opendaylight.netvirt.fibmanager.api.FibHelper;
35 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
36 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
37 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronUtils;
38 import org.opendaylight.netvirt.vpnmanager.VpnConstants;
39 import org.opendaylight.netvirt.vpnmanager.VpnUtil;
40 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
41 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.IVpnLinkService;
42 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
43 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.FibEntries;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMap;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.opendaylight.yangtools.yang.common.Uint32;
61 import org.opendaylight.yangtools.yang.common.Uint64;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 public class IVpnLinkServiceImpl implements IVpnLinkService, AutoCloseable {
68 private static final Logger LOG = LoggerFactory.getLogger(IVpnLinkServiceImpl.class);
70 private final DataBroker dataBroker;
71 private final ManagedNewTransactionRunner txRunner;
72 private final IdManagerService idManager;
73 private final IBgpManager bgpManager;
74 private final IFibManager fibManager;
75 private final InterVpnLinkCache interVpnLinkCache;
76 private final VpnUtil vpnUtil;
77 private final InterVpnLinkUtil interVpnLinkUtil;
80 public IVpnLinkServiceImpl(final DataBroker dataBroker, final IdManagerService idMgr, final IBgpManager bgpMgr,
81 final IFibManager fibMgr, final InterVpnLinkCache interVpnLinkCache,
82 VpnUtil vpnUtil, InterVpnLinkUtil interVpnLinkUtil) {
83 this.dataBroker = dataBroker;
84 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
85 this.idManager = idMgr;
86 this.bgpManager = bgpMgr;
87 this.fibManager = fibMgr;
88 this.interVpnLinkCache = interVpnLinkCache;
89 this.vpnUtil = vpnUtil;
90 this.interVpnLinkUtil = interVpnLinkUtil;
95 LOG.info("{} start", getClass().getSimpleName());
100 public void close() {
104 public void leakRoute(String vpnName, String prefix, List<String> nextHopList, Uint32 label, int addOrRemove) {
105 LOG.trace("leakRoute: vpnName={} prefix={} nhList={} label={}", vpnName, prefix, nextHopList, label);
106 Optional<InterVpnLinkDataComposite> optIVpnLink = interVpnLinkCache.getInterVpnLinkByVpnId(vpnName);
107 if (!optIVpnLink.isPresent()) {
108 LOG.debug("Vpn {} not involved in any InterVpnLink", vpnName);
111 leakRoute(optIVpnLink.get(), vpnName, prefix, nextHopList, label, addOrRemove);
114 // TODO Clean up the exception handling
115 @SuppressWarnings("checkstyle:IllegalCatch")
116 private void leakRoute(InterVpnLinkDataComposite interVpnLink, String vpnName, String prefix,
117 List<String> nextHopList, Uint32 label, int addOrRemove) {
119 String dstVpnName = interVpnLink.getOtherVpnName(vpnName);
121 LOG.trace("leakingRoute: from VPN={} to VPN={}: prefix={} nhList={} label={}",
122 vpnName, dstVpnName, prefix, nextHopList, label);
124 // For leaking, we need the InterVpnLink to be active.
125 if (addOrRemove == NwConstants.ADD_FLOW && !interVpnLink.isActive()) {
126 LOG.warn("Cannot leak route [prefix={}, label={}] from VPN {} to VPN {} because "
127 + "InterVpnLink {} is not active",
128 prefix, label, vpnName, dstVpnName, interVpnLink.getInterVpnLinkName());
132 String dstVpnRd = vpnUtil.getVpnRd(dstVpnName);
133 if (addOrRemove == NwConstants.ADD_FLOW) {
134 LOG.debug("Leaking route (prefix={}, nexthop={}) from Vpn={} to Vpn={} (RD={})",
135 prefix, nextHopList, vpnName, dstVpnName, dstVpnRd);
136 String key = dstVpnRd + VpnConstants.SEPARATOR + prefix;
137 Uint32 leakedLabel = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME, key);
138 String leakedNexthop = interVpnLink.getEndpointIpAddr(vpnName);
139 fibManager.addOrUpdateFibEntry(dstVpnRd, null /*macAddress*/, prefix,
140 Collections.singletonList(leakedNexthop), VrfEntry.EncapType.Mplsgre,
141 leakedLabel, Uint32.ZERO /*l3vni*/, null /*gatewayMacAddress*/,
142 null /*parentVpnRd*/, RouteOrigin.INTERVPN, null /*writeConfigTxn*/);
144 List<String> ivlNexthops =
145 interVpnLink.getEndpointDpnsByVpnName(dstVpnName).stream()
146 .map(dpnId -> InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId))
147 .collect(Collectors.toList());
149 bgpManager.advertisePrefix(dstVpnRd, null /*macAddress*/, prefix, ivlNexthops,
150 VrfEntry.EncapType.Mplsgre, leakedLabel, Uint32.ZERO /*l3vni*/,
151 Uint32.ZERO /*l2vni*/, null /*gwMacAddress*/);
152 } catch (Exception e) {
153 LOG.error("Exception while advertising prefix {} on vpnRd {} for intervpn link", prefix, dstVpnRd, e);
156 LOG.debug("Removing leaked route to {} from VPN {}", prefix, dstVpnName);
157 fibManager.removeFibEntry(dstVpnRd, prefix, null, null /*writeConfigTxn*/);
158 bgpManager.withdrawPrefix(dstVpnRd, prefix);
162 // TODO Clean up the exception handling
163 @SuppressWarnings("checkstyle:IllegalCatch")
165 public void leakRoute(InterVpnLinkDataComposite interVpnLink, String srcVpnUuid, String dstVpnUuid,
166 String prefix, Uint32 label, @Nullable RouteOrigin forcedOrigin) {
167 String ivpnLinkName = interVpnLink.getInterVpnLinkName();
168 // The source VPN must participate in the InterVpnLink
169 Preconditions.checkArgument(interVpnLink.isVpnLinked(srcVpnUuid),
170 "The source VPN {} does not participate in the interVpnLink {}",
171 srcVpnUuid, ivpnLinkName);
172 // The destination VPN must participate in the InterVpnLink
173 Preconditions.checkArgument(interVpnLink.isVpnLinked(dstVpnUuid),
174 "The destination VPN {} does not participate in the interVpnLink {}",
175 dstVpnUuid, ivpnLinkName);
177 String endpointIp = interVpnLink.getOtherEndpointIpAddr(dstVpnUuid);
178 String leakedOrigin = forcedOrigin != null ? forcedOrigin.getValue() : RouteOrigin.INTERVPN.getValue();
179 FibHelper.buildRoutePath(endpointIp, label);
180 VrfEntry newVrfEntry =
181 new VrfEntryBuilder().withKey(new VrfEntryKey(prefix)).setDestPrefix(prefix)
183 Collections.singletonList(FibHelper.buildRoutePath(endpointIp, label)))
184 .setOrigin(leakedOrigin).build();
186 String dstVpnRd = vpnUtil.getVpnRd(dstVpnUuid);
187 InstanceIdentifier<VrfEntry> newVrfEntryIid =
188 InstanceIdentifier.builder(FibEntries.class)
189 .child(VrfTables.class, new VrfTablesKey(dstVpnRd))
190 .child(VrfEntry.class, new VrfEntryKey(newVrfEntry.getDestPrefix()))
192 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx ->
193 tx.put(newVrfEntryIid, newVrfEntry)), LOG, "Error adding VRF entry {}", newVrfEntry);
195 // Finally, route is advertised it to the DC-GW. But while in the FibEntries the nexthop is the other
196 // endpoint's IP, in the DC-GW the nexthop for those prefixes are the IPs of those DPNs where the target
197 // VPN has been instantiated
198 List<Uint64> srcDpnList = interVpnLink.getEndpointDpnsByVpnName(srcVpnUuid);
199 List<String> nexthops =
200 srcDpnList.stream().map(dpnId -> InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId))
201 .collect(Collectors.toList());
203 LOG.debug("Advertising route in VPN={} [prefix={} label={} nexthops={}] to DC-GW",
204 dstVpnRd, newVrfEntry.getDestPrefix(), label.intValue(), nexthops);
206 bgpManager.advertisePrefix(dstVpnRd, null /*macAddress*/, prefix, nexthops,
207 VrfEntry.EncapType.Mplsgre, label, Uint32.ZERO /*l3vni*/, Uint32.ZERO /*l2vni*/,
208 null /*gwMacAddress*/);
209 } catch (Exception e) {
210 LOG.error("Exception while advertising prefix {} on vpnRd {} for intervpn link", prefix, dstVpnRd, e);
215 public void leakRouteIfNeeded(String vpnName, String prefix, List<String> nextHopList, Uint32 label,
216 RouteOrigin origin, int addOrRemove) {
218 Optional<InterVpnLinkDataComposite> optIVpnLink = interVpnLinkCache.getInterVpnLinkByVpnId(vpnName);
219 if (!optIVpnLink.isPresent()) {
220 LOG.debug("Vpn {} not involved in any InterVpnLink", vpnName);
223 InterVpnLinkDataComposite ivpnLink = optIVpnLink.get();
224 if (addOrRemove == NwConstants.ADD_FLOW && !ivpnLink.isActive()) {
225 // Note: for the removal case it is not necessary that ivpnlink is ACTIVE
226 LOG.debug("Route to {} in VPN {} cannot be leaked because InterVpnLink {} is not ACTIVE",
227 prefix, vpnName, ivpnLink.getInterVpnLinkName());
233 if (!ivpnLink.isBgpRoutesLeaking()) {
234 LOG.debug("BGP route to {} not leaked because bgp-routes-leaking is disabled", prefix);
237 leakRoute(vpnName, prefix, nextHopList, label, addOrRemove);
240 /* NOTE: There are 2 types of static routes depending on the next hop:
241 + static route when next hop is a VM, the DC-GW or a DPNIP
242 + static route when next hop is an Inter-VPN Link
243 Only the 1st type should be considered since the 2nd has a special treatment */
244 if (!ivpnLink.isStaticRoutesLeaking()) {
245 LOG.debug("Static route to {} not leaked because static-routes-leaking is disabled", prefix);
248 leakRoute(vpnName, prefix, nextHopList, label, addOrRemove);
251 if (!ivpnLink.isConnectedRoutesLeaking()) {
252 LOG.debug("Connected route to {} not leaked because connected-routes-leaking is disabled", prefix);
255 leakRoute(vpnName, prefix, nextHopList, label, addOrRemove);
258 LOG.warn("origin {} not considered in Route-leaking", origin.getValue());
264 public void exchangeRoutes(InterVpnLinkDataComposite ivpnLink) {
265 if (!ivpnLink.isComplete()) {
269 // The type of routes to exchange depend on the leaking flags that have been activated
270 List<RouteOrigin> originsToConsider = new ArrayList<>();
271 if (ivpnLink.isBgpRoutesLeaking()) {
272 originsToConsider.add(RouteOrigin.BGP);
274 if (ivpnLink.isStaticRoutesLeaking()) {
275 originsToConsider.add(RouteOrigin.STATIC);
277 if (ivpnLink.isConnectedRoutesLeaking()) {
278 originsToConsider.add(RouteOrigin.CONNECTED);
281 String vpn1Uuid = ivpnLink.getFirstEndpointVpnUuid().get();
282 String vpn2Uuid = ivpnLink.getSecondEndpointVpnUuid().get();
284 if (! originsToConsider.isEmpty()) {
285 // 1st Endpoint ==> 2nd endpoint
286 leakRoutes(ivpnLink, vpn1Uuid, vpn2Uuid, originsToConsider);
289 // 2nd Endpoint ==> 1st endpoint
290 leakRoutes(ivpnLink, vpn2Uuid, vpn1Uuid, originsToConsider);
293 // Static routes in Vpn1 pointing to Vpn2's endpoint
294 leakExtraRoutesToVpnEndpoint(ivpnLink, vpn1Uuid, vpn2Uuid);
296 // Static routes in Vpn2 pointing to Vpn1's endpoint
297 leakExtraRoutesToVpnEndpoint(ivpnLink, vpn2Uuid, vpn1Uuid);
301 * Checks if there are static routes in Vpn1 whose nexthop is Vpn2's endpoint.
302 * Those routes must be leaked to Vpn1.
308 private void leakExtraRoutesToVpnEndpoint(InterVpnLinkDataComposite vpnLink, String vpn1Uuid, String vpn2Uuid) {
310 String vpn1Rd = vpnUtil.getVpnRd(vpn1Uuid);
311 String vpn2Endpoint = vpnLink.getOtherEndpointIpAddr(vpn2Uuid);
312 List<VrfEntry> allVpnVrfEntries = vpnUtil.getAllVrfEntries(vpn1Rd);
313 for (VrfEntry vrfEntry : allVpnVrfEntries) {
314 vrfEntry.nonnullRoutePaths().stream()
315 .filter(routePath -> Objects.equals(routePath.getNexthopAddress(), vpn2Endpoint))
316 .forEach(routePath -> {
317 // Vpn1 has a route pointing to Vpn2's endpoint. Forcing the leaking of the route will update
318 // the BGP accordingly
319 Uint32 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
320 VpnUtil.getNextHopLabelKey(vpn1Rd, vrfEntry.getDestPrefix()));
321 if (label.longValue() == VpnConstants.INVALID_LABEL) {
322 LOG.error("Unable to fetch label from Id Manager. Bailing out of leaking extra routes for "
323 + "InterVpnLink {} rd {} prefix {}",
324 vpnLink.getInterVpnLinkName(), vpn1Rd, vrfEntry.getDestPrefix());
326 leakRoute(vpnLink, vpn2Uuid, vpn1Uuid, vrfEntry.getDestPrefix(), label,
327 RouteOrigin.value(vrfEntry.getOrigin()));
334 private void leakRoutes(InterVpnLinkDataComposite vpnLink, String srcVpnUuid, String dstVpnUuid,
335 List<RouteOrigin> originsToConsider) {
336 String srcVpnRd = vpnUtil.getVpnRd(srcVpnUuid);
337 String dstVpnRd = vpnUtil.getVpnRd(dstVpnUuid);
338 List<VrfEntry> srcVpnRemoteVrfEntries = vpnUtil.getVrfEntriesByOrigin(srcVpnRd, originsToConsider);
339 for (VrfEntry vrfEntry : srcVpnRemoteVrfEntries) {
340 Uint32 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
341 VpnUtil.getNextHopLabelKey(dstVpnRd, vrfEntry.getDestPrefix()));
342 if (label.longValue() == VpnConstants.INVALID_LABEL) {
343 LOG.error("Unable to fetch label from Id Manager. Bailing out of leaking routes for InterVpnLink {} "
345 vpnLink.getInterVpnLinkName(), dstVpnRd, vrfEntry.getDestPrefix());
348 leakRoute(vpnLink, srcVpnUuid, dstVpnUuid, vrfEntry.getDestPrefix(), label, null /*NotForcedOrigin*/);
352 private Map<String, String> buildRouterXL3VPNMap() {
353 InstanceIdentifier<VpnMaps> vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build();
354 Optional<VpnMaps> optVpnMaps =
355 MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, vpnMapsIdentifier);
356 if (!optVpnMaps.isPresent()) {
357 LOG.info("Could not retrieve VpnMaps object from Configurational DS");
358 return new HashMap<>();
360 Map<String,String> vmap = new HashMap<>();
361 final List<VpnMap> VpnMapList = optVpnMaps.get().nonnullVpnMap();
362 for (VpnMap map : VpnMapList) {
363 if (map.getRouterIds() == null) {
366 final List<Uuid> vpnRouterIds = NeutronUtils.getVpnMapRouterIdsListUuid(map.getRouterIds());
367 for (Uuid routerId : vpnRouterIds) {
368 if (map.getVpnId().getValue().equalsIgnoreCase(routerId.getValue())) {
369 break; // VPN is internal
371 vmap.put(routerId.getValue(), map.getVpnId().getValue());
379 public void handleStaticRoutes(InterVpnLinkDataComposite ivpnLink) {
381 * Checks if there are any static routes pointing to any of both
382 * InterVpnLink's endpoints. Goes through all routers checking if they have
383 * a route whose nexthop is an InterVpnLink endpoint
386 // Map that corresponds a routerId with the L3VPN that it's been assigned to.
387 Map<String, String> routerXL3VpnMap = buildRouterXL3VPNMap();
389 // Retrieving all Routers
390 InstanceIdentifier<Routers> routersIid = InstanceIdentifier.builder(Neutron.class)
391 .child(Routers.class).build();
392 Optional<Routers> routerOpData = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routersIid);
393 if (!routerOpData.isPresent()) {
397 List<Router> routers = routerOpData.get().nonnullRouter();
398 for (Router router : routers) {
399 String vpnId = routerXL3VpnMap.get(router.getUuid().getValue());
401 LOG.warn("Could not find suitable VPN for router {}", router.getUuid());
402 continue; // with next router
404 List<Routes> routerRoutes = router.getRoutes();
405 if (routerRoutes != null) {
406 for (Routes route : routerRoutes) {
407 handleStaticRoute(vpnId, route, ivpnLink);
414 * Takes care of an static route to see if flows related to interVpnLink
415 * must be installed in tables 20 and 17
417 * @param vpnId Vpn to which the route belongs
418 * @param route Route to handle. Will only be considered if its nexthop is the VPN's endpoint IpAddress
419 * at the other side of the InterVpnLink
422 @SuppressWarnings("checkstyle:IllegalCatch")
423 private void handleStaticRoute(String vpnId, Routes route, InterVpnLinkDataComposite ivpnLink) {
425 IpAddress nhIpAddr = route.getNexthop();
426 String routeNextHop = nhIpAddr.getIpv4Address() != null ? nhIpAddr.getIpv4Address().getValue()
427 : nhIpAddr.getIpv6Address().getValue();
428 String destination = route.getDestination().stringValue();
430 // is nexthop the other endpoint's IP
431 String otherEndpoint = ivpnLink.getOtherEndpoint(vpnId);
432 if (!routeNextHop.equals(otherEndpoint)) {
433 LOG.debug("VPN {}: Route to {} nexthop={} points to an InterVpnLink endpoint, but its not "
434 + "the other endpoint. Other endpoint is {}",
435 vpnId, destination, routeNextHop, otherEndpoint);
439 // Lets work: 1) write Fibentry, 2) advertise to BGP and 3) check if it must be leaked
440 String vpnRd = vpnUtil.getVpnRd(vpnId);
442 LOG.warn("Could not find Route-Distinguisher for VpnName {}", vpnId);
446 Uint32 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
447 VpnUtil.getNextHopLabelKey(vpnId, destination));
450 interVpnLinkUtil.handleStaticRoute(ivpnLink, vpnId, destination, routeNextHop, label);
451 } catch (Exception e) {
452 LOG.error("Exception while advertising prefix for intervpn link", e);