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.fibmanager.rev150330.vrfentries.VrfEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveStaticRouteInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveVpnLabelInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.VpnRpcService;
34 import org.opendaylight.yangtools.yang.common.RpcError;
35 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class VpnRpcServiceImpl implements VpnRpcService {
42 private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
43 private final DataBroker dataBroker;
44 private final IdManagerService idManager;
45 private final VpnInterfaceManager vpnInterfaceMgr;
46 private IFibManager fibManager;
47 private IBgpManager bgpManager;
49 public VpnRpcServiceImpl(final DataBroker dataBroker, final IdManagerService idManager,
50 final VpnInterfaceManager vpnIfaceMgr, final IFibManager fibManager, IBgpManager bgpManager) {
51 this.dataBroker = dataBroker;
52 this.idManager = idManager;
53 this.vpnInterfaceMgr = vpnIfaceMgr;
54 this.fibManager = fibManager;
55 this.bgpManager = bgpManager;
58 public void setFibManager(IFibManager fibMgr) {
59 this.fibManager = fibMgr;
64 * Generate label for the given ip prefix from the associated VPN.
67 public Future<RpcResult<GenerateVpnLabelOutput>> generateVpnLabel(GenerateVpnLabelInput input) {
68 String vpnName = input.getVpnName();
69 String ipPrefix = input.getIpPrefix();
70 SettableFuture<RpcResult<GenerateVpnLabelOutput>> futureResult = SettableFuture.create();
71 String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
72 long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
73 VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
75 String msg = "Could not retrieve the label for prefix " + ipPrefix + " in VPN " + vpnName;
77 futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
80 GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
81 futureResult.set(RpcResultBuilder.success(output).build());
87 * Remove label for the given ip prefix from the associated VPN.
90 public Future<RpcResult<Void>> removeVpnLabel(RemoveVpnLabelInput input) {
91 String vpnName = input.getVpnName();
92 String ipPrefix = input.getIpPrefix();
93 String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
94 SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
95 VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
96 VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
97 futureResult.set(RpcResultBuilder.<Void>success().build());
101 private Collection<RpcError> validateAddStaticRouteInput(AddStaticRouteInput input) {
102 Collection<RpcError> rpcErrors = new ArrayList<>();
103 String destination = input.getDestination();
104 String vpnInstanceName = input.getVpnInstanceName();
105 String nexthop = input.getNexthop();
106 if (destination == null || destination.isEmpty()) {
107 String message = "destination parameter is mandatory";
108 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
110 if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
111 String message = "vpnInstanceName parameter is mandatory";
112 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
114 if (nexthop == null || nexthop.isEmpty()) {
115 String message = "nexthop parameter is mandatory";
116 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
121 // TODO Clean up the exception handling
122 @SuppressWarnings("checkstyle:IllegalCatch")
124 public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
126 SettableFuture<RpcResult<AddStaticRouteOutput>> result = SettableFuture.create();
127 String destination = input.getDestination();
128 String vpnInstanceName = input.getVpnInstanceName();
129 String nexthop = input.getNexthop();
130 Long label = input.getLabel();
131 LOG.info("Adding static route for Vpn {} with destination {}, nexthop {} and label {}",
132 vpnInstanceName, destination, nexthop, label);
134 Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
135 if (!rpcErrors.isEmpty()) {
136 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
140 if (label == null || label == 0) {
141 label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
142 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
144 String message = "Unable to retrieve a new Label for the new Route";
145 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
151 String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
152 VpnInstanceOpDataEntry vpnOpEntry = VpnUtil.getVpnInstanceOpData(dataBroker, vpnRd);
153 Boolean isVxlan = VpnUtil.isL3VpnOverVxLan(vpnOpEntry.getL3vni());
154 VrfEntry.EncapType encapType = VpnUtil.getEncapType(isVxlan);
156 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
157 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
162 Optional<InterVpnLinkDataComposite> optIVpnLink = InterVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
163 if (optIVpnLink.isPresent()) {
165 InterVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop,
167 dataBroker, fibManager, bgpManager);
168 } catch (Exception e) {
169 String errMsg = "Could not advertise route [vpn=" + vpnRd + ", prefix=" + destination + ", label="
170 + label + ", nexthop=" + nexthop + ", ] to BGP. Reason: " + e.getMessage();
172 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION, errMsg)
177 vpnInterfaceMgr.addExtraRoute(vpnInstanceName, destination, nexthop, vpnRd, null /* routerId */,
178 label.intValue(), vpnOpEntry.getL3vni(), RouteOrigin.STATIC, null /* intfName */,
179 null /*Adjacency*/, encapType, null);
182 AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
183 result.set(RpcResultBuilder.success(labelOutput).build());
187 private Collection<RpcError> validateRemoveStaticRouteInput(RemoveStaticRouteInput input) {
188 Collection<RpcError> rpcErrors = new ArrayList<>();
189 String destination = input.getDestination();
190 String vpnInstanceName = input.getVpnInstanceName();
191 String nexthop = input.getNexthop();
192 if (destination == null || destination.isEmpty()) {
193 String message = "destination parameter is mandatory";
194 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
196 if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
197 String message = "vpnInstanceName parameter is mandatory";
198 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
200 if (nexthop == null || nexthop.isEmpty()) {
201 String message = "nexthop parameter is mandatory";
202 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
208 public Future<RpcResult<Void>> removeStaticRoute(RemoveStaticRouteInput input) {
210 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
212 String destination = input.getDestination();
213 String vpnInstanceName = input.getVpnInstanceName();
214 String nexthop = input.getNexthop();
215 LOG.info("Removing static route with destination={}, nexthop={} in VPN={}",
216 destination, nexthop, vpnInstanceName);
217 Collection<RpcError> rpcErrors = validateRemoveStaticRouteInput(input);
218 if (!rpcErrors.isEmpty()) {
219 result.set(RpcResultBuilder.<Void>failed().withRpcErrors(rpcErrors).build());
223 String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
225 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
226 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, message).build());
230 Optional<InterVpnLinkDataComposite> optVpnLink = InterVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
231 if (optVpnLink.isPresent()) {
232 fibManager.removeOrUpdateFibEntry(dataBroker, vpnRd, destination, nexthop, /*writeTx*/ null);
233 bgpManager.withdrawPrefix(vpnRd, destination);
235 vpnInterfaceMgr.delExtraRoute(vpnInstanceName, destination,
236 nexthop, vpnRd, null /* routerId */, null /* intfName */, null);
238 result.set(RpcResultBuilder.<Void>success().build());