Use Java declarations instead of Google Collections
[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.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;
39
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;
47
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;
55     }
56
57     public void setFibManager(IFibManager fibMgr) {
58         this.fibManager = fibMgr;
59     }
60
61
62     /**
63      * Generate label for the given ip prefix from the associated VPN.
64      */
65     @Override
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));
73         if (label == 0) {
74             String msg = "Could not retrieve the label for prefix " + ipPrefix + " in VPN " + vpnName;
75             LOG.error(msg);
76             futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
77                 .build());
78         } else {
79             GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
80             futureResult.set(RpcResultBuilder.success(output).build());
81         }
82         return futureResult;
83     }
84
85     /**
86      * Remove label for the given ip prefix from the associated VPN.
87      */
88     @Override
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());
97         return futureResult;
98     }
99
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));
108         }
109         if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
110             String message = "vpnInstanceName parameter is mandatory";
111             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
112         }
113         if (nexthop == null || nexthop.isEmpty()) {
114             String message = "nexthop parameter is mandatory";
115             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
116         }
117         return rpcErrors;
118     }
119
120     // TODO Clean up the exception handling
121     @SuppressWarnings("checkstyle:IllegalCatch")
122     @Override
123     public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
124
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);
132
133         Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
134         if (!rpcErrors.isEmpty()) {
135             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
136             return result;
137         }
138
139         if (label == null || label == 0) {
140             label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
141                 VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
142             if (label == 0) {
143                 String message = "Unable to retrieve a new Label for the new Route";
144                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
145                     message).build());
146                 return result;
147             }
148         }
149
150         String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
151         if (vpnRd == null) {
152             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
153             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
154                 message).build());
155             return result;
156         }
157
158         Optional<InterVpnLinkDataComposite> optIVpnLink = InterVpnLinkCache.getInterVpnLinkByEndpoint(nexthop);
159         if (optIVpnLink.isPresent()) {
160
161             try {
162                 InterVpnLinkUtil.handleStaticRoute(optIVpnLink.get(), vpnInstanceName, destination, nexthop,
163                                                    label.intValue(),
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();
168                 LOG.warn(errMsg, e);
169                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(ErrorType.APPLICATION, errMsg)
170                       .build());
171                 return result;
172             }
173         } else {
174             vpnInterfaceMgr.addExtraRoute(destination, nexthop, vpnRd, null /*routerId */, label.intValue(),
175                 RouteOrigin.STATIC, null /* intfName */, null, null);
176         }
177
178         AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
179         result.set(RpcResultBuilder.success(labelOutput).build());
180         return result;
181     }
182
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));
191         }
192         if (vpnInstanceName == null || vpnInstanceName.isEmpty()) {
193             String message = "vpnInstanceName parameter is mandatory";
194             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
195         }
196         if (nexthop == null || nexthop.isEmpty()) {
197             String message = "nexthop parameter is mandatory";
198             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
199         }
200         return rpcErrors;
201     }
202
203     @Override
204     public Future<RpcResult<Void>> removeStaticRoute(RemoveStaticRouteInput input) {
205
206         SettableFuture<RpcResult<Void>> result = SettableFuture.create();
207
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());
216             return result;
217         }
218
219         String vpnRd = VpnUtil.getVpnRd(dataBroker, input.getVpnInstanceName());
220         if (vpnRd == null) {
221             String message = "Could not find Route-Distinguisher for VpnName " + vpnInstanceName;
222             result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, message).build());
223             return result;
224         }
225
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);
230         } else {
231             vpnInterfaceMgr.delExtraRoute(destination, nexthop, vpnRd, null /*routerId*/, null /*intfName*/, null);
232         }
233         result.set(RpcResultBuilder.<Void>success().build());
234
235         return result;
236     }
237
238 }