Initial push of Neutron interface
[controller.git] / opendaylight / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / NeutronSubnet_IPAllocationPool.java
diff --git a/opendaylight/networkconfiguration/neutron/src/main/java/org/opendaylight/controller/networkconfig/neutron/NeutronSubnet_IPAllocationPool.java b/opendaylight/networkconfiguration/neutron/src/main/java/org/opendaylight/controller/networkconfig/neutron/NeutronSubnet_IPAllocationPool.java
new file mode 100644 (file)
index 0000000..15401b7
--- /dev/null
@@ -0,0 +1,171 @@
+/*\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;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import javax.xml.bind.annotation.XmlAccessType;\r
+import javax.xml.bind.annotation.XmlAccessorType;\r
+import javax.xml.bind.annotation.XmlElement;\r
+import javax.xml.bind.annotation.XmlRootElement;\r
+\r
+@XmlRootElement\r
+@XmlAccessorType(XmlAccessType.NONE)\r
+public class NeutronSubnet_IPAllocationPool {\r
+    // See OpenStack Network API v2.0 Reference for description of\r
+    // annotated attributes\r
+\r
+    @XmlElement(name="start")\r
+    String poolStart;\r
+\r
+    @XmlElement(name="end")\r
+    String poolEnd;\r
+\r
+    public NeutronSubnet_IPAllocationPool() { }\r
+\r
+    public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {\r
+        poolStart = lowAddress;\r
+        poolEnd = highAddress;\r
+    }\r
+\r
+    public String getPoolStart() {\r
+        return poolStart;\r
+    }\r
+\r
+    public void setPoolStart(String poolStart) {\r
+        this.poolStart = poolStart;\r
+    }\r
+\r
+    public String getPoolEnd() {\r
+        return poolEnd;\r
+    }\r
+\r
+    public void setPoolEnd(String poolEnd) {\r
+        this.poolEnd = poolEnd;\r
+    }\r
+\r
+    /**\r
+     * This method determines if this allocation pool contains the\r
+     * input IPv4 address\r
+     *\r
+     * @param inputString\r
+     *            IPv4 address in dotted decimal format\r
+     * @returns a boolean on whether the pool contains the address or not\r
+     */\r
+\r
+    public boolean contains(String inputString) {\r
+        long inputIP = convert(inputString);\r
+        long startIP = convert(poolStart);\r
+        long endIP = convert(poolEnd);\r
+        return (inputIP >= startIP && inputIP <= endIP);\r
+    }\r
+\r
+    /**\r
+     * This static method converts the supplied IPv4 address to a long\r
+     * integer for comparison\r
+     *\r
+     * @param inputString\r
+     *            IPv4 address in dotted decimal format\r
+     * @returns high-endian representation of the IPv4 address as a long\r
+     */\r
+\r
+    static long convert(String inputString) {\r
+        long ans = 0;\r
+        String[] parts = inputString.split("\\.");\r
+        for (String part: parts) {\r
+            ans <<= 8;\r
+            ans |= Integer.parseInt(part);\r
+        }\r
+        return ans;\r
+    }\r
+\r
+    /**\r
+     * This static method converts the supplied high-ending long back\r
+     * into a dotted decimal representation of an IPv4 address\r
+     *\r
+     * @param l\r
+     *            high-endian representation of the IPv4 address as a long\r
+     * @returns IPv4 address in dotted decimal format\r
+     */\r
+    static String longtoIP(long l) {\r
+        int i;\r
+        String[] parts = new String[4];\r
+        for (i=0; i<4; i++) {\r
+            parts[3-i] = String.valueOf(l & 255);\r
+            l >>= 8;\r
+        }\r
+        return join(parts,".");\r
+    }\r
+\r
+    /*\r
+     * helper routine used by longtoIP\r
+     */\r
+    public static String join(String r[],String d)\r
+    {\r
+        if (r.length == 0) return "";\r
+        StringBuilder sb = new StringBuilder();\r
+        int i;\r
+        for(i=0;i<r.length-1;i++)\r
+            sb.append(r[i]+d);\r
+        return sb.toString()+r[i];\r
+    }\r
+\r
+    /*\r
+     * This method splits the current instance by removing the supplied\r
+     * parameter.\r
+     *\r
+     * If the parameter is either the low or high address,\r
+     * then that member is adjusted and a list containing just this instance\r
+     * is returned.\r
+     *\r
+     * If the parameter is in the middle of the pool, then\r
+     * create two new instances, one ranging from low to parameter-1\r
+     * the other ranging from parameter+1 to high\r
+     */\r
+    public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {\r
+        List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();\r
+        long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);\r
+        long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);\r
+        long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);\r
+        long i;\r
+        NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();\r
+        boolean poolStarted = false;\r
+        for (i=sIP; i<=eIP; i++) {\r
+            if (i == sIP) {\r
+                if (i != gIP) {\r
+                    p.setPoolStart(poolStart);\r
+                    poolStarted = true;\r
+                }\r
+            }\r
+            if (i == eIP) {\r
+                if (i != gIP) {\r
+                    p.setPoolEnd(poolEnd);\r
+                } else {\r
+                    p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));\r
+                }\r
+                ans.add(p);\r
+            }\r
+            if (i != sIP && i != eIP) {\r
+                if (i != gIP) {\r
+                    if (!poolStarted) {\r
+                        p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));\r
+                        poolStarted = true;\r
+                    }\r
+                } else {\r
+                    p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));\r
+                    poolStarted = false;\r
+                    ans.add(p);\r
+                    p = new NeutronSubnet_IPAllocationPool();\r
+                }\r
+            }\r
+        }\r
+        return ans;\r
+    }\r
+}\r