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.aclservice.rev160608.IpPrefixOrAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortAddedToSubnetBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.neutron.port.data.PortFixedipToPortNameBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
53 import org.opendaylight.yangtools.concepts.ListenerRegistration;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
59 public class NeutronPortChangeListener extends AbstractDataChangeListener<Port> implements AutoCloseable {
60 private static final Logger LOG = LoggerFactory.getLogger(NeutronPortChangeListener.class);
62 private ListenerRegistration<DataChangeListener> listenerRegistration;
63 private final DataBroker broker;
64 private NeutronvpnManager nvpnManager;
65 private NeutronvpnNatManager nvpnNatManager;
66 private LockManagerService lockManager;
67 private NotificationPublishService notificationPublishService;
68 private NotificationService notificationService;
71 public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr,NeutronvpnNatManager nVpnNatMgr,
72 NotificationPublishService notiPublishService, NotificationService notiService) {
75 nvpnManager = nVpnMgr;
76 nvpnNatManager = nVpnNatMgr;
77 notificationPublishService = notiPublishService;
78 notificationService = notiService;
82 public void setLockManager(LockManagerService lockManager) {
83 this.lockManager = lockManager;
87 public void close() throws Exception {
88 if (listenerRegistration != null) {
90 listenerRegistration.close();
91 } catch (final Exception e) {
92 LOG.error("Error when cleaning up DataChangeListener.", e);
94 listenerRegistration = null;
96 LOG.info("N_Port listener Closed");
100 private void registerListener(final DataBroker db) {
102 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
103 InstanceIdentifier.create(Neutron.class).child(Ports.class).child(Port.class),
104 NeutronPortChangeListener.this, DataChangeScope.SUBTREE);
105 } catch (final Exception e) {
106 LOG.error("Neutron Manager Port DataChange listener registration fail!", e);
107 throw new IllegalStateException("Neutron Manager Port DataChange listener registration failed.", e);
112 protected void add(InstanceIdentifier<Port> identifier, Port input) {
113 if (LOG.isTraceEnabled()) {
114 LOG.trace("Adding Port : key: " + identifier + ", value=" + input);
116 Network network = NeutronvpnUtils.getNeutronNetwork(broker, input.getNetworkId());
117 if (network == null || NeutronvpnUtils.isNetworkTypeVlanOrGre(network)) {
118 //FIXME: This should be removed when support for VLAN and GRE network types is added
119 LOG.error("neutron vpn doesn't support vlan/gre network provider type for the port {} which is part of network {}.",
120 input.getName(), network);
123 NeutronvpnUtils.addToPortCache(input);
125 /* check if router interface has been created */
126 if ((input.getDeviceOwner() != null) && (input.getDeviceId() != null)) {
127 if (input.getDeviceOwner().equals(NeutronConstants.DEVICE_OWNER_ROUTER_INF)) {
128 handleRouterInterfaceAdded(input);
129 /* nothing else to do here */
133 if (input.getFixedIps() != null && !input.getFixedIps().isEmpty()) {
134 handleNeutronPortCreated(input);
140 protected void remove(InstanceIdentifier<Port> identifier, Port input) {
141 if (LOG.isTraceEnabled()) {
142 LOG.trace("Removing Port : key: " + identifier + ", value=" + input);
144 Network network = NeutronvpnUtils.getNeutronNetwork(broker, input.getNetworkId());
145 if (network == null || NeutronvpnUtils.isNetworkTypeVlanOrGre(network)) {
146 //FIXME: This should be removed when support for VLAN and GRE network types is added
147 LOG.error("neutron vpn doesn't support vlan/gre network provider type for the port {} which is part of network {}.",
148 input.getName(), network);
151 NeutronvpnUtils.removeFromPortCache(input);
153 if ((input.getDeviceOwner() != null) && (input.getDeviceId() != null)) {
154 if (input.getDeviceOwner().equals(NeutronConstants.DEVICE_OWNER_ROUTER_INF)) {
155 handleRouterInterfaceRemoved(input);
156 /* nothing else to do here */
160 if (input.getFixedIps() != null && !input.getFixedIps().isEmpty()) {
161 handleNeutronPortDeleted(input);
166 protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
167 if (LOG.isTraceEnabled()) {
168 LOG.trace("Updating Port : key: " + identifier + ", original value=" + original + ", update value=" +
172 Network network = NeutronvpnUtils.getNeutronNetwork(broker, update.getNetworkId());
173 if (network == null || NeutronvpnUtils.isNetworkTypeVlanOrGre(network)) {
174 LOG.error("neutron vpn doesn't support vlan/gre network provider type for the port {} which is part of network {}."
175 + " Skipping the processing of Port update DCN", update.getName(), network);
178 List<FixedIps> oldIPs = (original.getFixedIps() != null) ? original.getFixedIps() : new ArrayList<FixedIps>();
179 List<FixedIps> newIPs = (update.getFixedIps() != null) ? update.getFixedIps() : new ArrayList<FixedIps>();
181 /* check if VIF type updated as part of port binding */
182 if (NeutronvpnUtils.isPortVifTypeUpdated(original, update)) {
183 updateOfPortInterface(original, update);
185 NeutronvpnUtils.addToPortCache(update);
187 /* check if router interface has been updated */
188 if ((update.getDeviceOwner() != null) && (update.getDeviceId() != null)) {
189 if (update.getDeviceOwner().equals(NeutronConstants.DEVICE_OWNER_ROUTER_INF)) {
190 handleRouterInterfaceAdded(update);
191 /* nothing else to do here */
196 if (!oldIPs.equals(newIPs)) {
197 Iterator<FixedIps> iterator = newIPs.iterator();
198 while (iterator.hasNext()) {
199 FixedIps ip = iterator.next();
200 if (oldIPs.remove(ip)) {
204 handleNeutronPortUpdated(original, update);
206 handlePortSecurityUpdated(original, update);
209 private void handleRouterInterfaceAdded(Port routerPort) {
210 if (routerPort.getDeviceId() != null) {
211 Uuid routerId = new Uuid(routerPort.getDeviceId());
212 Uuid infNetworkId = routerPort.getNetworkId();
213 Uuid existingVpnId = NeutronvpnUtils.getVpnForNetwork(broker, infNetworkId);
214 if (existingVpnId == null) {
215 for (FixedIps portIP : routerPort.getFixedIps()) {
216 if (portIP.getIpAddress().getIpv4Address() != null) {
217 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
221 nvpnManager.addSubnetToVpn(vpnId, portIP.getSubnetId());
222 nvpnNatManager.handleSubnetsForExternalRouter(routerId, broker);
226 LOG.error("Neutron network {} corresponding to router interface port {} for neutron router {} already" +
227 " associated to VPN {}", infNetworkId.getValue(), routerPort.getUuid().getValue(), routerId
228 .getValue(), existingVpnId.getValue());
233 private void handleRouterInterfaceRemoved(Port routerPort) {
234 if (routerPort.getDeviceId() != null) {
235 Uuid routerId = new Uuid(routerPort.getDeviceId());
236 for (FixedIps portIP : routerPort.getFixedIps()) {
237 if (portIP.getIpAddress().getIpv4Address() != null) {
238 Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
242 nvpnManager.removeSubnetFromVpn(vpnId, portIP.getSubnetId());
243 nvpnNatManager.handleSubnetsForExternalRouter(routerId, broker);
249 private void handleNeutronPortCreated(Port port) {
250 if (!NeutronUtils.isPortVnicTypeNormal(port)) {
251 nvpnManager.updateSubnetmapNodeWithPorts(port.getFixedIps().get(0).getSubnetId(), null, port.getUuid());
252 LOG.info("Port {} is not a NORMAL VNIC Type port; OF Port interfaces are not created",
253 port.getUuid().getValue());
256 LOG.info("Of-port-interface creation");
257 // Create of-port interface for this neutron port
258 String portInterfaceName = createOfPortInterface(port);
259 LOG.debug("Creating ELAN Interface");
260 createElanInterface(port, portInterfaceName);
261 LOG.debug("Add port to subnet");
262 // add port to local Subnets DS
263 Uuid vpnId = addPortToSubnets(port);
266 // create vpn-interface on this neutron port
267 LOG.debug("Adding VPN Interface");
268 nvpnManager.createVpnInterface(vpnId, port);
269 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
270 if (routerId != null) {
271 nvpnManager.addToNeutronRouterInterfacesMap(routerId, port.getUuid().getValue());
276 private void handleNeutronPortDeleted(Port port) {
277 if (!NeutronUtils.isPortVnicTypeNormal(port)) {
278 nvpnManager.removePortsFromSubnetmapNode(port.getFixedIps().get(0).getSubnetId(), null, port.getUuid());
279 LOG.info("Port {} is not a NORMAL VNIC Type port; OF Port interfaces are not created",
280 port.getUuid().getValue());
283 //dissociate fixedIP from floatingIP if associated
284 nvpnManager.dissociatefixedIPFromFloatingIP(port.getUuid().getValue());
285 LOG.debug("Remove port from subnet");
286 // remove port from local Subnets DS
287 Uuid vpnId = removePortFromSubnets(port);
290 // remove vpn-interface for this neutron port
291 LOG.debug("removing VPN Interface");
292 nvpnManager.deleteVpnInterface(port);
294 // Remove of-port interface for this neutron port
295 // ELAN interface is also implicitly deleted as part of this operation
296 LOG.debug("Of-port-interface removal", port);
297 deleteOfPortInterface(port);
299 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
300 if (routerId != null) {
301 nvpnManager.removeFromNeutronRouterInterfacesMap(routerId, port.getUuid().getValue());
306 private void handleNeutronPortUpdated(Port portoriginal, Port portupdate) {
307 if (portoriginal.getFixedIps() == null || portoriginal.getFixedIps().isEmpty()) {
308 handleNeutronPortCreated(portupdate);
311 LOG.debug("Add port to subnet");
312 // add port FixedIP to local Subnets DS
313 Uuid vpnIdup = addPortToSubnets(portupdate);
315 if (vpnIdup != null) {
316 nvpnManager.createVpnInterface(vpnIdup, portupdate);
317 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnIdup).getRouterId();
318 if(routerId != null) {
319 nvpnManager.addToNeutronRouterInterfacesMap(routerId, portupdate.getUuid().getValue());
323 // remove port FixedIP from local Subnets DS
324 Uuid vpnIdor = removePortFromSubnets(portoriginal);
326 if (vpnIdor != null) {
327 nvpnManager.deleteVpnInterface(portoriginal);
328 Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnIdor).getRouterId();
329 if(routerId != null) {
330 nvpnManager.removeFromNeutronRouterInterfacesMap(routerId, portoriginal.getUuid().getValue());
335 private void handlePortSecurityUpdated(Port portOriginal, Port portUpdated) {
336 Boolean origSecurityEnabled = NeutronvpnUtils.getPortSecurityEnabled(portOriginal);
337 Boolean updatedSecurityEnabled = NeutronvpnUtils.getPortSecurityEnabled(portUpdated);
338 String interfaceName = portUpdated.getUuid().getValue();
339 Interface portInterface = NeutronvpnUtils.getOfPortInterface(broker, portUpdated);
340 if (portInterface != null) {
341 InterfaceAclBuilder interfaceAclBuilder = null;
342 if (origSecurityEnabled != updatedSecurityEnabled) {
343 interfaceAclBuilder = new InterfaceAclBuilder();
344 interfaceAclBuilder.setPortSecurityEnabled(updatedSecurityEnabled);
345 if (updatedSecurityEnabled) {
346 // Handle security group enabled
347 List<Uuid> securityGroups = portUpdated.getSecurityGroups();
348 if (securityGroups != null) {
349 interfaceAclBuilder.setSecurityGroups(securityGroups);
351 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> portAllowedAddressPairs =
352 portUpdated.getAllowedAddressPairs();
353 if (portAllowedAddressPairs != null) {
355 .setAllowedAddressPairs(getAllowedAddressPairsForAclService(portAllowedAddressPairs));
358 // Handle security group disabled
359 interfaceAclBuilder.setSecurityGroups(Lists.newArrayList());
360 interfaceAclBuilder.setAllowedAddressPairs(Lists.newArrayList());
363 if (updatedSecurityEnabled) {
364 // handle SG add/delete delta
365 InterfaceAcl interfaceAcl = portInterface.getAugmentation(InterfaceAcl.class);
366 interfaceAclBuilder = new InterfaceAclBuilder(interfaceAcl);
367 List<Uuid> addedGroups = getsecurityGroupChanged(portUpdated.getSecurityGroups(),
368 portOriginal.getSecurityGroups());
369 List<Uuid> deletedGroups = getsecurityGroupChanged(portOriginal.getSecurityGroups(),
370 portUpdated.getSecurityGroups());
371 List<Uuid> securityGroups = interfaceAcl.getSecurityGroups();
372 List<Uuid> updatedSecurityGroups =
373 (securityGroups != null) ? new ArrayList<>(securityGroups) : new ArrayList<>();
374 if (addedGroups != null) {
375 updatedSecurityGroups.addAll(addedGroups);
377 if (deletedGroups != null) {
378 updatedSecurityGroups.removeAll(deletedGroups);
380 interfaceAclBuilder.setSecurityGroups(updatedSecurityGroups);
382 List<AllowedAddressPairs> addedAllowedAddressPairs = getAllowedAddressPairsChanged(
383 portUpdated.getAllowedAddressPairs(), portOriginal.getAllowedAddressPairs());
384 List<AllowedAddressPairs> deletedAllowedAddressPairs = getAllowedAddressPairsChanged(
385 portOriginal.getAllowedAddressPairs(), portUpdated.getAllowedAddressPairs());
386 List<AllowedAddressPairs> allowedAddressPairs = interfaceAcl.getAllowedAddressPairs();
387 List<AllowedAddressPairs> updatedAllowedAddressPairs =
388 (allowedAddressPairs != null) ? new ArrayList<>(allowedAddressPairs) : new ArrayList<>();
389 if (addedAllowedAddressPairs != null) {
390 updatedAllowedAddressPairs.addAll(addedAllowedAddressPairs);
392 if (deletedAllowedAddressPairs != null) {
393 updatedAllowedAddressPairs.removeAll(deletedAllowedAddressPairs);
395 interfaceAclBuilder.setAllowedAddressPairs(updatedAllowedAddressPairs);
399 if (interfaceAclBuilder != null) {
400 InterfaceBuilder builder = new InterfaceBuilder(portInterface).addAugmentation(InterfaceAcl.class,
401 interfaceAclBuilder.build());
402 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(interfaceName);
403 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, builder.build());
406 LOG.error("Interface {} is not present", interfaceName);
410 private List<Uuid> getsecurityGroupChanged(List<Uuid> port1SecurityGroups, List<Uuid> port2SecurityGroups) {
411 if (port1SecurityGroups == null) {
415 if (port2SecurityGroups == null) {
416 return port1SecurityGroups;
419 List<Uuid> list1 = new ArrayList<>(port1SecurityGroups);
420 List<Uuid> list2 = new ArrayList<>(port2SecurityGroups);
421 for (Iterator<Uuid> iterator = list1.iterator(); iterator.hasNext();) {
422 Uuid securityGroup1 = iterator.next();
423 for (Uuid securityGroup2 : list2) {
424 if (securityGroup1.getValue().equals(securityGroup2.getValue())) {
433 private List<AllowedAddressPairs> getAllowedAddressPairsChanged(
434 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> port1AllowedAddressPairs,
435 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> port2AllowedAddressPairs) {
436 if (port1AllowedAddressPairs == null) {
440 if (port2AllowedAddressPairs == null) {
441 return getAllowedAddressPairsForAclService(port1AllowedAddressPairs);
444 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> list1 =
445 new ArrayList<>(port1AllowedAddressPairs);
446 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> list2 =
447 new ArrayList<>(port2AllowedAddressPairs);
448 for (Iterator<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> iterator =
449 list1.iterator(); iterator.hasNext();) {
450 org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs allowedAddressPair1 =
452 for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs allowedAddressPair2 : list2) {
453 if (allowedAddressPair1.getKey().equals(allowedAddressPair2.getKey())) {
459 return getAllowedAddressPairsForAclService(list1);
462 private List<AllowedAddressPairs> getAllowedAddressPairsForAclService(
463 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> portAllowedAddressPairs) {
464 List<AllowedAddressPairs> aclAllowedAddressPairs = new ArrayList<>();
465 for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs portAllowedAddressPair : portAllowedAddressPairs) {
466 AllowedAddressPairsBuilder aclAllowedAdressPairBuilder = new AllowedAddressPairsBuilder();
467 org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.types.rev160517.IpPrefixOrAddress ipAddress =
468 portAllowedAddressPair.getIpAddress();
469 if (ipAddress != null && ipAddress.getValue() != null) {
470 if (ipAddress.getIpPrefix() != null) {
471 aclAllowedAdressPairBuilder.setIpAddress(new IpPrefixOrAddress(ipAddress.getIpPrefix()));
473 aclAllowedAdressPairBuilder.setIpAddress(new IpPrefixOrAddress(ipAddress.getIpAddress()));
477 aclAllowedAdressPairBuilder.setMacAddress(portAllowedAddressPair.getMacAddress());
478 aclAllowedAddressPairs.add(aclAllowedAdressPairBuilder.build());
480 return aclAllowedAddressPairs;
483 private String createOfPortInterface(Port port) {
484 Interface inf = createInterface(port);
485 String infName = inf.getName();
487 LOG.debug("Creating OFPort Interface {}", infName);
488 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
490 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
491 interfaceIdentifier);
492 if (!optionalInf.isPresent()) {
493 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
495 LOG.error("Interface {} is already present", infName);
497 } catch (Exception e) {
498 LOG.error("failed to create interface {} due to the exception {} ", infName, e.getMessage());
503 private Interface createInterface(Port port) {
505 String parentRefName = NeutronvpnUtils.getVifPortName(port);
506 String interfaceName = port.getUuid().getValue();
507 IfL2vlan.L2vlanMode l2VlanMode = IfL2vlan.L2vlanMode.Trunk;
508 InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
509 IfL2vlanBuilder ifL2vlanBuilder = new IfL2vlanBuilder();
510 ifL2vlanBuilder.setL2vlanMode(l2VlanMode);
511 if (parentRefName != null) {
512 ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
513 interfaceBuilder.addAugmentation(ParentRefs.class, parentRefsBuilder.build());
516 if (NeutronvpnUtils.isPortSecurityEnabled(port)) {
517 InterfaceAclBuilder interfaceAclBuilder = new InterfaceAclBuilder();
518 interfaceAclBuilder.setPortSecurityEnabled(true);
519 List<Uuid> securityGroups = port.getSecurityGroups();
520 if (securityGroups != null) {
521 interfaceAclBuilder.setSecurityGroups(securityGroups);
524 List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.AllowedAddressPairs> portAllowedAddressPairs =
525 port.getAllowedAddressPairs();
526 if (portAllowedAddressPairs != null) {
528 .setAllowedAddressPairs(getAllowedAddressPairsForAclService(portAllowedAddressPairs));
530 interfaceBuilder.addAugmentation(InterfaceAcl.class, interfaceAclBuilder.build());
533 interfaceBuilder.setEnabled(true).setName(interfaceName).setType(L2vlan.class)
534 .addAugmentation(IfL2vlan.class, ifL2vlanBuilder.build());
535 return interfaceBuilder.build();
538 private void deleteOfPortInterface(Port port) {
539 String name = port.getUuid().getValue();
540 LOG.debug("Removing OFPort Interface {}", name);
541 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
543 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
544 interfaceIdentifier);
545 if (optionalInf.isPresent()) {
546 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier);
548 LOG.error("Interface {} is not present", name);
550 } catch (Exception e) {
551 LOG.error("Failed to delete interface {} due to the exception {}", name, e.getMessage());
555 private Interface updateInterface(Port original, Port update) {
556 String parentRefName = NeutronvpnUtils.getVifPortName(update);
557 String interfaceName = original.getUuid().getValue();
558 InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
560 if (parentRefName != null) {
561 ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
562 interfaceBuilder.addAugmentation(ParentRefs.class, parentRefsBuilder.build());
565 interfaceBuilder.setName(interfaceName);
566 return interfaceBuilder.build();
569 private String updateOfPortInterface(Port original, Port updated) {
570 Interface inf = updateInterface(original, updated);
571 String infName = inf.getName();
573 LOG.debug("Updating OFPort Interface {}", infName);
574 InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
576 Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
577 interfaceIdentifier);
578 if (optionalInf.isPresent()) {
579 MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
581 LOG.error("Interface {} doesn't exist", infName);
583 } catch (Exception e) {
584 LOG.error("failed to update interface {} due to the exception {} ", infName, e);
590 private void createElanInterface(Port port, String name) {
591 String elanInstanceName = port.getNetworkId().getValue();
592 List<PhysAddress> physAddresses = new ArrayList<>();
593 physAddresses.add(new PhysAddress(port.getMacAddress().getValue()));
595 InstanceIdentifier<ElanInterface> id = InstanceIdentifier.builder(ElanInterfaces.class).child(ElanInterface
596 .class, new ElanInterfaceKey(name)).build();
597 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
598 .setName(name).setStaticMacEntries(physAddresses).setKey(new ElanInterfaceKey(name)).build();
599 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, elanInterface);
600 LOG.debug("Creating new ELan Interface {}", elanInterface);
603 // adds port to subnet list and creates vpnInterface
604 private Uuid addPortToSubnets(Port port) {
605 Uuid subnetId = null;
607 Subnetmap subnetmap = null;
608 String infName = port.getUuid().getValue();
609 boolean isLockAcquired = false;
610 String lockName = port.getUuid().getValue();
612 // find the subnet to which this port is associated
613 if(port.getFixedIps() == null || port.getFixedIps().isEmpty()) {
614 LOG.debug("port {} doesn't have ip", port.getName());
617 FixedIps ip = port.getFixedIps().get(0);
618 String ipValue = (ip.getIpAddress().getIpv4Address() != null ) ? ip.getIpAddress().getIpv4Address().getValue() :
619 ip.getIpAddress().getIpv6Address().getValue();
620 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
621 PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
622 .setPortName(infName);
623 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
624 LOG.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
626 subnetId = ip.getSubnetId();
627 subnetmap = nvpnManager.updateSubnetmapNodeWithPorts(subnetId, port.getUuid(), null);
628 if (subnetmap != null) {
629 vpnId = subnetmap.getVpnId();
633 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
634 checkAndPublishPortAddNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
635 LOG.debug("Port added to subnet notification sent");
636 } catch (Exception e) {
637 LOG.error("Port added to subnet notification failed", e);
639 if (isLockAcquired) {
640 NeutronvpnUtils.unlock(lockManager, lockName);
647 private Uuid removePortFromSubnets(Port port) {
648 Uuid subnetId = null;
650 Subnetmap subnetmap = null;
651 boolean isLockAcquired = false;
652 String lockName = port.getUuid().getValue();
654 // find the subnet to which this port is associated
655 FixedIps ip = port.getFixedIps().get(0);
656 String ipValue = ip.getIpAddress().getIpv4Address().getValue();
657 InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
658 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
659 LOG.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS", ipValue);
660 subnetId = ip.getSubnetId();
661 subnetmap = nvpnManager.removePortsFromSubnetmapNode(subnetId, port.getUuid(), null);
662 if (subnetmap != null) {
663 vpnId = subnetmap.getVpnId();
667 isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
668 checkAndPublishPortRemoveNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
669 LOG.debug("Port removed from subnet notification sent");
670 } catch (Exception e) {
671 LOG.error("Port removed from subnet notification failed", e);
673 if (isLockAcquired) {
674 NeutronvpnUtils.unlock(lockManager, lockName);
681 private void checkAndPublishPortAddNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
682 PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
684 LOG.info("publish notification called");
686 builder.setSubnetIp(subnetIp);
687 builder.setSubnetId(subnetId);
688 builder.setPortId(portId);
690 notificationPublishService.putNotification(builder.build());
693 private void checkAndPublishPortRemoveNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
694 PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
696 LOG.info("publish notification called");
698 builder.setPortId(portId);
699 builder.setSubnetIp(subnetIp);
700 builder.setSubnetId(subnetId);
702 notificationPublishService.putNotification(builder.build());