2 * Copyright (c) 2015 - 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.netvirt.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.binding.api.NotificationPublishService;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.mdsalutil.MDSALUtil;
18 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
19 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
20 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
21 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargets;
22 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargetsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
24 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTargetBuilder;
25 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTargetKey;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.vpn.instance.Ipv4FamilyBuilder;
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.genius.lockmanager.rev160413.LockManagerService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.Adjacencies;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.Adjacency;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateNetworksInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateNetworksOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateNetworksOutputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateRouterInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateL3VPNInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateL3VPNOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateL3VPNOutputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DeleteL3VPNInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DeleteL3VPNOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DeleteL3VPNOutputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateNetworksInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateNetworksOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateNetworksOutputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateRouterInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetFixedIPsForNeutronPortInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetFixedIPsForNeutronPortOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetFixedIPsForNeutronPortOutputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNInputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNOutputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.L3vpnInstance;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NeutronvpnService;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpn;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpnBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpn;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpnBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterInterfacesMap;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetAddedToVpnBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetDeletedFromVpnBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetUpdatedInVpnBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfacesBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfacesKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.Interfaces;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.InterfacesBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.InterfacesKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.AdjacencyKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.ext.rev150712.NetworkL3Extension;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.createl3vpn.input.L3vpn;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.getl3vpn.output.L3vpnInstances;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.getl3vpn.output.L3vpnInstancesBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapBuilder;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMap;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMapBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.vpnmaps.VpnMapKey;
101 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
102 import org.opendaylight.yangtools.yang.common.RpcError;
103 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
104 import org.opendaylight.yangtools.yang.common.RpcResult;
105 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
107 import java.util.EventListener;
109 import org.slf4j.Logger;
110 import org.slf4j.LoggerFactory;
112 import java.util.ArrayList;
113 import java.util.Arrays;
114 import java.util.List;
115 import java.util.concurrent.ExecutionException;
116 import java.util.concurrent.Future;
118 public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, EventListener {
120 private static final Logger logger = LoggerFactory.getLogger(NeutronvpnManager.class);
121 private final DataBroker broker;
122 private LockManagerService lockManager;
123 private NeutronvpnNatManager nvpnNatManager;
124 IMdsalApiManager mdsalUtil;
125 private NotificationPublishService notificationPublishService;
126 private NotificationService notificationService;
127 Boolean isExternalVpn;
130 * @param db - dataBroker reference
131 * @param mdsalManager - MDSAL Util API access
133 public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager,NotificationPublishService notiPublishService,
134 NotificationService notiService, NeutronvpnNatManager vpnNatMgr) {
136 mdsalUtil = mdsalManager;
137 nvpnNatManager = vpnNatMgr;
138 notificationPublishService = notiPublishService;
139 notificationService = notiService;
142 public void setLockManager(LockManagerService lockManager) {
143 this.lockManager = lockManager;
147 public void close() throws Exception {
148 logger.info("Neutron VPN Manager Closed");
151 protected Subnetmap updateSubnetNode(Uuid subnetId, String subnetIp, Uuid tenantId, Uuid networkId, Uuid routerId,
152 Uuid vpnId, Uuid portId) {
153 Subnetmap subnetmap = null;
154 SubnetmapBuilder builder = null;
155 boolean isLockAcquired = false;
156 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
157 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
159 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
160 logger.debug("updating Subnet :read: ");
161 if (sn.isPresent()) {
162 builder = new SubnetmapBuilder(sn.get());
163 logger.debug("updating Subnet :existing: ");
165 builder = new SubnetmapBuilder().setKey(new SubnetmapKey(subnetId)).setId(subnetId);
166 logger.debug("updating Subnet :new: ");
169 if (subnetIp != null) {
170 builder.setSubnetIp(subnetIp);
172 if (routerId != null) {
173 builder.setRouterId(routerId);
175 if (networkId != null) {
176 builder.setNetworkId(networkId);
179 builder.setVpnId(vpnId);
181 if (tenantId != null) {
182 builder.setTenantId(tenantId);
185 if (portId != null) {
186 List<Uuid> portList = builder.getPortList();
187 if (portList == null) {
188 portList = new ArrayList<>();
190 portList.add(portId);
191 builder.setPortList(portList);
194 subnetmap = builder.build();
195 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
196 logger.debug("Creating/Updating subnetMap node: {} ", subnetId.getValue());
197 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
198 } catch (Exception e) {
199 logger.error("Updation of subnetMap failed for node: {}", subnetId.getValue());
201 if (isLockAcquired) {
202 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
208 protected Subnetmap removeFromSubnetNode(Uuid subnetId, Uuid networkId, Uuid routerId, Uuid vpnId, Uuid portId) {
209 Subnetmap subnetmap = null;
210 boolean isLockAcquired = false;
211 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
212 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
214 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
215 if (sn.isPresent()) {
216 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
217 if (routerId != null) {
218 builder.setRouterId(null);
220 if (networkId != null) {
221 builder.setNetworkId(null);
224 builder.setVpnId(null);
226 if (portId != null && builder.getPortList() != null) {
227 List<Uuid> portList = builder.getPortList();
228 portList.remove(portId);
229 builder.setPortList(portList);
232 subnetmap = builder.build();
233 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
234 logger.debug("Removing from existing subnetmap node: {} ", subnetId.getValue());
235 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
237 logger.warn("removing from non-existing subnetmap node: {} ", subnetId.getValue());
239 } catch (Exception e) {
240 logger.error("Removal from subnetmap failed for node: {}", subnetId.getValue());
242 if (isLockAcquired) {
243 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
249 protected void deleteSubnetMapNode(Uuid subnetId) {
250 boolean isLockAcquired = false;
251 InstanceIdentifier<Subnetmap> subnetMapIdentifier = InstanceIdentifier.builder(Subnetmaps.class)
252 .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
253 logger.debug("removing subnetMap node: {} ", subnetId.getValue());
255 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
256 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, subnetMapIdentifier);
257 } catch (Exception e) {
258 logger.error("Delete subnetMap node failed for subnet : {} ", subnetId.getValue());
260 if (isLockAcquired) {
261 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
266 private void updateVpnInstanceNode(String vpnName, List<String> rd, List<String> irt, List<String> ert) {
268 VpnInstanceBuilder builder = null;
269 List<VpnTarget> vpnTargetList = new ArrayList<>();
270 boolean isLockAcquired = false;
271 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
272 child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
274 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
276 logger.debug("Creating/Updating a new vpn-instance node: {} ", vpnName);
277 if (optionalVpn.isPresent()) {
278 builder = new VpnInstanceBuilder(optionalVpn.get());
279 logger.debug("updating existing vpninstance node");
281 builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(vpnName)).setVpnInstanceName(vpnName);
283 if (irt != null && !irt.isEmpty()) {
284 if (ert != null && !ert.isEmpty()) {
285 List<String> commonRT = new ArrayList<>(irt);
286 commonRT.retainAll(ert);
288 for (String common : commonRT) {
291 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(common)).setVrfRTValue
292 (common).setVrfRTType(VpnTarget.VrfRTType.Both).build();
293 vpnTargetList.add(vpnTarget);
296 for (String importRT : irt) {
297 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(importRT)).setVrfRTValue
298 (importRT).setVrfRTType(VpnTarget.VrfRTType.ImportExtcommunity).build();
299 vpnTargetList.add(vpnTarget);
303 if (ert != null && !ert.isEmpty()) {
304 for (String exportRT : ert) {
305 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(exportRT)).setVrfRTValue
306 (exportRT).setVrfRTType(VpnTarget.VrfRTType.ExportExtcommunity).build();
307 vpnTargetList.add(vpnTarget);
311 VpnTargets vpnTargets = new VpnTargetsBuilder().setVpnTarget(vpnTargetList).build();
313 Ipv4FamilyBuilder ipv4vpnBuilder = new Ipv4FamilyBuilder().setVpnTargets(vpnTargets);
315 if (rd != null && !rd.isEmpty()) {
316 ipv4vpnBuilder.setRouteDistinguisher(rd.get(0));
319 VpnInstance newVpn = builder.setIpv4Family(ipv4vpnBuilder.build()).build();
320 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnName);
321 logger.debug("Creating/Updating vpn-instance for {} ", vpnName);
322 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier, newVpn);
323 } catch (Exception e) {
324 logger.error("Update VPN Instance node failed for node: {} {} {} {}", vpnName, rd, irt, ert);
326 if (isLockAcquired) {
327 NeutronvpnUtils.unlock(lockManager, vpnName);
332 private void deleteVpnMapsNode(Uuid vpnid) {
333 boolean isLockAcquired = false;
334 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
335 .child(VpnMap.class, new VpnMapKey(vpnid)).build();
336 logger.debug("removing vpnMaps node: {} ", vpnid.getValue());
338 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnid.getValue());
339 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
340 } catch (Exception e) {
341 logger.error("Delete vpnMaps node failed for vpn : {} ", vpnid.getValue());
343 if (isLockAcquired) {
344 NeutronvpnUtils.unlock(lockManager, vpnid.getValue());
349 private void updateVpnMaps(Uuid vpnId, String name, Uuid router, Uuid tenantId, List<Uuid> networks) {
350 VpnMapBuilder builder;
351 boolean isLockAcquired = false;
352 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
353 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
355 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
357 if (optionalVpnMap.isPresent()) {
358 builder = new VpnMapBuilder(optionalVpnMap.get());
360 builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId);
364 builder.setName(name);
366 if (tenantId != null) {
367 builder.setTenantId(tenantId);
369 if (router != null) {
370 builder.setRouterId(router);
372 if (networks != null) {
373 List<Uuid> nwList = builder.getNetworkIds();
374 if (nwList == null) {
375 nwList = new ArrayList<>();
377 nwList.addAll(networks);
378 builder.setNetworkIds(nwList);
381 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
382 logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue());
383 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build());
384 logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue());
385 } catch (Exception e) {
386 logger.error("UpdateVpnMaps failed for node: {} ", vpnId.getValue());
388 if (isLockAcquired) {
389 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
394 private void clearFromVpnMaps(Uuid vpnId, Uuid routerId, List<Uuid> networkIds) {
395 boolean isLockAcquired = false;
396 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
397 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
398 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
400 if (optionalVpnMap.isPresent()) {
401 VpnMap vpnMap = optionalVpnMap.get();
402 VpnMapBuilder vpnMapBuilder = new VpnMapBuilder(vpnMap);
403 if (routerId != null) {
404 if (vpnMap.getNetworkIds() == null && routerId.equals(vpnMap.getVpnId())) {
406 // remove entire node in case of internal VPN
407 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
408 logger.debug("removing vpnMaps node: {} ", vpnId);
409 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
410 } catch (Exception e) {
411 logger.error("Deletion of vpnMaps node failed for vpn {}", vpnId.getValue());
413 if (isLockAcquired) {
414 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
419 vpnMapBuilder.setRouterId(null);
421 if (networkIds != null) {
422 List<Uuid> vpnNw = vpnMap.getNetworkIds();
423 for (Uuid nw : networkIds) {
426 if (vpnNw.isEmpty()) {
427 logger.debug("setting networks null in vpnMaps node: {} ", vpnId.getValue());
428 vpnMapBuilder.setNetworkIds(null);
430 vpnMapBuilder.setNetworkIds(vpnNw);
435 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
436 logger.debug("clearing from vpnMaps node: {} ", vpnId.getValue());
437 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build
439 } catch (Exception e) {
440 logger.error("Clearing from vpnMaps node failed for vpn {}", vpnId.getValue());
442 if (isLockAcquired) {
443 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
447 logger.error("VPN : {} not found", vpnId.getValue());
449 logger.debug("Clear from VPNMaps DS successful for VPN {} ", vpnId.getValue());
452 private void deleteVpnInstance(Uuid vpnId) {
453 boolean isLockAcquired = false;
454 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
455 child(VpnInstance.class, new VpnInstanceKey(vpnId.getValue())).build();
457 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
458 logger.debug("Deleting vpnInstance {}", vpnId.getValue());
459 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
460 } catch (Exception e) {
461 logger.error("Deletion of VPNInstance node failed for VPN {}", vpnId.getValue());
463 if (isLockAcquired) {
464 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
469 protected void createVpnInterface(Uuid vpnId, Port port) {
470 boolean isLockAcquired = false;
471 if (vpnId == null || port == null) {
474 String infName = port.getUuid().getValue();
475 List<Adjacency> adjList = new ArrayList<>();
476 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
478 // find router associated to vpn
479 Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId);
481 if (routerId != null) {
482 rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
484 // find all subnets to which this port is associated
485 List<FixedIps> ips = port.getFixedIps();
486 // create adjacency list
487 for (FixedIps ip : ips) {
488 // create vm adjacency
489 StringBuilder IpPrefixBuild = new StringBuilder(ip.getIpAddress().getIpv4Address().getValue());
490 String IpPrefix = IpPrefixBuild.append("/32").toString();
491 Adjacency vmAdj = new AdjacencyBuilder().setKey(new AdjacencyKey(IpPrefix)).setIpAddress(IpPrefix)
492 .setMacAddress(port.getMacAddress().getValue()).build();
494 // create extra route adjacency
495 if (rtr != null && rtr.getRoutes() != null) {
496 List<Routes> routeList = rtr.getRoutes();
497 List<Adjacency> erAdjList = addAdjacencyforExtraRoute(routeList, false, infName);
498 if (erAdjList != null && !erAdjList.isEmpty()) {
499 adjList.addAll(erAdjList);
503 // create vpn-interface on this neutron port
504 Adjacencies adjs = new AdjacenciesBuilder().setAdjacency(adjList).build();
505 VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName)).
506 setName(infName).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs);
507 VpnInterface vpnIf = vpnb.build();
510 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
511 logger.debug("Creating vpn interface {}", vpnIf);
512 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
513 } catch (Exception ex) {
514 logger.error("Creation of vpninterface {} failed due to {}", infName, ex);
516 if (isLockAcquired) {
517 NeutronvpnUtils.unlock(lockManager, infName);
522 protected void deleteVpnInterface(Port port) {
525 boolean isLockAcquired = false;
526 String infName = port.getUuid().getValue();
527 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
530 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
531 logger.debug("Deleting vpn interface {}", infName);
532 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
533 } catch (Exception ex) {
534 logger.error("Deletion of vpninterface {} failed due to {}", infName, ex);
536 if (isLockAcquired) {
537 NeutronvpnUtils.unlock(lockManager, infName);
543 protected void updateVpnInterface(Uuid vpnId, Port port) {
544 if (vpnId == null || port == null) {
547 boolean isLockAcquired = false;
548 String infName = port.getUuid().getValue();
549 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
551 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
552 .CONFIGURATION, vpnIfIdentifier);
553 if (optionalVpnInterface.isPresent()) {
554 VpnInterfaceBuilder vpnIfBuilder = new VpnInterfaceBuilder(optionalVpnInterface.get());
555 VpnInterface vpnIf = vpnIfBuilder.setVpnInstanceName(vpnId.getValue()).build();
556 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
557 logger.debug("Updating vpn interface {}", vpnIf);
558 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
560 logger.error("VPN Interface {} not found", infName);
562 } catch (Exception ex) {
563 logger.error("Updation of vpninterface {} failed due to {}", infName, ex);
565 if (isLockAcquired) {
566 NeutronvpnUtils.unlock(lockManager, infName);
571 public void createL3Vpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
572 Uuid router, List<Uuid> networks) {
574 // Update VPN Instance node
575 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
577 // Update local vpn-subnet DS
578 updateVpnMaps(vpn, name, router, tenant, networks);
580 if (router != null) {
581 associateRouterToVpn(vpn, router);
583 if (networks != null) {
584 associateNetworksToVpn(vpn, networks);
589 public Future<RpcResult<CreateL3VPNOutput>> createL3VPN(CreateL3VPNInput input) {
591 CreateL3VPNOutputBuilder opBuilder = new CreateL3VPNOutputBuilder();
592 SettableFuture<RpcResult<CreateL3VPNOutput>> result = SettableFuture.create();
593 List<RpcError> errorList = new ArrayList<>();
594 int failurecount = 0;
595 int warningcount = 0;
597 List<L3vpn> vpns = input.getL3vpn();
598 for (L3vpn vpn : vpns) {
599 RpcError error = null;
601 if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
602 msg = String.format("Creation of L3VPN failed for VPN %s due to absence of RD/iRT/eRT input",
603 vpn.getId().getValue());
605 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
606 errorList.add(error);
610 if (vpn.getRouteDistinguisher().size() > 1) {
611 msg = String.format("Creation of L3VPN failed for VPN %s due to multiple RD input %s",
612 vpn.getId().getValue(), vpn.getRouteDistinguisher());
614 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
615 errorList.add(error);
619 if (vpn.getRouterId() != null) {
620 if (NeutronvpnUtils.getNeutronRouter(broker, vpn.getRouterId()) == null) {
621 msg = String.format("Creation of L3VPN failed for VPN %s due to router not found %s",
622 vpn.getId().getValue(), vpn.getRouterId().getValue());
624 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
625 errorList.add(error);
629 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, vpn.getRouterId(), true);
631 msg = String.format("Creation of L3VPN failed for VPN %s due to router %s already associated to " +
632 "another VPN %s", vpn.getId().getValue(), vpn.getRouterId().getValue(), vpnId.getValue());
634 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
635 errorList.add(error);
640 if (vpn.getNetworkIds() != null) {
641 for (Uuid nw : vpn.getNetworkIds()) {
642 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
643 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
644 if (network == null) {
645 msg = String.format("Creation of L3VPN failed for VPN %s due to network not found %s",
646 vpn.getId().getValue(), nw.getValue());
648 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
649 errorList.add(error);
651 } else if (vpnId != null) {
652 msg = String.format("Creation of L3VPN failed for VPN %s due to network %s already associated" +
653 " to another VPN %s", vpn.getId().getValue(), nw.getValue(), vpnId.getValue());
655 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
656 errorList.add(error);
665 createL3Vpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(),
666 vpn.getImportRT(), vpn.getExportRT(), vpn.getRouterId(), vpn.getNetworkIds());
667 } catch (Exception ex) {
668 msg = String.format("Creation of L3VPN failed for VPN %s", vpn.getId().getValue());
669 logger.error(msg, ex);
670 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
671 errorList.add(error);
675 // if at least one succeeds; result is success
676 // if none succeeds; result is failure
677 if (failurecount + warningcount == vpns.size()) {
678 result.set(RpcResultBuilder.<CreateL3VPNOutput>failed().withRpcErrors(errorList).build());
680 List<String> errorResponseList = new ArrayList<>();
681 if (!errorList.isEmpty()) {
682 for (RpcError rpcError : errorList) {
683 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
684 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
685 errorResponseList.add(errorResponse);
688 errorResponseList.add("Operation successful with no errors");
690 opBuilder.setResponse(errorResponseList);
691 result.set(RpcResultBuilder.<CreateL3VPNOutput>success().withResult(opBuilder.build()).build());
697 public Future<RpcResult<GetL3VPNOutput>> getL3VPN(GetL3VPNInput input) {
699 GetL3VPNOutputBuilder opBuilder = new GetL3VPNOutputBuilder();
700 SettableFuture<RpcResult<GetL3VPNOutput>> result = SettableFuture.create();
701 Uuid inputVpnId = input.getId();
702 List<VpnInstance> vpns = new ArrayList<>();
705 if (inputVpnId == null) {
707 InstanceIdentifier<VpnInstances> vpnsIdentifier =
708 InstanceIdentifier.builder(VpnInstances.class).build();
709 Optional<VpnInstances> optionalVpns = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
711 if (optionalVpns.isPresent() && optionalVpns.get().getVpnInstance() != null) {
712 for (VpnInstance vpn : optionalVpns.get().getVpnInstance()) {
713 // eliminating internal VPNs from getL3VPN output
714 if (vpn.getIpv4Family().getRouteDistinguisher() != null) {
720 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
721 .withWarning(ErrorType.PROTOCOL, "", "No VPN is present").build());
725 String name = inputVpnId.getValue();
726 InstanceIdentifier<VpnInstance> vpnIdentifier =
727 InstanceIdentifier.builder(VpnInstances.class)
728 .child(VpnInstance.class, new VpnInstanceKey(name)).build();
729 // read VpnInstance Info
730 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
732 if (optionalVpn.isPresent()) {
733 vpns.add(optionalVpn.get());
735 String message = String.format("GetL3VPN failed because VPN %s is not present", name);
736 logger.error(message);
737 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
738 .withWarning(ErrorType.PROTOCOL, "invalid-value", message).build());
741 List<L3vpnInstances> l3vpnList = new ArrayList<>();
742 for (VpnInstance vpnInstance : vpns) {
743 Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName());
745 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class).child(VpnMap
746 .class, new VpnMapKey(vpnId)).build();
747 L3vpnInstancesBuilder l3vpn = new L3vpnInstancesBuilder();
749 List<String> rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(","));
750 List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
752 List<String> ertList = new ArrayList<>();
753 List<String> irtList = new ArrayList<>();
755 for (VpnTarget vpnTarget : vpnTargetList) {
756 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
757 ertList.add(vpnTarget.getVrfRTValue());
759 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ImportExtcommunity) {
760 irtList.add(vpnTarget.getVrfRTValue());
762 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.Both) {
763 ertList.add(vpnTarget.getVrfRTValue());
764 irtList.add(vpnTarget.getVrfRTValue());
768 l3vpn.setId(vpnId).setRouteDistinguisher(rd).setImportRT(irtList).setExportRT(ertList);
769 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
771 if (optionalVpnMap.isPresent()) {
772 VpnMap vpnMap = optionalVpnMap.get();
773 l3vpn.setRouterId(vpnMap.getRouterId()).setNetworkIds(vpnMap.getNetworkIds())
774 .setTenantId(vpnMap.getTenantId()).setName(vpnMap.getName());
776 l3vpnList.add(l3vpn.build());
779 opBuilder.setL3vpnInstances(l3vpnList);
780 result.set(RpcResultBuilder.<GetL3VPNOutput>success().withResult(opBuilder.build()).build());
782 } catch (Exception ex) {
783 String message = String.format("GetL3VPN failed due to %s", ex.getMessage());
784 logger.error(message, ex);
785 result.set(RpcResultBuilder.<GetL3VPNOutput>failed().withError(ErrorType.APPLICATION, message).build());
791 public Future<RpcResult<DeleteL3VPNOutput>> deleteL3VPN(DeleteL3VPNInput input) {
793 DeleteL3VPNOutputBuilder opBuilder = new DeleteL3VPNOutputBuilder();
794 SettableFuture<RpcResult<DeleteL3VPNOutput>> result = SettableFuture.create();
795 List<RpcError> errorList = new ArrayList<>();
797 int failurecount = 0;
798 int warningcount = 0;
799 List<Uuid> vpns = input.getId();
800 for (Uuid vpn : vpns) {
804 InstanceIdentifier<VpnInstance> vpnIdentifier =
805 InstanceIdentifier.builder(VpnInstances.class)
806 .child(VpnInstance.class, new VpnInstanceKey(vpn.getValue())).build();
807 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
809 if (optionalVpn.isPresent()) {
812 msg = String.format("VPN with vpnid: %s does not exist", vpn.getValue());
814 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-value", msg);
815 errorList.add(error);
818 } catch (Exception ex) {
819 msg = String.format("Deletion of L3VPN failed when deleting for uuid %s", vpn.getValue());
820 logger.error(msg, ex);
821 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
822 errorList.add(error);
826 // if at least one succeeds; result is success
827 // if none succeeds; result is failure
828 if (failurecount + warningcount == vpns.size()) {
829 result.set(RpcResultBuilder.<DeleteL3VPNOutput>failed().withRpcErrors(errorList).build());
831 List<String> errorResponseList = new ArrayList<>();
832 if (!errorList.isEmpty()) {
833 for (RpcError rpcError : errorList) {
834 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
835 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
836 errorResponseList.add(errorResponse);
839 errorResponseList.add("Operation successful with no errors");
841 opBuilder.setResponse(errorResponseList);
842 result.set(RpcResultBuilder.<DeleteL3VPNOutput>success().withResult(opBuilder.build()).build());
847 protected void addSubnetToVpn(Uuid vpnId, Uuid subnet) {
848 logger.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
849 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null);
850 boolean isLockAcquired = false;
851 String lockName = vpnId.getValue() + subnet.getValue();
852 String elanInstanceName = sn.getNetworkId().getValue();
853 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
854 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
855 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
857 //TODO: Cache it in add so can reuse it in update and delete. Best would be to cache in some ElanUtils
858 long elanTag = elanInstance.get().getElanTag();
859 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
860 if (vpnId.equals(routerId)) {
861 isExternalVpn = false;
863 isExternalVpn = true;
866 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
867 checkAndPublishSubnetAddNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag);
868 logger.debug("Subnet added to Vpn notification sent");
869 }catch (Exception e){
870 logger.error("Subnet added to Vpn notification failed",e);
872 if (isLockAcquired) {
873 NeutronvpnUtils.unlock(lockManager, lockName);
876 // Check if there are ports on this subnet and add corresponding vpn-interfaces
877 List<Uuid> portList = sn.getPortList();
878 if (portList != null) {
879 for (Uuid port : sn.getPortList()) {
880 logger.debug("adding vpn-interface for port {}", port.getValue());
881 createVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port));
886 protected void updateVpnForSubnet(Uuid vpnId, Uuid subnet, boolean isBeingAssociated) {
887 logger.debug("Updating VPN {} for subnet {}", vpnId.getValue(), subnet.getValue());
888 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null);
889 boolean isLockAcquired = false;
890 String lockName = vpnId.getValue() + subnet.getValue();
891 String elanInstanceName = sn.getNetworkId().getValue();
892 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
893 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
894 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
896 long elanTag = elanInstance.get().getElanTag();
898 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
899 checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated,
901 logger.debug("Subnet updated in Vpn notification sent");
902 } catch (Exception e) {
903 logger.error("Subnet updated in Vpn notification failed", e);
905 if (isLockAcquired) {
906 NeutronvpnUtils.unlock(lockManager, lockName);
909 // Check for ports on this subnet and update association of corresponding vpn-interfaces to external vpn
910 List<Uuid> portList = sn.getPortList();
911 if (portList != null) {
912 for (Uuid port : sn.getPortList()) {
913 logger.debug("Updating vpn-interface for port {}", port.getValue());
914 updateVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port));
919 InstanceIdentifier<RouterInterfaces> getRouterInterfacesId(Uuid routerId) {
920 return InstanceIdentifier.builder(RouterInterfacesMap.class)
921 .child(RouterInterfaces.class, new RouterInterfacesKey(routerId)).build();
923 void addToNeutronRouterInterfacesMap(Uuid routerId, String interfaceName) {
924 InstanceIdentifier<RouterInterfaces> routerInterfacesId = getRouterInterfacesId(routerId);
925 Optional<RouterInterfaces> optRouterInterfaces = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
926 Interfaces routerInterface = new InterfacesBuilder().setKey(new InterfacesKey(interfaceName)).setInterfaceId(interfaceName).build();
927 if(optRouterInterfaces.isPresent()) {
928 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId.child(Interfaces.class, new InterfacesKey(interfaceName)), routerInterface);
930 RouterInterfacesBuilder builder = new RouterInterfacesBuilder().setRouterId(routerId);
931 List<Interfaces> interfaces = new ArrayList<>();
932 interfaces.add(routerInterface);
933 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId, builder.setInterfaces(interfaces).build());
937 void removeFromNeutronRouterInterfacesMap(Uuid routerId, String interfaceName) {
938 InstanceIdentifier<RouterInterfaces> routerInterfacesId = getRouterInterfacesId(routerId);
939 Optional<RouterInterfaces> optRouterInterfaces = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
940 Interfaces routerInterface = new InterfacesBuilder().setKey(new InterfacesKey(interfaceName)).setInterfaceId(interfaceName).build();
941 if(optRouterInterfaces.isPresent()) {
942 RouterInterfaces routerInterfaces = optRouterInterfaces.get();
943 List<Interfaces> interfaces = routerInterfaces.getInterfaces();
944 if(interfaces != null && interfaces.remove(routerInterface)) {
945 if(interfaces.isEmpty()) {
946 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
948 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId.child(Interfaces.class, new InterfacesKey(interfaceName)));
954 protected List<Adjacency> addAdjacencyforExtraRoute(List<Routes> routeList, boolean rtrUp, String vpnifname) {
955 List<Adjacency> adjList = new ArrayList<>();
956 for (Routes route : routeList) {
957 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
958 boolean isLockAcquired = false;
959 String nextHop = String.valueOf(route.getNexthop().getValue());
960 String destination = String.valueOf(route.getDestination().getValue());
962 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
963 logger.trace("Adding extra route with nexthop {}, destination {}, infName {}", nextHop,
964 destination, infName);
965 Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey
966 (new AdjacencyKey(destination)).build();
968 if (infName.equals(vpnifname)) {
973 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
974 child(VpnInterface.class, new VpnInterfaceKey(infName)).build();
976 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
977 .CONFIGURATION, vpnIfIdentifier);
978 if (optionalVpnInterface.isPresent()) {
979 Adjacencies erAdjs = new AdjacenciesBuilder().setAdjacency(Arrays.asList(erAdj)).build();
980 VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName))
981 .addAugmentation(Adjacencies.class, erAdjs).build();
982 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
983 logger.debug("Adding extra route {}", route);
984 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
986 logger.error("VM adjacency for interface {} not present ; cannot add extra route adjacency",
989 } catch (Exception e) {
990 logger.error("exception in adding extra route: {}" + e);
992 if (isLockAcquired) {
993 NeutronvpnUtils.unlock(lockManager, infName);
997 logger.error("Incorrect input received for extra route. {}", route);
1003 protected void removeAdjacencyforExtraRoute(List<Routes> routeList) {
1004 for (Routes route : routeList) {
1005 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
1006 boolean isLockAcquired = false;
1007 String nextHop = String.valueOf(route.getNexthop().getValue());
1008 String destination = String.valueOf(route.getDestination().getValue());
1010 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
1011 logger.trace("Removing extra route with nexthop {}, destination {}, infName {}", nextHop,
1012 destination, infName);
1013 InstanceIdentifier<Adjacency> adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
1014 child(VpnInterface.class, new VpnInterfaceKey(infName)).augmentation(Adjacencies.class)
1015 .child(Adjacency.class, new AdjacencyKey(destination)).build();
1017 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
1018 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, adjacencyIdentifier);
1019 logger.trace("extra route {} deleted successfully", route);
1020 } catch (Exception e) {
1021 logger.error("exception in deleting extra route: {}" + e);
1023 if (isLockAcquired) {
1024 NeutronvpnUtils.unlock(lockManager, infName);
1028 logger.error("Incorrect input received for extra route. {}", route);
1033 protected void removeL3Vpn(Uuid id) {
1035 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, id);
1036 Uuid router = vpnMap.getRouterId();
1037 // dissociate router
1038 if (router != null) {
1039 dissociateRouterFromVpn(id, router);
1041 // dissociate networks
1042 if (!id.equals(router)) {
1043 dissociateNetworksFromVpn(id, vpnMap.getNetworkIds());
1045 // remove entire vpnMaps node
1046 deleteVpnMapsNode(id);
1048 // remove vpn-instance
1049 deleteVpnInstance(id);
1052 protected void removeSubnetFromVpn(Uuid vpnId, Uuid subnet) {
1053 logger.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
1054 Subnetmap sn = NeutronvpnUtils.getSubnetmap(broker, subnet);
1055 boolean isLockAcquired = false;
1056 String lockName = vpnId.getValue() + subnet.getValue();
1057 String elanInstanceName = sn.getNetworkId().getValue();
1058 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
1059 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
1060 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1062 long elanTag = elanInstance.get().getElanTag();
1063 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
1064 if (vpnId.equals(routerId)) {
1065 isExternalVpn = false;
1067 isExternalVpn = true;
1070 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
1071 checkAndPublishSubnetDelNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag);
1072 logger.debug("Subnet removed from Vpn notification sent");
1073 }catch (Exception e){
1074 logger.error("Subnet removed from Vpn notification failed",e);
1076 if (isLockAcquired) {
1077 NeutronvpnUtils.unlock(lockManager, lockName);
1081 // Check if there are ports on this subnet; remove corresponding vpn-interfaces
1082 List<Uuid> portList = sn.getPortList();
1083 if (portList != null) {
1084 for (Uuid port : sn.getPortList()) {
1085 logger.debug("removing vpn-interface for port {}", port.getValue());
1086 deleteVpnInterface(NeutronvpnUtils.getNeutronPort(broker, port));
1087 if (routerId != null) {
1088 removeFromNeutronRouterInterfacesMap(routerId, port.getValue());
1092 // update subnet-vpn association
1093 removeFromSubnetNode(subnet, null, null, vpnId, null);
1095 logger.warn("Subnetmap for subnet {} not found", subnet.getValue());
1099 protected void associateRouterToVpn(Uuid vpnId, Uuid routerId) {
1100 updateVpnMaps(vpnId, null, routerId, null, null);
1101 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1102 if (!vpnId.equals(routerId)) {
1103 logger.debug("Updating association of subnets to external vpn {}", vpnId.getValue());
1104 if (routerSubnets != null) {
1105 for (Uuid subnetId : routerSubnets) {
1106 updateVpnForSubnet(vpnId, subnetId, true);
1110 checkAndPublishRouterAssociatedtoVpnNotification(routerId, vpnId);
1111 logger.debug("notification upon association of router {} to VPN {} published", routerId.getValue(),
1113 } catch (Exception e) {
1114 logger.error("publishing of notification upon association of router {} to VPN {} failed : ", routerId
1115 .getValue(), vpnId.getValue(), e);
1118 logger.debug("Adding subnets to internal vpn {}", vpnId.getValue());
1119 for (Uuid subnet : routerSubnets) {
1120 addSubnetToVpn(vpnId, subnet);
1125 protected void dissociateRouterFromVpn(Uuid vpnId, Uuid routerId) {
1127 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1128 if (routerSubnets != null) {
1129 for (Uuid subnetId : routerSubnets) {
1130 logger.debug("Updating association of subnets to internal vpn {}", routerId.getValue());
1131 updateVpnForSubnet(routerId, subnetId,false);
1134 clearFromVpnMaps(vpnId, routerId, null);
1136 checkAndPublishRouterDisassociatedFromVpnNotification(routerId, vpnId);
1137 logger.debug("notification upon disassociation of router {} from VPN {} published", routerId.getValue(),
1139 } catch (Exception e) {
1140 logger.error("publishing of notification upon disassociation of router {} from VPN {} failed : ", routerId
1141 .getValue(), vpnId.getValue(), e);
1145 protected List<String> associateNetworksToVpn(Uuid vpn, List<Uuid> networks) {
1147 List<String> failedNwList = new ArrayList<String>();
1148 List<Uuid> passedNwList = new ArrayList<>();
1149 if (!networks.isEmpty()) {
1150 // process corresponding subnets for VPN
1151 for (Uuid nw : networks) {
1152 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
1153 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
1154 if (network == null) {
1155 failedNwList.add(String.format("network %s not found", nw.getValue()));
1156 } else if (vpnId != null) {
1157 failedNwList.add(String.format("network %s already associated to another VPN %s", nw.getValue(),
1160 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1161 logger.debug("Adding network subnets...{}", networkSubnets);
1162 if (networkSubnets != null) {
1163 for (Uuid subnet : networkSubnets) {
1164 // check if subnet added as router interface to some router
1165 Uuid subnetVpnId = NeutronvpnUtils.getVpnForSubnet(broker, subnet);
1166 if (subnetVpnId == null) {
1167 addSubnetToVpn(vpn, subnet);
1168 passedNwList.add(nw);
1170 failedNwList.add(String.format("subnet %s already added as router interface bound to " +
1171 "internal/external VPN %s", subnet.getValue (), subnetVpnId.getValue()));
1175 if (network.getAugmentation(NetworkL3Extension.class) != null && network.getAugmentation
1176 (NetworkL3Extension.class).isExternal()) {
1177 nvpnNatManager.addExternalNetworkToVpn(network, vpn);
1181 updateVpnMaps(vpn, null, null, null, passedNwList);
1183 return failedNwList;
1186 protected List<String> dissociateNetworksFromVpn(Uuid vpn, List<Uuid> networks) {
1188 List<String> failedNwList = new ArrayList<String>();
1189 List<Uuid> passedNwList = new ArrayList<>();
1190 if (networks != null && !networks.isEmpty()) {
1191 // process corresponding subnets for VPN
1192 for (Uuid nw : networks) {
1193 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
1194 if (network == null) {
1195 failedNwList.add(String.format("network %s not found", nw.getValue()));
1197 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
1198 if (vpn.equals(vpnId)) {
1199 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1200 logger.debug("Removing network subnets...");
1201 if (networkSubnets != null) {
1202 for (Uuid subnet : networkSubnets) {
1203 removeSubnetFromVpn(vpn, subnet);
1204 passedNwList.add(nw);
1208 if (vpnId == null) {
1209 failedNwList.add(String.format("input network %s not associated to any vpn yet", nw
1212 failedNwList.add(String.format("input network %s associated to a another vpn %s instead " +
1213 "of the one given as input", nw.getValue(), vpnId.getValue()));
1216 if (network.getAugmentation(NetworkL3Extension.class).isExternal()) {
1217 nvpnNatManager.removeExternalNetworkFromVpn(network);
1221 clearFromVpnMaps(vpn, null, passedNwList);
1223 return failedNwList;
1227 public Future<RpcResult<AssociateNetworksOutput>> associateNetworks(AssociateNetworksInput input) {
1229 AssociateNetworksOutputBuilder opBuilder = new AssociateNetworksOutputBuilder();
1230 SettableFuture<RpcResult<AssociateNetworksOutput>> result = SettableFuture.create();
1231 logger.debug("associateNetworks {}", input);
1232 StringBuilder returnMsg = new StringBuilder();
1233 Uuid vpnId = input.getVpnId();
1236 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1237 List<Uuid> netIds = input.getNetworkId();
1238 if (netIds != null && !netIds.isEmpty()) {
1239 List<String> failed = associateNetworksToVpn(vpnId, netIds);
1240 if (!failed.isEmpty()) {
1241 returnMsg.append(failed);
1245 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1247 if (returnMsg.length() != 0) {
1248 String message = String.format("associate Networks to vpn %s failed due to %s", vpnId.getValue(),
1250 logger.error(message);
1251 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1253 opBuilder.setResponse(errorResponse);
1254 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().withResult(opBuilder.build()).build());
1256 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().build());
1258 } catch (Exception ex) {
1259 String message = String.format("associate Networks to vpn %s failed due to %s", input.getVpnId().getValue(),
1261 logger.error(message, ex);
1262 result.set(RpcResultBuilder.<AssociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1265 logger.debug("associateNetworks returns..");
1270 public Future<RpcResult<Void>> associateRouter(AssociateRouterInput input) {
1272 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1273 logger.debug("associateRouter {}", input);
1274 StringBuilder returnMsg = new StringBuilder();
1275 Uuid vpnId = input.getVpnId();
1276 Uuid routerId = input.getRouterId();
1278 if (routerId != null && vpnId != null) {
1279 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1280 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, vpnId);
1281 if (rtr != null && vpnMap != null) {
1282 Uuid extVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1283 if (vpnMap.getRouterId() != null) {
1284 returnMsg.append("vpn ").append(vpnId.getValue()).append(" already associated to router ")
1285 .append(vpnMap.getRouterId().getValue());
1286 } else if (extVpnId != null) {
1287 returnMsg.append("router ").append(routerId.getValue()).append(" already associated to " +
1288 "another VPN ").append(extVpnId.getValue());
1290 associateRouterToVpn(vpnId, routerId);
1293 returnMsg.append("router not found : ").append(routerId.getValue());
1296 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1298 if (returnMsg.length() != 0) {
1299 String message = String.format("associate router to vpn %s failed due to %s", routerId.getValue(),
1301 logger.error(message);
1302 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1305 result.set(RpcResultBuilder.<Void>success().build());
1307 } catch (Exception ex) {
1308 String message = String.format("associate router %s to vpn %s failed due to %s", routerId.getValue(),
1309 vpnId.getValue(), ex.getMessage());
1310 logger.error(message, ex);
1311 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1313 logger.debug("associateRouter returns..");
1318 public Future<RpcResult<DissociateNetworksOutput>> dissociateNetworks(DissociateNetworksInput input) {
1320 DissociateNetworksOutputBuilder opBuilder = new DissociateNetworksOutputBuilder();
1321 SettableFuture<RpcResult<DissociateNetworksOutput>> result = SettableFuture.create();
1323 logger.debug("dissociateNetworks {}", input);
1324 StringBuilder returnMsg = new StringBuilder();
1325 Uuid vpnId = input.getVpnId();
1328 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1329 List<Uuid> netIds = input.getNetworkId();
1330 if (netIds != null && !netIds.isEmpty()) {
1331 List<String> failed = dissociateNetworksFromVpn(vpnId, netIds);
1332 if (!failed.isEmpty()) {
1333 returnMsg.append(failed);
1337 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1339 if (returnMsg.length() != 0) {
1340 String message = String.format("dissociate Networks to vpn %s failed due to %s", vpnId.getValue(),
1342 logger.error(message);
1343 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1345 opBuilder.setResponse(errorResponse);
1346 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().withResult(opBuilder.build()).build());
1348 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().build());
1350 } catch (Exception ex) {
1351 String message = String.format("dissociate Networks to vpn %s failed due to %s", input.getVpnId().
1352 getValue(), ex.getMessage());
1353 logger.error(message, ex);
1354 result.set(RpcResultBuilder.<DissociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1357 logger.debug("dissociateNetworks returns..");
1362 public Future<RpcResult<Void>> dissociateRouter(DissociateRouterInput input) {
1364 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1366 logger.debug("dissociateRouter {}", input);
1367 StringBuilder returnMsg = new StringBuilder();
1368 Uuid vpnId = input.getVpnId();
1369 Uuid routerId = input.getRouterId();
1371 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1372 if (routerId != null) {
1373 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1375 Uuid routerVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1376 if (vpnId.equals(routerVpnId)) {
1377 dissociateRouterFromVpn(vpnId, routerId);
1379 if (routerVpnId == null) {
1380 returnMsg.append("input router ").append(routerId.getValue()).append(" not associated" +
1383 returnMsg.append("input router ").append(routerId.getValue()).append(" associated to " +
1384 "vpn ").append(routerVpnId.getValue()).append("instead of the vpn given as " +
1389 returnMsg.append("router not found : ").append(routerId.getValue());
1393 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1395 if (returnMsg.length() != 0) {
1396 String message = String.format("dissociate router %s to vpn %s failed due to %s", routerId.getValue(),
1397 vpnId.getValue(), returnMsg);
1398 logger.error(message);
1399 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1401 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1404 result.set(RpcResultBuilder.<Void>success().build());
1406 } catch (Exception ex) {
1407 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1408 vpnId.getValue(), ex.getMessage());
1409 logger.error(message, ex);
1410 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1412 logger.debug("dissociateRouter returns..");
1418 public Future<RpcResult<GetFixedIPsForNeutronPortOutput>> getFixedIPsForNeutronPort(GetFixedIPsForNeutronPortInput
1420 GetFixedIPsForNeutronPortOutputBuilder opBuilder = new GetFixedIPsForNeutronPortOutputBuilder();
1421 SettableFuture<RpcResult<GetFixedIPsForNeutronPortOutput>> result = SettableFuture.create();
1422 Uuid portId = input.getPortId();
1423 StringBuilder returnMsg = new StringBuilder();
1425 List<String> fixedIPList = new ArrayList<>();
1426 Port port = NeutronvpnUtils.getNeutronPort(broker, portId);
1428 List<FixedIps> fixedIPs = port.getFixedIps();
1429 for (FixedIps ip : fixedIPs) {
1430 fixedIPList.add(ip.getIpAddress().getIpv4Address().getValue());
1433 returnMsg.append("neutron port: ").append(portId.getValue()).append(" not found");
1435 if (returnMsg.length() != 0) {
1436 String message = String.format("Retrieval of FixedIPList for neutron port failed due to %s", returnMsg);
1437 logger.error(message);
1438 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>failed().withWarning(ErrorType.PROTOCOL,
1439 "invalid-value", message).build());
1441 opBuilder.setFixedIPs(fixedIPList);
1442 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>success().withResult(opBuilder.build())
1444 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>success().build());
1446 } catch (Exception ex) {
1447 String message = String.format("Retrieval of FixedIPList for neutron port %s failed due to %s", portId
1448 .getValue(), ex.getMessage());
1449 logger.error(message, ex);
1450 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>failed().withError(ErrorType.APPLICATION,
1456 protected void handleNeutronRouterDeleted(Uuid routerId, List<Uuid> routerSubnetIds) {
1457 // check if the router is associated to some VPN
1458 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1459 if (vpnId != null) {
1460 // remove existing external vpn interfaces
1461 for (Uuid subnetId : routerSubnetIds) {
1462 removeSubnetFromVpn(vpnId, subnetId);
1464 clearFromVpnMaps(vpnId, routerId, null);
1466 // remove existing internal vpn interfaces
1467 for (Uuid subnetId : routerSubnetIds) {
1468 removeSubnetFromVpn(routerId, subnetId);
1471 // delete entire vpnMaps node for internal VPN
1472 deleteVpnMapsNode(routerId);
1474 // delete vpn-instance for internal VPN
1475 deleteVpnInstance(routerId);
1478 protected Subnet getNeutronSubnet(Uuid subnetId){
1479 return NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
1482 protected IpAddress getNeutronSubnetGateway(Uuid subnetId) {
1483 Subnet sn = NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
1485 return sn.getGatewayIp();
1490 protected Port getNeutronPort(String name) {
1491 return NeutronvpnUtils.getNeutronPort(broker, new Uuid(name));
1494 protected Port getNeutronPort(Uuid portId) {
1495 return NeutronvpnUtils.getNeutronPort(broker, portId);
1498 protected List<Uuid> getSubnetsforVpn(Uuid vpnid) {
1499 List<Uuid> subnets = new ArrayList<>();
1501 InstanceIdentifier<Subnetmaps> subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build();
1502 Optional<Subnetmaps> subnetmaps = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1504 if (subnetmaps.isPresent() && subnetmaps.get().getSubnetmap() != null) {
1505 List<Subnetmap> subnetMapList = subnetmaps.get().getSubnetmap();
1506 for (Subnetmap subnetMap : subnetMapList) {
1507 if (subnetMap.getVpnId() != null && subnetMap.getVpnId().equals(vpnid)) {
1508 subnets.add(subnetMap.getId());
1515 public List<String> showNeutronPortsCLI() {
1516 List<String> result = new ArrayList<>();
1517 result.add(String.format(" %-34s %-22s %-22s %-6s ", "PortName", "Mac Address", "IP Address",
1519 result.add("---------------------------------------------------------------------------------------");
1520 InstanceIdentifier<Ports> portidentifier = InstanceIdentifier.create(Neutron.class).child(Ports.class);
1522 Optional<Ports> ports = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, portidentifier);
1523 if (ports.isPresent() && ports.get().getPort() != null) {
1524 for (Port port : ports.get().getPort()) {
1525 if (port.getFixedIps() != null && !port.getFixedIps().isEmpty()) {
1526 result.add(String.format(" %-34s %-22s %-22s %-6s ", port.getUuid().getValue(), port.
1527 getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address().getValue(),
1528 NeutronvpnUtils.getIPPrefixFromPort(broker, port)));
1532 } catch (Exception e) {
1533 logger.error("Failed to retrieve neutronPorts info : ", e);
1534 System.out.println("Failed to retrieve neutronPorts info : " + e.getMessage());
1539 public List<String> showVpnConfigCLI(Uuid vpnuuid) {
1540 List<String> result = new ArrayList<>();
1541 if (vpnuuid == null) {
1542 System.out.println("");
1543 System.out.println("Displaying VPN config for all VPNs");
1544 System.out.println("To display VPN config for a particular VPN, use the following syntax");
1545 System.out.println(getshowVpnConfigCLIHelp());
1548 RpcResult<GetL3VPNOutput> rpcResult = getL3VPN(new GetL3VPNInputBuilder().setId(vpnuuid).build()).get();
1549 if (rpcResult.isSuccessful()) {
1551 result.add(String.format(" %-37s %-37s %-7s ", "VPN ID", "Tenant ID", "RD"));
1553 result.add(String.format(" %-80s ", "Import-RTs"));
1555 result.add(String.format(" %-80s ", "Export-RTs"));
1557 result.add(String.format(" %-76s ", "Subnet IDs"));
1559 result.add("------------------------------------------------------------------------------------");
1561 List<L3vpnInstances> VpnList = rpcResult.getResult().getL3vpnInstances();
1562 for (L3vpnInstance Vpn : VpnList) {
1563 String tenantId = Vpn.getTenantId() != null ? Vpn.getTenantId().getValue() : "\" " +
1565 result.add(String.format(" %-37s %-37s %-7s ", Vpn.getId().getValue(), tenantId, Vpn
1566 .getRouteDistinguisher()));
1568 result.add(String.format(" %-80s ", Vpn.getImportRT()));
1570 result.add(String.format(" %-80s ", Vpn.getExportRT()));
1573 Uuid vpnid = Vpn.getId();
1574 List<Uuid> subnetList = getSubnetsforVpn(vpnid);
1575 if (!subnetList.isEmpty()) {
1576 for (Uuid subnetuuid : subnetList) {
1577 result.add(String.format(" %-76s ", subnetuuid.getValue()));
1580 result.add(String.format(" %-76s ", "\" \""));
1583 result.add("----------------------------------------");
1587 String errortag = rpcResult.getErrors().iterator().next().getTag();
1588 if (errortag == "") {
1589 System.out.println("");
1590 System.out.println("No VPN has been configured yet");
1591 } else if (errortag == "invalid-value") {
1592 System.out.println("");
1593 System.out.println("VPN " + vpnuuid.getValue() + " is not present");
1595 System.out.println("error getting VPN info : " + rpcResult.getErrors());
1596 System.out.println(getshowVpnConfigCLIHelp());
1599 } catch (InterruptedException | ExecutionException e) {
1600 logger.error("error getting VPN info : ", e);
1601 System.out.println("error getting VPN info : " + e.getMessage());
1606 private String getshowVpnConfigCLIHelp() {
1607 StringBuilder help = new StringBuilder("Usage:");
1608 help.append("display vpn-config [-vid/--vpnid <id>]");
1609 return help.toString();
1612 private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName,
1613 Boolean isExternalvpn, Long elanTag)throws InterruptedException {
1614 SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder();
1616 logger.info("publish notification called");
1618 builder.setSubnetId(subnetId);
1619 builder.setSubnetIp(subnetIp);
1620 builder.setVpnName(vpnName);
1621 builder.setExternalVpn(isExternalvpn);
1622 builder.setElanTag(elanTag);
1624 notificationPublishService.putNotification(builder.build());
1627 private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName,
1628 Boolean isExternalvpn, Long elanTag) throws InterruptedException {
1629 SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder();
1631 logger.info("publish notification called");
1633 builder.setSubnetId(subnetId);
1634 builder.setSubnetIp(subnetIp);
1635 builder.setVpnName(vpnName);
1636 builder.setExternalVpn(isExternalvpn);
1637 builder.setElanTag(elanTag);
1639 notificationPublishService.putNotification(builder.build());
1642 private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName,
1643 Boolean isExternalvpn, Long elanTag) throws InterruptedException {
1644 SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder();
1646 logger.info("publish notification called");
1648 builder.setSubnetId(subnetId);
1649 builder.setSubnetIp(subnetIp);
1650 builder.setVpnName(vpnName);
1651 builder.setExternalVpn(isExternalvpn);
1652 builder.setElanTag(elanTag);
1654 notificationPublishService.putNotification(builder.build());
1657 private void checkAndPublishRouterAssociatedtoVpnNotification(Uuid routerId, Uuid vpnId) throws
1658 InterruptedException {
1659 RouterAssociatedToVpn routerAssociatedToVpn = new RouterAssociatedToVpnBuilder().setRouterId(routerId)
1660 .setVpnId(vpnId).build();
1661 logger.info("publishing notification upon association of router to VPN");
1662 notificationPublishService.putNotification(routerAssociatedToVpn);
1665 private void checkAndPublishRouterDisassociatedFromVpnNotification(Uuid routerId, Uuid vpnId) throws
1666 InterruptedException {
1667 RouterDisassociatedFromVpn routerDisassociatedFromVpn = new RouterDisassociatedFromVpnBuilder().setRouterId
1668 (routerId).setVpnId(vpnId).build();
1669 logger.info("publishing notification upon disassociation of router from VPN");
1670 notificationPublishService.putNotification(routerDisassociatedFromVpn);