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.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Objects;
21 import java.util.stream.Collectors;
22 import javax.annotation.Nullable;
23 import javax.annotation.PostConstruct;
24 import javax.annotation.PreDestroy;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
31 import org.opendaylight.genius.mdsalutil.MDSALUtil;
32 import org.opendaylight.genius.mdsalutil.NwConstants;
33 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
34 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
35 import org.opendaylight.netvirt.fibmanager.api.FibHelper;
36 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
37 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
38 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronUtils;
39 import org.opendaylight.netvirt.vpnmanager.VpnConstants;
40 import org.opendaylight.netvirt.vpnmanager.VpnUtil;
41 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
42 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.IVpnLinkService;
43 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
44 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.FibEntries;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMap;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.Routers;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 public class IVpnLinkServiceImpl implements IVpnLinkService, AutoCloseable {
67 private static final Logger LOG = LoggerFactory.getLogger(IVpnLinkServiceImpl.class);
69 private final DataBroker dataBroker;
70 private final ManagedNewTransactionRunner txRunner;
71 private final IdManagerService idManager;
72 private final IBgpManager bgpManager;
73 private final IFibManager fibManager;
74 private final InterVpnLinkCache interVpnLinkCache;
75 private final VpnUtil vpnUtil;
76 private final InterVpnLinkUtil interVpnLinkUtil;
79 public IVpnLinkServiceImpl(final DataBroker dataBroker, final IdManagerService idMgr, final IBgpManager bgpMgr,
80 final IFibManager fibMgr, final InterVpnLinkCache interVpnLinkCache,
81 VpnUtil vpnUtil, InterVpnLinkUtil interVpnLinkUtil) {
82 this.dataBroker = dataBroker;
83 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
84 this.idManager = idMgr;
85 this.bgpManager = bgpMgr;
86 this.fibManager = fibMgr;
87 this.interVpnLinkCache = interVpnLinkCache;
88 this.vpnUtil = vpnUtil;
89 this.interVpnLinkUtil = interVpnLinkUtil;
94 LOG.info("{} start", getClass().getSimpleName());
103 public void leakRoute(String vpnName, String prefix, List<String> nextHopList, int label, int addOrRemove) {
104 LOG.trace("leakRoute: vpnName={} prefix={} nhList={} label={}", vpnName, prefix, nextHopList, label);
105 Optional<InterVpnLinkDataComposite> optIVpnLink = interVpnLinkCache.getInterVpnLinkByVpnId(vpnName);
106 if (!optIVpnLink.isPresent()) {
107 LOG.debug("Vpn {} not involved in any InterVpnLink", vpnName);
110 leakRoute(optIVpnLink.get(), vpnName, prefix, nextHopList, label, addOrRemove);
113 // TODO Clean up the exception handling
114 @SuppressWarnings("checkstyle:IllegalCatch")
115 private void leakRoute(InterVpnLinkDataComposite interVpnLink, String vpnName, String prefix,
116 List<String> nextHopList, int label, int addOrRemove) {
118 String dstVpnName = interVpnLink.getOtherVpnName(vpnName);
120 LOG.trace("leakingRoute: from VPN={} to VPN={}: prefix={} nhList={} label={}",
121 vpnName, dstVpnName, prefix, nextHopList, label);
123 // For leaking, we need the InterVpnLink to be active.
124 if (addOrRemove == NwConstants.ADD_FLOW && !interVpnLink.isActive()) {
125 LOG.warn("Cannot leak route [prefix={}, label={}] from VPN {} to VPN {} because "
126 + "InterVpnLink {} is not active",
127 prefix, label, vpnName, dstVpnName, interVpnLink.getInterVpnLinkName());
131 String dstVpnRd = vpnUtil.getVpnRd(dstVpnName);
132 if (addOrRemove == NwConstants.ADD_FLOW) {
133 LOG.debug("Leaking route (prefix={}, nexthop={}) from Vpn={} to Vpn={} (RD={})",
134 prefix, nextHopList, vpnName, dstVpnName, dstVpnRd);
135 String key = dstVpnRd + VpnConstants.SEPARATOR + prefix;
136 long leakedLabel = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME, key);
137 String leakedNexthop = interVpnLink.getEndpointIpAddr(vpnName);
138 fibManager.addOrUpdateFibEntry(dstVpnRd, null /*macAddress*/, prefix,
139 Collections.singletonList(leakedNexthop), VrfEntry.EncapType.Mplsgre,
140 (int) leakedLabel, 0 /*l3vni*/, null /*gatewayMacAddress*/,
141 null /*parentVpnRd*/, RouteOrigin.INTERVPN, null /*writeConfigTxn*/);
143 List<String> ivlNexthops =
144 interVpnLink.getEndpointDpnsByVpnName(dstVpnName).stream()
145 .map(dpnId -> InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId))
146 .collect(Collectors.toList());
148 bgpManager.advertisePrefix(dstVpnRd, null /*macAddress*/, prefix, ivlNexthops,
149 VrfEntry.EncapType.Mplsgre, (int)leakedLabel, 0 /*l3vni*/, 0 /*l2vni*/,
150 null /*gwMacAddress*/);
151 } catch (Exception e) {
152 LOG.error("Exception while advertising prefix {} on vpnRd {} for intervpn link", prefix, dstVpnRd, e);
155 LOG.debug("Removing leaked route to {} from VPN {}", prefix, dstVpnName);
156 fibManager.removeFibEntry(dstVpnRd, prefix, null /*writeConfigTxn*/);
157 bgpManager.withdrawPrefix(dstVpnRd, prefix);
161 // TODO Clean up the exception handling
162 @SuppressWarnings("checkstyle:IllegalCatch")
164 public void leakRoute(InterVpnLinkDataComposite interVpnLink, String srcVpnUuid, String dstVpnUuid,
165 String prefix, Long label, @Nullable RouteOrigin forcedOrigin) {
166 String ivpnLinkName = interVpnLink.getInterVpnLinkName();
167 // The source VPN must participate in the InterVpnLink
168 Preconditions.checkArgument(interVpnLink.isVpnLinked(srcVpnUuid),
169 "The source VPN {} does not participate in the interVpnLink {}",
170 srcVpnUuid, ivpnLinkName);
171 // The destination VPN must participate in the InterVpnLink
172 Preconditions.checkArgument(interVpnLink.isVpnLinked(dstVpnUuid),
173 "The destination VPN {} does not participate in the interVpnLink {}",
174 dstVpnUuid, ivpnLinkName);
176 String endpointIp = interVpnLink.getOtherEndpointIpAddr(dstVpnUuid);
177 String leakedOrigin = forcedOrigin != null ? forcedOrigin.getValue() : RouteOrigin.INTERVPN.getValue();
178 FibHelper.buildRoutePath(endpointIp, label);
179 VrfEntry newVrfEntry =
180 new VrfEntryBuilder().withKey(new VrfEntryKey(prefix)).setDestPrefix(prefix)
181 .setRoutePaths(Collections.singletonList(FibHelper.buildRoutePath(endpointIp, label)))
182 .setOrigin(leakedOrigin).build();
184 String dstVpnRd = vpnUtil.getVpnRd(dstVpnUuid);
185 InstanceIdentifier<VrfEntry> newVrfEntryIid =
186 InstanceIdentifier.builder(FibEntries.class)
187 .child(VrfTables.class, new VrfTablesKey(dstVpnRd))
188 .child(VrfEntry.class, new VrfEntryKey(newVrfEntry.getDestPrefix()))
190 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx ->
191 tx.put(newVrfEntryIid, newVrfEntry)), LOG, "Error adding VRF entry {}", newVrfEntry);
193 // Finally, route is advertised it to the DC-GW. But while in the FibEntries the nexthop is the other
194 // endpoint's IP, in the DC-GW the nexthop for those prefixes are the IPs of those DPNs where the target
195 // VPN has been instantiated
196 List<BigInteger> srcDpnList = interVpnLink.getEndpointDpnsByVpnName(srcVpnUuid);
197 List<String> nexthops =
198 srcDpnList.stream().map(dpnId -> InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, dpnId))
199 .collect(Collectors.toList());
201 LOG.debug("Advertising route in VPN={} [prefix={} label={} nexthops={}] to DC-GW",
202 dstVpnRd, newVrfEntry.getDestPrefix(), label.intValue(), nexthops);
204 bgpManager.advertisePrefix(dstVpnRd, null /*macAddress*/, prefix, nexthops,
205 VrfEntry.EncapType.Mplsgre, label.intValue(), 0 /*l3vni*/, 0 /*l2vni*/,
206 null /*gwMacAddress*/);
207 } catch (Exception e) {
208 LOG.error("Exception while advertising prefix {} on vpnRd {} for intervpn link", prefix, dstVpnRd, e);
213 public void leakRouteIfNeeded(String vpnName, String prefix, List<String> nextHopList, int label,
214 RouteOrigin origin, int addOrRemove) {
216 Optional<InterVpnLinkDataComposite> optIVpnLink = interVpnLinkCache.getInterVpnLinkByVpnId(vpnName);
217 if (!optIVpnLink.isPresent()) {
218 LOG.debug("Vpn {} not involved in any InterVpnLink", vpnName);
221 InterVpnLinkDataComposite ivpnLink = optIVpnLink.get();
222 if (addOrRemove == NwConstants.ADD_FLOW && !ivpnLink.isActive()) {
223 // Note: for the removal case it is not necessary that ivpnlink is ACTIVE
224 LOG.debug("Route to {} in VPN {} cannot be leaked because InterVpnLink {} is not ACTIVE",
225 prefix, vpnName, ivpnLink.getInterVpnLinkName());
231 if (!ivpnLink.isBgpRoutesLeaking()) {
232 LOG.debug("BGP route to {} not leaked because bgp-routes-leaking is disabled", prefix);
235 leakRoute(vpnName, prefix, nextHopList, label, addOrRemove);
238 /* NOTE: There are 2 types of static routes depending on the next hop:
239 + static route when next hop is a VM, the DC-GW or a DPNIP
240 + static route when next hop is an Inter-VPN Link
241 Only the 1st type should be considered since the 2nd has a special treatment */
242 if (!ivpnLink.isStaticRoutesLeaking()) {
243 LOG.debug("Static route to {} not leaked because static-routes-leaking is disabled", prefix);
246 leakRoute(vpnName, prefix, nextHopList, label, addOrRemove);
249 if (!ivpnLink.isConnectedRoutesLeaking()) {
250 LOG.debug("Connected route to {} not leaked because connected-routes-leaking is disabled", prefix);
253 leakRoute(vpnName, prefix, nextHopList, label, addOrRemove);
256 LOG.warn("origin {} not considered in Route-leaking", origin.getValue());
262 public void exchangeRoutes(InterVpnLinkDataComposite ivpnLink) {
263 if (!ivpnLink.isComplete()) {
267 // The type of routes to exchange depend on the leaking flags that have been activated
268 List<RouteOrigin> originsToConsider = new ArrayList<>();
269 if (ivpnLink.isBgpRoutesLeaking()) {
270 originsToConsider.add(RouteOrigin.BGP);
272 if (ivpnLink.isStaticRoutesLeaking()) {
273 originsToConsider.add(RouteOrigin.STATIC);
275 if (ivpnLink.isConnectedRoutesLeaking()) {
276 originsToConsider.add(RouteOrigin.CONNECTED);
279 String vpn1Uuid = ivpnLink.getFirstEndpointVpnUuid().get();
280 String vpn2Uuid = ivpnLink.getSecondEndpointVpnUuid().get();
282 if (! originsToConsider.isEmpty()) {
283 // 1st Endpoint ==> 2nd endpoint
284 leakRoutes(ivpnLink, vpn1Uuid, vpn2Uuid, originsToConsider);
287 // 2nd Endpoint ==> 1st endpoint
288 leakRoutes(ivpnLink, vpn2Uuid, vpn1Uuid, originsToConsider);
291 // Static routes in Vpn1 pointing to Vpn2's endpoint
292 leakExtraRoutesToVpnEndpoint(ivpnLink, vpn1Uuid, vpn2Uuid);
294 // Static routes in Vpn2 pointing to Vpn1's endpoint
295 leakExtraRoutesToVpnEndpoint(ivpnLink, vpn2Uuid, vpn1Uuid);
299 * Checks if there are static routes in Vpn1 whose nexthop is Vpn2's endpoint.
300 * Those routes must be leaked to Vpn1.
306 private void leakExtraRoutesToVpnEndpoint(InterVpnLinkDataComposite vpnLink, String vpn1Uuid, String vpn2Uuid) {
308 String vpn1Rd = vpnUtil.getVpnRd(vpn1Uuid);
309 String vpn2Endpoint = vpnLink.getOtherEndpointIpAddr(vpn2Uuid);
310 List<VrfEntry> allVpnVrfEntries = vpnUtil.getAllVrfEntries(vpn1Rd);
311 for (VrfEntry vrfEntry : allVpnVrfEntries) {
312 vrfEntry.nonnullRoutePaths().stream()
313 .filter(routePath -> Objects.equals(routePath.getNexthopAddress(), vpn2Endpoint))
314 .forEach(routePath -> {
315 // Vpn1 has a route pointing to Vpn2's endpoint. Forcing the leaking of the route will update
316 // the BGP accordingly
317 long label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
318 VpnUtil.getNextHopLabelKey(vpn1Rd, vrfEntry.getDestPrefix()));
319 if (label == VpnConstants.INVALID_LABEL) {
320 LOG.error("Unable to fetch label from Id Manager. Bailing out of leaking extra routes for "
321 + "InterVpnLink {} rd {} prefix {}",
322 vpnLink.getInterVpnLinkName(), vpn1Rd, vrfEntry.getDestPrefix());
324 leakRoute(vpnLink, vpn2Uuid, vpn1Uuid, vrfEntry.getDestPrefix(), label,
325 RouteOrigin.value(vrfEntry.getOrigin()));
332 private void leakRoutes(InterVpnLinkDataComposite vpnLink, String srcVpnUuid, String dstVpnUuid,
333 List<RouteOrigin> originsToConsider) {
334 String srcVpnRd = vpnUtil.getVpnRd(srcVpnUuid);
335 String dstVpnRd = vpnUtil.getVpnRd(dstVpnUuid);
336 List<VrfEntry> srcVpnRemoteVrfEntries = vpnUtil.getVrfEntriesByOrigin(srcVpnRd, originsToConsider);
337 for (VrfEntry vrfEntry : srcVpnRemoteVrfEntries) {
338 long label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
339 VpnUtil.getNextHopLabelKey(dstVpnRd, vrfEntry.getDestPrefix()));
340 if (label == VpnConstants.INVALID_LABEL) {
341 LOG.error("Unable to fetch label from Id Manager. Bailing out of leaking routes for InterVpnLink {} "
343 vpnLink.getInterVpnLinkName(), dstVpnRd, vrfEntry.getDestPrefix());
346 leakRoute(vpnLink, srcVpnUuid, dstVpnUuid, vrfEntry.getDestPrefix(), label, null /*NotForcedOrigin*/);
350 private Map<String, String> buildRouterXL3VPNMap() {
351 InstanceIdentifier<VpnMaps> vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build();
352 Optional<VpnMaps> optVpnMaps =
353 MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, vpnMapsIdentifier);
354 if (!optVpnMaps.isPresent()) {
355 LOG.info("Could not retrieve VpnMaps object from Configurational DS");
356 return new HashMap<>();
358 Map<String,String> vmap = new HashMap<>();
359 final List<VpnMap> VpnMapList = optVpnMaps.get().nonnullVpnMap();
360 for (VpnMap map : VpnMapList) {
361 if (map.getRouterIds() == null) {
364 final List<Uuid> vpnRouterIds = NeutronUtils.getVpnMapRouterIdsListUuid(map.getRouterIds());
365 for (Uuid routerId : vpnRouterIds) {
366 if (map.getVpnId().getValue().equalsIgnoreCase(routerId.getValue())) {
367 break; // VPN is internal
369 vmap.put(routerId.getValue(), map.getVpnId().getValue());
377 public void handleStaticRoutes(InterVpnLinkDataComposite ivpnLink) {
379 * Checks if there are any static routes pointing to any of both
380 * InterVpnLink's endpoints. Goes through all routers checking if they have
381 * a route whose nexthop is an InterVpnLink endpoint
384 // Map that corresponds a routerId with the L3VPN that it's been assigned to.
385 Map<String, String> routerXL3VpnMap = buildRouterXL3VPNMap();
387 // Retrieving all Routers
388 InstanceIdentifier<Routers> routersIid = InstanceIdentifier.builder(Neutron.class)
389 .child(Routers.class).build();
390 Optional<Routers> routerOpData = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routersIid);
391 if (!routerOpData.isPresent()) {
395 List<Router> routers = routerOpData.get().nonnullRouter();
396 for (Router router : routers) {
397 String vpnId = routerXL3VpnMap.get(router.getUuid().getValue());
399 LOG.warn("Could not find suitable VPN for router {}", router.getUuid());
400 continue; // with next router
402 List<Routes> routerRoutes = router.getRoutes();
403 if (routerRoutes != null) {
404 for (Routes route : routerRoutes) {
405 handleStaticRoute(vpnId, route, ivpnLink);
412 * Takes care of an static route to see if flows related to interVpnLink
413 * must be installed in tables 20 and 17
415 * @param vpnId Vpn to which the route belongs
416 * @param route Route to handle. Will only be considered if its nexthop is the VPN's endpoint IpAddress
417 * at the other side of the InterVpnLink
420 @SuppressWarnings("checkstyle:IllegalCatch")
421 private void handleStaticRoute(String vpnId, Routes route, InterVpnLinkDataComposite ivpnLink) {
423 IpAddress nhIpAddr = route.getNexthop();
424 String routeNextHop = nhIpAddr.getIpv4Address() != null ? nhIpAddr.getIpv4Address().getValue()
425 : nhIpAddr.getIpv6Address().getValue();
426 String destination = route.getDestination().stringValue();
428 // is nexthop the other endpoint's IP
429 String otherEndpoint = ivpnLink.getOtherEndpoint(vpnId);
430 if (!routeNextHop.equals(otherEndpoint)) {
431 LOG.debug("VPN {}: Route to {} nexthop={} points to an InterVpnLink endpoint, but its not "
432 + "the other endpoint. Other endpoint is {}",
433 vpnId, destination, routeNextHop, otherEndpoint);
437 // Lets work: 1) write Fibentry, 2) advertise to BGP and 3) check if it must be leaked
438 String vpnRd = vpnUtil.getVpnRd(vpnId);
440 LOG.warn("Could not find Route-Distinguisher for VpnName {}", vpnId);
444 int label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME, VpnUtil.getNextHopLabelKey(vpnId, destination));
447 interVpnLinkUtil.handleStaticRoute(ivpnLink, vpnId, destination, routeNextHop, label);
448 } catch (Exception e) {
449 LOG.error("Exception while advertising prefix for intervpn link, {}", e);