Some rework stratosphere ssp plugin

* add missing command entry in commands.properties
* migrate httpclient 3.x to 4.x
* fix the broken SspClient
* add webapp session checking in mock ssp server
This commit is contained in:
Hiroaki KAWAI 2014-01-30 20:24:57 +09:00 committed by U-kawai-x230\kawai
parent 1e9f066df2
commit db3dc2ee17
5 changed files with 207 additions and 219 deletions

View File

@ -587,6 +587,11 @@ addBigSwitchVnsDevice=1
deleteBigSwitchVnsDevice=1
listBigSwitchVnsDevices=1
#### stratosphere ssp commands
addStratosphereSsp=1
deleteStratoshereSsp=1
#### host simulator commands
configureSimulator=1

View File

@ -17,26 +17,33 @@
package org.apache.cloudstack.network.element;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
/**
@ -44,117 +51,75 @@ import com.google.gson.annotations.SerializedName;
*/
public class SspClient {
private static final Logger s_logger = Logger.getLogger(SspClient.class);
private static final HttpConnectionManager s_httpclient_manager = new MultiThreadedHttpConnectionManager();
private static final HttpClientParams s_httpclient_params = new HttpClientParams();
private static final HttpClient s_client = new DefaultHttpClient(
new PoolingClientConnectionManager());
static {
s_httpclient_params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
s_client.getParams()
.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY)
.setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
}
private final String apiUrl;
private final String username;
private final String password;
protected HttpClient client;
protected PostMethod postMethod;
protected DeleteMethod deleteMethod;
protected PutMethod putMethod;
public SspClient(String apiUrl, String username, String password) {
super();
this.apiUrl = apiUrl;
this.username = username;
this.password = password;
client = new HttpClient(s_httpclient_params, s_httpclient_manager);
postMethod = new PostMethod(apiUrl);
deleteMethod = new DeleteMethod(apiUrl);
putMethod = new PutMethod(apiUrl);
}
public boolean login(){
PostMethod method = postMethod;
method.setPath("/ws.v1/login"); // NOTE: /ws.v1/login is correct
method.addParameter("username", username);
method.addParameter("password", password);
protected HttpClient getHttpClient() { // for mock test
return s_client;
}
private HttpResponse innerExecuteMethod(HttpRequestBase req, String path) {
try {
client.executeMethod(method);
} catch (HttpException e) {
s_logger.info("Login "+username+" to "+apiUrl+" failed", e);
return false;
URI base = new URI(apiUrl);
req.setURI(new URI(base.getScheme(), base.getUserInfo(), base.getHost(),
base.getPort(), path, null, null));
} catch (URISyntaxException e) {
s_logger.error("invalid API URL " + apiUrl + " path " + path, e);
return null;
}
HttpResponse res = null;
try {
res = getHttpClient().execute(req);
s_logger.info("ssp api call:" + req + " status=" + res.getStatusLine());
} catch (IOException e) {
s_logger.info("Login "+username+" to "+apiUrl+" failed", e);
return false;
} finally {
method.releaseConnection();
s_logger.error("ssp api call failed: " + req, e);
}
String apiCallPath = null;
return res;
}
private HttpResponse executeMethod(HttpRequestBase req, String path) {
HttpResponse res = innerExecuteMethod(req, path);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) {
req.reset();
res = innerExecuteMethod(req, path);
}
return res;
}
public boolean login() {
HttpPost method = new HttpPost();
try {
apiCallPath = method.getName() + " " + method.getURI().toString();
} catch (URIException e) {
s_logger.error("method getURI failed", e);
method.setEntity(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("username", username),
new BasicNameValuePair("password", password))));
} catch (UnsupportedEncodingException e) {
s_logger.error("invalid username or password", e);
return false;
}
s_logger.info("ssp api call:" + apiCallPath + " user="+username+" status="+method.getStatusLine());
if(method.getStatusCode() == HttpStatus.SC_OK){
HttpResponse res = this.innerExecuteMethod(method, "/ws.v1/login");
if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return true;
}
return false;
}
private String executeMethod(HttpMethod method){
String apiCallPath = null;
try {
apiCallPath = method.getName() + " " + method.getURI().toString();
} catch (URIException e) {
s_logger.error("method getURI failed", e);
}
String response = null;
try {
client.executeMethod(method);
response = method.getResponseBodyAsString();
} catch (HttpException e) {
s_logger.error("ssp api call failed "+apiCallPath, e);
return null;
} catch (IOException e) {
s_logger.error("ssp api call failed "+apiCallPath, e);
return null;
} finally {
method.releaseConnection();
}
if(method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED){
if(!login()){
return null;
}
try {
client.executeMethod(method);
response = method.getResponseBodyAsString();
} catch (HttpException e) {
s_logger.error("ssp api call failed "+apiCallPath, e);
return null;
} catch (IOException e) {
s_logger.error("ssp api call failed "+apiCallPath, e);
return null;
} finally {
method.releaseConnection();
}
}
s_logger.info("ssp api call:" + apiCallPath + " user="+username+" status="+method.getStatusLine());
if(method instanceof EntityEnclosingMethod){
EntityEnclosingMethod emethod = (EntityEnclosingMethod)method;
RequestEntity reqEntity = emethod.getRequestEntity();
if(reqEntity instanceof StringRequestEntity){
StringRequestEntity strReqEntity = (StringRequestEntity)reqEntity;
s_logger.debug("ssp api request body:"+strReqEntity.getContent());
}else{
s_logger.debug("ssp api request body:"+emethod.getRequestEntity());
}
}
s_logger.debug("ssp api response body:" + response);
return response;
}
public class TenantNetwork {
public String uuid;
public String name;
@ -162,35 +127,36 @@ public class SspClient {
public String tenantUuid;
}
public TenantNetwork createTenantNetwork(String tenantUuid, String networkName){
public TenantNetwork createTenantNetwork(String tenantUuid, String networkName) {
TenantNetwork req = new TenantNetwork();
req.name = networkName;
req.tenantUuid = tenantUuid;
PostMethod method = postMethod;
method.setPath("/ssp.v1/tenant-networks");
StringRequestEntity entity = null;
try {
entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
} catch (UnsupportedEncodingException e) {
s_logger.error("failed creating http request body", e);
HttpPost method = new HttpPost();
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks");
if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
return null;
}
method.setRequestEntity(entity);
String response = executeMethod(method);
if(response != null && method.getStatusCode() == HttpStatus.SC_CREATED){
return new Gson().fromJson(response, TenantNetwork.class);
try {
return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
TenantNetwork.class);
} catch (JsonSyntaxException e) {
s_logger.error("reading response body failed", e);
} catch (JsonIOException e) {
s_logger.error("reading response body failed", e);
} catch (IllegalStateException e) {
s_logger.error("reading response body failed", e);
} catch (IOException e) {
s_logger.error("reading response body failed", e);
}
return null;
}
public boolean deleteTenantNetwork(String tenantNetworkUuid){
DeleteMethod method = deleteMethod;
method.setPath("/ssp.v1/tenant-networks/"+tenantNetworkUuid);
executeMethod(method);
if(method.getStatusCode() == HttpStatus.SC_NO_CONTENT){
public boolean deleteTenantNetwork(String tenantNetworkUuid) {
HttpDelete method = new HttpDelete();
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks/" + tenantNetworkUuid);
if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
return true;
}
return false;
@ -209,63 +175,69 @@ public class SspClient {
public Integer vlanId;
}
public TenantPort createTenantPort(String tenantNetworkUuid){
public TenantPort createTenantPort(String tenantNetworkUuid) {
TenantPort req = new TenantPort();
req.networkUuid = tenantNetworkUuid;
req.attachmentType = "NoAttachment";
PostMethod method = postMethod;
method.setPath("/ssp.v1/tenant-ports");
StringRequestEntity entity = null;
try {
entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
} catch (UnsupportedEncodingException e) {
s_logger.error("failed creating http request body", e);
HttpPost method = new HttpPost();
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports");
if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
return null;
}
method.setRequestEntity(entity);
String response = executeMethod(method);
if(response != null && method.getStatusCode() == HttpStatus.SC_CREATED){
return new Gson().fromJson(response, TenantPort.class);
try {
return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
TenantPort.class);
} catch (JsonSyntaxException e) {
s_logger.error("reading response body failed", e);
} catch (JsonIOException e) {
s_logger.error("reading response body failed", e);
} catch (IllegalStateException e) {
s_logger.error("reading response body failed", e);
} catch (IOException e) {
s_logger.error("reading response body failed", e);
}
return null;
}
public boolean deleteTenantPort(String tenantPortUuid){
DeleteMethod method = deleteMethod;
method.setPath("/ssp.v1/tenant-ports/"+tenantPortUuid);
public boolean deleteTenantPort(String tenantPortUuid) {
HttpDelete method = new HttpDelete();
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + tenantPortUuid);
executeMethod(method);
if(method.getStatusCode() == HttpStatus.SC_NO_CONTENT){
if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
return true;
}
return false;
}
public TenantPort updateTenantVifBinding(String portUuid, String hypervisorIpAddress){
public TenantPort updateTenantVifBinding(String portUuid, String hypervisorIpAddress) {
TenantPort req = new TenantPort();
if(hypervisorIpAddress != null){
if (hypervisorIpAddress != null) {
req.attachmentType = "VifAttachment";
req.hypervisorIpAddress = hypervisorIpAddress;
}else{
} else {
req.attachmentType = "NoAttachment";
}
PutMethod method = putMethod;
method.setPath("/ssp.v1/tenant-ports/"+portUuid);
StringRequestEntity entity = null;
try {
entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
} catch (UnsupportedEncodingException e) {
s_logger.error("failed creating http request body", e);
HttpPut method = new HttpPut();
method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + portUuid);
if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
return null;
}
method.setRequestEntity(entity);
String response = executeMethod(method);
if(response != null && method.getStatusCode() == HttpStatus.SC_OK){
return new Gson().fromJson(response, TenantPort.class);
try {
return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
TenantPort.class);
} catch (JsonSyntaxException e) {
s_logger.error("reading response body failed", e);
} catch (JsonIOException e) {
s_logger.error("reading response body failed", e);
} catch (IllegalStateException e) {
s_logger.error("reading response body failed", e);
} catch (IOException e) {
s_logger.error("reading response body failed", e);
}
return null;
}

View File

@ -189,14 +189,10 @@ public class SspElement extends AdapterBase implements ConnectivityProvider, Ssp
public boolean isReady(PhysicalNetworkServiceProvider provider) {
PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(provider.getPhysicalNetworkId());
assert(physicalNetwork!=null);
if(physicalNetwork != null){
if(fetchSspClients(physicalNetwork.getId(), physicalNetwork.getDataCenterId(), false).size() > 0){
return true;
}
s_logger.warn("Ssp api endpoint not found. "+physicalNetwork.toString());
}else{
s_logger.warn("PhysicalNetwork is NULL.");
if(fetchSspClients(physicalNetwork.getId(), physicalNetwork.getDataCenterId(), false).size() > 0){
return true;
}
s_logger.warn("Ssp api endpoint not found. "+physicalNetwork.toString());
return false;
}

View File

@ -17,7 +17,8 @@
import json
import uuid
from flask import Flask
from flask import Flask,request,make_response
from beaker.middleware import SessionMiddleware
app = Flask(__name__)
tenant_networks = []
@ -25,50 +26,59 @@ tenant_ports = []
@app.route("/ws.v1/login", methods=["POST",])
def login():
response.content_type = "application/json"
return ""
assert "username" in request.form
assert "password" in request.form
request.environ["beaker.session"]["login"] = True
res = make_response("", 200)
res.headers["Content-type"] = "application/json"
return res
@app.route("/ssp.v1/tenant-networks", methods=["POST",])
def create_tenant_network():
response.content_type = "application/json"
response.status = 201
if "login" not in request.environ["beaker.session"]:
return make_response("", 401)
obj = request.json
obj["uuid"] = str(uuid.uuid1())
tenant_networks.append(obj)
return json.dumps(obj)
res = make_response(json.dumps(obj), 201)
res.headers["Content-type"] = "application/json"
return res
@app.route("/ssp.v1/tenant-networks/<tenant_net_uuid>", methods=["DELETE",])
def delete_tenant_network(tenant_net_uuid):
if "login" not in request.environ["beaker.session"]:
return make_response("", 401)
for net in tenant_networks:
if net["uuid"] == tenant_net_uuid:
tenant_networks.remove(net)
response.status = 204
return ""
response.status = 404
return ""
return make_response("", 204)
return make_response("", 404)
@app.route("/ssp.v1/tenant-ports", methods=["POST",])
def create_tenant_port():
response.content_type = "application/json"
response.status = 201
if "login" not in request.environ["beaker.session"]:
return make_response("", 401)
obj = request.json
obj["uuid"] = str(uuid.uuid1())
tenant_ports.append(obj)
return json.dumps(obj)
res = make_response(json.dumps(obj), 201)
res.headers["Content-type"] = "application/json"
return res
@app.route("/ssp.v1/tenant-ports/<tenant_port_uuid>", methods=["DELETE",])
def delete_tenant_port(tenant_port_uuid):
if "login" not in request.environ["beaker.session"]:
return make_response("", 401)
for port in tenant_ports:
if port["uuid"] == tenant_port_uuid:
tenant_ports.remove(port)
response.status = 204
return ""
response.status = 404
return ""
return make_response("", 204)
return make_response("", 404)
@app.route("/ssp.v1/tenant-ports/<tenant_port_uuid>", methods=["PUT",])
def update_tenant_port(tenant_port_uuid):
response.content_type = "application/json"
if "login" not in request.environ["beaker.session"]:
return make_response("", 401)
for port in tenant_ports:
if port["uuid"] == tenant_port_uuid:
obj = request.json
@ -76,10 +86,14 @@ def update_tenant_port(tenant_port_uuid):
obj["vlan_id"] = 100
tenant_ports.remove(port)
tenant_ports.append(obj)
response.status = 200
return json.dumps(obj)
response.status = 404
return ""
res = make_response(json.dumps(obj), 200)
res.headers["Content-type"] = "application/json"
return res
return make_response("", 404)
if __name__=="__main__":
app.wsgi_app = SessionMiddleware(app.wsgi_app, {
"session.auto":True,
"session.type":"cookie",
"session.validate_key":"hoge"})
app.run(host="0.0.0.0", port=9080, debug=True)

View File

@ -16,49 +16,41 @@
// under the License.
package org.apache.cloudstack.network.element;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import java.io.ByteArrayInputStream;
import java.util.UUID;
import org.apache.cloudstack.network.element.SspClient;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class SspClientTest {
HttpClient _client = mock(HttpClient.class);
PostMethod _postMethod = mock(PostMethod.class);
PutMethod _putMethod = mock(PutMethod.class);
DeleteMethod _deleteMethod = mock(DeleteMethod.class);
String uuid = UUID.randomUUID().toString();
String apiUrl = "http://a.example.jp/";
String username = "foo";
String password = "bar";
SspClient sspClient = new SspClient(apiUrl, username, password){
{
client = _client;
postMethod = _postMethod;
putMethod = _putMethod;
deleteMethod = _deleteMethod;
}
};
@SuppressWarnings("deprecation")
private URI getUri() throws Exception{
return new URI(apiUrl);
}
@Test
public void loginTest() throws Exception {
when(_postMethod.getURI()).thenReturn(getUri());
when(_postMethod.getStatusCode()).thenReturn(HttpStatus.SC_OK);
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
HttpClient client = mock(HttpClient.class);
HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
doReturn(client).when(sspClient).getHttpClient();
when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
assertTrue(sspClient.login());
assertTrue(sspClient.login());
assertTrue(sspClient.login());
@ -68,13 +60,18 @@ public class SspClientTest {
public void createNetworkTest() throws Exception {
String networkName = "example network 1";
String tenant_net_uuid = UUID.randomUUID().toString();
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
HttpClient client = mock(HttpClient.class);
HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
doReturn(client).when(sspClient).getHttpClient();
when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
String body = "{\"uuid\":\"" + tenant_net_uuid + "\",\"name\":\"" + networkName
+ "\",\"tenant_uuid\":\"" + uuid + "\"}";
when(res.getEntity().getContent()).thenReturn(
new ByteArrayInputStream(body.getBytes("UTF-8")));
when(_postMethod.getURI()).thenReturn(getUri());
when(_postMethod.getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
when(_postMethod.getResponseBodyAsString()).thenReturn(
"{\"uuid\":\""+tenant_net_uuid+
"\",\"name\":\""+networkName+
"\",\"tenant_uuid\":\""+uuid+"\"}");
SspClient.TenantNetwork tnet = sspClient.createTenantNetwork(uuid, networkName);
assertEquals(tnet.name, networkName);
assertEquals(tnet.uuid, tenant_net_uuid);
@ -84,9 +81,13 @@ public class SspClientTest {
@Test
public void deleteNetworkTest() throws Exception {
String tenant_net_uuid = UUID.randomUUID().toString();
SspClient sspClient = spy(new SspClient(apiUrl, username, password));
when(_deleteMethod.getURI()).thenReturn(getUri());
when(_deleteMethod.getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
HttpClient client = mock(HttpClient.class);
HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
doReturn(client).when(sspClient).getHttpClient();
when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
sspClient.deleteTenantNetwork(tenant_net_uuid);
}