ELAN: skip remote unicast MACs
[netvirt.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnRpcServiceImpl.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.vpnmanager;
9
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 javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
19 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
20 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
21 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
22 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkCache;
23 import org.opendaylight.netvirt.vpnmanager.api.intervpnlink.InterVpnLinkDataComposite;
24 import org.opendaylight.netvirt.vpnmanager.intervpnlink.InterVpnLinkUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.AddStaticRouteOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.GenerateVpnLabelOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveStaticRouteInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.RemoveVpnLabelInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.vpn.rpc.rev160201.VpnRpcService;
37 import org.opendaylight.yangtools.yang.common.RpcError;
38 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 @Singleton
45 public class VpnRpcServiceImpl implements VpnRpcService {
46     private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
47     private final DataBroker dataBroker;
48     private final IdManagerService idManager;
49     private final IFibManager fibManager;
50     private final IBgpManager bgpManager;
51     private final IVpnManager vpnManager;
52     private final InterVpnLinkCache interVpnLinkCache;
53
54     @Inject
55     public VpnRpcServiceImpl(final DataBroker dataBroker, final IdManagerService idManager,
56             final IFibManager fibManager, IBgpManager bgpManager, final IVpnManager vpnManager,
57             final InterVpnLinkCache interVpnLinkCache) {
58         this.dataBroker = dataBroker;
59         this.idManager = idManager;
60         this.fibManager = fibManager;
61         this.bgpManager = bgpManager;
62         this.vpnManager = vpnManager;
63         this.interVpnLinkCache = interVpnLinkCache;
64     }
65
66     /**
67      * Generate label for the given ip prefix from the associated VPN.
68      */
69     @Override
70     public Future<RpcResult<GenerateVpnLabelOutput>> generateVpnLabel(GenerateVpnLabelInput input) {
71         String vpnName = input.getVpnName();
72         String ipPrefix = input.getIpPrefix();
73         SettableFuture<RpcResult<GenerateVpnLabelOutput>> futureResult = SettableFuture.create();
74         String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
75         long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
76             VpnUtil.getNextHopLabelKey(rd != null ? rd : vpnName, ipPrefix));
77         if (label == 0) {
78             String msg = "Could not retrieve the label for prefix " + ipPrefix + " in VPN " + vpnName;
79             LOG.error(msg);
80             futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
81                 .build());
82         } else {
83             GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
84             futureResult.set(RpcResultBuilder.success(output).build());
85         }
86         return futureResult;
87     }
88
89     /**
90      * Remove label for the given ip prefix from the associated VPN.
91      */
92     @Override
93     public Future<RpcResult<Void>> removeVpnLabel(RemoveVpnLabelInput input) {
94         String vpnName = input.getVpnName();
95         String ipPrefix = input.getIpPrefix();
96         String rd = VpnUtil.getVpnRd(dataBroker, vpnName);
97         SettableFuture<RpcResult<Void>> futureResult = SettableFuture.create();
98         VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
99             VpnUtil.getNextHopLabelKey(rd != null ? rd : vpnName, ipPrefix));
100         futureResult.set(RpcResultBuilder.<Void>success().build());
101         return futureResult;
102     }
103
104     private Collection<RpcError> validateAddStaticRouteInput(AddStaticRouteInput input) {
105         Collection<RpcError> rpcErrors = new ArrayList<>();
106         String destination = input.getDestination();
107         String vpnInstanceName = input.getVpnInstanceName();
108         String nexthop = input.getNexthop();
109         if (destination == null || destination.isEmpty()) {
110             String message = "destination parameter is mandatory";
111             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
112         }
113         if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
114             String message = "vpnInstanceName parameter is mandatory";
115             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
116         }
117         if (nexthop == null || nexthop.isEmpty()) {
118             String message = "nexthop parameter is mandatory";
119             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
120         }
121         return rpcErrors;
122     }
123
124     // TODO Clean up the exception handling
125     @SuppressWarnings("checkstyle:IllegalCatch")
126     @Override
127     public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
128
129         SettableFuture<RpcResult<AddStaticRouteOutput>> result = SettableFuture.create();
130         String destination = input.getDestination();
131         String vpnInstanceName = input.getVpnInstanceName();
132         String nexthop = input.getNexthop();
133         Long label = input.getLabel();
134         LOG.info("Adding static route for Vpn {} with destination {}, nexthop {} and label {}",
135             vpnInstanceName, destination, nexthop, label);
136
137         Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
138         if (!rpcErrors.isEmpty()) {
139             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
140             return result;
141         }
142
143         if (label == null || label == 0) {
144             label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
145                 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
146             if (label == 0) {
147                 String message = "Unable to retrieve a new Label for the new Route";
148                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
149                     message).build());
150                 return result;
151             }
152         }
153
154         String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
155         VpnInstanceOpDataEntry vpnOpEntry = VpnUtil.getVpnInstanceOpData(dataBroker, vpnRd);
156         Boolean isVxlan = VpnUtil.isL3VpnOverVxLan(vpnOpEntry.getL3vni());
157         VrfEntry.EncapType encapType = VpnUtil.getEncapType(isVxlan);
158         if (vpnRd == null) {
159             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
160             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
161                 message).build());
162             return result;
163         }
164
165         Optional<InterVpnLinkDataComposite> optIVpnLink = interVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
166         if (optIVpnLink.isPresent()) {
167             try {
168                 InterVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop,
169                                                    label.intValue(),
170                                                    dataBroker, fibManager, bgpManager);
171             } catch (Exception e) {
172                 String errMsg = "Could not advertise route [vpn=" + vpnRd + ", prefix=" + destination + ", label="
173                         + label + ", nexthop=" + nexthop + ", ] to BGP. Reason: " + e.getMessage();
174                 LOG.warn(errMsg, e);
175                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION, errMsg)
176                       .build());
177                 return result;
178             }
179         } else {
180             vpnManager.addExtraRoute(vpnInstanceName, destination, nexthop, vpnRd, null /* routerId */,
181                     label.intValue(), vpnOpEntry.getL3vni(), RouteOrigin.STATIC, null /* intfName */,
182                             null /*Adjacency*/, encapType, null);
183         }
184
185         AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
186         result.set(RpcResultBuilder.success(labelOutput).build());
187         return result;
188     }
189
190     private Collection<RpcError> validateRemoveStaticRouteInput(RemoveStaticRouteInput input) {
191         Collection<RpcError> rpcErrors = new ArrayList<>();
192         String destination = input.getDestination();
193         String vpnInstanceName = input.getVpnInstanceName();
194         String nexthop = input.getNexthop();
195         if (destination == null || destination.isEmpty()) {
196             String message = "destination parameter is mandatory";
197             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
198         }
199         if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
200             String message = "vpnInstanceName parameter is mandatory";
201             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
202         }
203         if (nexthop == null || nexthop.isEmpty()) {
204             String message = "nexthop parameter is mandatory";
205             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
206         }
207         return rpcErrors;
208     }
209
210     @Override
211     public Future<RpcResult<Void>> removeStaticRoute(RemoveStaticRouteInput input) {
212
213         SettableFuture<RpcResult<Void>> result = SettableFuture.create();
214
215         String destination = input.getDestination();
216         String vpnInstanceName = input.getVpnInstanceName();
217         String nexthop = input.getNexthop();
218         LOG.info("Removing static route with destination={}, nexthop={} in VPN={}",
219             destination, nexthop, vpnInstanceName);
220         Collection<RpcError> rpcErrors = validateRemoveStaticRouteInput(input);
221         if (!rpcErrors.isEmpty()) {
222             result.set(RpcResultBuilder.<Void>failed().withRpcErrors(rpcErrors).build());
223             return result;
224         }
225
226         String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
227         if (vpnRd == null) {
228             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
229             result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, message).build());
230             return result;
231         }
232
233         Optional<InterVpnLinkDataComposite> optVpnLink = interVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
234         if (optVpnLink.isPresent()) {
235             fibManager.removeOrUpdateFibEntry(dataBroker,  vpnRd, destination, nexthop, /*writeTx*/ null);
236             bgpManager.withdrawPrefix(vpnRd, destination);
237         } else {
238             vpnManager.delExtraRoute(vpnInstanceName, destination,
239                     nexthop, vpnRd, null /* routerId */, null /* intfName */, null);
240         }
241         result.set(RpcResultBuilder.<Void>success().build());
242
243         return result;
244     }
245 }