Multiple fixes in various modules
[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.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;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.concurrent.Future;
26
27 public class VpnRpcServiceImpl implements VpnRpcService {
28
29     private static final Logger LOG = LoggerFactory.getLogger(VpnRpcServiceImpl.class);
30     private IdManagerService idManager;
31     private VpnInterfaceManager vpnInterfaceMgr;
32     private DataBroker dataBroker;
33
34     public VpnRpcServiceImpl(IdManagerService idManager, VpnInterfaceManager vpnIfaceMgr, DataBroker dataBroker) {
35         this.idManager = idManager;
36         this.vpnInterfaceMgr = vpnIfaceMgr;
37         this.dataBroker = dataBroker;
38     }
39
40     /**
41      * to generate label for the given ip prefix from the associated VPN
42      *
43      */
44     @Override
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));
52         if (label == 0) {
53             String msg = String.format("Could not retrieve the label for prefix {} in VPN {}", ipPrefix, vpnName);
54             LOG.error(msg);
55             futureResult.set(RpcResultBuilder.<GenerateVpnLabelOutput>failed().withError(ErrorType.APPLICATION, msg)
56                                                                               .build());
57         } else {
58             GenerateVpnLabelOutput output = new GenerateVpnLabelOutputBuilder().setLabel(label).build();
59             futureResult.set(RpcResultBuilder.success(output).build());
60         }
61         return futureResult;
62     }
63
64     /**
65      * to remove label for the given ip prefix from the associated VPN
66      *
67      */
68     @Override
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());
77         return futureResult;
78     }
79
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));
88         }
89         if ( vpnInstanceName == null || vpnInstanceName.isEmpty() ) {
90             String message = "vpnInstanceName parameter is mandatory";
91             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
92         }
93         if ( nexthop == null || nexthop.isEmpty() ) {
94             String message = "nexthop parameter is mandatory";
95             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "addStaticRoute", message));
96         }
97         return rpcErrors;
98     }
99
100     @Override
101     public Future<RpcResult<AddStaticRouteOutput>> addStaticRoute(AddStaticRouteInput input) {
102
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);
110
111         Collection<RpcError> rpcErrors = validateAddStaticRouteInput(input);
112         if ( !rpcErrors.isEmpty() ) {
113             result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withRpcErrors(rpcErrors).build());
114             return result;
115         }
116
117         if ( label == null || label == 0 ) {
118             label = (long) VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
119                                                VpnUtil.getNextHopLabelKey(vpnInstanceName, destination));
120             if ( label == 0 ) {
121                 String message = "Unable to retrieve a new Label for the new Route";
122                 result.set(RpcResultBuilder.<AddStaticRouteOutput>failed().withError(RpcError.ErrorType.APPLICATION,
123                                                                                      message).build());
124                 return result;
125             }
126         }
127
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,
132                                                                                  message).build());
133             return result;
134         }
135
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);
140         } else {
141             vpnInterfaceMgr.addExtraRoute(destination, nexthop, vpnRd, null /*routerId */, label.intValue(),
142                                           null /* intfName */);
143         }
144
145         AddStaticRouteOutput labelOutput = new AddStaticRouteOutputBuilder().setLabel(label).build();
146         result.set(RpcResultBuilder.success(labelOutput).build());
147         return result;
148     }
149
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));
158         }
159         if ( vpnInstanceName == null || vpnInstanceName.isEmpty() ) {
160             String message = "vpnInstanceName parameter is mandatory";
161             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
162         }
163         if ( nexthop == null || nexthop.isEmpty() ) {
164             String message = "nexthop parameter is mandatory";
165             rpcErrors.add(RpcResultBuilder.newError(RpcError.ErrorType.PROTOCOL, "removeStaticRoute", message));
166         }
167         return rpcErrors;
168     }
169
170     @Override
171     public Future<RpcResult<Void>> removeStaticRoute(RemoveStaticRouteInput input) {
172
173         SettableFuture<RpcResult<Void>> result = SettableFuture.create();
174
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());
183             return result;
184         }
185
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());
190             return result;
191         }
192
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);
197         } else {
198             vpnInterfaceMgr.delExtraRoute(destination, nexthop, vpnRd, null /*routerId*/, null /*intfName*/);
199         }
200         result.set(RpcResultBuilder.<Void>success().build());
201
202         return result;
203     }
204
205 }