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;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
15 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
16 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
17 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
18 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargets;
19 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargetsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
21 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets
23 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTargetKey;
24 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
25 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceBuilder;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.vpn.instance
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceBuilder;
31 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.AdjacenciesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
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;
89 import java.util.ArrayList;
90 import java.util.Arrays;
91 import java.util.List;
92 import java.util.concurrent.ExecutionException;
93 import java.util.concurrent.Future;
95 public class NeutronvpnManager implements NeutronvpnService, AutoCloseable {
97 private static final Logger logger = LoggerFactory.getLogger(NeutronvpnManager.class);
98 private final DataBroker broker;
99 private LockManagerService lockManager;
100 IMdsalApiManager mdsalUtil;
103 * @param db - dataBroker reference
104 * @param mdsalManager - MDSAL Util API access
106 public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager) {
108 mdsalUtil = mdsalManager;
111 public void setLockManager(LockManagerService lockManager) {
112 this.lockManager = lockManager;
116 public void close() throws Exception {
117 logger.info("Neutron VPN Manager Closed");
120 protected Subnetmap updateSubnetNode(Uuid subnetId, String subnetIp, Uuid tenantId, Uuid networkId, Uuid routerId,
121 Uuid vpnId, Uuid portId) {
122 Subnetmap subnetmap = null;
123 SubnetmapBuilder builder = null;
124 boolean isLockAcquired = false;
125 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
126 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
128 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
129 logger.debug("updating Subnet :read: ");
130 if (sn.isPresent()) {
131 builder = new SubnetmapBuilder(sn.get());
132 logger.debug("updating Subnet :existing: ");
134 builder = new SubnetmapBuilder().setKey(new SubnetmapKey(subnetId)).setId(subnetId);
135 logger.debug("updating Subnet :new: ");
138 if (subnetIp != null) {
139 builder.setSubnetIp(subnetIp);
141 if (routerId != null) {
142 builder.setRouterId(routerId);
144 if (networkId != null) {
145 builder.setNetworkId(networkId);
148 builder.setVpnId(vpnId);
150 if (tenantId != null) {
151 builder.setTenantId(tenantId);
154 if (portId != null) {
155 List<Uuid> portList = builder.getPortList();
156 if (portList == null) {
157 portList = new ArrayList<Uuid>();
159 portList.add(portId);
160 builder.setPortList(portList);
163 subnetmap = builder.build();
164 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
165 logger.debug("Creating/Updating subnetMap node: {} ", subnetId.getValue());
166 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
167 } catch (Exception e) {
168 logger.error("Updation of subnetMap failed for node: {}", subnetId.getValue());
170 if (isLockAcquired) {
171 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
177 protected Subnetmap removeFromSubnetNode(Uuid subnetId, Uuid networkId, Uuid routerId, Uuid vpnId, Uuid portId) {
178 Subnetmap subnetmap = null;
179 boolean isLockAcquired = false;
180 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
181 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
183 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
184 if (sn.isPresent()) {
185 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
186 if (routerId != null) {
187 builder.setRouterId(null);
189 if (networkId != null) {
190 builder.setNetworkId(null);
193 builder.setVpnId(null);
195 if (portId != null && builder.getPortList() != null) {
196 List<Uuid> portList = builder.getPortList();
197 portList.remove(portId);
198 builder.setPortList(portList);
201 subnetmap = builder.build();
202 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
203 logger.debug("Removing from existing subnetmap node: {} ", subnetId.getValue());
204 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
206 logger.warn("removing from non-existing subnetmap node: {} ", subnetId.getValue());
208 } catch (Exception e) {
209 logger.error("Removal from subnetmap failed for node: {}", subnetId.getValue());
211 if (isLockAcquired) {
212 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
218 protected void deleteSubnetMapNode(Uuid subnetId) {
219 boolean isLockAcquired = false;
220 InstanceIdentifier<Subnetmap> subnetMapIdentifier = InstanceIdentifier.builder(Subnetmaps.class)
221 .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
222 logger.debug("removing subnetMap node: {} ", subnetId.getValue());
224 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
225 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, subnetMapIdentifier);
226 } catch (Exception e) {
227 logger.error("Delete subnetMap node failed for subnet : {} ", subnetId.getValue());
229 if (isLockAcquired) {
230 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
235 private void updateVpnInstanceNode(String vpnName, List<String> rd, List<String> irt, List<String> ert) {
237 VpnInstanceBuilder builder = null;
238 List<VpnTarget> vpnTargetList = new ArrayList<VpnTarget>();
239 boolean isLockAcquired = false;
240 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
241 child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
243 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
245 logger.debug("Creating/Updating a new vpn-instance node: {} ", vpnName);
246 if (optionalVpn.isPresent()) {
247 builder = new VpnInstanceBuilder(optionalVpn.get());
248 logger.debug("updating existing vpninstance node");
250 builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(vpnName)).setVpnInstanceName(vpnName);
252 if (irt != null && !irt.isEmpty()) {
253 if (ert != null && !ert.isEmpty()) {
254 List<String> commonRT = new ArrayList<String>(irt);
255 commonRT.retainAll(ert);
257 for (String common : commonRT) {
260 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(common)).setVrfRTValue
261 (common).setVrfRTType(VpnTarget.VrfRTType.Both).build();
262 vpnTargetList.add(vpnTarget);
265 for (String importRT : irt) {
266 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(importRT)).setVrfRTValue
267 (importRT).setVrfRTType(VpnTarget.VrfRTType.ImportExtcommunity).build();
268 vpnTargetList.add(vpnTarget);
272 if (ert != null && !ert.isEmpty()) {
273 for (String exportRT : ert) {
274 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(exportRT)).setVrfRTValue
275 (exportRT).setVrfRTType(VpnTarget.VrfRTType.ExportExtcommunity).build();
276 vpnTargetList.add(vpnTarget);
280 VpnTargets vpnTargets = new VpnTargetsBuilder().setVpnTarget(vpnTargetList).build();
282 Ipv4FamilyBuilder ipv4vpnBuilder = new Ipv4FamilyBuilder().setVpnTargets(vpnTargets);
284 if (rd != null && !rd.isEmpty()) {
285 ipv4vpnBuilder.setRouteDistinguisher(rd.get(0));
288 VpnInstance newVpn = builder.setIpv4Family(ipv4vpnBuilder.build()).build();
289 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnName);
290 logger.debug("Creating/Updating vpn-instance for {} ", vpnName);
291 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier, newVpn);
292 } catch (Exception e) {
293 logger.error("Update VPN Instance node failed for node: {} {} {} {}", vpnName, rd, irt, ert);
295 if (isLockAcquired) {
296 NeutronvpnUtils.unlock(lockManager, vpnName);
301 private void deleteVpnMapsNode(Uuid vpnid) {
302 boolean isLockAcquired = false;
303 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
304 .child(VpnMap.class, new VpnMapKey(vpnid)).build();
305 logger.debug("removing vpnMaps node: {} ", vpnid.getValue());
307 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnid.getValue());
308 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
309 } catch (Exception e) {
310 logger.error("Delete vpnMaps node failed for vpn : {} ", vpnid.getValue());
312 if (isLockAcquired) {
313 NeutronvpnUtils.unlock(lockManager, vpnid.getValue());
318 private void updateVpnMaps(Uuid vpnId, String name, Uuid router, Uuid tenantId, List<Uuid> networks) {
319 VpnMapBuilder builder;
320 boolean isLockAcquired = false;
321 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
322 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
324 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
326 if (optionalVpnMap.isPresent()) {
327 builder = new VpnMapBuilder(optionalVpnMap.get());
329 builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId);
333 builder.setName(name);
335 if (tenantId != null) {
336 builder.setTenantId(tenantId);
338 if (router != null) {
339 builder.setRouterId(router);
341 if (networks != null) {
342 List<Uuid> nwList = builder.getNetworkIds();
343 if (nwList == null) {
344 nwList = new ArrayList<Uuid>();
346 nwList.addAll(networks);
347 builder.setNetworkIds(nwList);
350 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
351 logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue());
352 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build());
353 logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue());
354 } catch (Exception e) {
355 logger.error("UpdateVpnMaps failed for node: {} ", vpnId.getValue());
357 if (isLockAcquired) {
358 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
363 private void clearFromVpnMaps(Uuid vpnId, Uuid routerId, List<Uuid> networkIds) {
364 boolean isLockAcquired = false;
365 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
366 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
367 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
369 if (optionalVpnMap.isPresent()) {
370 VpnMap vpnMap = optionalVpnMap.get();
371 VpnMapBuilder vpnMapBuilder = new VpnMapBuilder(vpnMap);
372 if (routerId != null) {
373 if (vpnMap.getNetworkIds() == null && routerId.equals(vpnMap.getVpnId())) {
375 // remove entire node in case of internal VPN
376 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
377 logger.debug("removing vpnMaps node: {} ", vpnId);
378 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
379 } catch (Exception e) {
380 logger.error("Deletion of vpnMaps node failed for vpn {}", vpnId.getValue());
382 if (isLockAcquired) {
383 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
388 vpnMapBuilder.setRouterId(null);
390 if (networkIds != null) {
391 List<Uuid> vpnNw = vpnMap.getNetworkIds();
392 for (Uuid nw : networkIds) {
395 if (vpnNw.isEmpty()) {
396 logger.debug("setting networks null in vpnMaps node: {} ", vpnId.getValue());
397 vpnMapBuilder.setNetworkIds(null);
399 vpnMapBuilder.setNetworkIds(vpnNw);
404 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
405 logger.debug("clearing from vpnMaps node: {} ", vpnId.getValue());
406 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build
408 } catch (Exception e) {
409 logger.error("Clearing from vpnMaps node failed for vpn {}", vpnId.getValue());
411 if (isLockAcquired) {
412 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
416 logger.error("VPN : {} not found", vpnId.getValue());
418 logger.debug("Clear from VPNMaps DS successful for VPN {} ", vpnId.getValue());
421 private void deleteVpnInstance(Uuid vpnId) {
422 boolean isLockAcquired = false;
423 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
424 child(VpnInstance.class, new VpnInstanceKey(vpnId.getValue())).build();
426 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
427 logger.debug("Deleting vpnInstance {}", vpnId.getValue());
428 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
429 } catch (Exception e) {
430 logger.error("Deletion of VPNInstance node failed for VPN {}", vpnId.getValue());
432 if (isLockAcquired) {
433 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
438 protected void createVpnInterface(Uuid vpnId, Port port) {
439 boolean isLockAcquired = false;
440 if (vpnId == null || port == null) {
443 String infName = port.getUuid().getValue();
444 List<Adjacency> adjList = new ArrayList<Adjacency>();
445 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
447 // find router associated to vpn
448 Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId);
450 if (routerId != null) {
451 rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
453 // find all subnets to which this port is associated
454 List<FixedIps> ips = port.getFixedIps();
455 // create adjacency list
456 for (FixedIps ip : ips) {
457 // create vm adjacency
458 StringBuilder IpPrefixBuild = new StringBuilder(ip.getIpAddress().getIpv4Address().getValue());
459 String IpPrefix = IpPrefixBuild.append("/32").toString();
460 Adjacency vmAdj = new AdjacencyBuilder().setKey(new AdjacencyKey(IpPrefix)).setIpAddress(IpPrefix)
461 .setMacAddress(port.getMacAddress()).build();
463 // create extra route adjacency
464 if (rtr != null && rtr.getRoutes() != null) {
465 List<Routes> routeList = rtr.getRoutes();
466 List<Adjacency> erAdjList = addAdjacencyforExtraRoute(routeList, false, infName);
467 if (erAdjList != null && !erAdjList.isEmpty()) {
468 adjList.addAll(erAdjList);
472 // create vpn-interface on this neutron port
473 Adjacencies adjs = new AdjacenciesBuilder().setAdjacency(adjList).build();
474 VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName)).
475 setName(infName).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs);
476 VpnInterface vpnIf = vpnb.build();
479 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
480 logger.debug("Creating vpn interface {}", vpnIf);
481 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
482 } catch (Exception ex) {
483 logger.error("Creation of vpninterface {} failed due to {}", infName, ex);
485 if (isLockAcquired) {
486 NeutronvpnUtils.unlock(lockManager, infName);
491 protected void deleteVpnInterface(Port port) {
494 boolean isLockAcquired = false;
495 String infName = port.getUuid().getValue();
496 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
499 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
500 logger.debug("Deleting vpn interface {}", infName);
501 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
502 } catch (Exception ex) {
503 logger.error("Deletion of vpninterface {} failed due to {}", infName, ex);
505 if (isLockAcquired) {
506 NeutronvpnUtils.unlock(lockManager, infName);
512 protected void updateVpnInterface(Uuid vpnId, Port port) {
513 if (vpnId == null || port == null) {
516 boolean isLockAcquired = false;
517 String infName = port.getUuid().getValue();
518 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
520 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
521 .CONFIGURATION, vpnIfIdentifier);
522 if (optionalVpnInterface.isPresent()) {
523 VpnInterfaceBuilder vpnIfBuilder = new VpnInterfaceBuilder(optionalVpnInterface.get());
524 VpnInterface vpnIf = vpnIfBuilder.setVpnInstanceName(vpnId.getValue()).build();
525 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
526 logger.debug("Updating vpn interface {}", vpnIf);
527 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
529 logger.error("VPN Interface {} not found", infName);
531 } catch (Exception ex) {
532 logger.error("Updation of vpninterface {} failed due to {}", infName, ex);
534 if (isLockAcquired) {
535 NeutronvpnUtils.unlock(lockManager, infName);
540 public void createL3Vpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
541 Uuid router, List<Uuid> networks) {
543 // Update VPN Instance node
544 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
546 // Update local vpn-subnet DS
547 updateVpnMaps(vpn, name, router, tenant, networks);
549 if (router != null) {
550 associateRouterToVpn(vpn, router);
552 if (networks != null) {
553 associateNetworksToVpn(vpn, networks);
558 public Future<RpcResult<CreateL3VPNOutput>> createL3VPN(CreateL3VPNInput input) {
560 CreateL3VPNOutputBuilder opBuilder = new CreateL3VPNOutputBuilder();
561 SettableFuture<RpcResult<CreateL3VPNOutput>> result = SettableFuture.create();
562 List<RpcError> errorList = new ArrayList<RpcError>();
563 int failurecount = 0;
564 int warningcount = 0;
566 List<L3vpn> vpns = input.getL3vpn();
567 for (L3vpn vpn : vpns) {
570 if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
571 msg = String.format("Creation of L3VPN failed for VPN %s due to absence of RD/iRT/eRT input",
572 vpn.getId().getValue());
574 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
575 errorList.add(error);
579 if (vpn.getRouteDistinguisher().size() > 1) {
580 msg = String.format("Creation of L3VPN failed for VPN %s due to multiple RD input %s",
581 vpn.getId().getValue(), vpn.getRouteDistinguisher());
583 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
584 errorList.add(error);
589 createL3Vpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(),
590 vpn.getImportRT(), vpn.getExportRT(), vpn.getRouterId(), vpn.getNetworkIds());
591 } catch (Exception ex) {
592 msg = String.format("Creation of L3VPN failed for VPN %s", vpn.getId().getValue());
593 logger.error(msg, ex);
594 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
595 errorList.add(error);
599 // if at least one succeeds; result is success
600 // if none succeeds; result is failure
601 if (failurecount + warningcount == vpns.size()) {
602 result.set(RpcResultBuilder.<CreateL3VPNOutput>failed().withRpcErrors(errorList).build());
604 List<String> errorResponseList = new ArrayList<>();
605 if (!errorList.isEmpty()) {
606 for (RpcError rpcError : errorList) {
607 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
608 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
609 errorResponseList.add(errorResponse);
612 errorResponseList.add("Operation successful with no errors");
614 opBuilder.setResponse(errorResponseList);
615 result.set(RpcResultBuilder.<CreateL3VPNOutput>success().withResult(opBuilder.build()).build());
621 public Future<RpcResult<GetL3VPNOutput>> getL3VPN(GetL3VPNInput input) {
623 GetL3VPNOutputBuilder opBuilder = new GetL3VPNOutputBuilder();
624 SettableFuture<RpcResult<GetL3VPNOutput>> result = SettableFuture.create();
625 Uuid inputVpnId = input.getId();
626 List<VpnInstance> vpns = new ArrayList<VpnInstance>();
629 if (inputVpnId == null) {
631 InstanceIdentifier<VpnInstances> vpnsIdentifier =
632 InstanceIdentifier.builder(VpnInstances.class).build();
633 Optional<VpnInstances> optionalVpns = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
635 if (optionalVpns.isPresent() && optionalVpns.get().getVpnInstance() != null) {
636 for (VpnInstance vpn : optionalVpns.get().getVpnInstance()) {
637 // eliminating internal VPNs from getL3VPN output
638 if (vpn.getIpv4Family().getRouteDistinguisher() != null) {
644 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
645 .withWarning(ErrorType.PROTOCOL, "", "No VPN is present").build());
649 String name = inputVpnId.getValue();
650 InstanceIdentifier<VpnInstance> vpnIdentifier =
651 InstanceIdentifier.builder(VpnInstances.class)
652 .child(VpnInstance.class, new VpnInstanceKey(name)).build();
653 // read VpnInstance Info
654 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
656 if (optionalVpn.isPresent()) {
657 vpns.add(optionalVpn.get());
659 String message = String.format("GetL3VPN failed because VPN %s is not present", name);
660 logger.error(message);
661 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
662 .withWarning(ErrorType.PROTOCOL, "invalid-value", message).build());
665 List<L3vpnInstances> l3vpnList = new ArrayList<L3vpnInstances>();
666 for (VpnInstance vpnInstance : vpns) {
667 Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName());
669 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class).child(VpnMap
670 .class, new VpnMapKey(vpnId)).build();
671 L3vpnInstancesBuilder l3vpn = new L3vpnInstancesBuilder();
673 List<String> rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(","));
674 List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
676 List<String> ertList = new ArrayList<String>();
677 List<String> irtList = new ArrayList<String>();
679 for (VpnTarget vpnTarget : vpnTargetList) {
680 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
681 ertList.add(vpnTarget.getVrfRTValue());
683 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ImportExtcommunity) {
684 irtList.add(vpnTarget.getVrfRTValue());
686 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.Both) {
687 ertList.add(vpnTarget.getVrfRTValue());
688 irtList.add(vpnTarget.getVrfRTValue());
692 l3vpn.setId(vpnId).setRouteDistinguisher(rd).setImportRT(irtList).setExportRT(ertList);
693 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
695 if (optionalVpnMap.isPresent()) {
696 VpnMap vpnMap = optionalVpnMap.get();
697 l3vpn.setRouterId(vpnMap.getRouterId()).setNetworkIds(vpnMap.getNetworkIds())
698 .setTenantId(vpnMap.getTenantId()).setName(vpnMap.getName());
700 l3vpnList.add(l3vpn.build());
703 opBuilder.setL3vpnInstances(l3vpnList);
704 result.set(RpcResultBuilder.<GetL3VPNOutput>success().withResult(opBuilder.build()).build());
706 } catch (Exception ex) {
707 String message = String.format("GetL3VPN failed due to %s", ex.getMessage());
708 logger.error(message, ex);
709 result.set(RpcResultBuilder.<GetL3VPNOutput>failed().withError(ErrorType.APPLICATION, message).build());
715 public Future<RpcResult<DeleteL3VPNOutput>> deleteL3VPN(DeleteL3VPNInput input) {
717 DeleteL3VPNOutputBuilder opBuilder = new DeleteL3VPNOutputBuilder();
718 SettableFuture<RpcResult<DeleteL3VPNOutput>> result = SettableFuture.create();
719 List<RpcError> errorList = new ArrayList<RpcError>();
721 int failurecount = 0;
722 int warningcount = 0;
723 List<Uuid> vpns = input.getId();
724 for (Uuid vpn : vpns) {
728 InstanceIdentifier<VpnInstance> vpnIdentifier =
729 InstanceIdentifier.builder(VpnInstances.class)
730 .child(VpnInstance.class, new VpnInstanceKey(vpn.getValue())).build();
731 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
733 if (optionalVpn.isPresent()) {
736 msg = String.format("VPN with vpnid: %s does not exist", vpn.getValue());
738 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-value", msg);
739 errorList.add(error);
742 } catch (Exception ex) {
743 msg = String.format("Deletion of L3VPN failed when deleting for uuid %s", vpn.getValue());
744 logger.error(msg, ex);
745 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
746 errorList.add(error);
750 // if at least one succeeds; result is success
751 // if none succeeds; result is failure
752 if (failurecount + warningcount == vpns.size()) {
753 result.set(RpcResultBuilder.<DeleteL3VPNOutput>failed().withRpcErrors(errorList).build());
755 List<String> errorResponseList = new ArrayList<>();
756 if (!errorList.isEmpty()) {
757 for (RpcError rpcError : errorList) {
758 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
759 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
760 errorResponseList.add(errorResponse);
763 errorResponseList.add("Operation successful with no errors");
765 opBuilder.setResponse(errorResponseList);
766 result.set(RpcResultBuilder.<DeleteL3VPNOutput>success().withResult(opBuilder.build()).build());
771 protected void addSubnetToVpn(Uuid vpnId, Uuid subnet) {
772 logger.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
773 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null);
774 // Check if there are ports on this subnet and add corresponding vpn-interfaces
775 List<Uuid> portList = sn.getPortList();
776 if (portList != null) {
777 for (Uuid port : sn.getPortList()) {
778 logger.debug("adding vpn-interface for port {}", port.getValue());
779 createVpnInterface(vpnId, getNeutronPort(port));
784 protected void updateVpnForSubnet(Uuid vpnId, Uuid subnet) {
785 logger.debug("Updating VPN {} for subnet {}", vpnId.getValue(), subnet.getValue());
786 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null);
787 // Check for ports on this subnet and update association of corresponding vpn-interfaces to external vpn
788 List<Uuid> portList = sn.getPortList();
789 if (portList != null) {
790 for (Uuid port : sn.getPortList()) {
791 logger.debug("Updating vpn-interface for port {}", port.getValue());
792 updateVpnInterface(vpnId, getNeutronPort(port));
797 protected List<Adjacency> addAdjacencyforExtraRoute(List<Routes> routeList, boolean rtrUp, String vpnifname) {
798 List<Adjacency> adjList = new ArrayList<Adjacency>();
799 for (Routes route : routeList) {
800 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
801 boolean isLockAcquired = false;
802 String nextHop = String.valueOf(route.getNexthop().getValue());
803 String destination = String.valueOf(route.getDestination().getValue());
805 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
806 logger.trace("Adding extra route with nexthop {}, destination {}, infName {}", nextHop,
807 destination, infName);
808 Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey
809 (new AdjacencyKey(destination)).build();
810 if (rtrUp == false) {
811 if (infName.equals(vpnifname)) {
816 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
817 child(VpnInterface.class, new VpnInterfaceKey(infName)).build();
819 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
820 .CONFIGURATION, vpnIfIdentifier);
821 if (optionalVpnInterface.isPresent()) {
822 Adjacencies erAdjs = new AdjacenciesBuilder().setAdjacency(Arrays.asList(erAdj)).build();
823 VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName))
824 .addAugmentation(Adjacencies.class, erAdjs).build();
825 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
826 logger.debug("Adding extra route {}", route);
827 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
829 logger.error("VM adjacency for interface {} not present ; cannot add extra route adjacency",
832 } catch (Exception e) {
833 logger.error("exception in adding extra route: {}" + e);
835 if (isLockAcquired) {
836 NeutronvpnUtils.unlock(lockManager, infName);
840 logger.error("Incorrect input received for extra route. {}", route);
846 protected void removeAdjacencyforExtraRoute(List<Routes> routeList) {
847 for (Routes route : routeList) {
848 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
849 boolean isLockAcquired = false;
850 String nextHop = String.valueOf(route.getNexthop().getValue());
851 String destination = String.valueOf(route.getDestination().getValue());
853 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
854 logger.trace("Removing extra route with nexthop {}, destination {}, infName {}", nextHop,
855 destination, infName);
856 InstanceIdentifier<Adjacency> adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
857 child(VpnInterface.class, new VpnInterfaceKey(infName)).augmentation(Adjacencies.class)
858 .child(Adjacency.class, new AdjacencyKey(destination)).build();
860 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
861 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, adjacencyIdentifier);
862 logger.trace("extra route {} deleted successfully", route);
863 } catch (Exception e) {
864 logger.error("exception in deleting extra route: {}" + e);
866 if (isLockAcquired) {
867 NeutronvpnUtils.unlock(lockManager, infName);
871 logger.error("Incorrect input received for extra route. {}", route);
876 protected void addPortToVpn(Uuid vpnId, Uuid port) {
877 logger.debug("Adding Port to vpn node...");
878 createVpnInterface(vpnId, getNeutronPort(port));
881 protected void removeL3Vpn(Uuid id) {
883 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, id);
884 Uuid router = vpnMap.getRouterId();
886 if (router != null) {
887 dissociateRouterFromVpn(id, router);
889 // dissociate networks
890 if (!id.equals(router)) {
891 dissociateNetworksFromVpn(id, vpnMap.getNetworkIds());
893 // remove entire vpnMaps node
894 deleteVpnMapsNode(id);
896 // remove vpn-instance
897 deleteVpnInstance(id);
900 protected void removePortFromVpn(Uuid vpnId, Uuid port) {
901 logger.debug("Removing Port from vpn node...");
902 deleteVpnInterface(getNeutronPort(port));
905 protected void removeSubnetFromVpn(Uuid vpnId, Uuid subnet) {
906 logger.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
907 Subnetmap sn = NeutronvpnUtils.getSubnetmap(broker, subnet);
909 // Check if there are ports on this subnet; remove corresponding vpn-interfaces
910 List<Uuid> portList = sn.getPortList();
911 if (portList != null) {
912 for (Uuid port : sn.getPortList()) {
913 logger.debug("removing vpn-interface for port {}", port.getValue());
914 deleteVpnInterface(getNeutronPort(port));
917 // update subnet-vpn association
918 removeFromSubnetNode(subnet, null, null, vpnId, null);
920 logger.warn("Subnetmap for subnet {} not found", subnet.getValue());
924 protected void associateRouterToVpn(Uuid vpnId, Uuid routerId) {
925 updateVpnMaps(vpnId, null, routerId, null, null);
926 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
927 if (!vpnId.equals(routerId)) {
928 logger.debug("Updating association of subnets to external vpn {}", vpnId.getValue());
929 if (routerSubnets != null) {
930 for (Uuid subnetId : routerSubnets) {
931 updateVpnForSubnet(vpnId, subnetId);
935 logger.debug("Adding subnets to internal vpn {}", vpnId.getValue());
936 for (Uuid subnet : routerSubnets) {
937 addSubnetToVpn(vpnId, subnet);
942 protected void dissociateRouterFromVpn(Uuid vpnId, Uuid routerId) {
944 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
945 if (routerSubnets != null) {
946 for (Uuid subnetId : routerSubnets) {
947 logger.debug("Updating association of subnets to internal vpn {}", routerId.getValue());
948 updateVpnForSubnet(routerId, subnetId);
951 clearFromVpnMaps(vpnId, routerId, null);
954 protected List<String> associateNetworksToVpn(Uuid vpn, List<Uuid> networks) {
955 List<String> failed = new ArrayList<String>();
956 if (!networks.isEmpty()) {
957 // store in Data Base
958 updateVpnMaps(vpn, null, null, null, networks);
959 // process corresponding subnets for VPN
960 for (Uuid nw : networks) {
961 if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
962 failed.add(nw.getValue());
964 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
965 logger.debug("Adding network subnets...");
966 if (networkSubnets != null) {
967 for (Uuid subnet : networkSubnets) {
968 addSubnetToVpn(vpn, subnet);
977 protected List<String> dissociateNetworksFromVpn(Uuid vpn, List<Uuid> networks) {
978 List<String> failed = new ArrayList<String>();
979 if (networks != null && !networks.isEmpty()) {
980 // store in Data Base
981 clearFromVpnMaps(vpn, null, networks);
982 // process corresponding subnets for VPN
983 for (Uuid nw : networks) {
984 if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
985 failed.add(nw.getValue());
987 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
988 logger.debug("Removing network subnets...");
989 if (networkSubnets != null) {
990 for (Uuid subnet : networkSubnets) {
991 removeSubnetFromVpn(vpn, subnet);
1001 public Future<RpcResult<AssociateNetworksOutput>> associateNetworks(AssociateNetworksInput input) {
1003 AssociateNetworksOutputBuilder opBuilder = new AssociateNetworksOutputBuilder();
1004 SettableFuture<RpcResult<AssociateNetworksOutput>> result = SettableFuture.create();
1005 logger.debug("associateNetworks {}", input);
1006 StringBuilder returnMsg = new StringBuilder();
1007 Uuid vpnId = input.getVpnId();
1010 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1011 List<Uuid> netIds = input.getNetworkId();
1012 if (netIds != null && !netIds.isEmpty()) {
1013 List<String> failed = associateNetworksToVpn(vpnId, netIds);
1014 if (!failed.isEmpty()) {
1015 returnMsg.append("network(s) not found : ").append(failed);
1019 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1021 if (returnMsg.length() != 0) {
1022 String message = String.format("associate Networks to vpn %s failed due to %s", vpnId.getValue(),
1024 logger.error(message);
1025 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1027 opBuilder.setResponse(errorResponse);
1028 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().withResult(opBuilder.build()).build());
1030 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().build());
1032 } catch (Exception ex) {
1033 String message = String.format("associate Networks to vpn %s failed due to %s", input.getVpnId().getValue(),
1035 logger.error(message, ex);
1036 result.set(RpcResultBuilder.<AssociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1039 logger.debug("associateNetworks returns..");
1044 public Future<RpcResult<Void>> associateRouter(AssociateRouterInput input) {
1046 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1047 logger.debug("associateRouter {}", input);
1048 StringBuilder returnMsg = new StringBuilder();
1049 Uuid vpnId = input.getVpnId();
1050 Uuid routerId = input.getRouterId();
1052 if (routerId != null && vpnId != null) {
1053 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1054 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, vpnId);
1055 if (rtr != null && vpnMap != null) {
1056 if (vpnMap.getRouterId() != null) {
1057 returnMsg.append("vpn ").append(vpnId.getValue()).append(" already associated to router ")
1058 .append(vpnMap.getRouterId().getValue());
1060 associateRouterToVpn(vpnId, routerId);
1063 returnMsg.append("router not found : ").append(routerId.getValue());
1066 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1068 if (returnMsg.length() != 0) {
1069 String message = String.format("associate router to vpn %s failed due to %s", routerId.getValue(),
1071 logger.error(message);
1072 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1075 result.set(RpcResultBuilder.<Void>success().build());
1077 } catch (Exception ex) {
1078 String message = String.format("associate router %s to vpn %s failed due to %s", routerId.getValue(),
1079 vpnId.getValue(), ex.getMessage());
1080 logger.error(message, ex);
1081 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1083 logger.debug("associateRouter returns..");
1088 public Future<RpcResult<DissociateNetworksOutput>> dissociateNetworks(DissociateNetworksInput input) {
1090 DissociateNetworksOutputBuilder opBuilder = new DissociateNetworksOutputBuilder();
1091 SettableFuture<RpcResult<DissociateNetworksOutput>> result = SettableFuture.create();
1093 logger.debug("dissociateNetworks {}", input);
1094 StringBuilder returnMsg = new StringBuilder();
1095 Uuid vpnId = input.getVpnId();
1098 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1099 List<Uuid> netIds = input.getNetworkId();
1100 if (netIds != null && !netIds.isEmpty()) {
1101 List<String> failed = dissociateNetworksFromVpn(vpnId, netIds);
1102 if (!failed.isEmpty()) {
1103 returnMsg.append("netowrk(s) not found : ").append(failed);
1107 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1109 if (returnMsg.length() != 0) {
1110 String message = String.format("dissociate Networks to vpn %s failed due to %s", vpnId.getValue(),
1112 logger.error(message);
1113 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1115 opBuilder.setResponse(errorResponse);
1116 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().withResult(opBuilder.build()).build());
1118 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().build());
1120 } catch (Exception ex) {
1121 String message = String.format("dissociate Networks to vpn %s failed due to %s", input.getVpnId().
1122 getValue(), ex.getMessage());
1123 logger.error(message, ex);
1124 result.set(RpcResultBuilder.<DissociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1127 logger.debug("dissociateNetworks returns..");
1132 public Future<RpcResult<Void>> dissociateRouter(DissociateRouterInput input) {
1134 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1136 logger.debug("dissociateRouter {}", input);
1137 StringBuilder returnMsg = new StringBuilder();
1138 Uuid vpnId = input.getVpnId();
1139 Uuid routerId = input.getRouterId();
1141 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1142 if (routerId != null) {
1143 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1145 dissociateRouterFromVpn(vpnId, routerId);
1147 returnMsg.append("router not found : ").append(routerId.getValue());
1151 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1153 if (returnMsg.length() != 0) {
1154 String message = String.format("dissociate router %s to vpn %s failed due to %s", routerId.getValue(),
1155 vpnId.getValue(), returnMsg);
1156 logger.error(message);
1157 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1159 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1162 result.set(RpcResultBuilder.<Void>success().build());
1164 } catch (Exception ex) {
1165 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1166 vpnId.getValue(), ex.getMessage());
1167 logger.error(message, ex);
1168 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1170 logger.debug("dissociateRouter returns..");
1175 protected void handleNeutronRouterDeleted(Uuid routerId, List<Uuid> routerSubnetIds) {
1176 // check if the router is associated to some VPN
1177 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1178 if (vpnId != null) {
1179 // remove existing external vpn interfaces
1180 for (Uuid subnetId : routerSubnetIds) {
1181 removeSubnetFromVpn(vpnId, subnetId);
1183 clearFromVpnMaps(vpnId, routerId, null);
1185 // remove existing internal vpn interfaces
1186 for (Uuid subnetId : routerSubnetIds) {
1187 removeSubnetFromVpn(routerId, subnetId);
1190 // delete entire vpnMaps node for internal VPN
1191 deleteVpnMapsNode(routerId);
1193 // delete vpn-instance for internal VPN
1194 deleteVpnInstance(routerId);
1197 protected Subnet getNeutronSubnet(Uuid subnetId) {
1198 InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).
1199 child(Subnets.class).child(Subnet.class, new SubnetKey(subnetId));
1200 Optional<Subnet> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
1202 if (sn.isPresent()) {
1208 protected IpAddress getNeutronSubnetGateway(Uuid subnetId) {
1209 Subnet sn = getNeutronSubnet(subnetId);
1211 return sn.getGatewayIp();
1216 protected Port getNeutronPort(String name) {
1217 Uuid portId = NeutronvpnUtils.getNeutronPortIdfromPortName(broker, name);
1218 if (portId != null) {
1219 InstanceIdentifier<Port> pid = InstanceIdentifier.create(Neutron.class).
1220 child(Ports.class).child(Port.class, new PortKey(portId));
1221 Optional<Port> optPort = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, pid);
1222 if (optPort.isPresent()) {
1223 return optPort.get();
1226 logger.error("Port {} not Found!!", name);
1231 protected Port getNeutronPort(Uuid portId) {
1232 InstanceIdentifier<Port> pid = InstanceIdentifier.create(Neutron.class).
1233 child(Ports.class).child(Port.class, new PortKey(portId));
1234 Optional<Port> optPort = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, pid);
1235 if (optPort.isPresent()) {
1236 return optPort.get();
1241 protected List<Uuid> getSubnetsforVpn(Uuid vpnid) {
1242 List<Uuid> subnets = new ArrayList<Uuid>();
1244 InstanceIdentifier<Subnetmaps> subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build();
1245 Optional<Subnetmaps> subnetmaps = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1247 if (subnetmaps.isPresent() && subnetmaps.get().getSubnetmap() != null) {
1248 List<Subnetmap> subnetMapList = subnetmaps.get().getSubnetmap();
1249 for (Subnetmap subnetMap : subnetMapList) {
1250 if (subnetMap.getVpnId() != null && subnetMap.getVpnId().equals(vpnid)) {
1251 subnets.add(subnetMap.getId());
1258 public List<String> showNeutronPortsCLI() {
1259 List<String> result = new ArrayList<String>();
1260 result.add(String.format(" %-34s %-22s %-22s %-6s ", "PortName", "Mac Address", "IP Address",
1262 result.add("---------------------------------------------------------------------------------------");
1263 InstanceIdentifier<Ports> portidentifier = InstanceIdentifier.create(Neutron.class).child(Ports.class);
1265 Optional<Ports> ports = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, portidentifier);
1266 if (ports.isPresent() && ports.get().getPort() != null) {
1267 List<Port> portList = ports.get().getPort();
1268 for (Port port : portList) {
1269 result.add(String.format(" %-34s %-22s %-22s %-6s ", port.getUuid().getValue(), port
1270 .getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address().getValue(),
1271 NeutronvpnUtils.getIPPrefixFromPort(broker, port)));
1274 } catch (Exception e) {
1275 logger.error("Failed to retrieve neutronPorts info : ", e);
1276 System.out.println("Failed to retrieve neutronPorts info : " + e.getMessage());
1281 public List<String> showVpnConfigCLI(Uuid vpnuuid) {
1282 List<String> result = new ArrayList<String>();
1283 if (vpnuuid == null) {
1284 System.out.println("");
1285 System.out.println("Displaying VPN config for all VPNs");
1286 System.out.println("To display VPN config for a particular VPN, use the following syntax");
1287 System.out.println(getshowVpnConfigCLIHelp());
1290 RpcResult<GetL3VPNOutput> rpcResult = getL3VPN(new GetL3VPNInputBuilder().setId(vpnuuid).build()).get();
1291 if (rpcResult.isSuccessful()) {
1293 result.add(String.format(" %-37s %-37s %-7s ", "VPN ID", "Tenant ID", "RD"));
1295 result.add(String.format(" %-80s ", "Import-RTs"));
1297 result.add(String.format(" %-80s ", "Export-RTs"));
1299 result.add(String.format(" %-76s ", "Subnet IDs"));
1301 result.add("------------------------------------------------------------------------------------");
1303 List<L3vpnInstances> VpnList = rpcResult.getResult().getL3vpnInstances();
1304 for (L3vpnInstance Vpn : VpnList) {
1305 String tenantId = Vpn.getTenantId() != null ? Vpn.getTenantId().getValue() : "\" " +
1307 result.add(String.format(" %-37s %-37s %-7s ", Vpn.getId().getValue(), tenantId, Vpn
1308 .getRouteDistinguisher()));
1310 result.add(String.format(" %-80s ", Vpn.getImportRT()));
1312 result.add(String.format(" %-80s ", Vpn.getExportRT()));
1315 Uuid vpnid = Vpn.getId();
1316 List<Uuid> subnetList = getSubnetsforVpn(vpnid);
1317 if (!subnetList.isEmpty()) {
1318 for (Uuid subnetuuid : subnetList) {
1319 result.add(String.format(" %-76s ", subnetuuid.getValue()));
1322 result.add(String.format(" %-76s ", "\" \""));
1325 result.add("----------------------------------------");
1329 String errortag = rpcResult.getErrors().iterator().next().getTag();
1330 if (errortag == "") {
1331 System.out.println("");
1332 System.out.println("No VPN has been configured yet");
1333 } else if (errortag == "invalid-value") {
1334 System.out.println("");
1335 System.out.println("VPN " + vpnuuid.getValue() + " is not present");
1337 System.out.println("error getting VPN info : " + rpcResult.getErrors());
1338 System.out.println(getshowVpnConfigCLIHelp());
1341 } catch (InterruptedException | ExecutionException e) {
1342 logger.error("error getting VPN info : ", e);
1343 System.out.println("error getting VPN info : " + e.getMessage());
1348 private String getshowVpnConfigCLIHelp() {
1349 StringBuilder help = new StringBuilder("Usage:");
1350 help.append("display vpn-config [-vid/--vpnid <id>]");
1351 return help.toString();