DHCP Handling for TOR VM
[vpnservice.git] / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / vpnservice / neutronvpn / NeutronPortChangeListener.java
index 9a14c570bf505181b97c7c29dff8633dddf8ad07..0c81884525df302336b00518c658fbada65e7aba 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -12,10 +12,13 @@ import com.google.common.base.Optional;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
+import org.opendaylight.controller.md.sal.binding.api.NotificationService;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
+import org.opendaylight.vpnservice.neutronvpn.api.utils.NeutronUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
@@ -33,10 +36,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlanBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.LockManagerService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.PortAddedToSubnetBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.PortRemovedFromSubnetBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
         .PortFixedipToPortNameBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.neutron.port.data
-        .PortNameToPortUuidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.subnetmaps.Subnetmap;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -54,15 +58,24 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
     private ListenerRegistration<DataChangeListener> listenerRegistration;
     private final DataBroker broker;
     private NeutronvpnManager nvpnManager;
+    private LockManagerService lockManager;
+    private NotificationPublishService notificationPublishService;
+    private NotificationService notificationService;
 
 
-    public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr) {
+    public NeutronPortChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr,NotificationPublishService notiPublishService, NotificationService notiService) {
         super(Port.class);
         broker = db;
         nvpnManager = nVpnMgr;
+        notificationPublishService = notiPublishService;
+        notificationService = notiService;
         registerListener(db);
     }
 
+    public void setLockManager(LockManagerService lockManager) {
+        this.lockManager = lockManager;
+    }
+
     @Override
     public void close() throws Exception {
         if (listenerRegistration != null) {
@@ -128,12 +141,16 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
     }
 
     private void handleNeutronPortCreated(Port port) {
+        if (!NeutronUtils.isPortVnicTypeNormal(port)) {
+            LOG.info("Port {} is not a NORMAL VNIC Type port; OF Port interfaces are not created",
+                    port.getUuid().getValue());
+            return;
+        }
         LOG.info("Of-port-interface creation");
-        int portVlanId = NeutronvpnUtils.getVlanFromNeutronPort(port);
         // Create of-port interface for this neutron port
-        createOfPortInterface(port, portVlanId);
+        String portInterfaceName = createOfPortInterface(port);
         LOG.debug("Creating ELAN Interface");
-        createElanInterface(port);
+        createElanInterface(port, portInterfaceName);
         LOG.debug("Add port to subnet");
         // add port to local Subnets DS
         Uuid vpnId = addPortToSubnets(port);
@@ -156,23 +173,22 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
             LOG.debug("removing VPN Interface");
             nvpnManager.deleteVpnInterface(port);
         }
-        int portVlanId = NeutronvpnUtils.getVlanFromNeutronPort(port);
         // Remove of-port interface for this neutron port
         // ELAN interface is also implicitly deleted as part of this operation
-        deleteOfPortInterface(port, portVlanId);
+        deleteOfPortInterface(port);
 
     }
 
     private void handleNeutronPortUpdated(Port portoriginal, Port portupdate) {
         LOG.debug("Add port to subnet");
-        // add port FixedIPs to local Subnets DS
+        // add port FixedIP to local Subnets DS
         Uuid vpnIdup = addPortToSubnets(portupdate);
 
         if (vpnIdup != null) {
             nvpnManager.createVpnInterface(vpnIdup, portupdate);
         }
 
-        // remove port FixedIPs from local Subnets DS
+        // remove port FixedIP from local Subnets DS
         Uuid vpnIdor = removePortFromSubnets(portoriginal);
 
         if (vpnIdor != null) {
@@ -180,40 +196,41 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
         }
     }
 
-    private void createOfPortInterface(Port port, int portVlanId) {
-        String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
-        //String ifname = new StringBuilder(name).append(":").append(Integer.toString(portVlanId)).toString();
-        //Network network = NeutronvpnUtils.getNeutronNetwork(broker, port.getNetworkId());
-        //Boolean isVlanTransparent = network.isVlanTransparent();
+    private String createOfPortInterface(Port port) {
+        Interface inf = createInterface(port);
+        String infName = inf.getName();
 
-        LOG.debug("Creating OFPort Interface {}", name);
-        InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
+        LOG.debug("Creating OFPort Interface {}", infName);
+        InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(infName);
         try {
             Optional<Interface> optionalInf = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
                     interfaceIdentifier);
             if (!optionalInf.isPresent()) {
-                // handle these for trunkport extensions : portVlanId, isVlanTransparent
-                IfL2vlan l2vlan = new IfL2vlanBuilder().setL2vlanMode(IfL2vlan.L2vlanMode.Trunk).build();
-                ParentRefs parentRefs = new ParentRefsBuilder().setParentInterface(name).build();
-                Interface inf = new InterfaceBuilder().setEnabled(true).setName(name).setType(L2vlan.class)
-                        .addAugmentation(IfL2vlan.class, l2vlan).addAugmentation(ParentRefs.class, parentRefs).build();
                 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf);
             } else {
-                LOG.error("Interface {} is already present", name);
+                LOG.error("Interface {} is already present", infName);
             }
         } catch (Exception e) {
-            LOG.error("failed to create interface {} due to the exception {} ", name, e.getMessage());
+            LOG.error("failed to create interface {} due to the exception {} ", infName, e.getMessage());
         }
+        return infName;
+    }
 
-        InstanceIdentifier portIdentifier = NeutronvpnUtils.buildPortNameToPortUuidIdentifier(name);
-        PortNameToPortUuidBuilder builder = new PortNameToPortUuidBuilder().setPortName(name).setPortId(port.getUuid());
-        MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, portIdentifier, builder.build());
-        LOG.debug("name-uuid map for port with name: {}, uuid: {} added to NeutronPortData DS", name, port.getUuid());
+    private Interface createInterface(Port port) {
+        String parentRefName = NeutronvpnUtils.uuidToTapPortName(port.getUuid());;
+        String interfaceName = port.getUuid().getValue();
+        IfL2vlan.L2vlanMode l2VlanMode = IfL2vlan.L2vlanMode.Trunk;
+        InterfaceBuilder interfaceBuilder = new InterfaceBuilder();
+        IfL2vlanBuilder ifL2vlanBuilder = new IfL2vlanBuilder();
+        ifL2vlanBuilder.setL2vlanMode(l2VlanMode);
+        ParentRefsBuilder parentRefsBuilder = new ParentRefsBuilder().setParentInterface(parentRefName);
+        interfaceBuilder.setEnabled(true).setName(interfaceName).setType(L2vlan.class).addAugmentation(IfL2vlan
+                .class, ifL2vlanBuilder.build()).addAugmentation(ParentRefs.class, parentRefsBuilder.build());
+        return interfaceBuilder.build();
     }
 
-    private void deleteOfPortInterface(Port port, int portVlanId) {
-        String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
-        //String ifname = new StringBuilder(name).append(":").append(Integer.toString(portVlanId)).toString();
+    private void deleteOfPortInterface(Port port) {
+        String name = port.getUuid().getValue();
         LOG.debug("Removing OFPort Interface {}", name);
         InstanceIdentifier interfaceIdentifier = NeutronvpnUtils.buildVlanInterfaceIdentifier(name);
         try {
@@ -227,15 +244,9 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
         } catch (Exception e) {
             LOG.error("Failed to delete interface {} due to the exception {}", name, e.getMessage());
         }
-
-        InstanceIdentifier portIdentifier = NeutronvpnUtils.buildPortNameToPortUuidIdentifier(name);
-        MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, portIdentifier);
-        LOG.debug("name-uuid map for port with name: {}, uuid: {} deleted from NeutronPortData DS", name, port
-                .getUuid());
     }
 
-    private void createElanInterface(Port port) {
-        String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
+    private void createElanInterface(Port port, String name) {
         String elanInstanceName = port.getNetworkId().getValue();
         List<PhysAddress> physAddresses = new ArrayList<>();
         physAddresses.add(new PhysAddress(port.getMacAddress()));
@@ -252,24 +263,36 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
     private Uuid addPortToSubnets(Port port) {
         Uuid subnetId = null;
         Uuid vpnId = null;
-        String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid());
-
-        // find all subnets to which this port is associated
-        List<FixedIps> ips = port.getFixedIps();
-        for (FixedIps ip : ips) {
-            String ipValue = ip.getIpAddress().getIpv4Address().getValue();
-
-            InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
-            PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
-                    .setPortName(name);
-            MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
-            LOG.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
-                    ipValue, name);
-
-            subnetId = ip.getSubnetId();
-            Subnetmap subnetmap = nvpnManager.updateSubnetNode(subnetId, null, null, null, null, null, port.getUuid());
-            if (subnetmap != null) {
-                vpnId = subnetmap.getVpnId();
+        Subnetmap subnetmap = null;
+        String infName = port.getUuid().getValue();
+        boolean isLockAcquired = false;
+        String lockName = port.getUuid().getValue();
+
+        // find the subnet to which this port is associated
+        FixedIps ip = port.getFixedIps().get(0);
+        String ipValue = ip.getIpAddress().getIpv4Address().getValue();
+        InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
+        PortFixedipToPortNameBuilder builder = new PortFixedipToPortNameBuilder().setPortFixedip(ipValue)
+                .setPortName(infName);
+        MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, builder.build());
+        LOG.debug("fixedIp-name map for neutron port with fixedIp: {}, name: {} added to NeutronPortData DS",
+                ipValue, infName);
+        subnetId = ip.getSubnetId();
+        subnetmap = nvpnManager.updateSubnetNode(subnetId, null, null, null, null, null, port.getUuid());
+        if (subnetmap != null) {
+            vpnId = subnetmap.getVpnId();
+        }
+        if(vpnId != null) {
+            try {
+                isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
+                checkAndPublishPortAddNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
+                LOG.debug("Port added to subnet notification sent");
+            } catch (Exception e) {
+                LOG.error("Port added to subnet notification failed", e);
+            } finally {
+                if (isLockAcquired) {
+                    NeutronvpnUtils.unlock(lockManager, lockName);
+                }
             }
         }
         return vpnId;
@@ -278,22 +301,58 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
     private Uuid removePortFromSubnets(Port port) {
         Uuid subnetId = null;
         Uuid vpnId = null;
-
-        // find all Subnets to which this port is associated
-        List<FixedIps> ips = port.getFixedIps();
-        for (FixedIps ip : ips) {
-            String ipValue = ip.getIpAddress().getIpv4Address().getValue();
-
-            InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
-            MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
-            LOG.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS", ipValue);
-
-            subnetId = ip.getSubnetId();
-            Subnetmap subnetmap = nvpnManager.removeFromSubnetNode(subnetId, null, null, null, port.getUuid());
-            if (vpnId == null && subnetmap != null) {
-                vpnId = subnetmap.getVpnId();
+        Subnetmap subnetmap = null;
+        boolean isLockAcquired = false;
+        String lockName = port.getUuid().getValue();
+
+        // find the subnet to which this port is associated
+        FixedIps ip = port.getFixedIps().get(0);
+        String ipValue = ip.getIpAddress().getIpv4Address().getValue();
+        InstanceIdentifier id = NeutronvpnUtils.buildFixedIpToPortNameIdentifier(ipValue);
+        MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, id);
+        LOG.debug("fixedIp-name map for neutron port with fixedIp: {} deleted from NeutronPortData DS", ipValue);
+        subnetId = ip.getSubnetId();
+        subnetmap = nvpnManager.removeFromSubnetNode(subnetId, null, null, null, port.getUuid());
+        if (subnetmap != null) {
+            vpnId = subnetmap.getVpnId();
+        }
+        if(vpnId != null) {
+            try {
+                isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
+                checkAndPublishPortRemoveNotification(subnetmap.getSubnetIp(), subnetId, port.getUuid());
+                LOG.debug("Port removed from subnet notification sent");
+            } catch (Exception e) {
+                LOG.error("Port removed from subnet notification failed", e);
+            } finally {
+                if (isLockAcquired) {
+                    NeutronvpnUtils.unlock(lockManager, lockName);
+                }
             }
         }
         return vpnId;
     }
+
+    private void checkAndPublishPortAddNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
+        PortAddedToSubnetBuilder builder = new PortAddedToSubnetBuilder();
+
+        LOG.info("publish notification called");
+
+        builder.setSubnetIp(subnetIp);
+        builder.setSubnetId(subnetId);
+        builder.setPortId(portId);
+
+        notificationPublishService.putNotification(builder.build());
+    }
+
+    private void checkAndPublishPortRemoveNotification(String subnetIp, Uuid subnetId, Uuid portId)throws InterruptedException{
+        PortRemovedFromSubnetBuilder builder = new PortRemovedFromSubnetBuilder();
+
+        LOG.info("publish notification called");
+
+        builder.setPortId(portId);
+        builder.setSubnetIp(subnetIp);
+        builder.setSubnetId(subnetId);
+
+        notificationPublishService.putNotification(builder.build());
+    }
 }