2323
2424/**
2525 * Network includes all of the enums used within networking.
26- *
26+ *
2727 */
2828public class Networks {
2929
@@ -66,33 +66,82 @@ public enum BroadcastDomainType {
6666 Pvlan ("pvlan" , String .class ),
6767 UnDecided (null , null );
6868
69- private String scheme ;
70- private Class <?> type ;
69+ private final String scheme ;
70+ private final Class <?> type ;
7171
7272 private BroadcastDomainType (String scheme , Class <?> type ) {
7373 this .scheme = scheme ;
7474 this .type = type ;
7575 }
7676
7777 /**
78- * @return scheme to be used in broadcast uri. Null indicates that this type does not have broadcast tags.
78+ * @return scheme to be used in broadcast uri. Null indicates that this
79+ * type does not have broadcast tags.
7980 */
8081 public String scheme () {
8182 return scheme ;
8283 }
8384
8485 /**
85- * @return type of the value in the broadcast uri. Null indicates that this type does not have broadcast tags.
86+ * @return type of the value in the broadcast uri. Null indicates that
87+ * this type does not have broadcast tags.
8688 */
8789 public Class <?> type () {
8890 return type ;
8991 }
9092
9193 public <T > URI toUri (T value ) {
9294 try {
93- return new URI (scheme + "://" + value );
95+ // do we need to check that value does not contain a scheme
96+ // part?
97+ if (value .toString ().contains (":" ))
98+ return new URI (value .toString ());
99+ else
100+ return new URI (scheme , value .toString (), null );
94101 } catch (URISyntaxException e ) {
95- throw new CloudRuntimeException ("Unable to convert to broadcast URI: " + value );
102+ throw new CloudRuntimeException (
103+ "Unable to convert to broadcast URI: " + value );
104+ }
105+ }
106+
107+ public static BroadcastDomainType getTypeOf (URI uri ) {
108+ return getType (uri .getScheme ());
109+ }
110+
111+ public static BroadcastDomainType getTypeOf (String str )
112+ throws URISyntaxException {
113+ return getTypeOf (new URI (str ));
114+ }
115+
116+ public static BroadcastDomainType getType (String scheme ) {
117+ if (scheme == null ) {
118+ return UnDecided ;
119+ }
120+ for (BroadcastDomainType type : values ()) {
121+ if (scheme .equalsIgnoreCase (type .scheme ())) {
122+ return type ;
123+ }
124+ }
125+ return UnDecided ;
126+ }
127+
128+ public static String getValue (String uriString )
129+ throws URISyntaxException {
130+ return getValue (new URI (uriString ));
131+ }
132+
133+ public static String getValue (URI uri ) {
134+ BroadcastDomainType type = getTypeOf (uri );
135+ if (type == Vlan ) {
136+ // do complicated stuff for backward compatibility
137+ try {
138+ Long .parseLong (uri .getSchemeSpecificPart ());
139+ return uri .getSchemeSpecificPart ();
140+ } catch (NumberFormatException e ) {
141+ return uri .getHost ();
142+ }
143+ } else {
144+ return uri .getSchemeSpecificPart ();
96145 }
97146 }
98147 };
@@ -110,8 +159,7 @@ public enum TrafficType {
110159 Vpn ;
111160
112161 public static boolean isSystemNetwork (TrafficType trafficType ) {
113- if (Storage .equals (trafficType )
114- || Management .equals (trafficType )
162+ if (Storage .equals (trafficType ) || Management .equals (trafficType )
115163 || Control .equals (trafficType )) {
116164 return true ;
117165 }
@@ -163,11 +211,18 @@ public Class<?> type() {
163211
164212 public <T > URI toUri (T value ) {
165213 try {
166- // assert(this!=Vlan || value.getClass().isAssignableFrom(Integer.class)) :
214+ // assert(this!=Vlan ||
215+ // value.getClass().isAssignableFrom(Integer.class)) :
216+ // do we need to check that value does not contain a scheme
217+ // part?
167218 // "Why are you putting non integer into vlan url";
168- return new URI (scheme + "://" + value .toString ());
219+ if (value .toString ().contains (":" ))
220+ return new URI (value .toString ());
221+ else
222+ return new URI (scheme , value .toString (), null );
169223 } catch (URISyntaxException e ) {
170- throw new CloudRuntimeException ("Unable to convert to isolation type URI: " + value );
224+ throw new CloudRuntimeException (
225+ "Unable to convert to isolation type URI: " + value );
171226 }
172227 }
173228 }
@@ -176,7 +231,7 @@ public enum BroadcastScheme {
176231 Vlan ("vlan" ),
177232 VSwitch ("vswitch" );
178233
179- private String scheme ;
234+ private final String scheme ;
180235
181236 private BroadcastScheme (String scheme ) {
182237 this .scheme = scheme ;
0 commit comments