Convert neutron service base classes to unix line delimiters.
[controller.git] / opendaylight / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / NeutronSubnet_IPAllocationPool.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.networkconfig.neutron;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import javax.xml.bind.annotation.XmlAccessType;
15 import javax.xml.bind.annotation.XmlAccessorType;
16 import javax.xml.bind.annotation.XmlElement;
17 import javax.xml.bind.annotation.XmlRootElement;
18
19 @XmlRootElement
20 @XmlAccessorType(XmlAccessType.NONE)
21 public class NeutronSubnet_IPAllocationPool {
22     // See OpenStack Network API v2.0 Reference for description of
23     // annotated attributes
24
25     @XmlElement(name="start")
26     String poolStart;
27
28     @XmlElement(name="end")
29     String poolEnd;
30
31     public NeutronSubnet_IPAllocationPool() { }
32
33     public NeutronSubnet_IPAllocationPool(String lowAddress, String highAddress) {
34         poolStart = lowAddress;
35         poolEnd = highAddress;
36     }
37
38     public String getPoolStart() {
39         return poolStart;
40     }
41
42     public void setPoolStart(String poolStart) {
43         this.poolStart = poolStart;
44     }
45
46     public String getPoolEnd() {
47         return poolEnd;
48     }
49
50     public void setPoolEnd(String poolEnd) {
51         this.poolEnd = poolEnd;
52     }
53
54     /**
55      * This method determines if this allocation pool contains the
56      * input IPv4 address
57      *
58      * @param inputString
59      *            IPv4 address in dotted decimal format
60      * @returns a boolean on whether the pool contains the address or not
61      */
62
63     public boolean contains(String inputString) {
64         long inputIP = convert(inputString);
65         long startIP = convert(poolStart);
66         long endIP = convert(poolEnd);
67         return (inputIP >= startIP && inputIP <= endIP);
68     }
69
70     /**
71      * This static method converts the supplied IPv4 address to a long
72      * integer for comparison
73      *
74      * @param inputString
75      *            IPv4 address in dotted decimal format
76      * @returns high-endian representation of the IPv4 address as a long
77      */
78
79     static long convert(String inputString) {
80         long ans = 0;
81         String[] parts = inputString.split("\\.");
82         for (String part: parts) {
83             ans <<= 8;
84             ans |= Integer.parseInt(part);
85         }
86         return ans;
87     }
88
89     /**
90      * This static method converts the supplied high-ending long back
91      * into a dotted decimal representation of an IPv4 address
92      *
93      * @param l
94      *            high-endian representation of the IPv4 address as a long
95      * @returns IPv4 address in dotted decimal format
96      */
97     static String longtoIP(long l) {
98         int i;
99         String[] parts = new String[4];
100         for (i=0; i<4; i++) {
101             parts[3-i] = String.valueOf(l & 255);
102             l >>= 8;
103         }
104         return join(parts,".");
105     }
106
107     /*
108      * helper routine used by longtoIP
109      */
110     public static String join(String r[],String d)
111     {
112         if (r.length == 0) {
113             return "";
114         }
115         StringBuilder sb = new StringBuilder();
116         int i;
117         for(i=0;i<r.length-1;i++) {
118             sb.append(r[i]+d);
119         }
120         return sb.toString()+r[i];
121     }
122
123     /*
124      * This method splits the current instance by removing the supplied
125      * parameter.
126      *
127      * If the parameter is either the low or high address,
128      * then that member is adjusted and a list containing just this instance
129      * is returned.
130      *
131      * If the parameter is in the middle of the pool, then
132      * create two new instances, one ranging from low to parameter-1
133      * the other ranging from parameter+1 to high
134      */
135     public List<NeutronSubnet_IPAllocationPool> splitPool(String ipAddress) {
136         List<NeutronSubnet_IPAllocationPool> ans = new ArrayList<NeutronSubnet_IPAllocationPool>();
137         long gIP = NeutronSubnet_IPAllocationPool.convert(ipAddress);
138         long sIP = NeutronSubnet_IPAllocationPool.convert(poolStart);
139         long eIP = NeutronSubnet_IPAllocationPool.convert(poolEnd);
140         long i;
141         NeutronSubnet_IPAllocationPool p = new NeutronSubnet_IPAllocationPool();
142         boolean poolStarted = false;
143         for (i=sIP; i<=eIP; i++) {
144             if (i == sIP) {
145                 if (i != gIP) {
146                     p.setPoolStart(poolStart);
147                     poolStarted = true;
148                 }
149             }
150             if (i == eIP) {
151                 if (i != gIP) {
152                     p.setPoolEnd(poolEnd);
153                 } else {
154                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
155                 }
156                 ans.add(p);
157             }
158             if (i != sIP && i != eIP) {
159                 if (i != gIP) {
160                     if (!poolStarted) {
161                         p.setPoolStart(NeutronSubnet_IPAllocationPool.longtoIP(i));
162                         poolStarted = true;
163                     }
164                 } else {
165                     p.setPoolEnd(NeutronSubnet_IPAllocationPool.longtoIP(i-1));
166                     poolStarted = false;
167                     ans.add(p);
168                     p = new NeutronSubnet_IPAllocationPool();
169                 }
170             }
171         }
172         return ans;
173     }
174 }