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,
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 subnetmap = builder.build();
189 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
190 logger.debug("Creating/Updating subnetMap node: {} ", subnetId.getValue());
191 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
192 } catch (Exception e) {
193 logger.error("Updation of subnetMap failed for node: {}", subnetId.getValue());
195 if (isLockAcquired) {
196 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
202 protected Subnetmap removeFromSubnetNode(Uuid subnetId, Uuid networkId, Uuid routerId, Uuid vpnId, Uuid portId) {
203 Subnetmap subnetmap = null;
204 boolean isLockAcquired = false;
205 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
206 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
208 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
209 if (sn.isPresent()) {
210 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
211 if (routerId != null) {
212 builder.setRouterId(null);
214 if (networkId != null) {
215 builder.setNetworkId(null);
218 builder.setVpnId(null);
220 if (portId != null && builder.getPortList() != null) {
221 List<Uuid> portList = builder.getPortList();
222 portList.remove(portId);
223 builder.setPortList(portList);
226 subnetmap = builder.build();
227 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
228 logger.debug("Removing from existing subnetmap node: {} ", subnetId.getValue());
229 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
231 logger.warn("removing from non-existing subnetmap node: {} ", subnetId.getValue());
233 } catch (Exception e) {
234 logger.error("Removal from subnetmap failed for node: {}", subnetId.getValue());
236 if (isLockAcquired) {
237 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
243 protected Subnetmap updateSubnetmapNodeWithPorts(Uuid subnetId, Uuid portId, Uuid directPortId) {
244 Subnetmap subnetmap = null;
245 boolean isLockAcquired = false;
246 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class,
247 new SubnetmapKey(subnetId)).build();
249 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
250 if (sn.isPresent()) {
251 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
252 if (null != portId) {
253 List<Uuid> portList = builder.getPortList();
254 if (null == portList) {
255 portList = new ArrayList<Uuid>();
257 portList.add(portId);
258 builder.setPortList(portList);
259 logger.debug("Updating existing subnetmap node {} with port {}", subnetId.getValue(),
262 if (null != directPortId) {
263 List<Uuid> directPortList = builder.getDirectPortList();
264 if (null == directPortList) {
265 directPortList = new ArrayList<Uuid>();
267 directPortList.add(directPortId);
268 builder.setDirectPortList(directPortList);
269 logger.debug("Updating existing subnetmap node {} with port {}", subnetId.getValue(),
270 directPortId.getValue());
272 subnetmap = builder.build();
273 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
274 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
276 logger.error("Trying to update non-existing subnetmap node {} ", subnetId.getValue());
278 } catch (Exception e) {
279 logger.error("Updating port list of a given subnetMap failed for node: {} with exception{}",
280 subnetId.getValue(), e);
282 if (isLockAcquired) {
283 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
289 protected Subnetmap removePortsFromSubnetmapNode(Uuid subnetId, Uuid portId, Uuid directPortId) {
290 Subnetmap subnetmap = null;
291 boolean isLockAcquired = false;
292 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class,
293 new SubnetmapKey(subnetId)).build();
295 Optional<Subnetmap> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
296 if (sn.isPresent()) {
297 SubnetmapBuilder builder = new SubnetmapBuilder(sn.get());
298 if (null != portId && null != builder.getPortList()) {
299 List<Uuid> portList = builder.getPortList();
300 portList.remove(portId);
301 builder.setPortList(portList);
302 logger.debug("Removing port {} from existing subnetmap node: {} ", portId.getValue(),
303 subnetId.getValue());
305 if (null != directPortId && null != builder.getDirectPortList()) {
306 List<Uuid> directPortList = builder.getDirectPortList();
307 directPortList.remove(directPortId);
308 builder.setDirectPortList(directPortList);
309 logger.debug("Removing direct port {} from existing subnetmap node: {} ", directPortId.getValue(),
310 subnetId.getValue());
312 subnetmap = builder.build();
313 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
314 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap);
316 logger.error("Trying to remove port from non-existing subnetmap node {}", subnetId.getValue());
318 } catch (Exception e) {
319 logger.error("Removing a port from port list of a subnetmap failed for node: {} with expection {}",
320 subnetId.getValue(), e);
322 if (isLockAcquired) {
323 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
329 protected void deleteSubnetMapNode(Uuid subnetId) {
330 boolean isLockAcquired = false;
331 InstanceIdentifier<Subnetmap> subnetMapIdentifier = InstanceIdentifier.builder(Subnetmaps.class)
332 .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
333 logger.debug("removing subnetMap node: {} ", subnetId.getValue());
335 isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue());
336 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, subnetMapIdentifier);
337 } catch (Exception e) {
338 logger.error("Delete subnetMap node failed for subnet : {} ", subnetId.getValue());
340 if (isLockAcquired) {
341 NeutronvpnUtils.unlock(lockManager, subnetId.getValue());
346 private void updateVpnInstanceNode(String vpnName, List<String> rd, List<String> irt, List<String> ert) {
348 VpnInstanceBuilder builder = null;
349 List<VpnTarget> vpnTargetList = new ArrayList<>();
350 boolean isLockAcquired = false;
351 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
352 child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
354 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
356 logger.debug("Creating/Updating a new vpn-instance node: {} ", vpnName);
357 if (optionalVpn.isPresent()) {
358 builder = new VpnInstanceBuilder(optionalVpn.get());
359 logger.debug("updating existing vpninstance node");
361 builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(vpnName)).setVpnInstanceName(vpnName);
363 if (irt != null && !irt.isEmpty()) {
364 if (ert != null && !ert.isEmpty()) {
365 List<String> commonRT = new ArrayList<>(irt);
366 commonRT.retainAll(ert);
368 for (String common : commonRT) {
371 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(common)).setVrfRTValue
372 (common).setVrfRTType(VpnTarget.VrfRTType.Both).build();
373 vpnTargetList.add(vpnTarget);
376 for (String importRT : irt) {
377 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(importRT)).setVrfRTValue
378 (importRT).setVrfRTType(VpnTarget.VrfRTType.ImportExtcommunity).build();
379 vpnTargetList.add(vpnTarget);
383 if (ert != null && !ert.isEmpty()) {
384 for (String exportRT : ert) {
385 VpnTarget vpnTarget = new VpnTargetBuilder().setKey(new VpnTargetKey(exportRT)).setVrfRTValue
386 (exportRT).setVrfRTType(VpnTarget.VrfRTType.ExportExtcommunity).build();
387 vpnTargetList.add(vpnTarget);
391 VpnTargets vpnTargets = new VpnTargetsBuilder().setVpnTarget(vpnTargetList).build();
393 Ipv4FamilyBuilder ipv4vpnBuilder = new Ipv4FamilyBuilder().setVpnTargets(vpnTargets);
395 if (rd != null && !rd.isEmpty()) {
396 ipv4vpnBuilder.setRouteDistinguisher(rd.get(0));
399 VpnInstance newVpn = builder.setIpv4Family(ipv4vpnBuilder.build()).build();
400 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnName);
401 logger.debug("Creating/Updating vpn-instance for {} ", vpnName);
402 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier, newVpn);
403 } catch (Exception e) {
404 logger.error("Update VPN Instance node failed for node: {} {} {} {}", vpnName, rd, irt, ert);
406 if (isLockAcquired) {
407 NeutronvpnUtils.unlock(lockManager, vpnName);
412 private void deleteVpnMapsNode(Uuid vpnid) {
413 boolean isLockAcquired = false;
414 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
415 .child(VpnMap.class, new VpnMapKey(vpnid)).build();
416 logger.debug("removing vpnMaps node: {} ", vpnid.getValue());
418 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnid.getValue());
419 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
420 } catch (Exception e) {
421 logger.error("Delete vpnMaps node failed for vpn : {} ", vpnid.getValue());
423 if (isLockAcquired) {
424 NeutronvpnUtils.unlock(lockManager, vpnid.getValue());
429 private void updateVpnMaps(Uuid vpnId, String name, Uuid router, Uuid tenantId, List<Uuid> networks) {
430 VpnMapBuilder builder;
431 boolean isLockAcquired = false;
432 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
433 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
435 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
437 if (optionalVpnMap.isPresent()) {
438 builder = new VpnMapBuilder(optionalVpnMap.get());
440 builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId);
444 builder.setName(name);
446 if (tenantId != null) {
447 builder.setTenantId(tenantId);
449 if (router != null) {
450 builder.setRouterId(router);
452 if (networks != null) {
453 List<Uuid> nwList = builder.getNetworkIds();
454 if (nwList == null) {
455 nwList = new ArrayList<>();
457 nwList.addAll(networks);
458 builder.setNetworkIds(nwList);
461 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
462 logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue());
463 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build());
464 logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue());
465 } catch (Exception e) {
466 logger.error("UpdateVpnMaps failed for node: {} ", vpnId.getValue());
468 if (isLockAcquired) {
469 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
474 private void clearFromVpnMaps(Uuid vpnId, Uuid routerId, List<Uuid> networkIds) {
475 boolean isLockAcquired = false;
476 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class)
477 .child(VpnMap.class, new VpnMapKey(vpnId)).build();
478 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
480 if (optionalVpnMap.isPresent()) {
481 VpnMap vpnMap = optionalVpnMap.get();
482 VpnMapBuilder vpnMapBuilder = new VpnMapBuilder(vpnMap);
483 if (routerId != null) {
484 if (vpnMap.getNetworkIds() == null && routerId.equals(vpnMap.getVpnId())) {
486 // remove entire node in case of internal VPN
487 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
488 logger.debug("removing vpnMaps node: {} ", vpnId);
489 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier);
490 } catch (Exception e) {
491 logger.error("Deletion of vpnMaps node failed for vpn {}", vpnId.getValue());
493 if (isLockAcquired) {
494 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
499 vpnMapBuilder.setRouterId(null);
501 if (networkIds != null) {
502 List<Uuid> vpnNw = vpnMap.getNetworkIds();
503 for (Uuid nw : networkIds) {
506 if (vpnNw.isEmpty()) {
507 logger.debug("setting networks null in vpnMaps node: {} ", vpnId.getValue());
508 vpnMapBuilder.setNetworkIds(null);
510 vpnMapBuilder.setNetworkIds(vpnNw);
515 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
516 logger.debug("clearing from vpnMaps node: {} ", vpnId.getValue());
517 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build
519 } catch (Exception e) {
520 logger.error("Clearing from vpnMaps node failed for vpn {}", vpnId.getValue());
522 if (isLockAcquired) {
523 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
527 logger.error("VPN : {} not found", vpnId.getValue());
529 logger.debug("Clear from VPNMaps DS successful for VPN {} ", vpnId.getValue());
532 private void deleteVpnInstance(Uuid vpnId) {
533 boolean isLockAcquired = false;
534 InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
535 child(VpnInstance.class, new VpnInstanceKey(vpnId.getValue())).build();
537 isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue());
538 logger.debug("Deleting vpnInstance {}", vpnId.getValue());
539 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier);
540 } catch (Exception e) {
541 logger.error("Deletion of VPNInstance node failed for VPN {}", vpnId.getValue());
543 if (isLockAcquired) {
544 NeutronvpnUtils.unlock(lockManager, vpnId.getValue());
549 protected void createVpnInterface(Uuid vpnId, Port port) {
550 boolean isLockAcquired = false;
551 if (vpnId == null || port == null) {
554 String infName = port.getUuid().getValue();
555 List<Adjacency> adjList = new ArrayList<>();
556 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
558 // find router associated to vpn
559 Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId);
561 if (routerId != null) {
562 rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
564 // find all subnets to which this port is associated
565 List<FixedIps> ips = port.getFixedIps();
566 // create adjacency list
567 for (FixedIps ip : ips) {
568 // create vm adjacency
569 StringBuilder IpPrefixBuild = new StringBuilder(ip.getIpAddress().getIpv4Address().getValue());
570 String IpPrefix = IpPrefixBuild.append("/32").toString();
571 Adjacency vmAdj = new AdjacencyBuilder().setKey(new AdjacencyKey(IpPrefix)).setIpAddress(IpPrefix)
572 .setMacAddress(port.getMacAddress().getValue()).build();
574 // create extra route adjacency
575 if (rtr != null && rtr.getRoutes() != null) {
576 List<Routes> routeList = rtr.getRoutes();
577 List<Adjacency> erAdjList = addAdjacencyforExtraRoute(routeList, false, infName);
578 if (erAdjList != null && !erAdjList.isEmpty()) {
579 adjList.addAll(erAdjList);
583 // create vpn-interface on this neutron port
584 Adjacencies adjs = new AdjacenciesBuilder().setAdjacency(adjList).build();
585 VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName)).
586 setName(infName).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs);
587 VpnInterface vpnIf = vpnb.build();
590 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
591 logger.debug("Creating vpn interface {}", vpnIf);
592 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
593 } catch (Exception ex) {
594 logger.error("Creation of vpninterface {} failed due to {}", infName, ex);
596 if (isLockAcquired) {
597 NeutronvpnUtils.unlock(lockManager, infName);
602 protected void deleteVpnInterface(Port port) {
605 boolean isLockAcquired = false;
606 String infName = port.getUuid().getValue();
607 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
610 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
611 logger.debug("Deleting vpn interface {}", infName);
612 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
613 } catch (Exception ex) {
614 logger.error("Deletion of vpninterface {} failed due to {}", infName, ex);
616 if (isLockAcquired) {
617 NeutronvpnUtils.unlock(lockManager, infName);
623 protected void updateVpnInterface(Uuid vpnId, Port port) {
624 if (vpnId == null || port == null) {
627 boolean isLockAcquired = false;
628 String infName = port.getUuid().getValue();
629 InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
631 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
632 .CONFIGURATION, vpnIfIdentifier);
633 if (optionalVpnInterface.isPresent()) {
634 VpnInterfaceBuilder vpnIfBuilder = new VpnInterfaceBuilder(optionalVpnInterface.get());
635 VpnInterface vpnIf = vpnIfBuilder.setVpnInstanceName(vpnId.getValue()).build();
636 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
637 logger.debug("Updating vpn interface {}", vpnIf);
638 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
640 logger.error("VPN Interface {} not found", infName);
642 } catch (Exception ex) {
643 logger.error("Updation of vpninterface {} failed due to {}", infName, ex);
645 if (isLockAcquired) {
646 NeutronvpnUtils.unlock(lockManager, infName);
651 public void createL3InternalVpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
652 Uuid router, List<Uuid> networks) {
654 // Update VPN Instance node
655 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
657 // Update local vpn-subnet DS
658 updateVpnMaps(vpn, name, router, tenant, networks);
660 if (router != null) {
661 Uuid existingVpn = NeutronvpnUtils.getVpnForRouter(broker, router, true);
662 if (existingVpn != null) {
663 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, router);
664 if (routerSubnets != null) {
665 // Update the router interfaces alone and exit
666 for (Uuid subnetId : routerSubnets) {
667 InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).
668 child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
669 Optional<Subnetmap> snMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id);
670 if (snMap.isPresent()) {
671 Subnetmap sn = snMap.get();
672 List<Uuid> portList = sn.getPortList();
673 if (portList != null) {
674 for (Uuid port : sn.getPortList()) {
675 addToNeutronRouterInterfacesMap(router, port.getValue());
681 logger.info("Creation of Internal L3VPN skipped for VPN {} due to router {} already associated to " +
682 "external VPN {}", vpn.getValue(), router.getValue(), existingVpn.getValue());
685 associateRouterToInternalVpn(vpn, router);
689 public void createL3Vpn(Uuid vpn, String name, Uuid tenant, List<String> rd, List<String> irt, List<String> ert,
690 Uuid router, List<Uuid> networks) {
692 // Update VPN Instance node
693 updateVpnInstanceNode(vpn.getValue(), rd, irt, ert);
695 // Update local vpn-subnet DS
696 updateVpnMaps(vpn, name, router, tenant, networks);
698 if (router != null) {
699 associateRouterToVpn(vpn, router);
701 if (networks != null) {
702 associateNetworksToVpn(vpn, networks);
707 public Future<RpcResult<CreateL3VPNOutput>> createL3VPN(CreateL3VPNInput input) {
709 CreateL3VPNOutputBuilder opBuilder = new CreateL3VPNOutputBuilder();
710 SettableFuture<RpcResult<CreateL3VPNOutput>> result = SettableFuture.create();
711 List<RpcError> errorList = new ArrayList<>();
712 int failurecount = 0;
713 int warningcount = 0;
715 List<L3vpn> vpns = input.getL3vpn();
716 for (L3vpn vpn : vpns) {
717 RpcError error = null;
719 if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
720 msg = String.format("Creation of L3VPN failed for VPN %s due to absence of RD/iRT/eRT input",
721 vpn.getId().getValue());
723 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
724 errorList.add(error);
728 if (vpn.getRouteDistinguisher().size() > 1) {
729 msg = String.format("Creation of L3VPN failed for VPN %s due to multiple RD input %s",
730 vpn.getId().getValue(), vpn.getRouteDistinguisher());
732 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
733 errorList.add(error);
737 if (vpn.getRouterId() != null) {
738 if (NeutronvpnUtils.getNeutronRouter(broker, vpn.getRouterId()) == null) {
739 msg = String.format("Creation of L3VPN failed for VPN %s due to router not found %s",
740 vpn.getId().getValue(), vpn.getRouterId().getValue());
742 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
743 errorList.add(error);
747 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, vpn.getRouterId(), true);
749 msg = String.format("Creation of L3VPN failed for VPN %s due to router %s already associated to " +
750 "another VPN %s", vpn.getId().getValue(), vpn.getRouterId().getValue(), vpnId.getValue());
752 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
753 errorList.add(error);
758 if (vpn.getNetworkIds() != null) {
759 for (Uuid nw : vpn.getNetworkIds()) {
760 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
761 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
762 if (network == null) {
763 msg = String.format("Creation of L3VPN failed for VPN %s due to network not found %s",
764 vpn.getId().getValue(), nw.getValue());
766 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
767 errorList.add(error);
769 } else if (vpnId != null) {
770 msg = String.format("Creation of L3VPN failed for VPN %s due to network %s already associated" +
771 " to another VPN %s", vpn.getId().getValue(), nw.getValue(), vpnId.getValue());
773 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
774 errorList.add(error);
783 createL3Vpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(),
784 vpn.getImportRT(), vpn.getExportRT(), vpn.getRouterId(), vpn.getNetworkIds());
785 } catch (Exception ex) {
786 msg = String.format("Creation of L3VPN failed for VPN %s", vpn.getId().getValue());
787 logger.error(msg, ex);
788 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
789 errorList.add(error);
793 // if at least one succeeds; result is success
794 // if none succeeds; result is failure
795 if (failurecount + warningcount == vpns.size()) {
796 result.set(RpcResultBuilder.<CreateL3VPNOutput>failed().withRpcErrors(errorList).build());
798 List<String> errorResponseList = new ArrayList<>();
799 if (!errorList.isEmpty()) {
800 for (RpcError rpcError : errorList) {
801 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
802 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
803 errorResponseList.add(errorResponse);
806 errorResponseList.add("Operation successful with no errors");
808 opBuilder.setResponse(errorResponseList);
809 result.set(RpcResultBuilder.<CreateL3VPNOutput>success().withResult(opBuilder.build()).build());
815 public Future<RpcResult<GetL3VPNOutput>> getL3VPN(GetL3VPNInput input) {
817 GetL3VPNOutputBuilder opBuilder = new GetL3VPNOutputBuilder();
818 SettableFuture<RpcResult<GetL3VPNOutput>> result = SettableFuture.create();
819 Uuid inputVpnId = input.getId();
820 List<VpnInstance> vpns = new ArrayList<>();
823 if (inputVpnId == null) {
825 InstanceIdentifier<VpnInstances> vpnsIdentifier =
826 InstanceIdentifier.builder(VpnInstances.class).build();
827 Optional<VpnInstances> optionalVpns = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
829 if (optionalVpns.isPresent() && optionalVpns.get().getVpnInstance() != null) {
830 for (VpnInstance vpn : optionalVpns.get().getVpnInstance()) {
831 // eliminating internal VPNs from getL3VPN output
832 if (vpn.getIpv4Family().getRouteDistinguisher() != null) {
838 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
839 .withWarning(ErrorType.PROTOCOL, "", "No VPN is present").build());
843 String name = inputVpnId.getValue();
844 InstanceIdentifier<VpnInstance> vpnIdentifier =
845 InstanceIdentifier.builder(VpnInstances.class)
846 .child(VpnInstance.class, new VpnInstanceKey(name)).build();
847 // read VpnInstance Info
848 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
850 if (optionalVpn.isPresent()) {
851 vpns.add(optionalVpn.get());
853 String message = String.format("GetL3VPN failed because VPN %s is not present", name);
854 logger.error(message);
855 result.set(RpcResultBuilder.<GetL3VPNOutput>failed()
856 .withWarning(ErrorType.PROTOCOL, "invalid-value", message).build());
859 List<L3vpnInstances> l3vpnList = new ArrayList<>();
860 for (VpnInstance vpnInstance : vpns) {
861 Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName());
863 InstanceIdentifier<VpnMap> vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class).child(VpnMap
864 .class, new VpnMapKey(vpnId)).build();
865 L3vpnInstancesBuilder l3vpn = new L3vpnInstancesBuilder();
867 List<String> rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(","));
868 List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
870 List<String> ertList = new ArrayList<>();
871 List<String> irtList = new ArrayList<>();
873 for (VpnTarget vpnTarget : vpnTargetList) {
874 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
875 ertList.add(vpnTarget.getVrfRTValue());
877 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ImportExtcommunity) {
878 irtList.add(vpnTarget.getVrfRTValue());
880 if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.Both) {
881 ertList.add(vpnTarget.getVrfRTValue());
882 irtList.add(vpnTarget.getVrfRTValue());
886 l3vpn.setId(vpnId).setRouteDistinguisher(rd).setImportRT(irtList).setExportRT(ertList);
887 Optional<VpnMap> optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
889 if (optionalVpnMap.isPresent()) {
890 VpnMap vpnMap = optionalVpnMap.get();
891 l3vpn.setRouterId(vpnMap.getRouterId()).setNetworkIds(vpnMap.getNetworkIds())
892 .setTenantId(vpnMap.getTenantId()).setName(vpnMap.getName());
894 l3vpnList.add(l3vpn.build());
897 opBuilder.setL3vpnInstances(l3vpnList);
898 result.set(RpcResultBuilder.<GetL3VPNOutput>success().withResult(opBuilder.build()).build());
900 } catch (Exception ex) {
901 String message = String.format("GetL3VPN failed due to %s", ex.getMessage());
902 logger.error(message, ex);
903 result.set(RpcResultBuilder.<GetL3VPNOutput>failed().withError(ErrorType.APPLICATION, message).build());
909 public Future<RpcResult<DeleteL3VPNOutput>> deleteL3VPN(DeleteL3VPNInput input) {
911 DeleteL3VPNOutputBuilder opBuilder = new DeleteL3VPNOutputBuilder();
912 SettableFuture<RpcResult<DeleteL3VPNOutput>> result = SettableFuture.create();
913 List<RpcError> errorList = new ArrayList<>();
915 int failurecount = 0;
916 int warningcount = 0;
917 List<Uuid> vpns = input.getId();
918 for (Uuid vpn : vpns) {
922 InstanceIdentifier<VpnInstance> vpnIdentifier =
923 InstanceIdentifier.builder(VpnInstances.class)
924 .child(VpnInstance.class, new VpnInstanceKey(vpn.getValue())).build();
925 Optional<VpnInstance> optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
927 if (optionalVpn.isPresent()) {
930 msg = String.format("VPN with vpnid: %s does not exist", vpn.getValue());
932 error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-value", msg);
933 errorList.add(error);
936 } catch (Exception ex) {
937 msg = String.format("Deletion of L3VPN failed when deleting for uuid %s", vpn.getValue());
938 logger.error(msg, ex);
939 error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage());
940 errorList.add(error);
944 // if at least one succeeds; result is success
945 // if none succeeds; result is failure
946 if (failurecount + warningcount == vpns.size()) {
947 result.set(RpcResultBuilder.<DeleteL3VPNOutput>failed().withRpcErrors(errorList).build());
949 List<String> errorResponseList = new ArrayList<>();
950 if (!errorList.isEmpty()) {
951 for (RpcError rpcError : errorList) {
952 String errorResponse = String.format("ErrorType: " + rpcError.getErrorType() + ", " + "ErrorTag: " +
953 rpcError.getTag() + ", " + "ErrorMessage: " + rpcError.getMessage());
954 errorResponseList.add(errorResponse);
957 errorResponseList.add("Operation successful with no errors");
959 opBuilder.setResponse(errorResponseList);
960 result.set(RpcResultBuilder.<DeleteL3VPNOutput>success().withResult(opBuilder.build()).build());
965 protected void addSubnetToVpn(Uuid vpnId, Uuid subnet) {
966 logger.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
967 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId);
968 boolean isLockAcquired = false;
969 String lockName = vpnId.getValue() + subnet.getValue();
970 String elanInstanceName = sn.getNetworkId().getValue();
971 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
972 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
973 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
975 //TODO: Cache it in add so can reuse it in update and delete. Best would be to cache in some ElanUtils
976 long elanTag = elanInstance.get().getElanTag();
977 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
978 if (vpnId.equals(routerId)) {
979 isExternalVpn = false;
981 isExternalVpn = true;
984 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
985 checkAndPublishSubnetAddNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag);
986 logger.debug("Subnet added to Vpn notification sent");
987 }catch (Exception e){
988 logger.error("Subnet added to Vpn notification failed",e);
990 if (isLockAcquired) {
991 NeutronvpnUtils.unlock(lockManager, lockName);
994 // Check if there are ports on this subnet and add corresponding vpn-interfaces
995 List<Uuid> portList = sn.getPortList();
996 if (portList != null) {
997 for (Uuid port : sn.getPortList()) {
998 logger.debug("adding vpn-interface for port {}", port.getValue());
999 createVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port));
1000 if (routerId != null) {
1001 addToNeutronRouterInterfacesMap(routerId, port.getValue());
1007 protected void updateVpnForSubnet(Uuid vpnId, Uuid subnet, boolean isBeingAssociated) {
1008 logger.debug("Updating VPN {} for subnet {}", vpnId.getValue(), subnet.getValue());
1009 Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId);
1010 boolean isLockAcquired = false;
1011 String lockName = vpnId.getValue() + subnet.getValue();
1012 String elanInstanceName = sn.getNetworkId().getValue();
1013 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
1014 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
1015 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1017 long elanTag = elanInstance.get().getElanTag();
1019 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
1020 checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated,
1022 logger.debug("Subnet updated in Vpn notification sent");
1023 } catch (Exception e) {
1024 logger.error("Subnet updated in Vpn notification failed", e);
1026 if (isLockAcquired) {
1027 NeutronvpnUtils.unlock(lockManager, lockName);
1030 // Check for ports on this subnet and update association of corresponding vpn-interfaces to external vpn
1031 List<Uuid> portList = sn.getPortList();
1032 if (portList != null) {
1033 for (Uuid port : sn.getPortList()) {
1034 logger.debug("Updating vpn-interface for port {}", port.getValue());
1035 updateVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port));
1040 InstanceIdentifier<RouterInterfaces> getRouterInterfacesId(Uuid routerId) {
1041 return InstanceIdentifier.builder(RouterInterfacesMap.class)
1042 .child(RouterInterfaces.class, new RouterInterfacesKey(routerId)).build();
1044 void addToNeutronRouterInterfacesMap(Uuid routerId, String interfaceName) {
1045 InstanceIdentifier<RouterInterfaces> routerInterfacesId = getRouterInterfacesId(routerId);
1046 Optional<RouterInterfaces> optRouterInterfaces = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
1047 Interfaces routerInterface = new InterfacesBuilder().setKey(new InterfacesKey(interfaceName)).setInterfaceId(interfaceName).build();
1048 if(optRouterInterfaces.isPresent()) {
1049 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId.child(Interfaces.class, new InterfacesKey(interfaceName)), routerInterface);
1051 RouterInterfacesBuilder builder = new RouterInterfacesBuilder().setRouterId(routerId);
1052 List<Interfaces> interfaces = new ArrayList<>();
1053 interfaces.add(routerInterface);
1054 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId, builder.setInterfaces(interfaces).build());
1058 void removeFromNeutronRouterInterfacesMap(Uuid routerId, String interfaceName) {
1059 InstanceIdentifier<RouterInterfaces> routerInterfacesId = getRouterInterfacesId(routerId);
1060 Optional<RouterInterfaces> optRouterInterfaces = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
1061 Interfaces routerInterface = new InterfacesBuilder().setKey(new InterfacesKey(interfaceName)).setInterfaceId(interfaceName).build();
1062 if(optRouterInterfaces.isPresent()) {
1063 RouterInterfaces routerInterfaces = optRouterInterfaces.get();
1064 List<Interfaces> interfaces = routerInterfaces.getInterfaces();
1065 if(interfaces != null && interfaces.remove(routerInterface)) {
1066 if(interfaces.isEmpty()) {
1067 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId);
1069 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, routerInterfacesId.child(Interfaces.class, new InterfacesKey(interfaceName)));
1075 protected List<Adjacency> addAdjacencyforExtraRoute(List<Routes> routeList, boolean rtrUp, String vpnifname) {
1076 List<Adjacency> adjList = new ArrayList<>();
1077 for (Routes route : routeList) {
1078 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
1079 boolean isLockAcquired = false;
1080 String nextHop = String.valueOf(route.getNexthop().getValue());
1081 String destination = String.valueOf(route.getDestination().getValue());
1083 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
1084 logger.trace("Adding extra route with nexthop {}, destination {}, infName {}", nextHop,
1085 destination, infName);
1086 Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey
1087 (new AdjacencyKey(destination)).build();
1089 if (infName.equals(vpnifname)) {
1094 InstanceIdentifier<VpnInterface> vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
1095 child(VpnInterface.class, new VpnInterfaceKey(infName)).build();
1097 Optional<VpnInterface> optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType
1098 .CONFIGURATION, vpnIfIdentifier);
1099 if (optionalVpnInterface.isPresent()) {
1100 Adjacencies erAdjs = new AdjacenciesBuilder().setAdjacency(Arrays.asList(erAdj)).build();
1101 VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName))
1102 .addAugmentation(Adjacencies.class, erAdjs).build();
1103 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
1104 logger.debug("Adding extra route {}", route);
1105 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf);
1107 logger.error("VM adjacency for interface {} not present ; cannot add extra route adjacency",
1110 } catch (Exception e) {
1111 logger.error("exception in adding extra route: {}" + e);
1113 if (isLockAcquired) {
1114 NeutronvpnUtils.unlock(lockManager, infName);
1118 logger.error("Incorrect input received for extra route. {}", route);
1124 protected void removeAdjacencyforExtraRoute(List<Routes> routeList) {
1125 for (Routes route : routeList) {
1126 if (route != null && route.getNexthop() != null && route.getDestination() != null) {
1127 boolean isLockAcquired = false;
1128 String nextHop = String.valueOf(route.getNexthop().getValue());
1129 String destination = String.valueOf(route.getDestination().getValue());
1131 String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop);
1132 logger.trace("Removing extra route with nexthop {}, destination {}, infName {}", nextHop,
1133 destination, infName);
1134 InstanceIdentifier<Adjacency> adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class).
1135 child(VpnInterface.class, new VpnInterfaceKey(infName)).augmentation(Adjacencies.class)
1136 .child(Adjacency.class, new AdjacencyKey(destination)).build();
1138 isLockAcquired = NeutronvpnUtils.lock(lockManager, infName);
1139 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, adjacencyIdentifier);
1140 logger.trace("extra route {} deleted successfully", route);
1141 } catch (Exception e) {
1142 logger.error("exception in deleting extra route: {}" + e);
1144 if (isLockAcquired) {
1145 NeutronvpnUtils.unlock(lockManager, infName);
1149 logger.error("Incorrect input received for extra route. {}", route);
1154 protected void removeL3Vpn(Uuid id) {
1156 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, id);
1157 Uuid router = vpnMap.getRouterId();
1158 // dissociate router
1159 if (router != null) {
1160 dissociateRouterFromVpn(id, router);
1162 // dissociate networks
1163 if (!id.equals(router)) {
1164 dissociateNetworksFromVpn(id, vpnMap.getNetworkIds());
1166 // remove entire vpnMaps node
1167 deleteVpnMapsNode(id);
1169 // remove vpn-instance
1170 deleteVpnInstance(id);
1173 protected void removeSubnetFromVpn(Uuid vpnId, Uuid subnet) {
1174 logger.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
1175 Subnetmap sn = NeutronvpnUtils.getSubnetmap(broker, subnet);
1176 boolean isLockAcquired = false;
1177 String lockName = vpnId.getValue() + subnet.getValue();
1178 String elanInstanceName = sn.getNetworkId().getValue();
1179 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
1180 (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
1181 Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1183 long elanTag = elanInstance.get().getElanTag();
1184 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
1185 if (vpnId.equals(routerId)) {
1186 isExternalVpn = false;
1188 isExternalVpn = true;
1191 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
1192 checkAndPublishSubnetDelNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag);
1193 logger.debug("Subnet removed from Vpn notification sent");
1194 }catch (Exception e){
1195 logger.error("Subnet removed from Vpn notification failed",e);
1197 if (isLockAcquired) {
1198 NeutronvpnUtils.unlock(lockManager, lockName);
1202 // Check if there are ports on this subnet; remove corresponding vpn-interfaces
1203 List<Uuid> portList = sn.getPortList();
1204 if (portList != null) {
1205 for (Uuid port : sn.getPortList()) {
1206 logger.debug("removing vpn-interface for port {}", port.getValue());
1207 deleteVpnInterface(NeutronvpnUtils.getNeutronPort(broker, port));
1208 if (routerId != null) {
1209 removeFromNeutronRouterInterfacesMap(routerId, port.getValue());
1213 // update subnet-vpn association
1214 removeFromSubnetNode(subnet, null, null, vpnId, null);
1216 logger.warn("Subnetmap for subnet {} not found", subnet.getValue());
1220 protected void associateRouterToVpn(Uuid vpnId, Uuid routerId) {
1221 updateVpnMaps(vpnId, null, routerId, null, null);
1222 logger.debug("Updating association of subnets to external vpn {}", vpnId.getValue());
1223 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1224 // if (!vpnId.equals(routerId)) {
1225 if (routerSubnets != null) {
1226 for (Uuid subnetId : routerSubnets) {
1227 updateVpnForSubnet(vpnId, subnetId, true);
1231 checkAndPublishRouterAssociatedtoVpnNotification(routerId, vpnId);
1232 logger.debug("notification upon association of router {} to VPN {} published", routerId.getValue(),
1234 } catch (Exception e) {
1235 logger.error("publishing of notification upon association of router {} to VPN {} failed : ", routerId
1236 .getValue(), vpnId.getValue(), e);
1240 protected void dissociatefixedIPFromFloatingIP(String fixedNeutronPortName) {
1241 floatingIpMapListener.dissociatefixedIPFromFloatingIP(fixedNeutronPortName);
1244 protected void associateRouterToInternalVpn(Uuid vpnId, Uuid routerId) {
1245 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1246 logger.debug("Adding subnets to internal vpn {}", vpnId.getValue());
1247 for (Uuid subnet : routerSubnets) {
1248 addSubnetToVpn(vpnId, subnet);
1252 protected void dissociateRouterFromVpn(Uuid vpnId, Uuid routerId) {
1254 List<Uuid> routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId);
1255 if (routerSubnets != null) {
1256 for (Uuid subnetId : routerSubnets) {
1257 logger.debug("Updating association of subnets to internal vpn {}", routerId.getValue());
1258 updateVpnForSubnet(routerId, subnetId,false);
1261 clearFromVpnMaps(vpnId, routerId, null);
1263 checkAndPublishRouterDisassociatedFromVpnNotification(routerId, vpnId);
1264 logger.debug("notification upon disassociation of router {} from VPN {} published", routerId.getValue(),
1266 } catch (Exception e) {
1267 logger.error("publishing of notification upon disassociation of router {} from VPN {} failed : ", routerId
1268 .getValue(), vpnId.getValue(), e);
1272 protected List<String> associateNetworksToVpn(Uuid vpn, List<Uuid> networks) {
1274 List<String> failedNwList = new ArrayList<String>();
1275 List<Uuid> passedNwList = new ArrayList<>();
1276 if (!networks.isEmpty()) {
1277 // process corresponding subnets for VPN
1278 for (Uuid nw : networks) {
1279 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
1280 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
1281 if (network == null) {
1282 failedNwList.add(String.format("network %s not found", nw.getValue()));
1283 } else if (vpnId != null) {
1284 failedNwList.add(String.format("network %s already associated to another VPN %s", nw.getValue(),
1287 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1288 logger.debug("Adding network subnets...{}", networkSubnets);
1289 if (networkSubnets != null) {
1290 for (Uuid subnet : networkSubnets) {
1291 // check if subnet added as router interface to some router
1292 Uuid subnetVpnId = NeutronvpnUtils.getVpnForSubnet(broker, subnet);
1293 if (subnetVpnId == null) {
1294 addSubnetToVpn(vpn, subnet);
1295 passedNwList.add(nw);
1297 failedNwList.add(String.format("subnet %s already added as router interface bound to " +
1298 "internal/external VPN %s", subnet.getValue (), subnetVpnId.getValue()));
1302 if (network.getAugmentation(NetworkL3Extension.class) != null && network.getAugmentation
1303 (NetworkL3Extension.class).isExternal()) {
1304 nvpnNatManager.addExternalNetworkToVpn(network, vpn);
1308 updateVpnMaps(vpn, null, null, null, passedNwList);
1310 return failedNwList;
1313 protected List<String> dissociateNetworksFromVpn(Uuid vpn, List<Uuid> networks) {
1315 List<String> failedNwList = new ArrayList<String>();
1316 List<Uuid> passedNwList = new ArrayList<>();
1317 if (networks != null && !networks.isEmpty()) {
1318 // process corresponding subnets for VPN
1319 for (Uuid nw : networks) {
1320 Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
1321 if (network == null) {
1322 failedNwList.add(String.format("network %s not found", nw.getValue()));
1324 Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
1325 if (vpn.equals(vpnId)) {
1326 List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
1327 logger.debug("Removing network subnets...");
1328 if (networkSubnets != null) {
1329 for (Uuid subnet : networkSubnets) {
1330 removeSubnetFromVpn(vpn, subnet);
1331 passedNwList.add(nw);
1335 if (vpnId == null) {
1336 failedNwList.add(String.format("input network %s not associated to any vpn yet", nw
1339 failedNwList.add(String.format("input network %s associated to a another vpn %s instead " +
1340 "of the one given as input", nw.getValue(), vpnId.getValue()));
1343 if (network.getAugmentation(NetworkL3Extension.class).isExternal()) {
1344 nvpnNatManager.removeExternalNetworkFromVpn(network);
1348 clearFromVpnMaps(vpn, null, passedNwList);
1350 return failedNwList;
1354 public Future<RpcResult<AssociateNetworksOutput>> associateNetworks(AssociateNetworksInput input) {
1356 AssociateNetworksOutputBuilder opBuilder = new AssociateNetworksOutputBuilder();
1357 SettableFuture<RpcResult<AssociateNetworksOutput>> result = SettableFuture.create();
1358 logger.debug("associateNetworks {}", input);
1359 StringBuilder returnMsg = new StringBuilder();
1360 Uuid vpnId = input.getVpnId();
1363 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1364 List<Uuid> netIds = input.getNetworkId();
1365 if (netIds != null && !netIds.isEmpty()) {
1366 List<String> failed = associateNetworksToVpn(vpnId, netIds);
1367 if (!failed.isEmpty()) {
1368 returnMsg.append(failed);
1372 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1374 if (returnMsg.length() != 0) {
1375 String message = String.format("associate Networks to vpn %s failed due to %s", vpnId.getValue(),
1377 logger.error(message);
1378 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1380 opBuilder.setResponse(errorResponse);
1381 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().withResult(opBuilder.build()).build());
1383 result.set(RpcResultBuilder.<AssociateNetworksOutput>success().build());
1385 } catch (Exception ex) {
1386 String message = String.format("associate Networks to vpn %s failed due to %s", input.getVpnId().getValue(),
1388 logger.error(message, ex);
1389 result.set(RpcResultBuilder.<AssociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1392 logger.debug("associateNetworks returns..");
1397 public Future<RpcResult<Void>> associateRouter(AssociateRouterInput input) {
1399 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1400 logger.debug("associateRouter {}", input);
1401 StringBuilder returnMsg = new StringBuilder();
1402 Uuid vpnId = input.getVpnId();
1403 Uuid routerId = input.getRouterId();
1405 if (routerId != null && vpnId != null) {
1406 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1407 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, vpnId);
1408 if (rtr != null && vpnMap != null) {
1409 Uuid extVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1410 if (vpnMap.getRouterId() != null) {
1411 returnMsg.append("vpn ").append(vpnId.getValue()).append(" already associated to router ")
1412 .append(vpnMap.getRouterId().getValue());
1413 } else if (extVpnId != null) {
1414 returnMsg.append("router ").append(routerId.getValue()).append(" already associated to " +
1415 "another VPN ").append(extVpnId.getValue());
1417 associateRouterToVpn(vpnId, routerId);
1420 returnMsg.append("router not found : ").append(routerId.getValue());
1423 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1425 if (returnMsg.length() != 0) {
1426 String message = String.format("associate router to vpn %s failed due to %s", routerId.getValue(),
1428 logger.error(message);
1429 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1432 result.set(RpcResultBuilder.<Void>success().build());
1434 } catch (Exception ex) {
1435 String message = String.format("associate router %s to vpn %s failed due to %s", routerId.getValue(),
1436 vpnId.getValue(), ex.getMessage());
1437 logger.error(message, ex);
1438 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1440 logger.debug("associateRouter returns..");
1445 public Future<RpcResult<DissociateNetworksOutput>> dissociateNetworks(DissociateNetworksInput input) {
1447 DissociateNetworksOutputBuilder opBuilder = new DissociateNetworksOutputBuilder();
1448 SettableFuture<RpcResult<DissociateNetworksOutput>> result = SettableFuture.create();
1450 logger.debug("dissociateNetworks {}", input);
1451 StringBuilder returnMsg = new StringBuilder();
1452 Uuid vpnId = input.getVpnId();
1455 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1456 List<Uuid> netIds = input.getNetworkId();
1457 if (netIds != null && !netIds.isEmpty()) {
1458 List<String> failed = dissociateNetworksFromVpn(vpnId, netIds);
1459 if (!failed.isEmpty()) {
1460 returnMsg.append(failed);
1464 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1466 if (returnMsg.length() != 0) {
1467 String message = String.format("dissociate Networks to vpn %s failed due to %s", vpnId.getValue(),
1469 logger.error(message);
1470 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1472 opBuilder.setResponse(errorResponse);
1473 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().withResult(opBuilder.build()).build());
1475 result.set(RpcResultBuilder.<DissociateNetworksOutput>success().build());
1477 } catch (Exception ex) {
1478 String message = String.format("dissociate Networks to vpn %s failed due to %s", input.getVpnId().
1479 getValue(), ex.getMessage());
1480 logger.error(message, ex);
1481 result.set(RpcResultBuilder.<DissociateNetworksOutput>failed().withError(ErrorType.APPLICATION, message)
1484 logger.debug("dissociateNetworks returns..");
1489 public Future<RpcResult<Void>> dissociateRouter(DissociateRouterInput input) {
1491 SettableFuture<RpcResult<Void>> result = SettableFuture.create();
1493 logger.debug("dissociateRouter {}", input);
1494 StringBuilder returnMsg = new StringBuilder();
1495 Uuid vpnId = input.getVpnId();
1496 Uuid routerId = input.getRouterId();
1498 if (NeutronvpnUtils.getVpnMap(broker, vpnId) != null) {
1499 if (routerId != null) {
1500 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
1502 Uuid routerVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1503 if (vpnId.equals(routerVpnId)) {
1504 dissociateRouterFromVpn(vpnId, routerId);
1506 if (routerVpnId == null) {
1507 returnMsg.append("input router ").append(routerId.getValue()).append(" not associated" +
1510 returnMsg.append("input router ").append(routerId.getValue()).append(" associated to " +
1511 "vpn ").append(routerVpnId.getValue()).append("instead of the vpn given as " +
1516 returnMsg.append("router not found : ").append(routerId.getValue());
1520 returnMsg.append("VPN not found : ").append(vpnId.getValue());
1522 if (returnMsg.length() != 0) {
1523 String message = String.format("dissociate router %s to vpn %s failed due to %s", routerId.getValue(),
1524 vpnId.getValue(), returnMsg);
1525 logger.error(message);
1526 String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " +
1528 result.set(RpcResultBuilder.<Void>failed().withWarning(ErrorType.PROTOCOL, "invalid-value", message)
1531 result.set(RpcResultBuilder.<Void>success().build());
1533 } catch (Exception ex) {
1534 String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(),
1535 vpnId.getValue(), ex.getMessage());
1536 logger.error(message, ex);
1537 result.set(RpcResultBuilder.<Void>failed().withError(ErrorType.APPLICATION, message).build());
1539 logger.debug("dissociateRouter returns..");
1545 public Future<RpcResult<GetFixedIPsForNeutronPortOutput>> getFixedIPsForNeutronPort(GetFixedIPsForNeutronPortInput
1547 GetFixedIPsForNeutronPortOutputBuilder opBuilder = new GetFixedIPsForNeutronPortOutputBuilder();
1548 SettableFuture<RpcResult<GetFixedIPsForNeutronPortOutput>> result = SettableFuture.create();
1549 Uuid portId = input.getPortId();
1550 StringBuilder returnMsg = new StringBuilder();
1552 List<String> fixedIPList = new ArrayList<>();
1553 Port port = NeutronvpnUtils.getNeutronPort(broker, portId);
1555 List<FixedIps> fixedIPs = port.getFixedIps();
1556 for (FixedIps ip : fixedIPs) {
1557 fixedIPList.add(ip.getIpAddress().getIpv4Address().getValue());
1560 returnMsg.append("neutron port: ").append(portId.getValue()).append(" not found");
1562 if (returnMsg.length() != 0) {
1563 String message = String.format("Retrieval of FixedIPList for neutron port failed due to %s", returnMsg);
1564 logger.error(message);
1565 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>failed().withWarning(ErrorType.PROTOCOL,
1566 "invalid-value", message).build());
1568 opBuilder.setFixedIPs(fixedIPList);
1569 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>success().withResult(opBuilder.build())
1571 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>success().build());
1573 } catch (Exception ex) {
1574 String message = String.format("Retrieval of FixedIPList for neutron port %s failed due to %s", portId
1575 .getValue(), ex.getMessage());
1576 logger.error(message, ex);
1577 result.set(RpcResultBuilder.<GetFixedIPsForNeutronPortOutput>failed().withError(ErrorType.APPLICATION,
1583 protected void handleNeutronRouterDeleted(Uuid routerId, List<Uuid> routerSubnetIds) {
1584 // check if the router is associated to some VPN
1585 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
1586 if (vpnId != null) {
1587 // remove existing external vpn interfaces
1588 for (Uuid subnetId : routerSubnetIds) {
1589 removeSubnetFromVpn(vpnId, subnetId);
1591 clearFromVpnMaps(vpnId, routerId, null);
1593 // remove existing internal vpn interfaces
1594 for (Uuid subnetId : routerSubnetIds) {
1595 removeSubnetFromVpn(routerId, subnetId);
1598 // delete entire vpnMaps node for internal VPN
1599 deleteVpnMapsNode(routerId);
1601 // delete vpn-instance for internal VPN
1602 deleteVpnInstance(routerId);
1605 protected Subnet getNeutronSubnet(Uuid subnetId){
1606 return NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
1609 protected IpAddress getNeutronSubnetGateway(Uuid subnetId) {
1610 Subnet sn = NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
1612 return sn.getGatewayIp();
1617 protected Port getNeutronPort(String name) {
1618 return NeutronvpnUtils.getNeutronPort(broker, new Uuid(name));
1621 protected Port getNeutronPort(Uuid portId) {
1622 return NeutronvpnUtils.getNeutronPort(broker, portId);
1625 protected List<Uuid> getSubnetsforVpn(Uuid vpnid) {
1626 List<Uuid> subnets = new ArrayList<>();
1628 InstanceIdentifier<Subnetmaps> subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build();
1629 Optional<Subnetmaps> subnetmaps = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
1631 if (subnetmaps.isPresent() && subnetmaps.get().getSubnetmap() != null) {
1632 List<Subnetmap> subnetMapList = subnetmaps.get().getSubnetmap();
1633 for (Subnetmap subnetMap : subnetMapList) {
1634 if (subnetMap.getVpnId() != null && subnetMap.getVpnId().equals(vpnid)) {
1635 subnets.add(subnetMap.getId());
1642 public List<String> showNeutronPortsCLI() {
1643 List<String> result = new ArrayList<>();
1644 result.add(String.format(" %-36s %-19s %-13s %-20s ", "Port ID", "Mac Address", "Prefix Length", "IP " +
1646 result.add("-------------------------------------------------------------------------------------------");
1647 InstanceIdentifier<Ports> portidentifier = InstanceIdentifier.create(Neutron.class).child(Ports.class);
1649 Optional<Ports> ports = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, portidentifier);
1650 if (ports.isPresent() && ports.get().getPort() != null) {
1651 for (Port port : ports.get().getPort()) {
1652 List<FixedIps> fixedIPs = port.getFixedIps();
1654 if (fixedIPs != null && !fixedIPs.isEmpty()) {
1655 List<String> ipList = new ArrayList<>();
1656 for (FixedIps fixedIp : fixedIPs) {
1657 IpAddress ipAddress = fixedIp.getIpAddress();
1658 if (ipAddress.getIpv4Address() != null) {
1659 ipList.add(ipAddress.getIpv4Address().getValue());
1661 ipList.add((ipAddress.getIpv6Address().getValue()));
1664 result.add(String.format(" %-36s %-19s %-13s %-20s ", port.getUuid().getValue(), port
1665 .getMacAddress().getValue(), NeutronvpnUtils.getIPPrefixFromPort(broker, port),
1666 ipList.toString()));
1668 result.add(String.format(" %-36s %-19s %-13s %-20s ", port.getUuid().getValue(), port
1669 .getMacAddress().getValue(), "Not Assigned", "Not Assigned"));
1671 } catch (Exception e) {
1672 logger.error("Failed to retrieve neutronPorts info for port {}: ", port.getUuid().getValue(),
1674 System.out.println("Failed to retrieve neutronPorts info for port: " + port.getUuid()
1675 .getValue() + ": " + e.getMessage());
1679 } catch (Exception e) {
1680 logger.error("Failed to retrieve neutronPorts info : ", e);
1681 System.out.println("Failed to retrieve neutronPorts info : " + e.getMessage());
1686 public List<String> showVpnConfigCLI(Uuid vpnuuid) {
1687 List<String> result = new ArrayList<>();
1688 if (vpnuuid == null) {
1689 System.out.println("");
1690 System.out.println("Displaying VPN config for all VPNs");
1691 System.out.println("To display VPN config for a particular VPN, use the following syntax");
1692 System.out.println(getshowVpnConfigCLIHelp());
1695 RpcResult<GetL3VPNOutput> rpcResult = getL3VPN(new GetL3VPNInputBuilder().setId(vpnuuid).build()).get();
1696 if (rpcResult.isSuccessful()) {
1698 result.add(String.format(" %-37s %-37s %-7s ", "VPN ID", "Tenant ID", "RD"));
1700 result.add(String.format(" %-80s ", "Import-RTs"));
1702 result.add(String.format(" %-80s ", "Export-RTs"));
1704 result.add(String.format(" %-76s ", "Subnet IDs"));
1706 result.add("------------------------------------------------------------------------------------");
1708 List<L3vpnInstances> VpnList = rpcResult.getResult().getL3vpnInstances();
1709 for (L3vpnInstance Vpn : VpnList) {
1710 String tenantId = Vpn.getTenantId() != null ? Vpn.getTenantId().getValue() : "\" " +
1712 result.add(String.format(" %-37s %-37s %-7s ", Vpn.getId().getValue(), tenantId, Vpn
1713 .getRouteDistinguisher()));
1715 result.add(String.format(" %-80s ", Vpn.getImportRT()));
1717 result.add(String.format(" %-80s ", Vpn.getExportRT()));
1720 Uuid vpnid = Vpn.getId();
1721 List<Uuid> subnetList = getSubnetsforVpn(vpnid);
1722 if (!subnetList.isEmpty()) {
1723 for (Uuid subnetuuid : subnetList) {
1724 result.add(String.format(" %-76s ", subnetuuid.getValue()));
1727 result.add(String.format(" %-76s ", "\" \""));
1730 result.add("----------------------------------------");
1734 String errortag = rpcResult.getErrors().iterator().next().getTag();
1735 if (errortag == "") {
1736 System.out.println("");
1737 System.out.println("No VPN has been configured yet");
1738 } else if (errortag == "invalid-value") {
1739 System.out.println("");
1740 System.out.println("VPN " + vpnuuid.getValue() + " is not present");
1742 System.out.println("error getting VPN info : " + rpcResult.getErrors());
1743 System.out.println(getshowVpnConfigCLIHelp());
1746 } catch (InterruptedException | ExecutionException e) {
1747 logger.error("error getting VPN info : ", e);
1748 System.out.println("error getting VPN info : " + e.getMessage());
1753 private String getshowVpnConfigCLIHelp() {
1754 StringBuilder help = new StringBuilder("Usage:");
1755 help.append("display vpn-config [-vid/--vpnid <id>]");
1756 return help.toString();
1759 private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName,
1760 Boolean isExternalvpn, Long elanTag)throws InterruptedException {
1761 SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder();
1763 logger.info("publish notification called");
1765 builder.setSubnetId(subnetId);
1766 builder.setSubnetIp(subnetIp);
1767 builder.setVpnName(vpnName);
1768 builder.setExternalVpn(isExternalvpn);
1769 builder.setElanTag(elanTag);
1771 notificationPublishService.putNotification(builder.build());
1774 private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName,
1775 Boolean isExternalvpn, Long elanTag) throws InterruptedException {
1776 SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder();
1778 logger.info("publish notification called");
1780 builder.setSubnetId(subnetId);
1781 builder.setSubnetIp(subnetIp);
1782 builder.setVpnName(vpnName);
1783 builder.setExternalVpn(isExternalvpn);
1784 builder.setElanTag(elanTag);
1786 notificationPublishService.putNotification(builder.build());
1789 private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName,
1790 Boolean isExternalvpn, Long elanTag) throws InterruptedException {
1791 SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder();
1793 logger.info("publish notification called");
1795 builder.setSubnetId(subnetId);
1796 builder.setSubnetIp(subnetIp);
1797 builder.setVpnName(vpnName);
1798 builder.setExternalVpn(isExternalvpn);
1799 builder.setElanTag(elanTag);
1801 notificationPublishService.putNotification(builder.build());
1804 private void checkAndPublishRouterAssociatedtoVpnNotification(Uuid routerId, Uuid vpnId) throws
1805 InterruptedException {
1806 RouterAssociatedToVpn routerAssociatedToVpn = new RouterAssociatedToVpnBuilder().setRouterId(routerId)
1807 .setVpnId(vpnId).build();
1808 logger.info("publishing notification upon association of router to VPN");
1809 notificationPublishService.putNotification(routerAssociatedToVpn);
1812 private void checkAndPublishRouterDisassociatedFromVpnNotification(Uuid routerId, Uuid vpnId) throws
1813 InterruptedException {
1814 RouterDisassociatedFromVpn routerDisassociatedFromVpn = new RouterDisassociatedFromVpnBuilder().setRouterId
1815 (routerId).setVpnId(vpnId).build();
1816 logger.info("publishing notification upon disassociation of router from VPN");
1817 notificationPublishService.putNotification(routerDisassociatedFromVpn);