2 * Copyright (c) 2016 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;
10 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashSet;
18 import java.util.concurrent.ExecutionException;
19 import java.util.function.Consumer;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
24 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
25 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
26 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
27 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
28 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
29 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
30 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
31 import org.opendaylight.netvirt.vpnmanager.intervpnlink.InterVpnLinkUtil;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.ApplyArpConfigInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.ApplyArpConfigOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.ApplyArpConfigOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveStaticRouteInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveStaticRouteOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveStaticRouteOutputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveVpnLabelInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveVpnLabelOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveVpnLabelOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.VpnRpcService;
50 import org.opendaylight.yangtools.yang.common.RpcError;
51 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
54 import org.opendaylight.yangtools.yang.common.Uint32;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.slf4j.helpers.FormattingTuple;
58 import org.slf4j.helpers.MessageFormatter;
61 public class VpnRpcServiceImpl implements VpnRpcService {
62 private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
63 private final ManagedNewTransactionRunner txRunner;
64 private final IFibManager fibManager;
65 private final IBgpManager bgpManager;
66 private final IVpnManager vpnManager;
67 private final InterVpnLinkCache interVpnLinkCache;
68 private final VpnUtil vpnUtil;
69 private final InterVpnLinkUtil interVpnLinkUtil;
72 public VpnRpcServiceImpl(final DataBroker dataBroker,
73 final IFibManager fibManager, IBgpManager bgpManager, final IVpnManager vpnManager,
74 final InterVpnLinkCache interVpnLinkCache, VpnUtil vpnUtil, InterVpnLinkUtil interVpnLinkUtil) {
75 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
76 this.fibManager = fibManager;
77 this.bgpManager = bgpManager;
78 this.vpnManager = vpnManager;
79 this.interVpnLinkCache = interVpnLinkCache;
80 this.vpnUtil = vpnUtil;
81 this.interVpnLinkUtil = interVpnLinkUtil;
85 * Generate label for the given ip prefix from the associated VPN.
88 public ListenableFuture<RpcResult<GenerateVpnLabelOutput>> generateVpnLabel(GenerateVpnLabelInput input) {
89 String vpnName = input.getVpnName();
90 String ipPrefix = input.getIpPrefix();
91 SettableFuture<RpcResult<GenerateVpnLabelOutput>> futureResult = SettableFuture.create();
92 String rd = vpnUtil.getVpnRd(vpnName);
93 Uint32 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
94 VpnUtil.getNextHopLabelKey(rd != null ? rd : vpnName, ipPrefix));
95 if (label == null || label.longValue() == 0) {
96 futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION,
97 formatAndLog(LOG::error, "Could not retrieve the label for prefix {} in VPN {}", ipPrefix,
100 GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
101 futureResult.set(RpcResultBuilder.success(output).build());
107 * Remove label for the given ip prefix from the associated VPN.
110 public ListenableFuture<RpcResult<RemoveVpnLabelOutput>> removeVpnLabel(RemoveVpnLabelInput input) {
111 String vpnName = input.getVpnName();
112 String ipPrefix = input.getIpPrefix();
113 String rd = vpnUtil.getVpnRd(vpnName);
114 vpnUtil.releaseId(VpnConstants.VPN_IDPOOL_NAME,
115 VpnUtil.getNextHopLabelKey(rd != null ? rd : vpnName, ipPrefix));
116 return RpcResultBuilder.success(new RemoveVpnLabelOutputBuilder().build()).buildFuture();
119 private Collection<RpcError> validateAddStaticRouteInput(AddStaticRouteInput input) {
120 Collection<RpcError> rpcErrors = new ArrayList<>();
121 String destination = input.getDestination();
122 String vpnInstanceName = input.getVpnInstanceName();
123 String nexthop = input.getNexthop();
124 if (destination == null || destination.isEmpty()) {
125 String message = "destination parameter is mandatory";
126 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
128 if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
129 String message = "vpnInstanceName parameter is mandatory";
130 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
132 if (nexthop == null || nexthop.isEmpty()) {
133 String message = "nexthop parameter is mandatory";
134 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
139 // TODO Clean up the exception handling
140 @SuppressWarnings("checkstyle:IllegalCatch")
142 public ListenableFuture<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
144 SettableFuture<RpcResult<AddStaticRouteOutput>> result = SettableFuture.create();
145 String destination = input.getDestination();
146 String vpnInstanceName = input.getVpnInstanceName();
147 String nexthop = input.getNexthop();
148 Uint32 label = input.getLabel();
149 LOG.info("Adding static route for Vpn {} with destination {}, nexthop {} and label {}",
150 vpnInstanceName, destination, nexthop, label);
152 Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
153 if (!rpcErrors.isEmpty()) {
154 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
158 if (label == null || label.longValue() == 0) {
159 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME,
160 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
161 if (label.longValue() == 0) {
162 String message = "Unable to retrieve a new Label for the new Route";
163 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
169 String vpnRd = vpnUtil.getVpnRd(input.getVpnInstanceName());
170 VpnInstanceOpDataEntry vpnOpEntry = vpnUtil.getVpnInstanceOpData(vpnRd);
171 Boolean isVxlan = VpnUtil.isL3VpnOverVxLan(vpnOpEntry.getL3vni());
172 VrfEntry.EncapType encapType = VpnUtil.getEncapType(isVxlan);
174 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
175 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
180 Optional<InterVpnLinkDataComposite> optIVpnLink = interVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
181 if (optIVpnLink.isPresent()) {
183 interVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop, label);
184 } catch (Exception e) {
185 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION,
186 formatAndLog(LOG::warn,
187 "Could not advertise route [vpn={}, prefix={}, label={}, nexthop={}] to BGP: {}", vpnRd,
188 destination, label, nexthop, e.getMessage(), e)).build());
193 txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
194 confTx -> vpnManager.addExtraRoute(vpnInstanceName, destination, nexthop, vpnRd,
195 null /* routerId */, vpnOpEntry.getL3vni(),
196 RouteOrigin.STATIC, null /* intfName */,
197 null /*Adjacency*/, encapType, new HashSet<>() /*prefixListForRefreshFib*/,confTx)).get();
198 } catch (InterruptedException | ExecutionException e) {
199 LOG.error("Error adding static route {}", input, e);
200 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION,
201 "Error adding static route " + input, e).build());
206 AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
207 result.set(RpcResultBuilder.success(labelOutput).build());
211 private Collection<RpcError> validateRemoveStaticRouteInput(RemoveStaticRouteInput input) {
212 Collection<RpcError> rpcErrors = new ArrayList<>();
213 String destination = input.getDestination();
214 String vpnInstanceName = input.getVpnInstanceName();
215 String nexthop = input.getNexthop();
216 if (destination == null || destination.isEmpty()) {
217 String message = "destination parameter is mandatory";
218 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
220 if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
221 String message = "vpnInstanceName parameter is mandatory";
222 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
224 if (nexthop == null || nexthop.isEmpty()) {
225 String message = "nexthop parameter is mandatory";
226 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
232 public ListenableFuture<RpcResult<RemoveStaticRouteOutput>> removeStaticRoute(RemoveStaticRouteInput input) {
234 SettableFuture<RpcResult<RemoveStaticRouteOutput>> result = SettableFuture.create();
236 String destination = input.getDestination();
237 String vpnInstanceName = input.getVpnInstanceName();
238 String nexthop = input.getNexthop();
239 LOG.info("Removing static route with destination={}, nexthop={} in VPN={}",
240 destination, nexthop, vpnInstanceName);
241 Collection<RpcError> rpcErrors = validateRemoveStaticRouteInput(input);
242 if (!rpcErrors.isEmpty()) {
243 result.set(RpcResultBuilder.<RemoveStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
247 String vpnRd = vpnUtil.getVpnRd(input.getVpnInstanceName());
249 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
250 result.set(RpcResultBuilder.<RemoveStaticRouteOutput>failed()
251 .withError(RpcError.ErrorType.APPLICATION, message).build());
255 Optional<InterVpnLinkDataComposite> optVpnLink = interVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
256 if (optVpnLink.isPresent()) {
257 fibManager.removeOrUpdateFibEntry(vpnRd, destination, nexthop, /*writeTx*/ null);
258 bgpManager.withdrawPrefix(vpnRd, destination);
260 vpnManager.delExtraRoute(vpnInstanceName, destination,
261 nexthop, vpnRd, null /* routerId */, null /* intfName */, null,
264 result.set(RpcResultBuilder.success(new RemoveStaticRouteOutputBuilder().build()).build());
270 private String formatAndLog(Consumer<String> logger, String template, Object arg1, Object arg2) {
271 return logAndReturnMessage(logger, MessageFormatter.format(template, arg1, arg2));
274 private String formatAndLog(Consumer<String> logger, String template, Object... args) {
275 return logAndReturnMessage(logger, MessageFormatter.arrayFormat(template, args));
278 private String logAndReturnMessage(Consumer<String> logger, FormattingTuple tuple) {
279 String message = tuple.getMessage();
280 logger.accept(message);
285 public ListenableFuture<RpcResult<ApplyArpConfigOutput>> applyArpConfig(ApplyArpConfigInput input) {
286 Boolean isArpLearningEnabled = input.isEnableArpLearning();
287 LOG.info("isArpLearningEnabled {}", isArpLearningEnabled);
288 SettableFuture<RpcResult<ApplyArpConfigOutput>> result = SettableFuture.create();
289 ApplyArpConfigOutputBuilder output = new ApplyArpConfigOutputBuilder();
290 VpnUtil.enableArpLearning(isArpLearningEnabled);
291 output.setEnableArpLearning(VpnUtil.isArpLearningEnabled());
292 result.set(RpcResultBuilder.success(output).build());