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.vpnservice.neutronvpn;
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.SettableFuture;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
16 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
17 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
18 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
19 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargets;
20 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargetsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
22 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets
24 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTargetKey;
25 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceBuilder;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.vpn.instance
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
31 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceBuilder;
32 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.AdjacenciesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.LockManagerService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateRouterInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNOutputBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksOutput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksOutputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateRouterInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNInputBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNOutput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNOutputBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.L3vpnInstance;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.NeutronvpnService;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.Subnetmaps;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.VpnMaps;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.createl3vpn.input.L3vpn;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.getl3vpn.output.L3vpnInstances;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.getl3vpn.output
75 .L3vpnInstancesBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.Subnetmap;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.SubnetmapBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMap;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMapBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMapKey;
82 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
83 import org.opendaylight.yangtools.yang.common.RpcError;
84 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
85 import org.opendaylight.yangtools.yang.common.RpcResult;
86 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
87 import org.slf4j.Logger;
88 import org.slf4j.LoggerFactory;
90 import java.util.ArrayList;
91 import java.util.Arrays;
92 import java.util.List;
93 import java.util.concurrent.ExecutionException;
94 import java.util.concurrent.Future;
96 public class NeutronvpnManager implements NeutronvpnService, AutoCloseable {
98 private static final Logger logger = LoggerFactory.getLogger(NeutronvpnManager.class);
99 private final DataBroker broker;
100 private LockManagerService lockManager;
101 IMdsalApiManager mdsalUtil;
104 * @param db - dataBroker reference
105 * @param mdsalManager - MDSAL Util API access
107 public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager) {
109 mdsalUtil = mdsalManager;
112 public void setLockManager(LockManagerService lockManager) {
113 this.lockManager = lockManager;
117 public void close() throws Exception {
118 logger.info("Neutron VPN Manager Closed");
121 protected Subnetmap updateSubnetNode(Uuid subnetId, Uuid tenantId, Uuid networkId, Uuid routerId, Uuid vpnId,
123 Subnetmap subnetmap = null;
124 SubnetmapBuilder builder = null;
125 boolean isLockAcquired = false;
126 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
127 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
129 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
130 logger.debug("updating Subnet :read: ");
131 if (sn.isPresent()) {
132 builder = new SubnetmapBuilder(sn.get());
133 logger.debug("updating Subnet :existing: ");
135 builder = new SubnetmapBuilder().setKey(new SubnetmapKey(subnetId)).setId(subnetId);
136 logger.debug("updating Subnet :new: ");
139 if (routerId != null) {
140 builder.setRouterId(routerId);
142 if (networkId != null) {
143 builder.setNetworkId(networkId);
146 builder.setVpnId(vpnId);
148 if (tenantId != null) {
149 builder.setTenantId(tenantId);
152 if (portId != null) {
153 List<Uuid> portList = builder.getPortList();
154 if (portList == null) {
155 portList = new ArrayList<Uuid>();
157 portList.add(portId);
158 builder.setPortList(portList);
161 subnetmap = builder.build();
162 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
163 logger.debug("Creating/Updating subnetMap node: {} ", subnetId.getValue());
164 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
165 } catch (Exception e) {
166 logger.error("Updation of subnetMap failed for node: {}", subnetId.getValue());
168 if (isLockAcquired) {
169 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
175 protected Subnetmap removeFromSubnetNode(Uuid subnetId, Uuid networkId, Uuid routerId, Uuid vpnId, Uuid portId) {
176 Subnetmap subnetmap = null;
177 boolean isLockAcquired = false;
178 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
179 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
181 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
182 if (sn.isPresent()) {
183 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
184 if (routerId != null) {
185 builder.setRouterId(null);
187 if (networkId != null) {
188 builder.setNetworkId(null);
191 builder.setVpnId(null);
193 if (portId != null && builder.getPortList() != null) {
194 List<Uuid> portList = builder.getPortList();
195 portList.remove(portId);
196 builder.setPortList(portList);
199 subnetmap = builder.build();
200 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
201 logger.debug("Removing from existing subnetmap node: {} ", subnetId.getValue());
202 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
204 logger.warn("removing from non-existing subnetmap node: {} ", subnetId.getValue());
206 } catch (Exception e) {
207 logger.error("Removal from subnetmap failed for node: {}", subnetId.getValue());
209 if (isLockAcquired) {
210 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
216 private void updateVpnInstanceNode(String vpnName, List<String> rd, List<String> irt, List<String> ert) {
218 VpnInstanceBuilder builder = null;
219 List<VpnTarget> vpnTargetList = new ArrayList<VpnTarget>();
220 boolean isLockAcquired = false;
221 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
222 child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
224 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
226 logger.debug("Creating/Updating a new vpn-instance node: {} ", vpnName);
227 if (optionalVpn.isPresent()) {
228 builder = new VpnInstanceBuilder(optionalVpn.get());
229 logger.debug("updating existing vpninstance node");
231 builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(vpnName)).setVpnInstanceName(vpnName);
233 if (irt != null && !irt.isEmpty()) {
234 if (ert != null && !ert.isEmpty()) {
235 List<String> commonRT = new ArrayList<String>(irt);
236 commonRT.retainAll(ert);
238 for (String common : commonRT) {
241 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(common)).setVrfRTValue
242 (common).setVrfRTType(VpnTarget.VrfRTType.Both).build();
243 vpnTargetList.add(vpnTarget);
246 for (String importRT : irt) {
247 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(importRT)).setVrfRTValue
248 (importRT).setVrfRTType(VpnTarget.VrfRTType.ImportExtcommunity).build();
249 vpnTargetList.add(vpnTarget);
253 if (ert != null && !ert.isEmpty()) {
254 for (String exportRT : ert) {
255 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(exportRT)).setVrfRTValue
256 (exportRT).setVrfRTType(VpnTarget.VrfRTType.ExportExtcommunity).build();
257 vpnTargetList.add(vpnTarget);
261 VpnTargets vpnTargets = new VpnTargetsBuilder().setVpnTarget(vpnTargetList).build();
263 Ipv4FamilyBuilder ipv4vpnBuilder = new Ipv4FamilyBuilder().setVpnTargets(vpnTargets);
265 if (rd != null && !rd.isEmpty()) {
266 ipv4vpnBuilder.setRouteDistinguisher(rd.get(0));
269 VpnInstance newVpn = builder.setIpv4Family(ipv4vpnBuilder.build()).build();
270 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnName);
271 logger.debug("Creating/Updating vpn-instance for {} ", vpnName);
272 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier, newVpn);
273 } catch (Exception e) {
274 logger.error("Update VPN Instance node failed for node: {} {} {} {}", vpnName, rd, irt, ert);
276 if (isLockAcquired) {
277 NeutronvpnUtils.unlock(lockManager, vpnName);
282 private void deleteVpnMapsNode(Uuid vpnid) {
283 boolean isLockAcquired = false;
284 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
285 .child(VpnMap.class, new VpnMapKey(vpnid)).build();
286 logger.debug("removing vpnMaps node: {} ", vpnid.getValue());
288 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnid.getValue());
289 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
290 } catch (Exception e) {
291 logger.error("Delete vpnMaps node failed for vpn : {} ", vpnid.getValue());
293 if (isLockAcquired) {
294 NeutronvpnUtils.unlock(lockManager, vpnid.getValue());
299 private void updateVpnMaps(Uuid vpnId, String name, Uuid router, Uuid tenantId, List<Uuid> networks) {
300 VpnMapBuilder builder;
301 boolean isLockAcquired = false;
302 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
303 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
305 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
307 if (optionalVpnMap.isPresent()) {
308 builder = new VpnMapBuilder(optionalVpnMap.get());
310 builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId);
314 builder.setName(name);
316 if (tenantId != null) {
317 builder.setTenantId(tenantId);
319 if (router != null) {
320 builder.setRouterId(router);
322 if (networks != null) {
323 List<Uuid> nwList = builder.getNetworkIds();
324 if (nwList == null) {
325 nwList = new ArrayList<Uuid>();
327 nwList.addAll(networks);
328 builder.setNetworkIds(nwList);
331 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
332 logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue());
333 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build());
334 logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue());
335 } catch (Exception e) {
336 logger.error("UpdateVpnMaps failed for node: {} ", vpnId.getValue());
338 if (isLockAcquired) {
339 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
344 private void clearFromVpnMaps(Uuid vpnId, Uuid routerId, List<Uuid> networkIds) {
345 boolean isLockAcquired = false;
346 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
347 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
348 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
350 if (optionalVpnMap.isPresent()) {
351 VpnMap vpnMap = optionalVpnMap.get();
352 VpnMapBuilder vpnMapBuilder = new VpnMapBuilder(vpnMap);
353 if (routerId != null) {
354 if (vpnMap.getNetworkIds() == null && routerId.equals(vpnMap.getVpnId())) {
356 // remove entire node in case of internal VPN
357 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
358 logger.debug("removing vpnMaps node: {} ", vpnId);
359 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
360 } catch (Exception e) {
361 logger.error("Deletion of vpnMaps node failed for vpn {}", vpnId.getValue());
363 if (isLockAcquired) {
364 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
369 vpnMapBuilder.setRouterId(null);
371 if (networkIds != null) {
372 List<Uuid> vpnNw = vpnMap.getNetworkIds();
373 for (Uuid nw : networkIds) {
376 if (vpnNw.isEmpty()) {
377 logger.debug("setting networks null in vpnMaps node: {} ", vpnId.getValue());
378 vpnMapBuilder.setNetworkIds(null);
380 vpnMapBuilder.setNetworkIds(vpnNw);
385 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
386 logger.debug("clearing from vpnMaps node: {} ", vpnId.getValue());
387 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build
389 } catch (Exception e) {
390 logger.error("Clearing from vpnMaps node failed for vpn {}", vpnId.getValue());
392 if (isLockAcquired) {
393 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
397 logger.error("VPN : {} not found", vpnId.getValue());
399 logger.debug("Clear from VPNMaps DS successful for VPN {} ", vpnId.getValue());
402 private void deleteVpnInstance(Uuid vpnId) {
403 boolean isLockAcquired = false;
404 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
405 child(VpnInstance.class, new VpnInstanceKey(vpnId.getValue())).build();
407 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
408 logger.debug("Deleting vpnInstance {}", vpnId.getValue());
409 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
410 } catch (Exception e) {
411 logger.error("Deletion of VPNInstance node failed for VPN {}", vpnId.getValue());
413 if (isLockAcquired) {
414 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
419 protected void createVpnInterface(Uuid vpnId, Port port) {
420 boolean isLockAcquired = false;
421 if (vpnId == null || port == null) {
424 String portname = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
425 List<Adjacency> adjList = new ArrayList<Adjacency>();
426 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
427 child(VpnInterface.class, new VpnInterfaceKey(portname)).build();
428 // find router associated to vpn
429 Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId);
431 if (routerId != null) {
432 rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
434 // find all subnets to which this port is associated
435 List<FixedIps> ips = port.getFixedIps();
436 // create adjacency list
437 for (FixedIps ip : ips) {
438 // create vm adjacency
439 StringBuilder IpPrefixBuild = new StringBuilder(ip.getIpAddress().getIpv4Address().getValue());
440 String IpPrefix = IpPrefixBuild.append("/32").toString();
441 Adjacency vmAdj = new AdjacencyBuilder().setKey(new AdjacencyKey(IpPrefix)).setIpAddress(IpPrefix)
442 .setMacAddress(port.getMacAddress()).build();
444 // create extra route adjacency
445 if (rtr != null && rtr.getRoutes() != null) {
446 List<String> routeList = rtr.getRoutes();
447 List<Adjacency> erAdjList = addAdjacencyforExtraRoute(routeList, false, portname);
448 if (erAdjList != null && !erAdjList.isEmpty()) {
449 adjList.addAll(erAdjList);
453 // create vpn-interface on this neutron port
454 Adjacencies adjs = new AdjacenciesBuilder().setAdjacency(adjList).build();
455 VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(portname)).
456 setName(portname).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs);
457 VpnInterface vpnIf = vpnb.build();
460 isLockAcquired = NeutronvpnUtils.lock(lockManager, portname);
461 logger.debug("Creating vpn interface {}", vpnIf);
462 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
463 } catch (Exception ex) {
464 logger.error("Creation of vpninterface {} failed due to {}", portname, ex);
466 if (isLockAcquired) {
467 NeutronvpnUtils.unlock(lockManager, portname);
472 protected void deleteVpnInterface(Port port) {
475 boolean isLockAcquired = false;
476 String pname = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
477 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
478 child(VpnInterface.class, new VpnInterfaceKey(pname)).build();
480 isLockAcquired = NeutronvpnUtils.lock(lockManager, pname);
481 logger.debug("Deleting vpn interface {}", pname);
482 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
483 } catch (Exception ex) {
484 logger.error("Deletion of vpninterface {} failed due to {}", pname, ex);
486 if (isLockAcquired) {
487 NeutronvpnUtils.unlock(lockManager, pname);
493 public void createL3Vpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
494 Uuid router, List<Uuid> networks) {
496 // Update VPN Instance node
497 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
499 // Update local vpn-subnet DS
500 updateVpnMaps(vpn, name, router, tenant, networks);
502 if (router != null) {
503 associateRouterToVpn(vpn, router);
505 if (networks != null) {
506 associateNetworksToVpn(vpn, networks);
511 public Future<RpcResult<CreateL3VPNOutput>> createL3VPN(CreateL3VPNInput input) {
513 CreateL3VPNOutputBuilder opBuilder = new CreateL3VPNOutputBuilder();
514 SettableFuture<RpcResult<CreateL3VPNOutput>> result = SettableFuture.create();
515 List<RpcError> errorList = new ArrayList<RpcError>();
516 int failurecount = 0;
517 int warningcount = 0;
519 List<L3vpn> vpns = input.getL3vpn();
520 for (L3vpn vpn : vpns) {
523 if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
524 msg = String.format("Creation of L3VPN failed for VPN %s due to absence of RD/iRT/eRT input",
525 vpn.getId().getValue());
527 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
528 errorList.add(error);
532 if (vpn.getRouteDistinguisher().size() > 1) {
533 msg = String.format("Creation of L3VPN failed for VPN %s due to multiple RD input %s",
534 vpn.getId().getValue(), vpn.getRouteDistinguisher());
536 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
537 errorList.add(error);
542 createL3Vpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(),
543 vpn.getImportRT(), vpn.getExportRT(), vpn.getRouterId(), vpn.getNetworkIds());
544 } catch (Exception ex) {
545 msg = String.format("Creation of L3VPN failed for VPN %s", vpn.getId().getValue());
546 logger.error(msg, ex.getMessage());
547 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
548 errorList.add(error);
552 // if at least one succeeds; result is success
553 // if none succeeds; result is failure
554 if (failurecount + warningcount == vpns.size()) {
555 result.set(RpcResultBuilder.<CreateL3VPNOutput>failed().withRpcErrors(errorList).build());
557 List<String> errorResponseList = new ArrayList<>();
558 if (!errorList.isEmpty()) {
559 for (RpcError rpcError : errorList) {
560 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
561 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
562 errorResponseList.add(errorResponse);
565 errorResponseList.add("Operation successful with no errors");
567 opBuilder.setResponse(errorResponseList);
568 result.set(RpcResultBuilder.<CreateL3VPNOutput>success().withResult(opBuilder.build()).build());
574 public Future<RpcResult<GetL3VPNOutput>> getL3VPN(GetL3VPNInput input) {
576 GetL3VPNOutputBuilder opBuilder = new GetL3VPNOutputBuilder();
577 SettableFuture<RpcResult<GetL3VPNOutput>> result = SettableFuture.create();
578 Uuid inputVpnId = input.getId();
579 List<VpnInstance> vpns = new ArrayList<VpnInstance>();
582 if (inputVpnId == null) {
584 InstanceIdentifier<VpnInstances> vpnsIdentifier =
585 InstanceIdentifier.builder(VpnInstances.class).build();
586 Optional<VpnInstances> optionalVpns = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
588 if (optionalVpns.isPresent()) {
589 for (VpnInstance vpn : optionalVpns.get().getVpnInstance()) {
594 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
595 .withWarning(ErrorType.PROTOCOL, "", "No VPN is present").build());
599 String name = inputVpnId.getValue();
600 InstanceIdentifier<VpnInstance> vpnIdentifier =
601 InstanceIdentifier.builder(VpnInstances.class)
602 .child(VpnInstance.class, new VpnInstanceKey(name)).build();
603 // read VpnInstance Info
604 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
606 if (optionalVpn.isPresent()) {
607 vpns.add(optionalVpn.get());
609 String message = String.format("GetL3VPN failed because VPN %s is not present", name);
610 logger.error(message);
611 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
612 .withWarning(ErrorType.PROTOCOL, "invalid-value", message).build());
615 List<L3vpnInstances> l3vpnList = new ArrayList<L3vpnInstances>();
616 for (VpnInstance vpnInstance : vpns) {
617 Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName());
619 InstanceIdentifier<VpnMap> vpnMapIdentifier =
620 InstanceIdentifier.builder(VpnMaps.class)
621 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
622 L3vpnInstancesBuilder l3vpn = new L3vpnInstancesBuilder();
624 List<String> rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(","));
625 List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
627 List<String> ertList = new ArrayList<String>();
628 List<String> irtList = new ArrayList<String>();
630 for (VpnTarget vpnTarget : vpnTargetList) {
631 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
632 ertList.add(vpnTarget.getVrfRTValue());
634 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ImportExtcommunity) {
635 irtList.add(vpnTarget.getVrfRTValue());
637 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.Both) {
638 ertList.add(vpnTarget.getVrfRTValue());
639 irtList.add(vpnTarget.getVrfRTValue());
643 l3vpn.setId(vpnId).setRouteDistinguisher(rd).setImportRT(irtList).setExportRT(ertList);
644 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
646 if (optionalVpnMap.isPresent()) {
647 VpnMap vpnMap = optionalVpnMap.get();
648 l3vpn.setRouterId(vpnMap.getRouterId()).setNetworkIds(vpnMap.getNetworkIds())
649 .setTenantId(vpnMap.getTenantId()).setName(vpnMap.getName());
651 l3vpnList.add(l3vpn.build());
654 opBuilder.setL3vpnInstances(l3vpnList);
655 result.set(RpcResultBuilder.<GetL3VPNOutput>success().withResult(opBuilder.build()).build());
657 } catch (Exception ex) {
658 String message = String.format("GetL3VPN failed due to %s", ex.getMessage());
659 logger.error(message);
660 result.set(RpcResultBuilder.<GetL3VPNOutput>failed().withError(ErrorType.APPLICATION, message).build());
667 public Future<RpcResult<DeleteL3VPNOutput>> deleteL3VPN(DeleteL3VPNInput input) {
669 DeleteL3VPNOutputBuilder opBuilder = new DeleteL3VPNOutputBuilder();
670 SettableFuture<RpcResult<DeleteL3VPNOutput>> result = SettableFuture.create();
671 List<RpcError> errorList = new ArrayList<RpcError>();
673 int failurecount = 0;
674 int warningcount = 0;
675 List<Uuid> vpns = input.getId();
676 for (Uuid vpn : vpns) {
680 InstanceIdentifier<VpnInstance> vpnIdentifier =
681 InstanceIdentifier.builder(VpnInstances.class)
682 .child(VpnInstance.class, new VpnInstanceKey(vpn.getValue())).build();
683 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
685 if (optionalVpn.isPresent()) {
688 msg = String.format("VPN with vpnid: %s does not exist", vpn.getValue());
690 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-value", msg);
691 errorList.add(error);
694 } catch (Exception ex) {
695 msg = String.format("Deletion of L3VPN failed when deleting for uuid %s", vpn.getValue());
696 logger.error(msg, ex.getMessage());
697 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
698 errorList.add(error);
702 // if at least one succeeds; result is success
703 // if none succeeds; result is failure
704 if (failurecount + warningcount == vpns.size()) {
705 result.set(RpcResultBuilder.<DeleteL3VPNOutput>failed().withRpcErrors(errorList).build());
707 List<String> errorResponseList = new ArrayList<>();
708 if (!errorList.isEmpty()) {
709 for (RpcError rpcError : errorList) {
710 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
711 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
712 errorResponseList.add(errorResponse);
715 errorResponseList.add("Operation successful with no errors");
717 opBuilder.setResponse(errorResponseList);
718 result.set(RpcResultBuilder.<DeleteL3VPNOutput>success().withResult(opBuilder.build()).build());
723 protected void addSubnetToVpn(Uuid vpnId, Uuid subnet) {
724 logger.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
725 Subnetmap sn = updateSubnetNode(subnet, null, null, null, vpnId, null);
726 // Check if there are ports on this subnet and add corresponding vpn-interfaces
727 List<Uuid> portList = sn.getPortList();
728 if (portList != null) {
729 for (Uuid port : sn.getPortList()) {
730 logger.debug("adding vpn-interface for port {}", port.getValue());
731 createVpnInterface(vpnId, getNeutronPort(port));
736 protected List<Adjacency> addAdjacencyforExtraRoute(List<String> routeList, boolean rtrUp, String vpnifname) {
737 List<Adjacency> adjList = new ArrayList<Adjacency>();
738 for (String route : routeList) {
739 // assuming extra route is strictly in the format "nexthop destination" > "10.1.1.10 40.0.1.0/24"
740 String[] parts = route.split(" ");
741 if (parts.length == 2) {
742 boolean isLockAcquired = false;
743 String nextHop = parts[0];
744 String destination = parts[1];
746 String tapPortName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
747 logger.trace("Adding extra route with nexthop {}, destination {}, ifName {}", nextHop,
748 destination, tapPortName);
749 Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey
750 (new AdjacencyKey(destination)).build();
751 if (rtrUp == false) {
752 if (tapPortName.equals(vpnifname)) {
757 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
758 child(VpnInterface.class, new VpnInterfaceKey(tapPortName)).build();
760 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
761 .CONFIGURATION, vpnIfIdentifier);
762 if (optionalVpnInterface.isPresent()) {
763 Adjacencies erAdjs = new AdjacenciesBuilder().setAdjacency(Arrays.asList(erAdj)).build();
764 VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(tapPortName))
765 .addAugmentation(Adjacencies.class, erAdjs).build();
766 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnifname);
767 logger.debug("Adding extra route {}", route);
768 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
770 logger.error("VM adjacency for interface {} not present ; cannot add extra route adjacency",
773 } catch (Exception e) {
774 logger.error("exception in adding extra route: {}" + e);
776 if (isLockAcquired) {
777 NeutronvpnUtils.unlock(lockManager, vpnifname);
781 logger.error("Incorrect input received for extra route. {}", parts);
787 protected void removeAdjacencyforExtraRoute(List<String> routeList) {
788 for (String route : routeList) {
789 // assuming extra route is strictly in the format "nexthop destination" > "10.1.1.10 40.0.1.0/24"
790 String[] parts = route.split(" ");
791 if (parts.length == 2) {
792 boolean isLockAcquired = false;
793 String nextHop = parts[0];
794 String destination = parts[1];
796 String tapPortName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
797 logger.trace("Removing extra route with nexthop {}, destination {}, ifName {}", nextHop,
798 destination, tapPortName);
799 InstanceIdentifier<Adjacency> adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
800 child(VpnInterface.class, new VpnInterfaceKey(tapPortName)).augmentation(Adjacencies.class)
801 .child(Adjacency.class, new AdjacencyKey(destination)).build();
803 isLockAcquired = NeutronvpnUtils.lock(lockManager, tapPortName);
804 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, adjacencyIdentifier);
805 logger.trace("extra route {} deleted successfully", route);
806 } catch (Exception e) {
807 logger.error("exception in deleting extra route: {}" + e);
809 if (isLockAcquired) {
810 NeutronvpnUtils.unlock(lockManager, tapPortName);
814 logger.error("Incorrect input received for extra route. {}", parts);
819 protected void addPortToVpn(Uuid vpnId, Uuid port) {
820 logger.debug("Adding Port to vpn node...");
821 createVpnInterface(vpnId, getNeutronPort(port));
824 protected void removeL3Vpn(Uuid id) {
826 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, id);
827 Uuid router = vpnMap.getRouterId();
829 if (router != null) {
830 dissociateRouterFromVpn(id, router);
832 // dissociate networks
833 if (!id.equals(router)) {
834 dissociateNetworksFromVpn(id, vpnMap.getNetworkIds());
836 // remove entire vpnMaps node
837 deleteVpnMapsNode(id);
839 // remove vpn-instance
840 deleteVpnInstance(id);
843 protected void removePortFromVpn(Uuid vpnId, Uuid port) {
844 logger.debug("Removing Port from vpn node...");
845 deleteVpnInterface(getNeutronPort(port));
848 protected void removeSubnetFromVpn(Uuid vpnId, Uuid subnet) {
849 logger.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
850 Subnetmap sn = NeutronvpnUtils.getSubnetmap(broker, subnet);
852 // Check if there are ports on this subnet; remove corresponding vpn-interfaces
853 List<Uuid> portList = sn.getPortList();
854 if (portList != null) {
855 for (Uuid port : sn.getPortList()) {
856 logger.debug("removing vpn-interface for port {}", port.getValue());
857 deleteVpnInterface(getNeutronPort(port));
860 // update subnet-vpn association
861 removeFromSubnetNode(subnet, null, null, vpnId, null);
863 logger.warn("Subnetmap for subnet {} not found", subnet.getValue());
867 protected void associateRouterToVpn(Uuid vpnId, Uuid routerId) {
869 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
871 if (!vpnId.equals(routerId)) {
872 logger.debug("Removing subnets from internal vpn {}", routerId.getValue());
873 if (routerSubnets != null) {
874 for (Uuid subnet : routerSubnets) {
875 removeSubnetFromVpn(routerId, subnet);
879 logger.debug("Adding subnets to vpn {}", vpnId.getValue());
880 for (Uuid subnet : routerSubnets) {
881 addSubnetToVpn(vpnId, subnet);
884 updateVpnMaps(vpnId, null, routerId, null, null);
887 protected void dissociateRouterFromVpn(Uuid vpnId, Uuid routerId) {
889 // remove existing external vpn interfaces
890 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
892 if (routerSubnets != null) {
893 for (Uuid subnet : routerSubnets) {
894 logger.debug("Removing subnets from external vpn {}", vpnId.getValue());
895 removeSubnetFromVpn(vpnId, subnet);
896 logger.debug("Adding subnets to internal vpn {}", routerId.getValue());
897 addSubnetToVpn(routerId, subnet);
900 clearFromVpnMaps(vpnId, routerId, null);
903 protected List<String> associateNetworksToVpn(Uuid vpn, List<Uuid> networks) {
904 List<String> failed = new ArrayList<String>();
905 if (!networks.isEmpty()) {
906 // store in Data Base
907 updateVpnMaps(vpn, null, null, null, networks);
908 // process corresponding subnets for VPN
909 for (Uuid nw : networks) {
910 if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
911 failed.add(nw.getValue());
913 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
914 logger.debug("Adding network subnets...");
915 if (networkSubnets != null) {
916 for (Uuid subnet : networkSubnets) {
917 addSubnetToVpn(vpn, subnet);
926 protected List<String> dissociateNetworksFromVpn(Uuid vpn, List<Uuid> networks) {
927 List<String> failed = new ArrayList<String>();
928 if (networks != null && !networks.isEmpty()) {
929 // store in Data Base
930 clearFromVpnMaps(vpn, null, networks);
931 // process corresponding subnets for VPN
932 for (Uuid nw : networks) {
933 if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
934 failed.add(nw.getValue());
936 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
937 logger.debug("Removing network subnets...");
938 if (networkSubnets != null) {
939 for (Uuid subnet : networkSubnets) {
940 removeSubnetFromVpn(vpn, subnet);
950 public Future<RpcResult<AssociateNetworksOutput>> associateNetworks(AssociateNetworksInput input) {
952 AssociateNetworksOutputBuilder opBuilder = new AssociateNetworksOutputBuilder();
953 SettableFuture<RpcResult<AssociateNetworksOutput>> result = SettableFuture.create();
954 logger.debug("associateNetworks {}", input);
955 StringBuilder returnMsg = new StringBuilder();
956 Uuid vpnId = input.getVpnId();
959 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
960 List<Uuid> netIds = input.getNetworkId();
961 if (netIds != null && !netIds.isEmpty()) {
962 List<String> failed = associateNetworksToVpn(vpnId, netIds);
963 if (!failed.isEmpty()) {
964 returnMsg.append("network(s) not found : ").append(failed);
968 returnMsg.append("VPN not found : ").append(vpnId.getValue());
970 if (returnMsg.length() != 0) {
971 String message = String.format("associate Networks to vpn %s failed due to %s", vpnId.getValue(),
973 logger.error(message);
974 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
976 opBuilder.setResponse(errorResponse);
977 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().withResult(opBuilder.build()).build());
979 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().build());
981 } catch (Exception ex) {
982 String message = String.format("associate Networks to vpn %s failed due to %s", input.getVpnId().getValue(),
984 logger.error(message);
985 result.set(RpcResultBuilder.<AssociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
988 logger.debug("associateNetworks returns..");
993 public Future<RpcResult<Void>> associateRouter(AssociateRouterInput input) {
995 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
996 logger.debug("associateRouter {}", input);
997 StringBuilder returnMsg = new StringBuilder();
998 Uuid vpnId = input.getVpnId();
999 Uuid routerId = input.getRouterId();
1001 if (routerId != null && vpnId != null) {
1002 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1003 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, vpnId);
1004 if (rtr != null && vpnMap != null) {
1005 if (vpnMap.getRouterId() != null) {
1006 returnMsg.append("vpn ").append(vpnId.getValue()).append(" already associated to router ")
1007 .append(vpnMap.getRouterId().getValue());
1009 associateRouterToVpn(vpnId, routerId);
1012 returnMsg.append("router not found : ").append(routerId.getValue());
1015 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1017 if (returnMsg.length() != 0) {
1018 String message = String.format("associate router to vpn %s failed due to %s", routerId.getValue(),
1020 logger.error(message);
1021 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1024 result.set(RpcResultBuilder.<Void>success().build());
1026 } catch (Exception ex) {
1027 String message = String.format("associate router %s to vpn %s failed due to %s", routerId.getValue(),
1028 vpnId.getValue(), ex.getMessage());
1029 logger.error(message);
1030 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1032 logger.debug("associateRouter returns..");
1037 public Future<RpcResult<DissociateNetworksOutput>> dissociateNetworks(DissociateNetworksInput input) {
1039 DissociateNetworksOutputBuilder opBuilder = new DissociateNetworksOutputBuilder();
1040 SettableFuture<RpcResult<DissociateNetworksOutput>> result = SettableFuture.create();
1042 logger.debug("dissociateNetworks {}", input);
1043 StringBuilder returnMsg = new StringBuilder();
1044 Uuid vpnId = input.getVpnId();
1047 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1048 List<Uuid> netIds = input.getNetworkId();
1049 if (netIds != null && !netIds.isEmpty()) {
1050 List<String> failed = dissociateNetworksFromVpn(vpnId, netIds);
1051 if (!failed.isEmpty()) {
1052 returnMsg.append("netowrk(s) not found : ").append(failed);
1056 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1058 if (returnMsg.length() != 0) {
1059 String message = String.format("disssociate Networks to vpn %s failed due to %s", vpnId.getValue(),
1061 logger.error(message);
1062 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1064 opBuilder.setResponse(errorResponse);
1065 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().withResult(opBuilder.build()).build());
1067 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().build());
1069 } catch (Exception ex) {
1070 String message = String.format("dissociate Networks to vpn %s failed due to %s", input.getVpnId().
1071 getValue(), ex.getMessage());
1072 logger.error(message);
1073 result.set(RpcResultBuilder.<DissociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1076 logger.debug("dissociateNetworks returns..");
1081 public Future<RpcResult<Void>> dissociateRouter(DissociateRouterInput input) {
1083 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1085 logger.debug("dissociateRouter {}", input);
1086 StringBuilder returnMsg = new StringBuilder();
1087 Uuid vpnId = input.getVpnId();
1088 Uuid routerId = input.getRouterId();
1090 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1091 if (routerId != null) {
1092 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1094 dissociateRouterFromVpn(vpnId, routerId);
1096 returnMsg.append("router not found : ").append(routerId.getValue());
1100 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1102 if (returnMsg.length() != 0) {
1103 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1104 vpnId.getValue(), returnMsg);
1105 logger.error(message);
1106 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1108 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1111 result.set(RpcResultBuilder.<Void>success().build());
1113 } catch (Exception ex) {
1114 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1115 vpnId.getValue(), ex.getMessage());
1116 logger.error(message);
1117 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1119 logger.debug("dissociateRouter returns..");
1124 protected void handleNeutronRouterDeleted(Uuid routerId, List<Uuid> routerSubnetIds) {
1125 // check if the router is associated to some VPN
1126 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1127 if (vpnId != null) {
1128 // remove existing external vpn interfaces
1129 for (Uuid subnetId : routerSubnetIds) {
1130 removeSubnetFromVpn(vpnId, subnetId);
1132 clearFromVpnMaps(vpnId, routerId, null);
1134 // remove existing internal vpn interfaces
1135 for (Uuid subnetId : routerSubnetIds) {
1136 removeSubnetFromVpn(routerId, subnetId);
1139 // delete entire vpnMaps node for internal VPN
1140 deleteVpnMapsNode(routerId);
1142 // delete vpn-instance for internal VPN
1143 deleteVpnInstance(routerId);
1146 protected Subnet getNeutronSubnet(Uuid subnetId) {
1147 InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).
1148 child(Subnets.class).child(Subnet.class, new SubnetKey(subnetId));
1149 Optional<Subnet> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
1151 if (sn.isPresent()) {
1157 protected IpAddress getNeutronSubnetGateway(Uuid subnetId) {
1158 Subnet sn = getNeutronSubnet(subnetId);
1160 return sn.getGatewayIp();
1165 protected Port getNeutronPort(String name) {
1166 Uuid portId = NeutronvpnUtils.getNeutronPortIdfromPortName(broker, name);
1167 if (portId != null) {
1168 InstanceIdentifier<Port> pid = InstanceIdentifier.create(Neutron.class).
1169 child(Ports.class).child(Port.class, new PortKey(portId));
1170 Optional<Port> optPort = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, pid);
1171 if (optPort.isPresent()) {
1172 return optPort.get();
1175 logger.error("Port {} not Found!!", name);
1180 protected Port getNeutronPort(Uuid portId) {
1181 InstanceIdentifier<Port> pid = InstanceIdentifier.create(Neutron.class).
1182 child(Ports.class).child(Port.class, new PortKey(portId));
1183 Optional<Port> optPort = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, pid);
1184 if (optPort.isPresent()) {
1185 return optPort.get();
1190 protected List<Uuid> getSubnetsforVpn(Uuid vpnid) {
1191 List<Uuid> subnets = new ArrayList<Uuid>();
1193 InstanceIdentifier<Subnetmaps> subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build();
1194 Optional<Subnetmaps> subnetmaps = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1196 if (subnetmaps.isPresent()) {
1197 Subnetmaps smaps = subnetmaps.get();
1198 List<Subnetmap> subnetMapList = smaps.getSubnetmap();
1199 for (Subnetmap subnetMap : subnetMapList) {
1200 if (subnetMap.getVpnId() != null && subnetMap.getVpnId().equals(vpnid)) {
1201 subnets.add(subnetMap.getId());
1208 public List<String> showNeutronPortsCLI() {
1209 List<String> result = new ArrayList<String>();
1210 result.add(String.format(" %-22s %-22s %-22s %-6s ", "PortName", "Mac Address", "IP Address",
1212 result.add("---------------------------------------------------------------------------------------");
1213 InstanceIdentifier<Ports> portidentifier = InstanceIdentifier.create(Neutron.class).child(Ports.class);
1215 Optional<Ports> ports = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, portidentifier);
1216 if (ports.isPresent()) {
1217 List<Port> portList = ports.get().getPort();
1218 for (Port port : portList) {
1219 result.add(String.format(" %-22s %-22s %-22s %-6s ", NeutronvpnUtils.uuidToTapPortName(port
1220 .getUuid()), port.getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address()
1221 .getValue(), NeutronvpnUtils.getIPPrefixFromPort(broker, port)));
1224 } catch (Exception e) {
1225 logger.error("Failed to retrieve neutronPorts info : ", e);
1226 System.out.println("Failed to retrieve neutronPorts info : " + e.getMessage());
1231 public List<String> showVpnConfigCLI(Uuid vpnuuid) {
1232 List<String> result = new ArrayList<String>();
1233 if (vpnuuid == null) {
1234 System.out.println("");
1235 System.out.println("Displaying VPN config for all VPNs");
1236 System.out.println("To display VPN config for a particular VPN, use the following syntax");
1237 System.out.println(getshowVpnConfigCLIHelp());
1240 RpcResult<GetL3VPNOutput> rpcResult = getL3VPN(new GetL3VPNInputBuilder().setId(vpnuuid).build()).get();
1241 if (rpcResult.isSuccessful()) {
1243 result.add(String.format(" %-37s %-37s %-7s ", "VPN ID", "Tenant ID", "RD"));
1245 result.add(String.format(" %-80s ", "Import-RTs"));
1247 result.add(String.format(" %-80s ", "Export-RTs"));
1249 result.add(String.format(" %-76s ", "Subnet IDs"));
1251 result.add("------------------------------------------------------------------------------------");
1253 List<L3vpnInstances> VpnList = rpcResult.getResult().getL3vpnInstances();
1254 for (L3vpnInstance Vpn : VpnList) {
1255 String tenantId = Vpn.getTenantId() != null ? Vpn.getTenantId().getValue() : "\" " +
1257 result.add(String.format(" %-37s %-37s %-7s ", Vpn.getId().getValue(), tenantId, Vpn
1258 .getRouteDistinguisher()));
1260 result.add(String.format(" %-80s ", Vpn.getImportRT()));
1262 result.add(String.format(" %-80s ", Vpn.getExportRT()));
1265 Uuid vpnid = Vpn.getId();
1266 List<Uuid> subnetList = getSubnetsforVpn(vpnid);
1267 if (!subnetList.isEmpty()) {
1268 for (Uuid subnetuuid : subnetList) {
1269 result.add(String.format(" %-76s ", subnetuuid.getValue()));
1272 result.add(String.format(" %-76s ", "\" \""));
1275 result.add("----------------------------------------");
1279 String errortag = rpcResult.getErrors().iterator().next().getTag();
1280 if (errortag == "") {
1281 System.out.println("");
1282 System.out.println("No VPN has been configured yet");
1283 } else if (errortag == "invalid-value") {
1284 System.out.println("");
1285 System.out.println("VPN " + vpnuuid.getValue() + " is not present");
1287 System.out.println("error getting VPN info : " + rpcResult.getErrors());
1288 System.out.println(getshowVpnConfigCLIHelp());
1291 } catch (InterruptedException | ExecutionException e) {
1292 logger.error("error getting VPN info : ", e);
1293 System.out.println("error getting VPN info : " + e.getMessage());
1298 private String getshowVpnConfigCLIHelp() {
1299 StringBuilder help = new StringBuilder("Usage:");
1300 help.append("display vpn-config [-vid/--vpnid <id>]");
1301 return help.toString();