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 com.google.common.base.Optional;
11 import com.google.common.util.concurrent.SettableFuture;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
17 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
18 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
19 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
20 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
21 import org.opendaylight.netvirt.vpnmanager.intervpnlink.InterVpnLinkUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveStaticRouteInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveVpnLabelInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.VpnRpcService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class VpnRpcServiceImpl implements VpnRpcService {
41 private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
42 private final DataBroker dataBroker;
43 private final IdManagerService idManager;
44 private final VpnInterfaceManager vpnInterfaceMgr;
45 private IFibManager fibManager;
46 private IBgpManager bgpManager;
48 public VpnRpcServiceImpl(final DataBroker dataBroker, final IdManagerService idManager,
49 final VpnInterfaceManager vpnIfaceMgr, final IFibManager fibManager, IBgpManager bgpManager) {
50 this.dataBroker = dataBroker;
51 this.idManager = idManager;
52 this.vpnInterfaceMgr = vpnIfaceMgr;
53 this.fibManager = fibManager;
54 this.bgpManager = bgpManager;
57 public void setFibManager(IFibManager fibMgr) {
58 this.fibManager = fibMgr;
63 * Generate label for the given ip prefix from the associated VPN.
66 public Future<RpcResult<GenerateVpnLabelOutput>> generateVpnLabel(GenerateVpnLabelInput input) {
67 String vpnName = input.getVpnName();
68 String ipPrefix = input.getIpPrefix();
69 SettableFuture<RpcResult<GenerateVpnLabelOutput>> futureResult = SettableFuture.create();
70 String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
71 long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
72 VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
74 String msg = "Could not retrieve the label for prefix " + ipPrefix + " in VPN " + vpnName;
76 futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
79 GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
80 futureResult.set(RpcResultBuilder.success(output).build());
86 * Remove label for the given ip prefix from the associated VPN.
89 public Future<RpcResult<Void>> removeVpnLabel(RemoveVpnLabelInput input) {
90 String vpnName = input.getVpnName();
91 String ipPrefix = input.getIpPrefix();
92 String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
93 SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
94 VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
95 VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
96 futureResult.set(RpcResultBuilder.<Void>success().build());
100 private Collection<RpcError> validateAddStaticRouteInput(AddStaticRouteInput input) {
101 Collection<RpcError> rpcErrors = new ArrayList<RpcError>();
102 String destination = input.getDestination();
103 String vpnInstanceName = input.getVpnInstanceName();
104 String nexthop = input.getNexthop();
105 if (destination == null || destination.isEmpty()) {
106 String message = "destination parameter is mandatory";
107 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
109 if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
110 String message = "vpnInstanceName parameter is mandatory";
111 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
113 if (nexthop == null || nexthop.isEmpty()) {
114 String message = "nexthop parameter is mandatory";
115 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
120 // TODO Clean up the exception handling
121 @SuppressWarnings("checkstyle:IllegalCatch")
123 public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
125 SettableFuture<RpcResult<AddStaticRouteOutput>> result = SettableFuture.create();
126 String destination = input.getDestination();
127 String vpnInstanceName = input.getVpnInstanceName();
128 String nexthop = input.getNexthop();
129 Long label = input.getLabel();
130 LOG.info("Adding static route for Vpn {} with destination {}, nexthop {} and label {}",
131 vpnInstanceName, destination, nexthop, label);
133 Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
134 if (!rpcErrors.isEmpty()) {
135 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
139 if (label == null || label == 0) {
140 label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
141 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
143 String message = "Unable to retrieve a new Label for the new Route";
144 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
150 String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
152 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
153 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
158 Optional<InterVpnLinkDataComposite> optIVpnLink = InterVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
159 if (optIVpnLink.isPresent()) {
162 InterVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop,
164 dataBroker, fibManager, bgpManager);
165 } catch (Exception e) {
166 String errMsg = "Could not advertise route [vpn=" + vpnRd + ", prefix=" + destination + ", label="
167 + label + ", nexthop=" + nexthop + ", ] to BGP. Reason: " + e.getMessage();
169 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION, errMsg)
174 vpnInterfaceMgr.addExtraRoute(destination, nexthop, vpnRd, null /*routerId */, label.intValue(),
175 RouteOrigin.STATIC, null /* intfName */, null, null);
178 AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
179 result.set(RpcResultBuilder.success(labelOutput).build());
183 private Collection<RpcError> validateRemoveStaticRouteInput(RemoveStaticRouteInput input) {
184 Collection<RpcError> rpcErrors = new ArrayList<>();
185 String destination = input.getDestination();
186 String vpnInstanceName = input.getVpnInstanceName();
187 String nexthop = input.getNexthop();
188 if (destination == null || destination.isEmpty()) {
189 String message = "destination parameter is mandatory";
190 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
192 if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
193 String message = "vpnInstanceName parameter is mandatory";
194 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
196 if (nexthop == null || nexthop.isEmpty()) {
197 String message = "nexthop parameter is mandatory";
198 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
204 public Future<RpcResult<Void>> removeStaticRoute(RemoveStaticRouteInput input) {
206 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
208 String destination = input.getDestination();
209 String vpnInstanceName = input.getVpnInstanceName();
210 String nexthop = input.getNexthop();
211 LOG.info("Removing static route with destination={}, nexthop={} in VPN={}",
212 destination, nexthop, vpnInstanceName);
213 Collection<RpcError> rpcErrors = validateRemoveStaticRouteInput(input);
214 if (!rpcErrors.isEmpty()) {
215 result.set(RpcResultBuilder.<Void>failed().withRpcErrors(rpcErrors).build());
219 String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
221 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
222 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, message).build());
226 Optional<InterVpnLink> optVpnLink = InterVpnLinkUtil.getInterVpnLinkByEndpointIp(dataBroker, nexthop);
227 if (optVpnLink.isPresent()) {
228 fibManager.removeOrUpdateFibEntry(dataBroker, vpnRd, destination, nexthop, null);
229 bgpManager.withdrawPrefix(vpnRd, destination);
231 vpnInterfaceMgr.delExtraRoute(destination, nexthop, vpnRd, null /*routerId*/, null /*intfName*/, null);
233 result.set(RpcResultBuilder.<Void>success().build());