Skip to content

Commit a93a305

Browse files
author
Sheng Yang
committed
CLOUDSTACK-6854: Fix inconsistent IPv6 address formats
fc00:0003:1373::0002 should be treated the same as fc00:3:1373::2.
1 parent aa1ce9a commit a93a305

5 files changed

Lines changed: 71 additions & 58 deletions

File tree

api/src/org/apache/cloudstack/api/command/admin/vlan/CreateVlanIpRangeCmd.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.admin.vlan;
1818

19+
import com.cloud.utils.net.NetUtils;
1920
import org.apache.log4j.Logger;
2021

2122
import org.apache.cloudstack.api.APICommand;
@@ -166,28 +167,28 @@ public String getStartIpv6() {
166167
if (startIpv6 == null) {
167168
return null;
168169
}
169-
return startIpv6.toLowerCase();
170+
return NetUtils.standardizeIp6Address(startIpv6);
170171
}
171172

172173
public String getEndIpv6() {
173174
if (endIpv6 == null) {
174175
return null;
175176
}
176-
return endIpv6.toLowerCase();
177+
return NetUtils.standardizeIp6Address(endIpv6);
177178
}
178179

179180
public String getIp6Gateway() {
180181
if (ip6Gateway == null) {
181182
return null;
182183
}
183-
return ip6Gateway.toLowerCase();
184+
return NetUtils.standardizeIp6Address(ip6Gateway);
184185
}
185186

186187
public String getIp6Cidr() {
187188
if (ip6Cidr == null) {
188189
return null;
189190
}
190-
return ip6Cidr.toLowerCase();
191+
return NetUtils.standardizeIp6Cidr(ip6Cidr);
191192
}
192193

193194
/////////////////////////////////////////////////////

api/src/org/apache/cloudstack/api/command/user/network/CreateNetworkCmd.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.network;
1818

19+
import com.cloud.utils.net.NetUtils;
1920
import org.apache.log4j.Logger;
2021

2122
import org.apache.cloudstack.acl.RoleType;
@@ -254,28 +255,28 @@ public String getStartIpv6() {
254255
if (startIpv6 == null) {
255256
return null;
256257
}
257-
return startIpv6.toLowerCase();
258+
return NetUtils.standardizeIp6Address(startIpv6);
258259
}
259260

260261
public String getEndIpv6() {
261262
if (endIpv6 == null) {
262263
return null;
263264
}
264-
return endIpv6.toLowerCase();
265+
return NetUtils.standardizeIp6Address(endIpv6);
265266
}
266267

267268
public String getIp6Gateway() {
268269
if (ip6Gateway == null) {
269270
return null;
270271
}
271-
return ip6Gateway.toLowerCase();
272+
return NetUtils.standardizeIp6Address(ip6Gateway);
272273
}
273274

274275
public String getIp6Cidr() {
275276
if (ip6Cidr == null) {
276277
return null;
277278
}
278-
return ip6Cidr.toLowerCase();
279+
return NetUtils.standardizeIp6Cidr(ip6Cidr);
279280
}
280281

281282
public Long getAclId() {

api/src/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,25 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.user.vm;
1818

19-
import java.util.ArrayList;
20-
import java.util.Collection;
21-
import java.util.HashMap;
22-
import java.util.Iterator;
23-
import java.util.LinkedHashMap;
24-
import java.util.List;
25-
import java.util.Map;
26-
27-
import org.apache.log4j.Logger;
28-
19+
import com.cloud.dc.DataCenter;
20+
import com.cloud.dc.DataCenter.NetworkType;
21+
import com.cloud.event.EventTypes;
22+
import com.cloud.exception.ConcurrentOperationException;
23+
import com.cloud.exception.InsufficientCapacityException;
24+
import com.cloud.exception.InsufficientServerCapacityException;
25+
import com.cloud.exception.InvalidParameterValueException;
26+
import com.cloud.exception.ResourceAllocationException;
27+
import com.cloud.exception.ResourceUnavailableException;
28+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
29+
import com.cloud.network.Network;
30+
import com.cloud.network.Network.IpAddresses;
31+
import com.cloud.offering.DiskOffering;
32+
import com.cloud.offering.ServiceOffering;
33+
import com.cloud.template.VirtualMachineTemplate;
34+
import com.cloud.user.Account;
35+
import com.cloud.uservm.UserVm;
36+
import com.cloud.utils.net.NetUtils;
37+
import com.cloud.vm.VirtualMachine;
2938
import org.apache.cloudstack.acl.RoleType;
3039
import org.apache.cloudstack.affinity.AffinityGroupResponse;
3140
import org.apache.cloudstack.api.ACL;
@@ -48,25 +57,15 @@
4857
import org.apache.cloudstack.api.response.UserVmResponse;
4958
import org.apache.cloudstack.api.response.ZoneResponse;
5059
import org.apache.cloudstack.context.CallContext;
60+
import org.apache.log4j.Logger;
5161

52-
import com.cloud.dc.DataCenter;
53-
import com.cloud.dc.DataCenter.NetworkType;
54-
import com.cloud.event.EventTypes;
55-
import com.cloud.exception.ConcurrentOperationException;
56-
import com.cloud.exception.InsufficientCapacityException;
57-
import com.cloud.exception.InsufficientServerCapacityException;
58-
import com.cloud.exception.InvalidParameterValueException;
59-
import com.cloud.exception.ResourceAllocationException;
60-
import com.cloud.exception.ResourceUnavailableException;
61-
import com.cloud.hypervisor.Hypervisor.HypervisorType;
62-
import com.cloud.network.Network;
63-
import com.cloud.network.Network.IpAddresses;
64-
import com.cloud.offering.DiskOffering;
65-
import com.cloud.offering.ServiceOffering;
66-
import com.cloud.template.VirtualMachineTemplate;
67-
import com.cloud.user.Account;
68-
import com.cloud.uservm.UserVm;
69-
import com.cloud.vm.VirtualMachine;
62+
import java.util.ArrayList;
63+
import java.util.Collection;
64+
import java.util.HashMap;
65+
import java.util.Iterator;
66+
import java.util.LinkedHashMap;
67+
import java.util.List;
68+
import java.util.Map;
7069

7170
@APICommand(name = "deployVirtualMachine", description = "Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.", responseObject = UserVmResponse.class, responseView = ResponseView.Restricted, entityType = {VirtualMachine.class},
7271
requestHasSensitiveInfo = false, responseHasSensitiveInfo = true)
@@ -353,7 +352,7 @@ private Map<Long, IpAddresses> getIpToNetworkMap() {
353352
String requestedIp = ips.get("ip");
354353
String requestedIpv6 = ips.get("ipv6");
355354
if (requestedIpv6 != null) {
356-
requestedIpv6 = requestedIpv6.toLowerCase();
355+
requestedIpv6 = NetUtils.standardizeIp6Address(requestedIpv6);
357356
}
358357
IpAddresses addrs = new IpAddresses(requestedIp, requestedIpv6);
359358
ipToNetworkMap.put(networkId, addrs);
@@ -367,7 +366,7 @@ public String getIp6Address() {
367366
if (ip6Address == null) {
368367
return null;
369368
}
370-
return ip6Address.toLowerCase();
369+
return NetUtils.standardizeIp6Address(ip6Address);
371370
}
372371

373372
public List<Long> getAffinityGroupIdList() {

utils/src/com/cloud/utils/net/NetUtils.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919

2020
package com.cloud.utils.net;
2121

22+
import com.cloud.utils.IteratorUtil;
23+
import com.cloud.utils.Pair;
24+
import com.cloud.utils.script.Script;
25+
import com.googlecode.ipv6.IPv6Address;
26+
import com.googlecode.ipv6.IPv6AddressRange;
27+
import com.googlecode.ipv6.IPv6Network;
28+
import org.apache.commons.lang.SystemUtils;
29+
import org.apache.commons.net.util.SubnetUtils;
30+
import org.apache.log4j.Logger;
31+
2232
import java.io.BufferedReader;
2333
import java.io.IOException;
2434
import java.io.InputStreamReader;
@@ -41,17 +51,6 @@
4151
import java.util.regex.Matcher;
4252
import java.util.regex.Pattern;
4353

44-
import org.apache.commons.lang.SystemUtils;
45-
import org.apache.commons.net.util.SubnetUtils;
46-
import org.apache.log4j.Logger;
47-
48-
import com.cloud.utils.IteratorUtil;
49-
import com.cloud.utils.Pair;
50-
import com.cloud.utils.script.Script;
51-
import com.googlecode.ipv6.IPv6Address;
52-
import com.googlecode.ipv6.IPv6AddressRange;
53-
import com.googlecode.ipv6.IPv6Network;
54-
5554
public class NetUtils {
5655
protected final static Logger s_logger = Logger.getLogger(NetUtils.class);
5756
public final static String HTTP_PORT = "80";
@@ -1372,6 +1371,14 @@ public static String getNextIp6InRange(String currentIp, String ipRange) {
13721371
return resultIp;
13731372
}
13741373

1374+
public static String standardizeIp6Address(String ip6Addr) {
1375+
return IPv6Address.fromString(ip6Addr).toString();
1376+
}
1377+
1378+
public static String standardizeIp6Cidr(String ip6Cidr){
1379+
return IPv6Network.fromString(ip6Cidr).toString();
1380+
}
1381+
13751382
static final String VLAN_PREFIX = "vlan://";
13761383
static final int VLAN_PREFIX_LENGTH = VLAN_PREFIX.length();
13771384

utils/test/com/cloud/utils/net/NetUtilsTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919

2020
package com.cloud.utils.net;
2121

22+
import com.googlecode.ipv6.IPv6Address;
23+
import org.apache.log4j.Logger;
24+
import org.junit.Test;
25+
26+
import java.math.BigInteger;
27+
import java.util.SortedSet;
28+
import java.util.TreeSet;
29+
2230
import static org.hamcrest.Matchers.anyOf;
2331
import static org.hamcrest.Matchers.contains;
2432
import static org.hamcrest.Matchers.equalTo;
@@ -31,15 +39,6 @@
3139
import static org.junit.Assert.assertThat;
3240
import static org.junit.Assert.assertTrue;
3341

34-
import java.math.BigInteger;
35-
import java.util.SortedSet;
36-
import java.util.TreeSet;
37-
38-
import org.apache.log4j.Logger;
39-
import org.junit.Test;
40-
41-
import com.googlecode.ipv6.IPv6Address;
42-
4342
public class NetUtilsTest {
4443

4544
private static final Logger s_logger = Logger.getLogger(NetUtilsTest.class);
@@ -198,6 +197,12 @@ public void testIsIp6RangeOverlap() {
198197
assertFalse(NetUtils.isIp6RangeOverlap("1234:5678::f-1234:5678::f", "1234:5678::2-1234:5678::e"));
199198
}
200199

200+
@Test
201+
public void testStandardizeIp6Address() {
202+
assertEquals(NetUtils.standardizeIp6Address("1234:0000:0000:5678:0000:000:ABCD:0001"), "1234::5678:0:0:abcd:1");
203+
assertEquals(NetUtils.standardizeIp6Cidr("1234:0000:0000:5678:0000:000:ABCD:0001/64"), "1234::5678:0:0:0:0/64");
204+
}
205+
201206
@Test
202207
public void testGenerateUriForPvlan() {
203208
assertEquals("pvlan://123-i456", NetUtils.generateUriForPvlan("123", "456").toString());

0 commit comments

Comments
 (0)