Bug 8346 - Conflicting modification for vpnNextHops.
[netvirt.git] / vpnservice / 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 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;
40
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;
48
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;
56     }
57
58     public void setFibManager(IFibManager fibMgr) {
59         this.fibManager = fibMgr;
60     }
61
62
63     /**
64      * Generate label for the given ip prefix from the associated VPN.
65      */
66     @Override
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));
74         if (label == 0) {
75             String msg = "Could not retrieve the label for prefix " + ipPrefix + " in VPN " + vpnName;
76             LOG.error(msg);
77             futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
78                 .build());
79         } else {
80             GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
81             futureResult.set(RpcResultBuilder.success(output).build());
82         }
83         return futureResult;
84     }
85
86     /**
87      * Remove label for the given ip prefix from the associated VPN.
88      */
89     @Override
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());
98         return futureResult;
99     }
100
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));
109         }
110         if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
111             String message = "vpnInstanceName parameter is mandatory";
112             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
113         }
114         if (nexthop == null || nexthop.isEmpty()) {
115             String message = "nexthop parameter is mandatory";
116             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
117         }
118         return rpcErrors;
119     }
120
121     // TODO Clean up the exception handling
122     @SuppressWarnings("checkstyle:IllegalCatch")
123     @Override
124     public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
125
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);
133
134         Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
135         if (!rpcErrors.isEmpty()) {
136             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
137             return result;
138         }
139
140         if (label == null || label == 0) {
141             label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
142                 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
143             if (label == 0) {
144                 String message = "Unable to retrieve a new Label for the new Route";
145                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
146                     message).build());
147                 return result;
148             }
149         }
150
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);
155         if (vpnRd == null) {
156             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
157             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
158                 message).build());
159             return result;
160         }
161
162         Optional<InterVpnLinkDataComposite> optIVpnLink = InterVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
163         if (optIVpnLink.isPresent()) {
164             try {
165                 InterVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop,
166                                                    label.intValue(),
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();
171                 LOG.warn(errMsg, e);
172                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION, errMsg)
173                       .build());
174                 return result;
175             }
176         } else {
177             vpnInterfaceMgr.addExtraRoute(vpnInstanceName, destination, nexthop, vpnRd, null /* routerId */,
178                     label.intValue(), vpnOpEntry.getL3vni(), RouteOrigin.STATIC, null /* intfName */,
179                             null /*Adjacency*/, encapType, null);
180         }
181
182         AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
183         result.set(RpcResultBuilder.success(labelOutput).build());
184         return result;
185     }
186
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));
195         }
196         if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
197             String message = "vpnInstanceName parameter is mandatory";
198             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
199         }
200         if (nexthop == null || nexthop.isEmpty()) {
201             String message = "nexthop parameter is mandatory";
202             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
203         }
204         return rpcErrors;
205     }
206
207     @Override
208     public Future<RpcResult<Void>> removeStaticRoute(RemoveStaticRouteInput input) {
209
210         SettableFuture<RpcResult<Void>> result = SettableFuture.create();
211
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());
220             return result;
221         }
222
223         String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
224         if (vpnRd == null) {
225             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
226             result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, message).build());
227             return result;
228         }
229
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);
234         } else {
235             vpnInterfaceMgr.delExtraRoute(vpnInstanceName, destination,
236                     nexthop, vpnRd, null /* routerId */, null /* intfName */, null);
237         }
238         result.set(RpcResultBuilder.<Void>success().build());
239
240         return result;
241     }
242
243 }