General Sonar clean-up
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / SecurityServicesImpl.java
index cf912dc4577c47797fcd910711d2b30c4737801f..ed6e22fa740453ac3d02a0d15b4533c397ee154c 100644 (file)
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.ovsdb.openstack.netvirt.impl;
 
-import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;
-import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
-import org.opendaylight.controller.networkconfig.neutron.NeutronSecurityGroup;
-import org.opendaylight.controller.sal.utils.ServiceHelper;
+import java.util.List;
+
+import org.opendaylight.neutron.spi.INeutronPortCRUD;
+import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
+import org.opendaylight.neutron.spi.NeutronPort;
+import org.opendaylight.neutron.spi.NeutronSecurityGroup;
+import org.opendaylight.neutron.spi.NeutronSubnet;
+import org.opendaylight.neutron.spi.Neutron_IPs;
+import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface;
 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
 import org.opendaylight.ovsdb.openstack.netvirt.api.SecurityServicesManager;
-
-import org.opendaylight.ovsdb.schema.openvswitch.Interface;
+import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.*;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.List;
-import java.util.Map;
-
-public class SecurityServicesImpl implements SecurityServicesManager {
-
-    static final Logger logger = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
-
-    public SecurityServicesImpl() {
-    }
+public class SecurityServicesImpl implements ConfigInterface, SecurityServicesManager {
+    private static final Logger LOG = LoggerFactory.getLogger(TenantNetworkManagerImpl.class);
+    private volatile INeutronPortCRUD neutronPortCache;
+    private volatile INeutronSubnetCRUD neutronSubnetCache;
+    private volatile Southbound southbound;
 
     /**
      * Is security group ready.
      *
-     * @param intf the intf
+     * @param terminationPointAugmentation the intf
      * @return the boolean
      */
-    public boolean isPortSecurityReady(Interface intf) {
-        logger.trace("getTenantNetworkForInterface for {}", intf);
-        if (intf == null) return false;
-        Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
-        logger.trace("externalIds {}", externalIds);
-        if (externalIds == null) return false;
-        String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
-        if (neutronPortId == null) return false;
-        INeutronPortCRUD neutronPortService = (INeutronPortCRUD) ServiceHelper.getGlobalInstance(INeutronPortCRUD.class,
-                this);
-        NeutronPort neutronPort = neutronPortService.getPort(neutronPortId);
+    public boolean isPortSecurityReady(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
+        if (neutronPortCache == null) {
+            LOG.error("neutron port is null");
+            return false;
+        }
+        LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
+        String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
+                Constants.EXTERNAL_ID_INTERFACE_ID);
+        if (neutronPortId == null) {
+            return false;
+        }
+        NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
+        if (neutronPort == null) {
+            return false;
+        }
         String deviceOwner = neutronPort.getDeviceOwner();
         if (!deviceOwner.contains("compute")) {
-            logger.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
+            LOG.debug("Port {} is not a compute host, it is a: {}", neutronPortId, deviceOwner);
         }
-        logger.debug("isPortSecurityReady() is a {} ", deviceOwner);
+        LOG.debug("isPortSecurityReady() is a {} ", deviceOwner);
         List<NeutronSecurityGroup> securityGroups = neutronPort.getSecurityGroups();
         if (securityGroups.isEmpty()) {
-            logger.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
+            LOG.debug("Check for device: {} does not contain a Security Group for port: {}", deviceOwner,
                     neutronPortId);
             return false;
         }
-        try {
-            String vmPort = externalIds.get("attached-mac");
-        } catch(Exception e) {
-            logger.debug("Error VMID did *NOT* work");
-        }
-        logger.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
+        LOG.debug("Security Group Check {} DOES contain a Neutron Security Group", neutronPortId);
         return true;
     }
 
     /**
      * Gets security group in port.
      *
-     * @param intf the intf
+     * @param terminationPointAugmentation the intf
      * @return the security group in port
      */
-    public NeutronSecurityGroup getSecurityGroupInPort(Interface intf) {
-        logger.trace("getTenantNetworkForInterface for {}", intf);
-        if (intf == null) return null;
-        Map<String, String> externalIds = intf.getExternalIdsColumn().getData();
-        logger.trace("externalIds {}", externalIds);
-        if (externalIds == null) return null;
-        String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
-        if (neutronPortId == null) return null;
-        INeutronPortCRUD neutronPortService = (INeutronPortCRUD)
-                ServiceHelper.getGlobalInstance(INeutronPortCRUD.class, this);
-        NeutronPort neutronPort = neutronPortService.getPort(neutronPortId);
+    public NeutronSecurityGroup getSecurityGroupInPort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
+        if (neutronPortCache == null) {
+            LOG.error("neutron port is null");
+            return null;
+        }
+        LOG.trace("isPortSecurityReady for {}", terminationPointAugmentation.getName());
+        String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
+                Constants.EXTERNAL_ID_INTERFACE_ID);
+        if (neutronPortId == null) {
+            return null;
+        }
+        NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
+        if (neutronPort == null) {
+            return null;
+        }
+
         List<NeutronSecurityGroup> neutronSecurityGroups = neutronPort.getSecurityGroups();
-        NeutronSecurityGroup neutronSecurityGroup = (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
-        return neutronSecurityGroup;
+        if (neutronSecurityGroups != null) {
+            return (NeutronSecurityGroup) neutronSecurityGroups.toArray()[0];
+        } else {
+            return null;
+        }
+    }
+
+    @Override
+    public NeutronPort getDHCPServerPort(
+            OvsdbTerminationPointAugmentation terminationPointAugmentation) {
+        if (neutronPortCache == null) {
+            LOG.error("getDHCPServerPort: neutron port is null");
+            return null;
+        }
+        LOG.trace("getDHCPServerPort for {}",
+                terminationPointAugmentation.getName());
+        String neutronPortId = southbound.getInterfaceExternalIdsValue(
+                terminationPointAugmentation,
+                Constants.EXTERNAL_ID_INTERFACE_ID);
+        if (neutronPortId == null) {
+            return null;
+        }
+        NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
+        //Since all the fixed ip assigned to a port should be from the same network, first port is sufficient.
+        List<Neutron_IPs> fixedIps = neutronPort.getFixedIPs();
+        if(null==fixedIps || 0 == fixedIps.size() )
+        {
+            LOG.error("getDHCPServerPort: No fixed ip is assigned");
+            return null;
+        }
+        String subnetUUID = fixedIps.iterator().next().getSubnetUUID();
+        NeutronSubnet neutronSubnet = neutronSubnetCache.getSubnet(subnetUUID);
+        List<NeutronPort> ports = neutronSubnet.getPortsInSubnet();
+        for (NeutronPort port : ports) {
+            if (port.getDeviceOwner().contains("dhcp")) {
+                return port;
+            }
+        }
+
+        return null;
+
+    }
+
+    @Override
+    public boolean isComputePort(OvsdbTerminationPointAugmentation terminationPointAugmentation) {
+        if (neutronPortCache == null) {
+            LOG.error("neutron port is null");
+            return false;
+        }
+        LOG.trace("isComputePort for {}", terminationPointAugmentation.getName());
+        String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
+                Constants.EXTERNAL_ID_INTERFACE_ID);
+        if (neutronPortId == null) {
+            return false;
+        }
+        NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
+        if (neutronPort == null) {
+            return false;
+        }
+        String deviceOwner = neutronPort.getDeviceOwner();
+        if (!deviceOwner.contains("compute")) {
+            LOG.debug("isComputePort : Port {} is not a DHCP server port", neutronPortId, deviceOwner);
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public boolean isLastPortinSubnet(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
+        if (neutronPortCache == null) {
+            LOG.error("isLastPortinSubnet: neutron port is null");
+            return false;
+        }
+        LOG.trace("isLastPortinSubnet: for {}", terminationPointAugmentation.getName());
+        String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
+                                                                       Constants.EXTERNAL_ID_INTERFACE_ID);
+        if (neutronPortId == null) {
+            return false;
+        }
+        NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
+        List<Neutron_IPs> neutronPortFixedIp = neutronPort.getFixedIPs();
+        if(null == neutronPortFixedIp || neutronPortFixedIp.isEmpty()) {
+            return false;
+        }
+        List<TerminationPoint> terminationPoints = node.getTerminationPoint();
+        if(terminationPoints != null && !terminationPoints.isEmpty()) {
+            for(TerminationPoint tp : terminationPoints) {
+                OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
+                        tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
+                if (ovsdbTerminationPointAugmentation != null && !ovsdbTerminationPointAugmentation.
+                        getName().equals(Constants.INTEGRATION_BRIDGE)) {
+                    String portId = southbound.getInterfaceExternalIdsValue(ovsdbTerminationPointAugmentation,
+                                                                            Constants.EXTERNAL_ID_INTERFACE_ID);
+                    if(null!=portId) {
+                        NeutronPort port = neutronPortCache.getPort(portId);
+                        if(null!=port) {
+                            if(!(port.getID().equals(neutronPort.getID())) && port.getDeviceOwner().contains("compute")) {
+                                List<Neutron_IPs> portFixedIp = port.getFixedIPs();
+                                if(null == portFixedIp || portFixedIp.isEmpty()) {
+                                    return false;
+                                }
+                                if(portFixedIp.iterator().next().getSubnetUUID().equals
+                                        (neutronPort.getFixedIPs().iterator().next().getSubnetUUID())) {
+                                    return false;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public boolean isLastPortinBridge(Node node, OvsdbTerminationPointAugmentation terminationPointAugmentation) {
+        LOG.trace("isLastPortinBridge: for {}", terminationPointAugmentation.getName());
+        List<TerminationPoint> terminationPoints = node.getTerminationPoint();
+        if(terminationPoints != null && !terminationPoints.isEmpty()){
+            for(TerminationPoint tp : terminationPoints){
+                OvsdbTerminationPointAugmentation ovsdbTerminationPointAugmentation =
+                        tp.getAugmentation(OvsdbTerminationPointAugmentation.class);
+                if(null!=ovsdbTerminationPointAugmentation)
+                {
+                    if(!(ovsdbTerminationPointAugmentation.getName().equals(Constants.INTEGRATION_BRIDGE))
+                            && !(terminationPointAugmentation.getInterfaceUuid().equals
+                                    (ovsdbTerminationPointAugmentation.getInterfaceUuid()))) {
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public List<Neutron_IPs> getIpAddress(Node node,
+                                OvsdbTerminationPointAugmentation terminationPointAugmentation) {
+        if (neutronPortCache == null) {
+            LOG.error("getIpAddress: neutron port is null");
+            return null;
+        }
+        LOG.trace("getIpAddress: for {}", terminationPointAugmentation.getName());
+        String neutronPortId = southbound.getInterfaceExternalIdsValue(terminationPointAugmentation,
+                Constants.EXTERNAL_ID_INTERFACE_ID);
+        if (neutronPortId == null) {
+            return null;
+        }
+        NeutronPort neutronPort = neutronPortCache.getPort(neutronPortId);
+        return neutronPort.getFixedIPs();
+    }
+
+    @Override
+    public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
+        southbound =
+                (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
+    }
+
+    @Override
+    public void setDependencies(Object impl) {
+        if (impl instanceof INeutronPortCRUD) {
+            neutronPortCache = (INeutronPortCRUD)impl;
+        }
+        else if (impl instanceof INeutronSubnetCRUD) {
+            neutronSubnetCache = (INeutronSubnetCRUD) impl;
+        }
     }
 }