40
40
import org .dasein .cloud .network .VLANState ;
41
41
import org .dasein .cloud .openstack .nova .os .NovaMethod ;
42
42
import org .dasein .cloud .openstack .nova .os .NovaOpenStack ;
43
+ import org .dasein .cloud .openstack .nova .os .OpenStackProvider ;
43
44
import org .dasein .cloud .util .APITrace ;
45
+ import org .dasein .cloud .util .Cache ;
46
+ import org .dasein .cloud .util .CacheLevel ;
44
47
import org .json .JSONArray ;
45
48
import org .json .JSONException ;
46
49
import org .json .JSONObject ;
47
50
48
51
import javax .annotation .Nonnull ;
49
52
import javax .annotation .Nullable ;
50
53
import java .util .ArrayList ;
54
+ import java .util .Collections ;
51
55
import java .util .HashMap ;
56
+ import java .util .Iterator ;
52
57
import java .util .Locale ;
53
58
import java .util .Map ;
54
59
@@ -66,13 +71,80 @@ public Quantum(@Nonnull NovaOpenStack provider) {
66
71
super (provider );
67
72
}
68
73
74
+ private enum QuantumType {
75
+ NONE , RACKSPACE , NOVA , QUANTUM ;
76
+
77
+ public String getNetworkResource () {
78
+ switch ( this ) {
79
+ case QUANTUM : return "/networks" ;
80
+ case RACKSPACE : return "/os-networksv2" ;
81
+ case NOVA : return "/os-networks" ;
82
+ }
83
+ return "/networks" ;
84
+ }
85
+
86
+ public String getSubnetResource () {
87
+ switch ( this ) {
88
+ case QUANTUM : return "/subnets" ;
89
+ }
90
+ return "/subnets" ;
91
+ }
92
+ }
93
+
94
+ private QuantumType getNetworkType () throws CloudException , InternalException {
95
+ Cache <QuantumType > cache = Cache .getInstance (getProvider (), "quantumness" , QuantumType .class , CacheLevel .CLOUD );
96
+
97
+ Iterable <QuantumType > it = cache .get (getContext ());
98
+
99
+ if ( it != null ) {
100
+ Iterator <QuantumType > b = it .iterator ();
101
+
102
+ if ( b .hasNext () ) {
103
+ return b .next ();
104
+ }
105
+ }
106
+ try {
107
+ if ( ((NovaOpenStack )getProvider ()).getCloudProvider ().equals (OpenStackProvider .RACKSPACE ) ) {
108
+ cache .put (getContext (), Collections .singletonList (QuantumType .RACKSPACE ));
109
+ return QuantumType .RACKSPACE ;
110
+ }
111
+ NovaMethod method = new NovaMethod ((NovaOpenStack )getProvider ());
112
+ try {
113
+ JSONObject ob = method .getServers ("/networks" , null , false );
114
+
115
+ if ( ob != null && ob .has ("networks" ) ) {
116
+ cache .put (getContext (), Collections .singletonList (QuantumType .QUANTUM ));
117
+ return QuantumType .QUANTUM ;
118
+ }
119
+ }
120
+ catch ( Throwable ignore ) {
121
+ // ignore
122
+ }
123
+ try {
124
+ JSONObject ob = method .getServers ("/os-networks" , null , false );
125
+
126
+ if ( ob != null && ob .has ("networks" ) ) {
127
+ cache .put (getContext (), Collections .singletonList (QuantumType .NOVA ));
128
+ return QuantumType .NOVA ;
129
+ }
130
+ }
131
+ catch ( Throwable ignore ) {
132
+ // ignore
133
+ }
134
+ return QuantumType .NONE ;
135
+ }
136
+ finally {
137
+ APITrace .end ();
138
+ }
139
+ }
140
+
69
141
private @ Nonnull String getTenantId () throws CloudException , InternalException {
70
142
return ((NovaOpenStack )getProvider ()).getAuthenticationContext ().getTenantId ();
71
143
}
72
144
73
145
@ Override
74
146
public boolean allowsNewSubnetCreation () throws CloudException , InternalException {
75
- return !(( NovaOpenStack ) getProvider ()). isRackspace ( );
147
+ return getNetworkType (). equals ( QuantumType . QUANTUM );
76
148
}
77
149
78
150
@ Override
@@ -115,7 +187,7 @@ public boolean allowsMultipleTrafficTypesOverVlan() throws CloudException, Inter
115
187
116
188
wrapper .put ("port" , json );
117
189
118
- JSONObject result = method .postServers ("/networks /" + subnet .getProviderVlanId () + "/ports" , null , new JSONObject (wrapper ), false );
190
+ JSONObject result = method .postServers (getNetworkResource () + " /" + subnet .getProviderVlanId () + "/ports" , null , new JSONObject (wrapper ), false );
119
191
120
192
if ( result != null && result .has ("port" ) ) {
121
193
try {
@@ -177,7 +249,7 @@ else if( versions[0].equals(IPVersion.IPV6) ) {
177
249
178
250
wrapper .put ("subnet" , json );
179
251
180
- JSONObject result = method .postServers ("/subnets" , null , new JSONObject (wrapper ), false );
252
+ JSONObject result = method .postServers (getSubnetResource () , null , new JSONObject (wrapper ), false );
181
253
182
254
if ( result != null && result .has ("subnet" ) ) {
183
255
try {
@@ -264,13 +336,12 @@ public int getMaxVlanCount() throws CloudException, InternalException {
264
336
return -2 ;
265
337
}
266
338
267
- private @ Nonnull String getNetworkResource () {
268
- if ( ((NovaOpenStack )getProvider ()).isRackspace () ) {
269
- return "/os-networksv2" ;
270
- }
271
- else {
272
- return "/networks" ;
273
- }
339
+ private @ Nonnull String getNetworkResource () throws CloudException , InternalException {
340
+ return getNetworkType ().getNetworkResource ();
341
+ }
342
+
343
+ private @ Nonnull String getSubnetResource () throws CloudException , InternalException {
344
+ return getNetworkType ().getSubnetResource ();
274
345
}
275
346
276
347
@ Override
@@ -292,8 +363,11 @@ public int getMaxVlanCount() throws CloudException, InternalException {
292
363
public Subnet getSubnet (@ Nonnull String subnetId ) throws CloudException , InternalException {
293
364
APITrace .begin (getProvider (), "VLAN.getSubnet" );
294
365
try {
366
+ if ( !getNetworkType ().equals (QuantumType .QUANTUM ) ) {
367
+ return null ;
368
+ }
295
369
NovaMethod method = new NovaMethod ((NovaOpenStack )getProvider ());
296
- JSONObject ob = method .getServers ("/subnets" , subnetId , false );
370
+ JSONObject ob = method .getServers (getSubnetResource () , subnetId , false );
297
371
298
372
try {
299
373
if ( ob != null && ob .has ("subnet" ) ) {
@@ -317,8 +391,8 @@ public Subnet getSubnet(@Nonnull String subnetId) throws CloudException, Interna
317
391
}
318
392
319
393
@ Override
320
- public @ Nonnull Requirement getSubnetSupport () {
321
- return ((( NovaOpenStack ) getProvider ()). isRackspace ( ) ? Requirement .NONE : Requirement .REQUIRED );
394
+ public @ Nonnull Requirement getSubnetSupport () throws CloudException , InternalException {
395
+ return (getNetworkType (). equals ( QuantumType . QUANTUM ) ? Requirement .REQUIRED : Requirement .NONE );
322
396
}
323
397
324
398
@ Override
@@ -400,8 +474,11 @@ public boolean isVlanDataCenterConstrained() throws CloudException, InternalExce
400
474
public @ Nonnull Iterable <Subnet > listSubnets (@ Nonnull String inVlanId ) throws CloudException , InternalException {
401
475
APITrace .begin (getProvider (), "VLAN.listSubnets" );
402
476
try {
477
+ if ( !getNetworkType ().equals (QuantumType .QUANTUM ) ) {
478
+ return Collections .emptyList ();
479
+ }
403
480
NovaMethod method = new NovaMethod ((NovaOpenStack )getProvider ());
404
- JSONObject ob = method .getServers ("/subnets" , null , false );
481
+ JSONObject ob = method .getServers (getSubnetResource () , null , false );
405
482
ArrayList <Subnet > subnets = new ArrayList <Subnet >();
406
483
407
484
try {
@@ -516,9 +593,12 @@ public boolean isVlanDataCenterConstrained() throws CloudException, InternalExce
516
593
public void removeSubnet (String subnetId ) throws CloudException , InternalException {
517
594
APITrace .begin (getProvider (), "VLAN.removeSubnet" );
518
595
try {
596
+ if ( !getNetworkType ().equals (QuantumType .QUANTUM ) ) {
597
+ throw new OperationNotSupportedException ("Cannot remove subnets in an OpenStack network of type: " + getNetworkType ());
598
+ }
519
599
NovaMethod method = new NovaMethod ((NovaOpenStack )getProvider ());
520
600
521
- method .deleteServers ("/subnets" , subnetId );
601
+ method .deleteServers (getSubnetResource () , subnetId );
522
602
}
523
603
finally {
524
604
APITrace .end ();
0 commit comments