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;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Lists;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
19 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
23 import org.opendaylight.genius.mdsalutil.MDSALUtil;
24 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlanBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefs;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.ParentRefsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAcl;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.InterfaceAclBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortAddedToSubnetBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.neutron.port.data.PortFixedipToPortNameBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
50 import org.opendaylight.yangtools.concepts.ListenerRegistration;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
57 private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
59 private ListenerRegistration<DataChangeListener> listenerRegistration;
60 private final DataBroker broker;
61 private NeutronvpnManager nvpnManager;
62 private NeutronvpnNatManager nvpnNatManager;
63 private LockManagerService lockManager;
64 private NotificationPublishService notificationPublishService;
65 private NotificationService notificationService;
68 public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr,NeutronvpnNatManager nVpnNatMgr,
69 NotificationPublishService notiPublishService, NotificationService notiService) {
72 nvpnManager = nVpnMgr;
73 nvpnNatManager = nVpnNatMgr;
74 notificationPublishService = notiPublishService;
75 notificationService = notiService;
79 public void setLockManager(LockManagerService lockManager) {
80 this.lockManager = lockManager;
84 public void close() throws Exception {
85 if (listenerRegistration != null) {
87 listenerRegistration.close();
88 } catch (final Exception e) {
89 LOG.error("Error when cleaning up DataChangeListener.", e);
91 listenerRegistration = null;
93 LOG.info("N_Port listener Closed");
97 private void registerListener(final DataBroker db) {
99 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
100 InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class),
101 NeutronPortChangeListener.this, DataChangeScope.SUBTREE);
102 } catch (final Exception e) {
103 LOG.error("Neutron Manager Port DataChange listener registration fail!", e);
104 throw new IllegalStateException("Neutron Manager Port DataChange listener registration failed.", e);
109 protected void add(InstanceIdentifier<Port> identifier, Port input) {
110 if (LOG.isTraceEnabled()) {
111 LOG.trace("Adding Port : key: " + identifier + ", value=" + input);
113 Network network = NeutronvpnUtils.getNeutronNetwork(broker, input.getNetworkId());
114 if (network == null || NeutronvpnUtils.isNetworkTypeVlanOrGre(network)) {
115 //FIXME: This should be removed when support for VLAN and GRE network types is added
116 LOG.error("neutron vpn doesn't support vlan/gre network provider type for the port {} which is part of network {}.",
117 input.getName(), network);
120 NeutronvpnUtils.addToPortCache(input);
122 /* check if router interface has been created */
123 if ((input.getDeviceOwner() != null) && (input.getDeviceId() != null)) {
124 if (input.getDeviceOwner().equals(NeutronConstants.DEVICE_OWNER_ROUTER_INF)) {
125 handleRouterInterfaceAdded(input);
126 /* nothing else to do here */
130 if (input.getFixedIps() != null && !input.getFixedIps().isEmpty()) {
131 handleNeutronPortCreated(input);
137 protected void remove(InstanceIdentifier<Port> identifier, Port input) {
138 if (LOG.isTraceEnabled()) {
139 LOG.trace("Removing Port : key: " + identifier + ", value=" + input);
141 Network network = NeutronvpnUtils.getNeutronNetwork(broker, input.getNetworkId());
142 if (network == null || NeutronvpnUtils.isNetworkTypeVlanOrGre(network)) {
143 //FIXME: This should be removed when support for VLAN and GRE network types is added
144 LOG.error("neutron vpn doesn't support vlan/gre network provider type for the port {} which is part of network {}.",
145 input.getName(), network);
148 NeutronvpnUtils.removeFromPortCache(input);
150 if ((input.getDeviceOwner() != null) && (input.getDeviceId() != null)) {
151 if (input.getDeviceOwner().equals(NeutronConstants.DEVICE_OWNER_ROUTER_INF)) {
152 handleRouterInterfaceRemoved(input);
153 /* nothing else to do here */
157 if (input.getFixedIps() != null && !input.getFixedIps().isEmpty()) {
158 handleNeutronPortDeleted(input);
163 protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
164 if (LOG.isTraceEnabled()) {
165 LOG.trace("Updating Port : key: " + identifier + ", original value=" + original + ", update value=" +
169 Network network = NeutronvpnUtils.getNeutronNetwork(broker, update.getNetworkId());
170 if (network == null || NeutronvpnUtils.isNetworkTypeVlanOrGre(network)) {
171 LOG.error("neutron vpn doesn't support vlan/gre network provider type for the port {} which is part of network {}."
172 + " Skipping the processing of Port update DCN", update.getName(), network);
175 List<FixedIps> oldIPs = (original.getFixedIps() != null) ? original.getFixedIps() : new ArrayList<FixedIps>();
176 List<FixedIps> newIPs = (update.getFixedIps() != null) ? update.getFixedIps() : new ArrayList<FixedIps>();
178 /* check if VIF type updated as part of port binding */
179 if (NeutronvpnUtils.isPortVifTypeUpdated(original, update)) {
180 updateOfPortInterface(original, update);
182 NeutronvpnUtils.addToPortCache(update);
184 /* check if router interface has been updated */
185 if ((update.getDeviceOwner() != null) && (update.getDeviceId() != null)) {
186 if (update.getDeviceOwner().equals(NeutronConstants.DEVICE_OWNER_ROUTER_INF)) {
187 handleRouterInterfaceAdded(update);
188 /* nothing else to do here */
193 if (!oldIPs.equals(newIPs)) {
194 Iterator<FixedIps> iterator = newIPs.iterator();
195 while (iterator.hasNext()) {
196 FixedIps ip = iterator.next();
197 if (oldIPs.remove(ip)) {
201 handleNeutronPortUpdated(original, update);
203 handlePortSecurityUpdated(original, update);
206 private void handleRouterInterfaceAdded(Port routerPort) {
207 if (routerPort.getDeviceId() != null) {
208 Uuid routerId = new Uuid(routerPort.getDeviceId());
209 Uuid infNetworkId = routerPort.getNetworkId();
210 Uuid existingVpnId = NeutronvpnUtils.getVpnForNetwork(broker, infNetworkId);
211 if (existingVpnId == null) {
212 for (FixedIps portIP : routerPort.getFixedIps()) {
213 if (portIP.getIpAddress().getIpv4Address() != null) {
214 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
218 nvpnManager.addSubnetToVpn(vpnId, portIP.getSubnetId());
219 nvpnNatManager.handleSubnetsForExternalRouter(routerId, broker);
223 LOG.error("Neutron network {} corresponding to router interface port {} for neutron router {} already" +
224 " associated to VPN {}", infNetworkId.getValue(), routerPort.getUuid().getValue(), routerId
225 .getValue(), existingVpnId.getValue());
230 private void handleRouterInterfaceRemoved(Port routerPort) {
231 if (routerPort.getDeviceId() != null) {
232 Uuid routerId = new Uuid(routerPort.getDeviceId());
233 for (FixedIps portIP : routerPort.getFixedIps()) {
234 if (portIP.getIpAddress().getIpv4Address() != null) {
235 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
239 nvpnManager.removeSubnetFromVpn(vpnId, portIP.getSubnetId());
240 nvpnNatManager.handleSubnetsForExternalRouter(routerId, broker);
246 private void handleNeutronPortCreated(Port port) {
247 if (!NeutronUtils.isPortVnicTypeNormal(port)) {
248 nvpnManager.updateSubnetmapNodeWithPorts(port.getFixedIps().get(0).getSubnetId(), null, port.getUuid());
249 LOG.info("Port {} is not a NORMAL VNIC Type port; OF Port interfaces are not created",
250 port.getUuid().getValue());
253 LOG.info("Of-port-interface creation");
254 // Create of-port interface for this neutron port
255 String portInterfaceName = createOfPortInterface(port);
256 LOG.debug("Creating ELAN Interface");
257 createElanInterface(port, portInterfaceName);
258 LOG.debug("Add port to subnet");
259 // add port to local Subnets DS
260 Uuid vpnId = addPortToSubnets(port);
263 // create vpn-interface on this neutron port
264 LOG.debug("Adding VPN Interface");
265 nvpnManager.createVpnInterface(vpnId, port);
266 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
267 if (routerId != null) {
268 nvpnManager.addToNeutronRouterInterfacesMap(routerId, port.getUuid().getValue());
273 private void handleNeutronPortDeleted(Port port) {
274 if (!NeutronUtils.isPortVnicTypeNormal(port)) {
275 nvpnManager.removePortsFromSubnetmapNode(port.getFixedIps().get(0).getSubnetId(), null, port.getUuid());
276 LOG.info("Port {} is not a NORMAL VNIC Type port; OF Port interfaces are not created",
277 port.getUuid().getValue());
280 //dissociate fixedIP from floatingIP if associated
281 nvpnManager.dissociatefixedIPFromFloatingIP(port.getUuid().getValue());
282 LOG.debug("Remove port from subnet");
283 // remove port from local Subnets DS
284 Uuid vpnId = removePortFromSubnets(port);
287 // remove vpn-interface for this neutron port
288 LOG.debug("removing VPN Interface");
289 nvpnManager.deleteVpnInterface(port);
291 // Remove of-port interface for this neutron port
292 // ELAN interface is also implicitly deleted as part of this operation
293 LOG.debug("Of-port-interface removal", port);
294 deleteOfPortInterface(port);
296 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
297 if (routerId != null) {
298 nvpnManager.removeFromNeutronRouterInterfacesMap(routerId, port.getUuid().getValue());
303 private void handleNeutronPortUpdated(Port portoriginal, Port portupdate) {
304 if (portoriginal.getFixedIps() == null || portoriginal.getFixedIps().isEmpty()) {
305 handleNeutronPortCreated(portupdate);
308 LOG.debug("Add port to subnet");
309 // add port FixedIP to local Subnets DS
310 Uuid vpnIdup = addPortToSubnets(portupdate);
312 if (vpnIdup != null) {
313 nvpnManager.createVpnInterface(vpnIdup, portupdate);
314 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnIdup).getRouterId();
315 if(routerId != null) {
316 nvpnManager.addToNeutronRouterInterfacesMap(routerId, portupdate.getUuid().getValue());
320 // remove port FixedIP from local Subnets DS
321 Uuid vpnIdor = removePortFromSubnets(portoriginal);
323 if (vpnIdor != null) {
324 nvpnManager.deleteVpnInterface(portoriginal);
325 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnIdor).getRouterId();
326 if(routerId != null) {
327 nvpnManager.removeFromNeutronRouterInterfacesMap(routerId, portoriginal.getUuid().getValue());
332 private void handlePortSecurityUpdated(Port portOriginal, Port portUpdated) {
333 Boolean origSecurityEnabled = NeutronvpnUtils.getPortSecurityEnabled(portOriginal);
334 Boolean updatedSecurityEnabled = NeutronvpnUtils.getPortSecurityEnabled(portUpdated);
335 String interfaceName = portUpdated.getUuid().getValue();
336 Interface portInterface = NeutronvpnUtils.getOfPortInterface(broker, portUpdated);
337 if (portInterface != null) {
338 InterfaceAclBuilder interfaceAclBuilder = null;
339 if (origSecurityEnabled != updatedSecurityEnabled) {
340 interfaceAclBuilder = new InterfaceAclBuilder();
341 interfaceAclBuilder.setPortSecurityEnabled(updatedSecurityEnabled);
342 if (updatedSecurityEnabled) {
343 // Handle security group enabled
344 List<Uuid> securityGroups = portUpdated.getSecurityGroups();
345 if (securityGroups != null) {
346 interfaceAclBuilder.setSecurityGroups(securityGroups);
349 // Handle security group disabled
350 interfaceAclBuilder.setSecurityGroups(Lists.newArrayList());
353 if (updatedSecurityEnabled) {
354 // handle SG add/delete delta
355 InterfaceAcl interfaceAcl = portInterface.getAugmentation(InterfaceAcl.class);
356 interfaceAclBuilder = new InterfaceAclBuilder(interfaceAcl);
357 List<Uuid> addedGroups = getsecurityGroupChanged(portUpdated.getSecurityGroups(),
358 portOriginal.getSecurityGroups());
359 List<Uuid> deletedGroups = getsecurityGroupChanged(portOriginal.getSecurityGroups(),
360 portUpdated.getSecurityGroups());
361 List<Uuid> securityGroups = interfaceAcl.getSecurityGroups();
362 if (addedGroups != null) {
363 securityGroups.addAll(addedGroups);
365 if (deletedGroups != null) {
366 securityGroups.removeAll(deletedGroups);
368 interfaceAclBuilder.setSecurityGroups(securityGroups);
372 if (interfaceAclBuilder != null) {
373 InterfaceBuilder builder = new InterfaceBuilder(portInterface)
374 .addAugmentation(InterfaceAcl.class, interfaceAclBuilder.build());
375 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(interfaceName);
376 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, builder.build());
379 LOG.error("Interface {} is not present", interfaceName);
383 private List<Uuid> getsecurityGroupChanged(List<Uuid> port1SecurityGroups, List<Uuid> port2SecurityGroups) {
384 if (port1SecurityGroups == null) {
388 if (port2SecurityGroups == null) {
389 return port1SecurityGroups;
392 List<Uuid> list1 = new ArrayList<>(port1SecurityGroups);
393 List<Uuid> list2 = new ArrayList<>(port2SecurityGroups);
394 for (Iterator<Uuid> iterator = list1.iterator(); iterator.hasNext();) {
395 Uuid securityGroup1 = iterator.next();
396 for (Uuid securityGroup2 : list2) {
397 if (securityGroup1.getValue().equals(securityGroup2.getValue())) {
406 private String createOfPortInterface(Port port) {
407 Interface inf = createInterface(port);
408 String infName = inf.getName();
410 LOG.debug("Creating OFPort Interface {}", infName);
411 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
413 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
414 interfaceIdentifier);
415 if (!optionalInf.isPresent()) {
416 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
418 LOG.error("Interface {} is already present", infName);
420 } catch (Exception e) {
421 LOG.error("failed to create interface {} due to the exception {} ", infName, e.getMessage());
426 private Interface createInterface(Port port) {
428 String parentRefName = NeutronvpnUtils.getVifPortName(port);
429 String interfaceName = port.getUuid().getValue();
430 IfL2vlan.L2vlanMode l2VlanMode = IfL2vlan.L2vlanMode.Trunk;
431 InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
432 IfL2vlanBuilder ifL2vlanBuilder = new IfL2vlanBuilder();
433 ifL2vlanBuilder.setL2vlanMode(l2VlanMode);
434 if (parentRefName != null) {
435 ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
436 interfaceBuilder.addAugmentation(ParentRefs.class, parentRefsBuilder.build());
439 if (NeutronvpnUtils.isPortSecurityEnabled(port)) {
440 InterfaceAclBuilder interfaceAclBuilder = new InterfaceAclBuilder();
441 interfaceAclBuilder.setPortSecurityEnabled(true);
442 List<Uuid> securityGroups = port.getSecurityGroups();
443 if (securityGroups != null) {
444 interfaceAclBuilder.setSecurityGroups(securityGroups);
446 interfaceBuilder.addAugmentation(InterfaceAcl.class, interfaceAclBuilder.build());
449 interfaceBuilder.setEnabled(true).setName(interfaceName).setType(L2vlan.class)
450 .addAugmentation(IfL2vlan.class, ifL2vlanBuilder.build());
451 return interfaceBuilder.build();
454 private void deleteOfPortInterface(Port port) {
455 String name = port.getUuid().getValue();
456 LOG.debug("Removing OFPort Interface {}", name);
457 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
459 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
460 interfaceIdentifier);
461 if (optionalInf.isPresent()) {
462 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
464 LOG.error("Interface {} is not present", name);
466 } catch (Exception e) {
467 LOG.error("Failed to delete interface {} due to the exception {}", name, e.getMessage());
471 private Interface updateInterface(Port original, Port update) {
472 String parentRefName = NeutronvpnUtils.getVifPortName(update);
473 String interfaceName = original.getUuid().getValue();
474 InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
476 if (parentRefName != null) {
477 ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
478 interfaceBuilder.addAugmentation(ParentRefs.class, parentRefsBuilder.build());
481 interfaceBuilder.setName(interfaceName);
482 return interfaceBuilder.build();
485 private String updateOfPortInterface(Port original, Port updated) {
486 Interface inf = updateInterface(original, updated);
487 String infName = inf.getName();
489 LOG.debug("Updating OFPort Interface {}", infName);
490 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
492 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
493 interfaceIdentifier);
494 if (optionalInf.isPresent()) {
495 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
497 LOG.error("Interface {} doesn't exist", infName);
499 } catch (Exception e) {
500 LOG.error("failed to update interface {} due to the exception {} ", infName, e);
506 private void createElanInterface(Port port, String name) {
507 String elanInstanceName = port.getNetworkId().getValue();
508 List<PhysAddress> physAddresses = new ArrayList<>();
509 physAddresses.add(new PhysAddress(port.getMacAddress().getValue()));
511 InstanceIdentifier<ElanInterface> id = InstanceIdentifier.builder(ElanInterfaces.class).child(ElanInterface
512 .class, new ElanInterfaceKey(name)).build();
513 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
514 .setName(name).setStaticMacEntries(physAddresses).setKey(new ElanInterfaceKey(name)).build();
515 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, elanInterface);
516 LOG.debug("Creating new ELan Interface {}", elanInterface);
519 // adds port to subnet list and creates vpnInterface
520 private Uuid addPortToSubnets(Port port) {
521 Uuid subnetId = null;
523 Subnetmap subnetmap = null;
524 String infName = port.getUuid().getValue();
525 boolean isLockAcquired = false;
526 String lockName = port.getUuid().getValue();
528 // find the subnet to which this port is associated
529 if(port.getFixedIps() == null || port.getFixedIps().isEmpty()) {
530 LOG.debug("port {} doesn't have ip", port.getName());
533 FixedIps ip = port.getFixedIps().get(0);
534 String ipValue = (ip.getIpAddress().getIpv4Address() != null ) ? ip.getIpAddress().getIpv4Address().getValue() :
535 ip.getIpAddress().getIpv6Address().getValue();
536 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
537 PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
538 .setPortName(infName);
539 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
540 LOG.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
542 subnetId = ip.getSubnetId();
543 subnetmap = nvpnManager.updateSubnetmapNodeWithPorts(subnetId, port.getUuid(), null);
544 if (subnetmap != null) {
545 vpnId = subnetmap.getVpnId();
549 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
550 checkAndPublishPortAddNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
551 LOG.debug("Port added to subnet notification sent");
552 } catch (Exception e) {
553 LOG.error("Port added to subnet notification failed", e);
555 if (isLockAcquired) {
556 NeutronvpnUtils.unlock(lockManager, lockName);
563 private Uuid removePortFromSubnets(Port port) {
564 Uuid subnetId = null;
566 Subnetmap subnetmap = null;
567 boolean isLockAcquired = false;
568 String lockName = port.getUuid().getValue();
570 // find the subnet to which this port is associated
571 FixedIps ip = port.getFixedIps().get(0);
572 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
573 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
574 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
575 LOG.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS", ipValue);
576 subnetId = ip.getSubnetId();
577 subnetmap = nvpnManager.removePortsFromSubnetmapNode(subnetId, port.getUuid(), null);
578 if (subnetmap != null) {
579 vpnId = subnetmap.getVpnId();
583 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
584 checkAndPublishPortRemoveNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
585 LOG.debug("Port removed from subnet notification sent");
586 } catch (Exception e) {
587 LOG.error("Port removed from subnet notification failed", e);
589 if (isLockAcquired) {
590 NeutronvpnUtils.unlock(lockManager, lockName);
597 private void checkAndPublishPortAddNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
598 PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
600 LOG.info("publish notification called");
602 builder.setSubnetIp(subnetIp);
603 builder.setSubnetId(subnetId);
604 builder.setPortId(portId);
606 notificationPublishService.putNotification(builder.build());
609 private void checkAndPublishPortRemoveNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
610 PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
612 LOG.info("publish notification called");
614 builder.setPortId(portId);
615 builder.setSubnetIp(subnetIp);
616 builder.setSubnetId(subnetId);
618 notificationPublishService.putNotification(builder.build());