Initial push of Neutron interface
[controller.git] / opendaylight / networkconfiguration / neutron / implementation / src / main / java / org / opendaylight / controller / networkconfig / neutron / implementation / NeutronPortInterface.java
diff --git a/opendaylight/networkconfiguration/neutron/implementation/src/main/java/org/opendaylight/controller/networkconfig/neutron/implementation/NeutronPortInterface.java b/opendaylight/networkconfiguration/neutron/implementation/src/main/java/org/opendaylight/controller/networkconfig/neutron/implementation/NeutronPortInterface.java
new file mode 100644 (file)
index 0000000..eea1977
--- /dev/null
@@ -0,0 +1,313 @@
+/*\r
+ * Copyright IBM Corporation, 2013.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+\r
+package org.opendaylight.controller.networkconfig.neutron.implementation;\r
+\r
+import java.lang.reflect.Method;\r
+import java.util.ArrayList;\r
+import java.util.Dictionary;\r
+import java.util.EnumSet;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Set;\r
+import java.util.Map.Entry;\r
+import java.util.concurrent.ConcurrentMap;\r
+\r
+import org.apache.felix.dm.Component;\r
+import org.opendaylight.controller.clustering.services.CacheConfigException;\r
+import org.opendaylight.controller.clustering.services.CacheExistException;\r
+import org.opendaylight.controller.clustering.services.IClusterContainerServices;\r
+import org.opendaylight.controller.clustering.services.IClusterServices;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronPortCRUD;\r
+import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;\r
+import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;\r
+import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;\r
+import org.opendaylight.controller.networkconfig.neutron.NeutronPort;\r
+import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;\r
+import org.opendaylight.controller.networkconfig.neutron.Neutron_IPs;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+public class NeutronPortInterface implements INeutronPortCRUD {\r
+    private static final Logger logger = LoggerFactory.getLogger(NeutronPortInterface.class);\r
+    private String containerName = null;\r
+\r
+    private IClusterContainerServices clusterContainerService = null;\r
+    private ConcurrentMap<String, NeutronPort> portDB;\r
+\r
+    // methods needed for creating caches\r
+\r
+    void setClusterContainerService(IClusterContainerServices s) {\r
+        logger.debug("Cluster Service set");\r
+        this.clusterContainerService = s;\r
+    }\r
+\r
+    void unsetClusterContainerService(IClusterContainerServices s) {\r
+        if (this.clusterContainerService == s) {\r
+            logger.debug("Cluster Service removed!");\r
+            this.clusterContainerService = null;\r
+        }\r
+    }\r
+\r
+    @SuppressWarnings("deprecation")\r
+    private void allocateCache() {\r
+        if (this.clusterContainerService == null) {\r
+            logger.error("un-initialized clusterContainerService, can't create cache");\r
+            return;\r
+        }\r
+        logger.debug("Creating Cache for OpenDOVE");\r
+        try {\r
+            // neutron caches\r
+            this.clusterContainerService.createCache("neutronPorts",\r
+                    EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));\r
+        } catch (CacheConfigException cce) {\r
+            logger.error("Cache couldn't be created for OpenDOVE -  check cache mode");\r
+        } catch (CacheExistException cce) {\r
+            logger.error("Cache for OpenDOVE already exists, destroy and recreate");\r
+        }\r
+        logger.debug("Cache successfully created for OpenDOVE");\r
+    }\r
+\r
+    @SuppressWarnings({ "unchecked", "deprecation" })\r
+    private void retrieveCache() {\r
+        if (this.clusterContainerService == null) {\r
+            logger.error("un-initialized clusterContainerService, can't retrieve cache");\r
+            return;\r
+        }\r
+\r
+        logger.debug("Retrieving cache for Neutron Ports");\r
+        portDB = (ConcurrentMap<String, NeutronPort>) this.clusterContainerService\r
+        .getCache("neutronPorts");\r
+        if (portDB == null) {\r
+            logger.error("Cache couldn't be retrieved for Neutron Ports");\r
+        }\r
+        logger.debug("Cache was successfully retrieved for Neutron Ports");\r
+    }\r
+\r
+    @SuppressWarnings("deprecation")\r
+    private void destroyCache() {\r
+        if (this.clusterContainerService == null) {\r
+            logger.error("un-initialized clusterMger, can't destroy cache");\r
+            return;\r
+        }\r
+        logger.debug("Destroying Cache for HostTracker");\r
+        this.clusterContainerService.destroyCache("neutronPorts");\r
+    }\r
+\r
+    private void startUp() {\r
+        allocateCache();\r
+        retrieveCache();\r
+    }\r
+\r
+    /**\r
+     * Function called by the dependency manager when all the required\r
+     * dependencies are satisfied\r
+     *\r
+     */\r
+    void init(Component c) {\r
+        Dictionary<?, ?> props = c.getServiceProperties();\r
+        if (props != null) {\r
+            this.containerName = (String) props.get("containerName");\r
+            logger.debug("Running containerName: {}", this.containerName);\r
+        } else {\r
+            // In the Global instance case the containerName is empty\r
+            this.containerName = "";\r
+        }\r
+        startUp();\r
+    }\r
+\r
+    /**\r
+     * Function called by the dependency manager when at least one dependency\r
+     * become unsatisfied or when the component is shutting down because for\r
+     * example bundle is being stopped.\r
+     *\r
+     */\r
+    void destroy() {\r
+        destroyCache();\r
+    }\r
+\r
+    /**\r
+     * Function called by dependency manager after "init ()" is called and after\r
+     * the services provided by the class are registered in the service registry\r
+     *\r
+     */\r
+    void start() {\r
+    }\r
+\r
+    /**\r
+     * Function called by the dependency manager before the services exported by\r
+     * the component are unregistered, this will be followed by a "destroy ()"\r
+     * calls\r
+     *\r
+     */\r
+    void stop() {\r
+    }\r
+\r
+    // this method uses reflection to update an object from it's delta.\r
+\r
+    private boolean overwrite(Object target, Object delta) {\r
+        Method[] methods = target.getClass().getMethods();\r
+\r
+        for(Method toMethod: methods){\r
+            if(toMethod.getDeclaringClass().equals(target.getClass())\r
+                    && toMethod.getName().startsWith("set")){\r
+\r
+                String toName = toMethod.getName();\r
+                String fromName = toName.replace("set", "get");\r
+\r
+                try {\r
+                    Method fromMethod = delta.getClass().getMethod(fromName);\r
+                    Object value = fromMethod.invoke(delta, (Object[])null);\r
+                    if(value != null){\r
+                        toMethod.invoke(target, value);\r
+                    }\r
+                } catch (Exception e) {\r
+                    e.printStackTrace();\r
+                    return false;\r
+                }\r
+            }\r
+        }\r
+        return true;\r
+    }\r
+\r
+    // IfNBPortCRUD methods\r
+\r
+    public boolean portExists(String uuid) {\r
+        return portDB.containsKey(uuid);\r
+    }\r
+\r
+    public NeutronPort getPort(String uuid) {\r
+        if (!portExists(uuid))\r
+            return null;\r
+        return portDB.get(uuid);\r
+    }\r
+\r
+    public List<NeutronPort> getAllPorts() {\r
+        Set<NeutronPort> allPorts = new HashSet<NeutronPort>();\r
+        for (Entry<String, NeutronPort> entry : portDB.entrySet()) {\r
+            NeutronPort port = entry.getValue();\r
+            allPorts.add(port);\r
+        }\r
+        logger.debug("Exiting getAllPorts, Found {} OpenStackPorts", allPorts.size());\r
+        List<NeutronPort> ans = new ArrayList<NeutronPort>();\r
+        ans.addAll(allPorts);\r
+        return ans;\r
+    }\r
+\r
+    public boolean addPort(NeutronPort input) {\r
+        if (portExists(input.getID()))\r
+            return false;\r
+        portDB.putIfAbsent(input.getID(), input);\r
+        // if there are no fixed IPs, allocate one for each subnet in the network\r
+        INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);\r
+        if (input.getFixedIPs().size() == 0) {\r
+            List<Neutron_IPs> list = input.getFixedIPs();\r
+            Iterator<NeutronSubnet> subnetIterator = systemCRUD.getAllSubnets().iterator();\r
+            while (subnetIterator.hasNext()) {\r
+                NeutronSubnet subnet = subnetIterator.next();\r
+                if (subnet.getNetworkUUID().equals(input.getNetworkUUID()))\r
+                    list.add(new Neutron_IPs(subnet.getID()));\r
+            }\r
+        }\r
+        Iterator<Neutron_IPs> fixedIPIterator = input.getFixedIPs().iterator();\r
+        while (fixedIPIterator.hasNext()) {\r
+            Neutron_IPs ip = fixedIPIterator.next();\r
+            NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());\r
+            if (ip.getIpAddress() == null)\r
+                ip.setIpAddress(subnet.getLowAddr());\r
+            if (!ip.getIpAddress().equals(subnet.getGatewayIP()))\r
+                subnet.allocateIP(ip.getIpAddress());\r
+            else\r
+                subnet.setGatewayIPAllocated();\r
+            subnet.addPort(input);\r
+        }\r
+        INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);\r
+\r
+        NeutronNetwork network = networkIf.getNetwork(input.getNetworkUUID());\r
+        network.addPort(input);\r
+        return true;\r
+    }\r
+\r
+    public boolean removePort(String uuid) {\r
+        if (!portExists(uuid))\r
+            return false;\r
+        NeutronPort port = getPort(uuid);\r
+        portDB.remove(uuid);\r
+        INeutronNetworkCRUD networkCRUD = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);\r
+        INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);\r
+\r
+        NeutronNetwork network = networkCRUD.getNetwork(port.getNetworkUUID());\r
+        network.removePort(port);\r
+        Iterator<Neutron_IPs> fixedIPIterator = port.getFixedIPs().iterator();\r
+        while (fixedIPIterator.hasNext()) {\r
+            Neutron_IPs ip = fixedIPIterator.next();\r
+            NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());\r
+            if (!ip.getIpAddress().equals(subnet.getGatewayIP()))\r
+                subnet.releaseIP(ip.getIpAddress());\r
+            else\r
+                subnet.resetGatewayIPAllocated();\r
+            subnet.removePort(port);\r
+        }\r
+        return true;\r
+    }\r
+\r
+    public boolean updatePort(String uuid, NeutronPort delta) {\r
+        if (!portExists(uuid))\r
+            return false;\r
+        NeutronPort target = portDB.get(uuid);\r
+        // remove old Fixed_IPs\r
+        NeutronPort port = getPort(uuid);\r
+        INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);\r
+        Iterator<Neutron_IPs> fixedIPIterator = port.getFixedIPs().iterator();\r
+        while (fixedIPIterator.hasNext()) {\r
+            Neutron_IPs ip = fixedIPIterator.next();\r
+            NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());\r
+            subnet.releaseIP(ip.getIpAddress());\r
+        }\r
+\r
+        // allocate new Fixed_IPs\r
+        fixedIPIterator = delta.getFixedIPs().iterator();\r
+        while (fixedIPIterator.hasNext()) {\r
+            Neutron_IPs ip = fixedIPIterator.next();\r
+            NeutronSubnet subnet = systemCRUD.getSubnet(ip.getSubnetUUID());\r
+            if (ip.getIpAddress() == null)\r
+                ip.setIpAddress(subnet.getLowAddr());\r
+            subnet.allocateIP(ip.getIpAddress());\r
+        }\r
+        return overwrite(target, delta);\r
+    }\r
+\r
+    public boolean macInUse(String macAddress) {\r
+        List<NeutronPort> ports = getAllPorts();\r
+        Iterator<NeutronPort> portIterator = ports.iterator();\r
+        while (portIterator.hasNext()) {\r
+            NeutronPort port = portIterator.next();\r
+            if (macAddress.equalsIgnoreCase(port.getMacAddress()))\r
+                return true;\r
+        }\r
+        return false;\r
+    }\r
+\r
+    public NeutronPort getGatewayPort(String subnetUUID) {\r
+        INeutronSubnetCRUD systemCRUD = NeutronCRUDInterfaces.getINeutronSubnetCRUD(this);\r
+        NeutronSubnet subnet = systemCRUD.getSubnet(subnetUUID);\r
+        Iterator<NeutronPort> portIterator = getAllPorts().iterator();\r
+        while (portIterator.hasNext()) {\r
+            NeutronPort port = portIterator.next();\r
+            List<Neutron_IPs> fixedIPs = port.getFixedIPs();\r
+            if (fixedIPs.size() == 1) {\r
+                if (subnet.getGatewayIP().equals(fixedIPs.get(0).getIpAddress()))\r
+                    return port;\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+}\r