Updated Branches: refs/heads/master c01d0f74c -> 53d09c6f1
uri code per broadcast/isolation type , default is to accept anything as uri , vlan and lswitch need some extra tlc Signed-off-by: Hugo Trippaers <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/53d09c6f Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/53d09c6f Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/53d09c6f Branch: refs/heads/master Commit: 53d09c6f1843f04c5f1ab76be9419f5584302d1e Parents: c01d0f7 Author: Daan Hoogland <[email protected]> Authored: Mon Aug 5 11:52:40 2013 +0200 Committer: Hugo Trippaers <[email protected]> Committed: Thu Aug 15 14:26:26 2013 +0200 ---------------------------------------------------------------------- api/src/com/cloud/network/Networks.java | 179 ++++++++++++++++++---- api/test/com/cloud/network/NetworksTest.java | 21 ++- 2 files changed, 162 insertions(+), 38 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/53d09c6f/api/src/com/cloud/network/Networks.java ---------------------------------------------------------------------- diff --git a/api/src/com/cloud/network/Networks.java b/api/src/com/cloud/network/Networks.java index c76c3d4..f8166c6 100755 --- a/api/src/com/cloud/network/Networks.java +++ b/api/src/com/cloud/network/Networks.java @@ -55,13 +55,57 @@ public class Networks { * Different types of broadcast domains. */ public enum BroadcastDomainType { - Native(null, null), - Vlan("vlan", Integer.class), + Native(null, null) { + @Override + public <T> URI toUri(T value) { + try { + if (value.toString().contains("://")) + return new URI(value.toString()); + else + // strange requirement but this is how the code expects it + return new URI("vlan://" + value.toString()); + } catch (URISyntaxException e) { + throw new CloudRuntimeException("Unable to convert to broadcast URI: " + value); + } + } + }, + Vlan("vlan", Integer.class) { + @Override + public <T> URI toUri(T value) { + try { + if (value.toString().contains("://")) + return new URI(value.toString()); + else + return new URI("vlan://" + value.toString()); + } catch (URISyntaxException e) { + throw new CloudRuntimeException( + "Unable to convert to broadcast URI: " + value); + } + } + }, Vswitch("vs", String.class), LinkLocal(null, null), Vnet("vnet", Long.class), Storage("storage", Integer.class), - Lswitch("lswitch", String.class), + Lswitch("lswitch", String.class) { + @Override + public <T> URI toUri(T value) { + try { + return new URI("lswitch",value.toString(),null,null); + } catch (URISyntaxException e) { + throw new CloudRuntimeException( + "Unable to convert to broadcast URI: " + value); + } + } + + /** + * gets scheme specific part instead of host + */ + @Override + public String getValueFrom(URI uri) { + return uri.getSchemeSpecificPart(); + } + }, Mido("mido", String.class), Pvlan("pvlan", String.class), UnDecided(null, null); @@ -90,30 +134,53 @@ public class Networks { return type; } + /** + * The default implementation of toUri returns an uri with the scheme and value as host + * + * @param value will be put in the host field + * @return the resulting URI + */ public <T> URI toUri(T value) { try { - // do we need to check that value does not contain a scheme - // part? - if (value.toString().contains(":")) - return new URI(value.toString()); - else - return new URI(scheme, value.toString(), null); + return new URI(scheme + "://" + value.toString()); } catch (URISyntaxException e) { throw new CloudRuntimeException( "Unable to convert to broadcast URI: " + value); } } - public static BroadcastDomainType getTypeOf(URI uri) { - return getType(uri.getScheme()); + /** + * get the enum value from this uri + * + * @param uri to get the scheme value from + * @return the scheme as BroadcastDomainType + */ + public static BroadcastDomainType getSchemeValue(URI uri) { + return toEnumValue(uri.getScheme()); } + /** + * gets the type from a string encoded uri + * + * @param str the uri string + * @return the scheme as BroadcastDomainType + * @throws URISyntaxException when the string can not be converted to URI + */ public static BroadcastDomainType getTypeOf(String str) throws URISyntaxException { - return getTypeOf(new URI(str)); + if (com.cloud.dc.Vlan.UNTAGGED.equalsIgnoreCase(str)) { + return Native; + } + return getSchemeValue(new URI(str)); } - public static BroadcastDomainType getType(String scheme) { + /** + * converts a String to a BroadcastDomainType + * + * @param scheme convert a string representation to a BroacastDomainType + * @return the value of this + */ + public static BroadcastDomainType toEnumValue(String scheme) { if (scheme == null) { return UnDecided; } @@ -125,23 +192,64 @@ public class Networks { return UnDecided; } + /** + * The default implementation of getValueFrom returns the host part of the uri + * + * @param uri to get the value from + * @return the host part as String + */ + public String getValueFrom(URI uri) { + return uri.getHost(); + } + + /** + * get the BroadcastDomain value from an arbitrary URI + * TODO what when the uri is useless + * + * @param uri the uri + * @return depending on the scheme/BroadcastDomainType + */ + public static String getValue(URI uri) { + return getSchemeValue(uri).getValueFrom(uri); + } + + /** + * get the BroadcastDomain value from an arbitrary String + * TODO what when the uriString is useless + * + * @param uriString the string encoded uri + * @return depending on the scheme/BroadcastDomainType + * @throws URISyntaxException the string is not even an uri + */ public static String getValue(String uriString) throws URISyntaxException { return getValue(new URI(uriString)); } - public static String getValue(URI uri) { - BroadcastDomainType type = getTypeOf(uri); - if (type == Vlan) { - // do complicated stuff for backward compatibility + /** + * encode a string into a BroadcastUri + * @param candidate the input string + * @return an URI containing an appropriate (possibly given) scheme and the value + */ + public static URI fromString(String candidate) { + try { + Long.parseLong(candidate); + return Vlan.toUri(candidate); + } catch (NumberFormatException nfe) { + if (com.cloud.dc.Vlan.UNTAGGED.equalsIgnoreCase(candidate)) { + return Native.toUri(candidate); + } try { - Long.parseLong(uri.getSchemeSpecificPart()); - return uri.getSchemeSpecificPart(); - } catch (NumberFormatException e) { - return uri.getHost(); + URI uri = new URI(candidate); + BroadcastDomainType tiep = getSchemeValue(uri); + if (tiep.scheme.equals(uri.getScheme())) { + return uri; + } else { + throw new CloudRuntimeException("string '" + candidate + "' has an unknown BroadcastDomainType."); + } + } catch (URISyntaxException e) { + throw new CloudRuntimeException("string is not a broadcast URI: " + candidate); } - } else { - return uri.getSchemeSpecificPart(); } } }; @@ -188,7 +296,20 @@ public class Networks { public enum IsolationType { None(null, null), Ec2("ec2", String.class), - Vlan("vlan", Integer.class), + Vlan("vlan", Integer.class) { + @Override + public <T> URI toUri(T value) { + try { + if (value.toString().contains(":")) + return new URI(value.toString()); + else + return new URI("vlan", value.toString(), null, null); + } catch (URISyntaxException e) { + throw new CloudRuntimeException( + "Unable to convert to isolation URI: " + value); + } + } + }, Vswitch("vs", String.class), Undecided(null, null), Vnet("vnet", Long.class); @@ -211,15 +332,7 @@ public class Networks { public <T> URI toUri(T value) { try { - // assert(this!=Vlan || - // value.getClass().isAssignableFrom(Integer.class)) : - // do we need to check that value does not contain a scheme - // part? - // "Why are you putting non integer into vlan url"; - if (value.toString().contains(":")) - return new URI(value.toString()); - else - return new URI(scheme, value.toString(), null); + return new URI(scheme + "://" + value.toString()); } catch (URISyntaxException e) { throw new CloudRuntimeException( "Unable to convert to isolation type URI: " + value); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/53d09c6f/api/test/com/cloud/network/NetworksTest.java ---------------------------------------------------------------------- diff --git a/api/test/com/cloud/network/NetworksTest.java b/api/test/com/cloud/network/NetworksTest.java index 31114e8..07b55d2 100644 --- a/api/test/com/cloud/network/NetworksTest.java +++ b/api/test/com/cloud/network/NetworksTest.java @@ -23,6 +23,7 @@ import org.junit.Before; import org.junit.Test; import com.cloud.network.Networks.BroadcastDomainType; +import com.cloud.network.Networks.IsolationType; /** * @author dhoogland @@ -45,7 +46,8 @@ public class NetworksTest { @Test public void vlanBroadcastDomainTypeTest() throws URISyntaxException { String uri1 = "vlan://1"; - String uri2 = "vlan:2"; + Long value2 = 2L; + String uri2 = BroadcastDomainType.Vlan.toUri(value2).toString(); BroadcastDomainType type1 = BroadcastDomainType.getTypeOf(uri1); BroadcastDomainType type2 = BroadcastDomainType.getTypeOf(uri2); String id1 = BroadcastDomainType.getValue(uri1); @@ -55,20 +57,29 @@ public class NetworksTest { Assert.assertEquals("uri2 should be of broadcasttype vlan", BroadcastDomainType.Vlan, type2); Assert.assertEquals("id1 should be \"1\"", "1", id1); - Assert.assertEquals("id1 should be \"2\"", "2", id2); + Assert.assertEquals("id2 should be \"2\"", "2", id2); + } + + @Test + public void vlanIsolationTypeTest() throws URISyntaxException { + String uri1 = "vlan://1"; + Long value2 = 2L; + String uri2 = IsolationType.Vlan.toUri(value2).toString(); + Assert.assertEquals("id1 should be \"vlan://1\"", "vlan://1", uri1); + Assert.assertEquals("id2 should be \"vlan://2\"", "vlan://2", uri2); } @Test public void otherTypesTest() throws URISyntaxException { String bogeyUri = "lswitch://1"; - String uri2 = "mido:2"; + String uri2 = "mido://2"; BroadcastDomainType type1 = BroadcastDomainType.getTypeOf(bogeyUri); BroadcastDomainType type2 = BroadcastDomainType.getTypeOf(uri2); String id1 = BroadcastDomainType.getValue(bogeyUri); String id2 = BroadcastDomainType.getValue(uri2); - Assert.assertEquals("uri1 should be of broadcasttype vlan", + Assert.assertEquals("uri1 should be of broadcasttype lswitch", BroadcastDomainType.Lswitch, type1); - Assert.assertEquals("uri2 should be of broadcasttype vlan", + Assert.assertEquals("uri2 should be of broadcasttype mido", BroadcastDomainType.Mido, type2); Assert.assertEquals("id1 should be \"//1\"", "//1", id1); Assert.assertEquals("id1 should be \"2\"", "2", id2);
