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 private NeutronFloatingToFixedIpMappingChangeListener floatingIpMapListener;
128 Boolean isExternalVpn;
131 * @param db - dataBroker reference
132 * @param mdsalManager - MDSAL Util API access
134 public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager,NotificationPublishService notiPublishService,
135 NotificationService notiService, NeutronvpnNatManager vpnNatMgr,
136 NeutronFloatingToFixedIpMappingChangeListener neutronFloatingToFixedIpMappingChangeListener) {
138 mdsalUtil = mdsalManager;
139 nvpnNatManager = vpnNatMgr;
140 notificationPublishService = notiPublishService;
141 notificationService = notiService;
142 floatingIpMapListener = neutronFloatingToFixedIpMappingChangeListener;
145 public void setLockManager(LockManagerService lockManager) {
146 this.lockManager = lockManager;
150 public void close() throws Exception {
151 logger.info("Neutron VPN Manager Closed");
154 protected Subnetmap updateSubnetNode(Uuid subnetId, String subnetIp, Uuid tenantId, Uuid networkId, Uuid routerId,
155 Uuid vpnId, Uuid portId) {
156 Subnetmap subnetmap = null;
157 SubnetmapBuilder builder = null;
158 boolean isLockAcquired = false;
159 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
160 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
162 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
163 logger.debug("updating Subnet :read: ");
164 if (sn.isPresent()) {
165 builder = new SubnetmapBuilder(sn.get());
166 logger.debug("updating Subnet :existing: ");
168 builder = new SubnetmapBuilder().setKey(new SubnetmapKey(subnetId)).setId(subnetId);
169 logger.debug("updating Subnet :new: ");
172 if (subnetIp != null) {
173 builder.setSubnetIp(subnetIp);
175 if (routerId != null) {
176 builder.setRouterId(routerId);
178 if (networkId != null) {
179 builder.setNetworkId(networkId);
182 builder.setVpnId(vpnId);
184 if (tenantId != null) {
185 builder.setTenantId(tenantId);
188 if (portId != null) {
189 List<Uuid> portList = builder.getPortList();
190 if (portList == null) {
191 portList = new ArrayList<>();
193 portList.add(portId);
194 builder.setPortList(portList);
197 subnetmap = builder.build();
198 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
199 logger.debug("Creating/Updating subnetMap node: {} ", subnetId.getValue());
200 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
201 } catch (Exception e) {
202 logger.error("Updation of subnetMap failed for node: {}", subnetId.getValue());
204 if (isLockAcquired) {
205 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
211 protected Subnetmap removeFromSubnetNode(Uuid subnetId, Uuid networkId, Uuid routerId, Uuid vpnId, Uuid portId) {
212 Subnetmap subnetmap = null;
213 boolean isLockAcquired = false;
214 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
215 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
217 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
218 if (sn.isPresent()) {
219 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
220 if (routerId != null) {
221 builder.setRouterId(null);
223 if (networkId != null) {
224 builder.setNetworkId(null);
227 builder.setVpnId(null);
229 if (portId != null && builder.getPortList() != null) {
230 List<Uuid> portList = builder.getPortList();
231 portList.remove(portId);
232 builder.setPortList(portList);
235 subnetmap = builder.build();
236 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
237 logger.debug("Removing from existing subnetmap node: {} ", subnetId.getValue());
238 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
240 logger.warn("removing from non-existing subnetmap node: {} ", subnetId.getValue());
242 } catch (Exception e) {
243 logger.error("Removal from subnetmap failed for node: {}", subnetId.getValue());
245 if (isLockAcquired) {
246 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
252 protected void deleteSubnetMapNode(Uuid subnetId) {
253 boolean isLockAcquired = false;
254 InstanceIdentifier<Subnetmap> subnetMapIdentifier = InstanceIdentifier.builder(Subnetmaps.class)
255 .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
256 logger.debug("removing subnetMap node: {} ", subnetId.getValue());
258 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
259 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, subnetMapIdentifier);
260 } catch (Exception e) {
261 logger.error("Delete subnetMap node failed for subnet : {} ", subnetId.getValue());
263 if (isLockAcquired) {
264 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
269 private void updateVpnInstanceNode(String vpnName, List<String> rd, List<String> irt, List<String> ert) {
271 VpnInstanceBuilder builder = null;
272 List<VpnTarget> vpnTargetList = new ArrayList<>();
273 boolean isLockAcquired = false;
274 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
275 child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
277 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
279 logger.debug("Creating/Updating a new vpn-instance node: {} ", vpnName);
280 if (optionalVpn.isPresent()) {
281 builder = new VpnInstanceBuilder(optionalVpn.get());
282 logger.debug("updating existing vpninstance node");
284 builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(vpnName)).setVpnInstanceName(vpnName);
286 if (irt != null && !irt.isEmpty()) {
287 if (ert != null && !ert.isEmpty()) {
288 List<String> commonRT = new ArrayList<>(irt);
289 commonRT.retainAll(ert);
291 for (String common : commonRT) {
294 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(common)).setVrfRTValue
295 (common).setVrfRTType(VpnTarget.VrfRTType.Both).build();
296 vpnTargetList.add(vpnTarget);
299 for (String importRT : irt) {
300 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(importRT)).setVrfRTValue
301 (importRT).setVrfRTType(VpnTarget.VrfRTType.ImportExtcommunity).build();
302 vpnTargetList.add(vpnTarget);
306 if (ert != null && !ert.isEmpty()) {
307 for (String exportRT : ert) {
308 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(exportRT)).setVrfRTValue
309 (exportRT).setVrfRTType(VpnTarget.VrfRTType.ExportExtcommunity).build();
310 vpnTargetList.add(vpnTarget);
314 VpnTargets vpnTargets = new VpnTargetsBuilder().setVpnTarget(vpnTargetList).build();
316 Ipv4FamilyBuilder ipv4vpnBuilder = new Ipv4FamilyBuilder().setVpnTargets(vpnTargets);
318 if (rd != null && !rd.isEmpty()) {
319 ipv4vpnBuilder.setRouteDistinguisher(rd.get(0));
322 VpnInstance newVpn = builder.setIpv4Family(ipv4vpnBuilder.build()).build();
323 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnName);
324 logger.debug("Creating/Updating vpn-instance for {} ", vpnName);
325 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier, newVpn);
326 } catch (Exception e) {
327 logger.error("Update VPN Instance node failed for node: {} {} {} {}", vpnName, rd, irt, ert);
329 if (isLockAcquired) {
330 NeutronvpnUtils.unlock(lockManager, vpnName);
335 private void deleteVpnMapsNode(Uuid vpnid) {
336 boolean isLockAcquired = false;
337 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
338 .child(VpnMap.class, new VpnMapKey(vpnid)).build();
339 logger.debug("removing vpnMaps node: {} ", vpnid.getValue());
341 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnid.getValue());
342 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
343 } catch (Exception e) {
344 logger.error("Delete vpnMaps node failed for vpn : {} ", vpnid.getValue());
346 if (isLockAcquired) {
347 NeutronvpnUtils.unlock(lockManager, vpnid.getValue());
352 private void updateVpnMaps(Uuid vpnId, String name, Uuid router, Uuid tenantId, List<Uuid> networks) {
353 VpnMapBuilder builder;
354 boolean isLockAcquired = false;
355 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
356 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
358 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
360 if (optionalVpnMap.isPresent()) {
361 builder = new VpnMapBuilder(optionalVpnMap.get());
363 builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId);
367 builder.setName(name);
369 if (tenantId != null) {
370 builder.setTenantId(tenantId);
372 if (router != null) {
373 builder.setRouterId(router);
375 if (networks != null) {
376 List<Uuid> nwList = builder.getNetworkIds();
377 if (nwList == null) {
378 nwList = new ArrayList<>();
380 nwList.addAll(networks);
381 builder.setNetworkIds(nwList);
384 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
385 logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue());
386 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build());
387 logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue());
388 } catch (Exception e) {
389 logger.error("UpdateVpnMaps failed for node: {} ", vpnId.getValue());
391 if (isLockAcquired) {
392 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
397 private void clearFromVpnMaps(Uuid vpnId, Uuid routerId, List<Uuid> networkIds) {
398 boolean isLockAcquired = false;
399 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
400 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
401 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
403 if (optionalVpnMap.isPresent()) {
404 VpnMap vpnMap = optionalVpnMap.get();
405 VpnMapBuilder vpnMapBuilder = new VpnMapBuilder(vpnMap);
406 if (routerId != null) {
407 if (vpnMap.getNetworkIds() == null && routerId.equals(vpnMap.getVpnId())) {
409 // remove entire node in case of internal VPN
410 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
411 logger.debug("removing vpnMaps node: {} ", vpnId);
412 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
413 } catch (Exception e) {
414 logger.error("Deletion of vpnMaps node failed for vpn {}", vpnId.getValue());
416 if (isLockAcquired) {
417 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
422 vpnMapBuilder.setRouterId(null);
424 if (networkIds != null) {
425 List<Uuid> vpnNw = vpnMap.getNetworkIds();
426 for (Uuid nw : networkIds) {
429 if (vpnNw.isEmpty()) {
430 logger.debug("setting networks null in vpnMaps node: {} ", vpnId.getValue());
431 vpnMapBuilder.setNetworkIds(null);
433 vpnMapBuilder.setNetworkIds(vpnNw);
438 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
439 logger.debug("clearing from vpnMaps node: {} ", vpnId.getValue());
440 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build
442 } catch (Exception e) {
443 logger.error("Clearing from vpnMaps node failed for vpn {}", vpnId.getValue());
445 if (isLockAcquired) {
446 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
450 logger.error("VPN : {} not found", vpnId.getValue());
452 logger.debug("Clear from VPNMaps DS successful for VPN {} ", vpnId.getValue());
455 private void deleteVpnInstance(Uuid vpnId) {
456 boolean isLockAcquired = false;
457 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
458 child(VpnInstance.class, new VpnInstanceKey(vpnId.getValue())).build();
460 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
461 logger.debug("Deleting vpnInstance {}", vpnId.getValue());
462 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
463 } catch (Exception e) {
464 logger.error("Deletion of VPNInstance node failed for VPN {}", vpnId.getValue());
466 if (isLockAcquired) {
467 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
472 protected void createVpnInterface(Uuid vpnId, Port port) {
473 boolean isLockAcquired = false;
474 if (vpnId == null || port == null) {
477 String infName = port.getUuid().getValue();
478 List<Adjacency> adjList = new ArrayList<>();
479 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
481 // find router associated to vpn
482 Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId);
484 if (routerId != null) {
485 rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
487 // find all subnets to which this port is associated
488 List<FixedIps> ips = port.getFixedIps();
489 // create adjacency list
490 for (FixedIps ip : ips) {
491 // create vm adjacency
492 StringBuilder IpPrefixBuild = new StringBuilder(ip.getIpAddress().getIpv4Address().getValue());
493 String IpPrefix = IpPrefixBuild.append("/32").toString();
494 Adjacency vmAdj = new AdjacencyBuilder().setKey(new AdjacencyKey(IpPrefix)).setIpAddress(IpPrefix)
495 .setMacAddress(port.getMacAddress().getValue()).build();
497 // create extra route adjacency
498 if (rtr != null && rtr.getRoutes() != null) {
499 List<Routes> routeList = rtr.getRoutes();
500 List<Adjacency> erAdjList = addAdjacencyforExtraRoute(routeList, false, infName);
501 if (erAdjList != null && !erAdjList.isEmpty()) {
502 adjList.addAll(erAdjList);
506 // create vpn-interface on this neutron port
507 Adjacencies adjs = new AdjacenciesBuilder().setAdjacency(adjList).build();
508 VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName)).
509 setName(infName).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs);
510 VpnInterface vpnIf = vpnb.build();
513 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
514 logger.debug("Creating vpn interface {}", vpnIf);
515 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
516 } catch (Exception ex) {
517 logger.error("Creation of vpninterface {} failed due to {}", infName, ex);
519 if (isLockAcquired) {
520 NeutronvpnUtils.unlock(lockManager, infName);
525 protected void deleteVpnInterface(Port port) {
528 boolean isLockAcquired = false;
529 String infName = port.getUuid().getValue();
530 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
533 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
534 logger.debug("Deleting vpn interface {}", infName);
535 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
536 } catch (Exception ex) {
537 logger.error("Deletion of vpninterface {} failed due to {}", infName, ex);
539 if (isLockAcquired) {
540 NeutronvpnUtils.unlock(lockManager, infName);
546 protected void updateVpnInterface(Uuid vpnId, Port port) {
547 if (vpnId == null || port == null) {
550 boolean isLockAcquired = false;
551 String infName = port.getUuid().getValue();
552 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
554 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
555 .CONFIGURATION, vpnIfIdentifier);
556 if (optionalVpnInterface.isPresent()) {
557 VpnInterfaceBuilder vpnIfBuilder = new VpnInterfaceBuilder(optionalVpnInterface.get());
558 VpnInterface vpnIf = vpnIfBuilder.setVpnInstanceName(vpnId.getValue()).build();
559 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
560 logger.debug("Updating vpn interface {}", vpnIf);
561 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
563 logger.error("VPN Interface {} not found", infName);
565 } catch (Exception ex) {
566 logger.error("Updation of vpninterface {} failed due to {}", infName, ex);
568 if (isLockAcquired) {
569 NeutronvpnUtils.unlock(lockManager, infName);
574 public void createL3InternalVpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
575 Uuid router, List<Uuid> networks) {
577 // Update VPN Instance node
578 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
580 // Update local vpn-subnet DS
581 updateVpnMaps(vpn, name, router, tenant, networks);
583 if (router != null) {
584 Uuid existingVpn = NeutronvpnUtils.getVpnForRouter(broker, router, true);
585 if (existingVpn != null) {
586 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, router);
587 if (routerSubnets != null) {
588 // Update the router interfaces alone and exit
589 for (Uuid subnetId : routerSubnets) {
590 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
591 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
592 Optional<Subnetmap> snMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
593 if (snMap.isPresent()) {
594 Subnetmap sn = snMap.get();
595 List<Uuid> portList = sn.getPortList();
596 if (portList != null) {
597 for (Uuid port : sn.getPortList()) {
598 addToNeutronRouterInterfacesMap(router, port.getValue());
604 logger.info("Creation of Internal L3VPN skipped for VPN {} due to router {} already associated to " +
605 "external VPN {}", vpn.getValue(), router.getValue(), existingVpn.getValue());
608 associateRouterToInternalVpn(vpn, router);
612 public void createL3Vpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
613 Uuid router, List<Uuid> networks) {
615 // Update VPN Instance node
616 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
618 // Update local vpn-subnet DS
619 updateVpnMaps(vpn, name, router, tenant, networks);
621 if (router != null) {
622 associateRouterToVpn(vpn, router);
624 if (networks != null) {
625 associateNetworksToVpn(vpn, networks);
630 public Future<RpcResult<CreateL3VPNOutput>> createL3VPN(CreateL3VPNInput input) {
632 CreateL3VPNOutputBuilder opBuilder = new CreateL3VPNOutputBuilder();
633 SettableFuture<RpcResult<CreateL3VPNOutput>> result = SettableFuture.create();
634 List<RpcError> errorList = new ArrayList<>();
635 int failurecount = 0;
636 int warningcount = 0;
638 List<L3vpn> vpns = input.getL3vpn();
639 for (L3vpn vpn : vpns) {
640 RpcError error = null;
642 if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
643 msg = String.format("Creation of L3VPN failed for VPN %s due to absence of RD/iRT/eRT input",
644 vpn.getId().getValue());
646 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
647 errorList.add(error);
651 if (vpn.getRouteDistinguisher().size() > 1) {
652 msg = String.format("Creation of L3VPN failed for VPN %s due to multiple RD input %s",
653 vpn.getId().getValue(), vpn.getRouteDistinguisher());
655 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
656 errorList.add(error);
660 if (vpn.getRouterId() != null) {
661 if (NeutronvpnUtils.getNeutronRouter(broker, vpn.getRouterId()) == null) {
662 msg = String.format("Creation of L3VPN failed for VPN %s due to router not found %s",
663 vpn.getId().getValue(), vpn.getRouterId().getValue());
665 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
666 errorList.add(error);
670 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, vpn.getRouterId(), true);
672 msg = String.format("Creation of L3VPN failed for VPN %s due to router %s already associated to " +
673 "another VPN %s", vpn.getId().getValue(), vpn.getRouterId().getValue(), vpnId.getValue());
675 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
676 errorList.add(error);
681 if (vpn.getNetworkIds() != null) {
682 for (Uuid nw : vpn.getNetworkIds()) {
683 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
684 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
685 if (network == null) {
686 msg = String.format("Creation of L3VPN failed for VPN %s due to network not found %s",
687 vpn.getId().getValue(), nw.getValue());
689 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
690 errorList.add(error);
692 } else if (vpnId != null) {
693 msg = String.format("Creation of L3VPN failed for VPN %s due to network %s already associated" +
694 " to another VPN %s", vpn.getId().getValue(), nw.getValue(), vpnId.getValue());
696 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
697 errorList.add(error);
706 createL3Vpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(),
707 vpn.getImportRT(), vpn.getExportRT(), vpn.getRouterId(), vpn.getNetworkIds());
708 } catch (Exception ex) {
709 msg = String.format("Creation of L3VPN failed for VPN %s", vpn.getId().getValue());
710 logger.error(msg, ex);
711 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
712 errorList.add(error);
716 // if at least one succeeds; result is success
717 // if none succeeds; result is failure
718 if (failurecount + warningcount == vpns.size()) {
719 result.set(RpcResultBuilder.<CreateL3VPNOutput>failed().withRpcErrors(errorList).build());
721 List<String> errorResponseList = new ArrayList<>();
722 if (!errorList.isEmpty()) {
723 for (RpcError rpcError : errorList) {
724 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
725 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
726 errorResponseList.add(errorResponse);
729 errorResponseList.add("Operation successful with no errors");
731 opBuilder.setResponse(errorResponseList);
732 result.set(RpcResultBuilder.<CreateL3VPNOutput>success().withResult(opBuilder.build()).build());
738 public Future<RpcResult<GetL3VPNOutput>> getL3VPN(GetL3VPNInput input) {
740 GetL3VPNOutputBuilder opBuilder = new GetL3VPNOutputBuilder();
741 SettableFuture<RpcResult<GetL3VPNOutput>> result = SettableFuture.create();
742 Uuid inputVpnId = input.getId();
743 List<VpnInstance> vpns = new ArrayList<>();
746 if (inputVpnId == null) {
748 InstanceIdentifier<VpnInstances> vpnsIdentifier =
749 InstanceIdentifier.builder(VpnInstances.class).build();
750 Optional<VpnInstances> optionalVpns = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
752 if (optionalVpns.isPresent() && optionalVpns.get().getVpnInstance() != null) {
753 for (VpnInstance vpn : optionalVpns.get().getVpnInstance()) {
754 // eliminating internal VPNs from getL3VPN output
755 if (vpn.getIpv4Family().getRouteDistinguisher() != null) {
761 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
762 .withWarning(ErrorType.PROTOCOL, "", "No VPN is present").build());
766 String name = inputVpnId.getValue();
767 InstanceIdentifier<VpnInstance> vpnIdentifier =
768 InstanceIdentifier.builder(VpnInstances.class)
769 .child(VpnInstance.class, new VpnInstanceKey(name)).build();
770 // read VpnInstance Info
771 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
773 if (optionalVpn.isPresent()) {
774 vpns.add(optionalVpn.get());
776 String message = String.format("GetL3VPN failed because VPN %s is not present", name);
777 logger.error(message);
778 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
779 .withWarning(ErrorType.PROTOCOL, "invalid-value", message).build());
782 List<L3vpnInstances> l3vpnList = new ArrayList<>();
783 for (VpnInstance vpnInstance : vpns) {
784 Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName());
786 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class).child(VpnMap
787 .class, new VpnMapKey(vpnId)).build();
788 L3vpnInstancesBuilder l3vpn = new L3vpnInstancesBuilder();
790 List<String> rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(","));
791 List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
793 List<String> ertList = new ArrayList<>();
794 List<String> irtList = new ArrayList<>();
796 for (VpnTarget vpnTarget : vpnTargetList) {
797 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
798 ertList.add(vpnTarget.getVrfRTValue());
800 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ImportExtcommunity) {
801 irtList.add(vpnTarget.getVrfRTValue());
803 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.Both) {
804 ertList.add(vpnTarget.getVrfRTValue());
805 irtList.add(vpnTarget.getVrfRTValue());
809 l3vpn.setId(vpnId).setRouteDistinguisher(rd).setImportRT(irtList).setExportRT(ertList);
810 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
812 if (optionalVpnMap.isPresent()) {
813 VpnMap vpnMap = optionalVpnMap.get();
814 l3vpn.setRouterId(vpnMap.getRouterId()).setNetworkIds(vpnMap.getNetworkIds())
815 .setTenantId(vpnMap.getTenantId()).setName(vpnMap.getName());
817 l3vpnList.add(l3vpn.build());
820 opBuilder.setL3vpnInstances(l3vpnList);
821 result.set(RpcResultBuilder.<GetL3VPNOutput>success().withResult(opBuilder.build()).build());
823 } catch (Exception ex) {
824 String message = String.format("GetL3VPN failed due to %s", ex.getMessage());
825 logger.error(message, ex);
826 result.set(RpcResultBuilder.<GetL3VPNOutput>failed().withError(ErrorType.APPLICATION, message).build());
832 public Future<RpcResult<DeleteL3VPNOutput>> deleteL3VPN(DeleteL3VPNInput input) {
834 DeleteL3VPNOutputBuilder opBuilder = new DeleteL3VPNOutputBuilder();
835 SettableFuture<RpcResult<DeleteL3VPNOutput>> result = SettableFuture.create();
836 List<RpcError> errorList = new ArrayList<>();
838 int failurecount = 0;
839 int warningcount = 0;
840 List<Uuid> vpns = input.getId();
841 for (Uuid vpn : vpns) {
845 InstanceIdentifier<VpnInstance> vpnIdentifier =
846 InstanceIdentifier.builder(VpnInstances.class)
847 .child(VpnInstance.class, new VpnInstanceKey(vpn.getValue())).build();
848 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
850 if (optionalVpn.isPresent()) {
853 msg = String.format("VPN with vpnid: %s does not exist", vpn.getValue());
855 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-value", msg);
856 errorList.add(error);
859 } catch (Exception ex) {
860 msg = String.format("Deletion of L3VPN failed when deleting for uuid %s", vpn.getValue());
861 logger.error(msg, ex);
862 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
863 errorList.add(error);
867 // if at least one succeeds; result is success
868 // if none succeeds; result is failure
869 if (failurecount + warningcount == vpns.size()) {
870 result.set(RpcResultBuilder.<DeleteL3VPNOutput>failed().withRpcErrors(errorList).build());
872 List<String> errorResponseList = new ArrayList<>();
873 if (!errorList.isEmpty()) {
874 for (RpcError rpcError : errorList) {
875 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
876 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
877 errorResponseList.add(errorResponse);
880 errorResponseList.add("Operation successful with no errors");
882 opBuilder.setResponse(errorResponseList);
883 result.set(RpcResultBuilder.<DeleteL3VPNOutput>success().withResult(opBuilder.build()).build());
888 protected void addSubnetToVpn(Uuid vpnId, Uuid subnet) {
889 logger.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
890 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null);
891 boolean isLockAcquired = false;
892 String lockName = vpnId.getValue() + subnet.getValue();
893 String elanInstanceName = sn.getNetworkId().getValue();
894 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
895 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
896 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
898 //TODO: Cache it in add so can reuse it in update and delete. Best would be to cache in some ElanUtils
899 long elanTag = elanInstance.get().getElanTag();
900 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
901 if (vpnId.equals(routerId)) {
902 isExternalVpn = false;
904 isExternalVpn = true;
907 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
908 checkAndPublishSubnetAddNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag);
909 logger.debug("Subnet added to Vpn notification sent");
910 }catch (Exception e){
911 logger.error("Subnet added to Vpn notification failed",e);
913 if (isLockAcquired) {
914 NeutronvpnUtils.unlock(lockManager, lockName);
917 // Check if there are ports on this subnet and add corresponding vpn-interfaces
918 List<Uuid> portList = sn.getPortList();
919 if (portList != null) {
920 for (Uuid port : sn.getPortList()) {
921 logger.debug("adding vpn-interface for port {}", port.getValue());
922 createVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port));
923 if (routerId != null) {
924 addToNeutronRouterInterfacesMap(routerId, port.getValue());
930 protected void updateVpnForSubnet(Uuid vpnId, Uuid subnet, boolean isBeingAssociated) {
931 logger.debug("Updating VPN {} for subnet {}", vpnId.getValue(), subnet.getValue());
932 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null);
933 boolean isLockAcquired = false;
934 String lockName = vpnId.getValue() + subnet.getValue();
935 String elanInstanceName = sn.getNetworkId().getValue();
936 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
937 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
938 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
940 long elanTag = elanInstance.get().getElanTag();
942 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
943 checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated,
945 logger.debug("Subnet updated in Vpn notification sent");
946 } catch (Exception e) {
947 logger.error("Subnet updated in Vpn notification failed", e);
949 if (isLockAcquired) {
950 NeutronvpnUtils.unlock(lockManager, lockName);
953 // Check for ports on this subnet and update association of corresponding vpn-interfaces to external vpn
954 List<Uuid> portList = sn.getPortList();
955 if (portList != null) {
956 for (Uuid port : sn.getPortList()) {
957 logger.debug("Updating vpn-interface for port {}", port.getValue());
958 updateVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port));
963 InstanceIdentifier<RouterInterfaces> getRouterInterfacesId(Uuid routerId) {
964 return InstanceIdentifier.builder(RouterInterfacesMap.class)
965 .child(RouterInterfaces.class, new RouterInterfacesKey(routerId)).build();
967 void addToNeutronRouterInterfacesMap(Uuid routerId, String interfaceName) {
968 InstanceIdentifier<RouterInterfaces> routerInterfacesId = getRouterInterfacesId(routerId);
969 Optional<RouterInterfaces> optRouterInterfaces = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
970 Interfaces routerInterface = new InterfacesBuilder().setKey(new InterfacesKey(interfaceName)).setInterfaceId(interfaceName).build();
971 if(optRouterInterfaces.isPresent()) {
972 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId.child(Interfaces.class, new InterfacesKey(interfaceName)), routerInterface);
974 RouterInterfacesBuilder builder = new RouterInterfacesBuilder().setRouterId(routerId);
975 List<Interfaces> interfaces = new ArrayList<>();
976 interfaces.add(routerInterface);
977 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId, builder.setInterfaces(interfaces).build());
981 void removeFromNeutronRouterInterfacesMap(Uuid routerId, String interfaceName) {
982 InstanceIdentifier<RouterInterfaces> routerInterfacesId = getRouterInterfacesId(routerId);
983 Optional<RouterInterfaces> optRouterInterfaces = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
984 Interfaces routerInterface = new InterfacesBuilder().setKey(new InterfacesKey(interfaceName)).setInterfaceId(interfaceName).build();
985 if(optRouterInterfaces.isPresent()) {
986 RouterInterfaces routerInterfaces = optRouterInterfaces.get();
987 List<Interfaces> interfaces = routerInterfaces.getInterfaces();
988 if(interfaces != null && interfaces.remove(routerInterface)) {
989 if(interfaces.isEmpty()) {
990 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
992 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId.child(Interfaces.class, new InterfacesKey(interfaceName)));
998 protected List<Adjacency> addAdjacencyforExtraRoute(List<Routes> routeList, boolean rtrUp, String vpnifname) {
999 List<Adjacency> adjList = new ArrayList<>();
1000 for (Routes route : routeList) {
1001 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
1002 boolean isLockAcquired = false;
1003 String nextHop = String.valueOf(route.getNexthop().getValue());
1004 String destination = String.valueOf(route.getDestination().getValue());
1006 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
1007 logger.trace("Adding extra route with nexthop {}, destination {}, infName {}", nextHop,
1008 destination, infName);
1009 Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey
1010 (new AdjacencyKey(destination)).build();
1012 if (infName.equals(vpnifname)) {
1017 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
1018 child(VpnInterface.class, new VpnInterfaceKey(infName)).build();
1020 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
1021 .CONFIGURATION, vpnIfIdentifier);
1022 if (optionalVpnInterface.isPresent()) {
1023 Adjacencies erAdjs = new AdjacenciesBuilder().setAdjacency(Arrays.asList(erAdj)).build();
1024 VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName))
1025 .addAugmentation(Adjacencies.class, erAdjs).build();
1026 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
1027 logger.debug("Adding extra route {}", route);
1028 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
1030 logger.error("VM adjacency for interface {} not present ; cannot add extra route adjacency",
1033 } catch (Exception e) {
1034 logger.error("exception in adding extra route: {}" + e);
1036 if (isLockAcquired) {
1037 NeutronvpnUtils.unlock(lockManager, infName);
1041 logger.error("Incorrect input received for extra route. {}", route);
1047 protected void removeAdjacencyforExtraRoute(List<Routes> routeList) {
1048 for (Routes route : routeList) {
1049 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
1050 boolean isLockAcquired = false;
1051 String nextHop = String.valueOf(route.getNexthop().getValue());
1052 String destination = String.valueOf(route.getDestination().getValue());
1054 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
1055 logger.trace("Removing extra route with nexthop {}, destination {}, infName {}", nextHop,
1056 destination, infName);
1057 InstanceIdentifier<Adjacency> adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
1058 child(VpnInterface.class, new VpnInterfaceKey(infName)).augmentation(Adjacencies.class)
1059 .child(Adjacency.class, new AdjacencyKey(destination)).build();
1061 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
1062 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, adjacencyIdentifier);
1063 logger.trace("extra route {} deleted successfully", route);
1064 } catch (Exception e) {
1065 logger.error("exception in deleting extra route: {}" + e);
1067 if (isLockAcquired) {
1068 NeutronvpnUtils.unlock(lockManager, infName);
1072 logger.error("Incorrect input received for extra route. {}", route);
1077 protected void removeL3Vpn(Uuid id) {
1079 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, id);
1080 Uuid router = vpnMap.getRouterId();
1081 // dissociate router
1082 if (router != null) {
1083 dissociateRouterFromVpn(id, router);
1085 // dissociate networks
1086 if (!id.equals(router)) {
1087 dissociateNetworksFromVpn(id, vpnMap.getNetworkIds());
1089 // remove entire vpnMaps node
1090 deleteVpnMapsNode(id);
1092 // remove vpn-instance
1093 deleteVpnInstance(id);
1096 protected void removeSubnetFromVpn(Uuid vpnId, Uuid subnet) {
1097 logger.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
1098 Subnetmap sn = NeutronvpnUtils.getSubnetmap(broker, subnet);
1099 boolean isLockAcquired = false;
1100 String lockName = vpnId.getValue() + subnet.getValue();
1101 String elanInstanceName = sn.getNetworkId().getValue();
1102 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
1103 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
1104 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1106 long elanTag = elanInstance.get().getElanTag();
1107 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
1108 if (vpnId.equals(routerId)) {
1109 isExternalVpn = false;
1111 isExternalVpn = true;
1114 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
1115 checkAndPublishSubnetDelNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag);
1116 logger.debug("Subnet removed from Vpn notification sent");
1117 }catch (Exception e){
1118 logger.error("Subnet removed from Vpn notification failed",e);
1120 if (isLockAcquired) {
1121 NeutronvpnUtils.unlock(lockManager, lockName);
1125 // Check if there are ports on this subnet; remove corresponding vpn-interfaces
1126 List<Uuid> portList = sn.getPortList();
1127 if (portList != null) {
1128 for (Uuid port : sn.getPortList()) {
1129 logger.debug("removing vpn-interface for port {}", port.getValue());
1130 deleteVpnInterface(NeutronvpnUtils.getNeutronPort(broker, port));
1131 if (routerId != null) {
1132 removeFromNeutronRouterInterfacesMap(routerId, port.getValue());
1136 // update subnet-vpn association
1137 removeFromSubnetNode(subnet, null, null, vpnId, null);
1139 logger.warn("Subnetmap for subnet {} not found", subnet.getValue());
1143 protected void associateRouterToVpn(Uuid vpnId, Uuid routerId) {
1144 updateVpnMaps(vpnId, null, routerId, null, null);
1145 logger.debug("Updating association of subnets to external vpn {}", vpnId.getValue());
1146 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1147 // if (!vpnId.equals(routerId)) {
1148 if (routerSubnets != null) {
1149 for (Uuid subnetId : routerSubnets) {
1150 updateVpnForSubnet(vpnId, subnetId, true);
1154 checkAndPublishRouterAssociatedtoVpnNotification(routerId, vpnId);
1155 logger.debug("notification upon association of router {} to VPN {} published", routerId.getValue(),
1157 } catch (Exception e) {
1158 logger.error("publishing of notification upon association of router {} to VPN {} failed : ", routerId
1159 .getValue(), vpnId.getValue(), e);
1163 protected void dissociatefixedIPFromFloatingIP(String fixedNeutronPortName) {
1164 floatingIpMapListener.dissociatefixedIPFromFloatingIP(fixedNeutronPortName);
1167 protected void associateRouterToInternalVpn(Uuid vpnId, Uuid routerId) {
1168 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1169 logger.debug("Adding subnets to internal vpn {}", vpnId.getValue());
1170 for (Uuid subnet : routerSubnets) {
1171 addSubnetToVpn(vpnId, subnet);
1175 protected void dissociateRouterFromVpn(Uuid vpnId, Uuid routerId) {
1177 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1178 if (routerSubnets != null) {
1179 for (Uuid subnetId : routerSubnets) {
1180 logger.debug("Updating association of subnets to internal vpn {}", routerId.getValue());
1181 updateVpnForSubnet(routerId, subnetId,false);
1184 clearFromVpnMaps(vpnId, routerId, null);
1186 checkAndPublishRouterDisassociatedFromVpnNotification(routerId, vpnId);
1187 logger.debug("notification upon disassociation of router {} from VPN {} published", routerId.getValue(),
1189 } catch (Exception e) {
1190 logger.error("publishing of notification upon disassociation of router {} from VPN {} failed : ", routerId
1191 .getValue(), vpnId.getValue(), e);
1195 protected List<String> associateNetworksToVpn(Uuid vpn, List<Uuid> networks) {
1197 List<String> failedNwList = new ArrayList<String>();
1198 List<Uuid> passedNwList = new ArrayList<>();
1199 if (!networks.isEmpty()) {
1200 // process corresponding subnets for VPN
1201 for (Uuid nw : networks) {
1202 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
1203 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
1204 if (network == null) {
1205 failedNwList.add(String.format("network %s not found", nw.getValue()));
1206 } else if (vpnId != null) {
1207 failedNwList.add(String.format("network %s already associated to another VPN %s", nw.getValue(),
1210 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1211 logger.debug("Adding network subnets...{}", networkSubnets);
1212 if (networkSubnets != null) {
1213 for (Uuid subnet : networkSubnets) {
1214 // check if subnet added as router interface to some router
1215 Uuid subnetVpnId = NeutronvpnUtils.getVpnForSubnet(broker, subnet);
1216 if (subnetVpnId == null) {
1217 addSubnetToVpn(vpn, subnet);
1218 passedNwList.add(nw);
1220 failedNwList.add(String.format("subnet %s already added as router interface bound to " +
1221 "internal/external VPN %s", subnet.getValue (), subnetVpnId.getValue()));
1225 if (network.getAugmentation(NetworkL3Extension.class) != null && network.getAugmentation
1226 (NetworkL3Extension.class).isExternal()) {
1227 nvpnNatManager.addExternalNetworkToVpn(network, vpn);
1231 updateVpnMaps(vpn, null, null, null, passedNwList);
1233 return failedNwList;
1236 protected List<String> dissociateNetworksFromVpn(Uuid vpn, List<Uuid> networks) {
1238 List<String> failedNwList = new ArrayList<String>();
1239 List<Uuid> passedNwList = new ArrayList<>();
1240 if (networks != null && !networks.isEmpty()) {
1241 // process corresponding subnets for VPN
1242 for (Uuid nw : networks) {
1243 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
1244 if (network == null) {
1245 failedNwList.add(String.format("network %s not found", nw.getValue()));
1247 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
1248 if (vpn.equals(vpnId)) {
1249 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1250 logger.debug("Removing network subnets...");
1251 if (networkSubnets != null) {
1252 for (Uuid subnet : networkSubnets) {
1253 removeSubnetFromVpn(vpn, subnet);
1254 passedNwList.add(nw);
1258 if (vpnId == null) {
1259 failedNwList.add(String.format("input network %s not associated to any vpn yet", nw
1262 failedNwList.add(String.format("input network %s associated to a another vpn %s instead " +
1263 "of the one given as input", nw.getValue(), vpnId.getValue()));
1266 if (network.getAugmentation(NetworkL3Extension.class).isExternal()) {
1267 nvpnNatManager.removeExternalNetworkFromVpn(network);
1271 clearFromVpnMaps(vpn, null, passedNwList);
1273 return failedNwList;
1277 public Future<RpcResult<AssociateNetworksOutput>> associateNetworks(AssociateNetworksInput input) {
1279 AssociateNetworksOutputBuilder opBuilder = new AssociateNetworksOutputBuilder();
1280 SettableFuture<RpcResult<AssociateNetworksOutput>> result = SettableFuture.create();
1281 logger.debug("associateNetworks {}", input);
1282 StringBuilder returnMsg = new StringBuilder();
1283 Uuid vpnId = input.getVpnId();
1286 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1287 List<Uuid> netIds = input.getNetworkId();
1288 if (netIds != null && !netIds.isEmpty()) {
1289 List<String> failed = associateNetworksToVpn(vpnId, netIds);
1290 if (!failed.isEmpty()) {
1291 returnMsg.append(failed);
1295 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1297 if (returnMsg.length() != 0) {
1298 String message = String.format("associate Networks to vpn %s failed due to %s", vpnId.getValue(),
1300 logger.error(message);
1301 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1303 opBuilder.setResponse(errorResponse);
1304 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().withResult(opBuilder.build()).build());
1306 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().build());
1308 } catch (Exception ex) {
1309 String message = String.format("associate Networks to vpn %s failed due to %s", input.getVpnId().getValue(),
1311 logger.error(message, ex);
1312 result.set(RpcResultBuilder.<AssociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1315 logger.debug("associateNetworks returns..");
1320 public Future<RpcResult<Void>> associateRouter(AssociateRouterInput input) {
1322 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1323 logger.debug("associateRouter {}", input);
1324 StringBuilder returnMsg = new StringBuilder();
1325 Uuid vpnId = input.getVpnId();
1326 Uuid routerId = input.getRouterId();
1328 if (routerId != null && vpnId != null) {
1329 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1330 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, vpnId);
1331 if (rtr != null && vpnMap != null) {
1332 Uuid extVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1333 if (vpnMap.getRouterId() != null) {
1334 returnMsg.append("vpn ").append(vpnId.getValue()).append(" already associated to router ")
1335 .append(vpnMap.getRouterId().getValue());
1336 } else if (extVpnId != null) {
1337 returnMsg.append("router ").append(routerId.getValue()).append(" already associated to " +
1338 "another VPN ").append(extVpnId.getValue());
1340 associateRouterToVpn(vpnId, routerId);
1343 returnMsg.append("router not found : ").append(routerId.getValue());
1346 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1348 if (returnMsg.length() != 0) {
1349 String message = String.format("associate router to vpn %s failed due to %s", routerId.getValue(),
1351 logger.error(message);
1352 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1355 result.set(RpcResultBuilder.<Void>success().build());
1357 } catch (Exception ex) {
1358 String message = String.format("associate router %s to vpn %s failed due to %s", routerId.getValue(),
1359 vpnId.getValue(), ex.getMessage());
1360 logger.error(message, ex);
1361 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1363 logger.debug("associateRouter returns..");
1368 public Future<RpcResult<DissociateNetworksOutput>> dissociateNetworks(DissociateNetworksInput input) {
1370 DissociateNetworksOutputBuilder opBuilder = new DissociateNetworksOutputBuilder();
1371 SettableFuture<RpcResult<DissociateNetworksOutput>> result = SettableFuture.create();
1373 logger.debug("dissociateNetworks {}", input);
1374 StringBuilder returnMsg = new StringBuilder();
1375 Uuid vpnId = input.getVpnId();
1378 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1379 List<Uuid> netIds = input.getNetworkId();
1380 if (netIds != null && !netIds.isEmpty()) {
1381 List<String> failed = dissociateNetworksFromVpn(vpnId, netIds);
1382 if (!failed.isEmpty()) {
1383 returnMsg.append(failed);
1387 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1389 if (returnMsg.length() != 0) {
1390 String message = String.format("dissociate Networks to vpn %s failed due to %s", vpnId.getValue(),
1392 logger.error(message);
1393 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1395 opBuilder.setResponse(errorResponse);
1396 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().withResult(opBuilder.build()).build());
1398 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().build());
1400 } catch (Exception ex) {
1401 String message = String.format("dissociate Networks to vpn %s failed due to %s", input.getVpnId().
1402 getValue(), ex.getMessage());
1403 logger.error(message, ex);
1404 result.set(RpcResultBuilder.<DissociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1407 logger.debug("dissociateNetworks returns..");
1412 public Future<RpcResult<Void>> dissociateRouter(DissociateRouterInput input) {
1414 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1416 logger.debug("dissociateRouter {}", input);
1417 StringBuilder returnMsg = new StringBuilder();
1418 Uuid vpnId = input.getVpnId();
1419 Uuid routerId = input.getRouterId();
1421 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1422 if (routerId != null) {
1423 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1425 Uuid routerVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1426 if (vpnId.equals(routerVpnId)) {
1427 dissociateRouterFromVpn(vpnId, routerId);
1429 if (routerVpnId == null) {
1430 returnMsg.append("input router ").append(routerId.getValue()).append(" not associated" +
1433 returnMsg.append("input router ").append(routerId.getValue()).append(" associated to " +
1434 "vpn ").append(routerVpnId.getValue()).append("instead of the vpn given as " +
1439 returnMsg.append("router not found : ").append(routerId.getValue());
1443 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1445 if (returnMsg.length() != 0) {
1446 String message = String.format("dissociate router %s to vpn %s failed due to %s", routerId.getValue(),
1447 vpnId.getValue(), returnMsg);
1448 logger.error(message);
1449 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1451 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1454 result.set(RpcResultBuilder.<Void>success().build());
1456 } catch (Exception ex) {
1457 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1458 vpnId.getValue(), ex.getMessage());
1459 logger.error(message, ex);
1460 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1462 logger.debug("dissociateRouter returns..");
1468 public Future<RpcResult<GetFixedIPsForNeutronPortOutput>> getFixedIPsForNeutronPort(GetFixedIPsForNeutronPortInput
1470 GetFixedIPsForNeutronPortOutputBuilder opBuilder = new GetFixedIPsForNeutronPortOutputBuilder();
1471 SettableFuture<RpcResult<GetFixedIPsForNeutronPortOutput>> result = SettableFuture.create();
1472 Uuid portId = input.getPortId();
1473 StringBuilder returnMsg = new StringBuilder();
1475 List<String> fixedIPList = new ArrayList<>();
1476 Port port = NeutronvpnUtils.getNeutronPort(broker, portId);
1478 List<FixedIps> fixedIPs = port.getFixedIps();
1479 for (FixedIps ip : fixedIPs) {
1480 fixedIPList.add(ip.getIpAddress().getIpv4Address().getValue());
1483 returnMsg.append("neutron port: ").append(portId.getValue()).append(" not found");
1485 if (returnMsg.length() != 0) {
1486 String message = String.format("Retrieval of FixedIPList for neutron port failed due to %s", returnMsg);
1487 logger.error(message);
1488 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>failed().withWarning(ErrorType.PROTOCOL,
1489 "invalid-value", message).build());
1491 opBuilder.setFixedIPs(fixedIPList);
1492 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>success().withResult(opBuilder.build())
1494 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>success().build());
1496 } catch (Exception ex) {
1497 String message = String.format("Retrieval of FixedIPList for neutron port %s failed due to %s", portId
1498 .getValue(), ex.getMessage());
1499 logger.error(message, ex);
1500 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>failed().withError(ErrorType.APPLICATION,
1506 protected void handleNeutronRouterDeleted(Uuid routerId, List<Uuid> routerSubnetIds) {
1507 // check if the router is associated to some VPN
1508 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1509 if (vpnId != null) {
1510 // remove existing external vpn interfaces
1511 for (Uuid subnetId : routerSubnetIds) {
1512 removeSubnetFromVpn(vpnId, subnetId);
1514 clearFromVpnMaps(vpnId, routerId, null);
1516 // remove existing internal vpn interfaces
1517 for (Uuid subnetId : routerSubnetIds) {
1518 removeSubnetFromVpn(routerId, subnetId);
1521 // delete entire vpnMaps node for internal VPN
1522 deleteVpnMapsNode(routerId);
1524 // delete vpn-instance for internal VPN
1525 deleteVpnInstance(routerId);
1528 protected Subnet getNeutronSubnet(Uuid subnetId){
1529 return NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
1532 protected IpAddress getNeutronSubnetGateway(Uuid subnetId) {
1533 Subnet sn = NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
1535 return sn.getGatewayIp();
1540 protected Port getNeutronPort(String name) {
1541 return NeutronvpnUtils.getNeutronPort(broker, new Uuid(name));
1544 protected Port getNeutronPort(Uuid portId) {
1545 return NeutronvpnUtils.getNeutronPort(broker, portId);
1548 protected List<Uuid> getSubnetsforVpn(Uuid vpnid) {
1549 List<Uuid> subnets = new ArrayList<>();
1551 InstanceIdentifier<Subnetmaps> subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build();
1552 Optional<Subnetmaps> subnetmaps = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1554 if (subnetmaps.isPresent() && subnetmaps.get().getSubnetmap() != null) {
1555 List<Subnetmap> subnetMapList = subnetmaps.get().getSubnetmap();
1556 for (Subnetmap subnetMap : subnetMapList) {
1557 if (subnetMap.getVpnId() != null && subnetMap.getVpnId().equals(vpnid)) {
1558 subnets.add(subnetMap.getId());
1565 public List<String> showNeutronPortsCLI() {
1566 List<String> result = new ArrayList<>();
1567 result.add(String.format(" %-36s %-19s %-13s %-20s ", "Port ID", "Mac Address", "Prefix Length", "IP " +
1569 result.add("-------------------------------------------------------------------------------------------");
1570 InstanceIdentifier<Ports> portidentifier = InstanceIdentifier.create(Neutron.class).child(Ports.class);
1572 Optional<Ports> ports = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, portidentifier);
1573 if (ports.isPresent() && ports.get().getPort() != null) {
1574 for (Port port : ports.get().getPort()) {
1575 List<FixedIps> fixedIPs = port.getFixedIps();
1577 if (fixedIPs != null && !fixedIPs.isEmpty()) {
1578 List<String> ipList = new ArrayList<>();
1579 for (FixedIps fixedIp : fixedIPs) {
1580 IpAddress ipAddress = fixedIp.getIpAddress();
1581 if (ipAddress.getIpv4Address() != null) {
1582 ipList.add(ipAddress.getIpv4Address().getValue());
1584 ipList.add((ipAddress.getIpv6Address().getValue()));
1587 result.add(String.format(" %-36s %-19s %-13s %-20s ", port.getUuid().getValue(), port
1588 .getMacAddress().getValue(), NeutronvpnUtils.getIPPrefixFromPort(broker, port),
1589 ipList.toString()));
1591 result.add(String.format(" %-36s %-19s %-13s %-20s ", port.getUuid().getValue(), port
1592 .getMacAddress().getValue(), "Not Assigned", "Not Assigned"));
1594 } catch (Exception e) {
1595 logger.error("Failed to retrieve neutronPorts info for port {}: ", port.getUuid().getValue(),
1597 System.out.println("Failed to retrieve neutronPorts info for port: " + port.getUuid()
1598 .getValue() + ": " + e.getMessage());
1602 } catch (Exception e) {
1603 logger.error("Failed to retrieve neutronPorts info : ", e);
1604 System.out.println("Failed to retrieve neutronPorts info : " + e.getMessage());
1609 public List<String> showVpnConfigCLI(Uuid vpnuuid) {
1610 List<String> result = new ArrayList<>();
1611 if (vpnuuid == null) {
1612 System.out.println("");
1613 System.out.println("Displaying VPN config for all VPNs");
1614 System.out.println("To display VPN config for a particular VPN, use the following syntax");
1615 System.out.println(getshowVpnConfigCLIHelp());
1618 RpcResult<GetL3VPNOutput> rpcResult = getL3VPN(new GetL3VPNInputBuilder().setId(vpnuuid).build()).get();
1619 if (rpcResult.isSuccessful()) {
1621 result.add(String.format(" %-37s %-37s %-7s ", "VPN ID", "Tenant ID", "RD"));
1623 result.add(String.format(" %-80s ", "Import-RTs"));
1625 result.add(String.format(" %-80s ", "Export-RTs"));
1627 result.add(String.format(" %-76s ", "Subnet IDs"));
1629 result.add("------------------------------------------------------------------------------------");
1631 List<L3vpnInstances> VpnList = rpcResult.getResult().getL3vpnInstances();
1632 for (L3vpnInstance Vpn : VpnList) {
1633 String tenantId = Vpn.getTenantId() != null ? Vpn.getTenantId().getValue() : "\" " +
1635 result.add(String.format(" %-37s %-37s %-7s ", Vpn.getId().getValue(), tenantId, Vpn
1636 .getRouteDistinguisher()));
1638 result.add(String.format(" %-80s ", Vpn.getImportRT()));
1640 result.add(String.format(" %-80s ", Vpn.getExportRT()));
1643 Uuid vpnid = Vpn.getId();
1644 List<Uuid> subnetList = getSubnetsforVpn(vpnid);
1645 if (!subnetList.isEmpty()) {
1646 for (Uuid subnetuuid : subnetList) {
1647 result.add(String.format(" %-76s ", subnetuuid.getValue()));
1650 result.add(String.format(" %-76s ", "\" \""));
1653 result.add("----------------------------------------");
1657 String errortag = rpcResult.getErrors().iterator().next().getTag();
1658 if (errortag == "") {
1659 System.out.println("");
1660 System.out.println("No VPN has been configured yet");
1661 } else if (errortag == "invalid-value") {
1662 System.out.println("");
1663 System.out.println("VPN " + vpnuuid.getValue() + " is not present");
1665 System.out.println("error getting VPN info : " + rpcResult.getErrors());
1666 System.out.println(getshowVpnConfigCLIHelp());
1669 } catch (InterruptedException | ExecutionException e) {
1670 logger.error("error getting VPN info : ", e);
1671 System.out.println("error getting VPN info : " + e.getMessage());
1676 private String getshowVpnConfigCLIHelp() {
1677 StringBuilder help = new StringBuilder("Usage:");
1678 help.append("display vpn-config [-vid/--vpnid <id>]");
1679 return help.toString();
1682 private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName,
1683 Boolean isExternalvpn, Long elanTag)throws InterruptedException {
1684 SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder();
1686 logger.info("publish notification called");
1688 builder.setSubnetId(subnetId);
1689 builder.setSubnetIp(subnetIp);
1690 builder.setVpnName(vpnName);
1691 builder.setExternalVpn(isExternalvpn);
1692 builder.setElanTag(elanTag);
1694 notificationPublishService.putNotification(builder.build());
1697 private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName,
1698 Boolean isExternalvpn, Long elanTag) throws InterruptedException {
1699 SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder();
1701 logger.info("publish notification called");
1703 builder.setSubnetId(subnetId);
1704 builder.setSubnetIp(subnetIp);
1705 builder.setVpnName(vpnName);
1706 builder.setExternalVpn(isExternalvpn);
1707 builder.setElanTag(elanTag);
1709 notificationPublishService.putNotification(builder.build());
1712 private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName,
1713 Boolean isExternalvpn, Long elanTag) throws InterruptedException {
1714 SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder();
1716 logger.info("publish notification called");
1718 builder.setSubnetId(subnetId);
1719 builder.setSubnetIp(subnetIp);
1720 builder.setVpnName(vpnName);
1721 builder.setExternalVpn(isExternalvpn);
1722 builder.setElanTag(elanTag);
1724 notificationPublishService.putNotification(builder.build());
1727 private void checkAndPublishRouterAssociatedtoVpnNotification(Uuid routerId, Uuid vpnId) throws
1728 InterruptedException {
1729 RouterAssociatedToVpn routerAssociatedToVpn = new RouterAssociatedToVpnBuilder().setRouterId(routerId)
1730 .setVpnId(vpnId).build();
1731 logger.info("publishing notification upon association of router to VPN");
1732 notificationPublishService.putNotification(routerAssociatedToVpn);
1735 private void checkAndPublishRouterDisassociatedFromVpnNotification(Uuid routerId, Uuid vpnId) throws
1736 InterruptedException {
1737 RouterDisassociatedFromVpn routerDisassociatedFromVpn = new RouterDisassociatedFromVpnBuilder().setRouterId
1738 (routerId).setVpnId(vpnId).build();
1739 logger.info("publishing notification upon disassociation of router from VPN");
1740 notificationPublishService.putNotification(routerDisassociatedFromVpn);