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.util.concurrent.SettableFuture;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.netvirt.bgpmanager.api.RouteOrigin;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.*;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.concurrent.Future;
27 public class VpnRpcServiceImpl implements VpnRpcService {
29 private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
30 private IdManagerService idManager;
31 private VpnInterfaceManager vpnInterfaceMgr;
32 private DataBroker dataBroker;
34 public VpnRpcServiceImpl(IdManagerService idManager, VpnInterfaceManager vpnIfaceMgr, DataBroker dataBroker) {
35 this.idManager = idManager;
36 this.vpnInterfaceMgr = vpnIfaceMgr;
37 this.dataBroker = dataBroker;
41 * to generate label for the given ip prefix from the associated VPN
45 public Future<RpcResult<GenerateVpnLabelOutput>> generateVpnLabel(GenerateVpnLabelInput input) {
46 String vpnName = input.getVpnName();
47 String ipPrefix = input.getIpPrefix();
48 SettableFuture<RpcResult<GenerateVpnLabelOutput>> futureResult = SettableFuture.create();
49 String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
50 long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
51 VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
53 String msg = String.format("Could not retrieve the label for prefix {} in VPN {}", ipPrefix, vpnName);
55 futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
58 GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
59 futureResult.set(RpcResultBuilder.success(output).build());
65 * to remove label for the given ip prefix from the associated VPN
69 public Future<RpcResult<Void>> removeVpnLabel(RemoveVpnLabelInput input) {
70 String vpnName = input.getVpnName();
71 String ipPrefix = input.getIpPrefix();
72 String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
73 SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
74 VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
75 VpnUtil.getNextHopLabelKey((rd != null) ? rd : vpnName, ipPrefix));
76 futureResult.set(RpcResultBuilder.<Void>success().build());
80 private Collection<RpcError> validateAddStaticRouteInput(AddStaticRouteInput input) {
81 Collection<RpcError> rpcErrors = new ArrayList<RpcError>();
82 String destination = input.getDestination();
83 String vpnInstanceName = input.getVpnInstanceName();
84 String nexthop = input.getNexthop();
85 if ( destination == null || destination.isEmpty() ) {
86 String message = "destination parameter is mandatory";
87 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
89 if ( vpnInstanceName == null || vpnInstanceName.isEmpty() ) {
90 String message = "vpnInstanceName parameter is mandatory";
91 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
93 if ( nexthop == null || nexthop.isEmpty() ) {
94 String message = "nexthop parameter is mandatory";
95 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
101 public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
103 SettableFuture<RpcResult<AddStaticRouteOutput>> result = SettableFuture.create();
104 String destination = input.getDestination();
105 String vpnInstanceName = input.getVpnInstanceName();
106 String nexthop = input.getNexthop();
107 Long label = input.getLabel();
108 LOG.info("Adding static route for Vpn {} with destination {}, nexthop {} and label {}",
109 vpnInstanceName, destination, nexthop, label);
111 Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
112 if ( !rpcErrors.isEmpty() ) {
113 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
117 if ( label == null || label == 0 ) {
118 label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
119 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
121 String message = "Unable to retrieve a new Label for the new Route";
122 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
128 String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
129 if ( vpnRd == null ) {
130 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
131 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
136 InterVpnLink interVpnLink = VpnUtil.getInterVpnLinkByEndpointIp(dataBroker, nexthop);
137 if ( interVpnLink != null ) {
138 // A static route pointing to an InterVpnLink endpoint: just write the VrfEntry
139 VpnUtil.addFibEntryToDS(dataBroker, vpnRd, destination, nexthop, label.intValue(), RouteOrigin.STATIC , null);
141 vpnInterfaceMgr.addExtraRoute(destination, nexthop, vpnRd, null /*routerId */, label.intValue(),
142 null /* intfName */);
145 AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
146 result.set(RpcResultBuilder.success(labelOutput).build());
150 private Collection<RpcError> validateRemoveStaticRouteInput(RemoveStaticRouteInput input) {
151 Collection<RpcError> rpcErrors = new ArrayList<RpcError>();
152 String destination = input.getDestination();
153 String vpnInstanceName = input.getVpnInstanceName();
154 String nexthop = input.getNexthop();
155 if ( destination == null || destination.isEmpty() ) {
156 String message = "destination parameter is mandatory";
157 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
159 if ( vpnInstanceName == null || vpnInstanceName.isEmpty() ) {
160 String message = "vpnInstanceName parameter is mandatory";
161 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
163 if ( nexthop == null || nexthop.isEmpty() ) {
164 String message = "nexthop parameter is mandatory";
165 rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
171 public Future<RpcResult<Void>> removeStaticRoute(RemoveStaticRouteInput input) {
173 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
175 String destination = input.getDestination();
176 String vpnInstanceName = input.getVpnInstanceName();
177 String nexthop = input.getNexthop();
178 LOG.info("Removing static route with destination={}, nexthop={} in VPN={}",
179 destination, nexthop, vpnInstanceName);
180 Collection<RpcError> rpcErrors = validateRemoveStaticRouteInput(input);
181 if ( !rpcErrors.isEmpty() ) {
182 result.set(RpcResultBuilder.<Void>failed().withRpcErrors(rpcErrors).build());
186 String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
187 if ( vpnRd == null ) {
188 String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
189 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, message).build());
193 InterVpnLink interVpnLink = VpnUtil.getInterVpnLinkByEndpointIp(dataBroker, nexthop);
194 if ( interVpnLink != null ) {
195 // A static route pointing to an InterVpnLink endpoint: just remove the VrfEntry from DS
196 VpnUtil.removeFibEntryFromDS(dataBroker, vpnRd, destination, nexthop, null);
198 vpnInterfaceMgr.delExtraRoute(destination, nexthop, vpnRd, null /*routerId*/, null /*intfName*/);
200 result.set(RpcResultBuilder.<Void>success().build());