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