Skip to content

Commit

Permalink
Merge pull request #18 from fogbow/develop
Browse files Browse the repository at this point in the history
Removing KeystoneApllication from Main class
  • Loading branch information
abmargb committed Apr 15, 2014
2 parents e4bf04a + 6ff0ac5 commit 9c72b8e
Show file tree
Hide file tree
Showing 34 changed files with 1,813 additions and 190 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<artifactId>httpclient</artifactId>
<version>4.0-alpha4</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
</project>
94 changes: 53 additions & 41 deletions src/main/java/org/fogbowcloud/manager/occi/OCCIApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import org.apache.log4j.Logger;
import org.fogbowcloud.manager.occi.core.Category;
import org.fogbowcloud.manager.occi.core.ErrorType;
import org.fogbowcloud.manager.occi.core.Resource;
import org.fogbowcloud.manager.occi.core.OCCIException;
import org.fogbowcloud.manager.occi.core.ResponseConstants;
import org.fogbowcloud.manager.occi.plugins.ComputePlugin;
import org.fogbowcloud.manager.occi.plugins.IdentityPlugin;
import org.fogbowcloud.manager.occi.request.RequestState;
import org.fogbowcloud.manager.occi.request.Request;
import org.fogbowcloud.manager.occi.request.RequestAttribute;
import org.fogbowcloud.manager.occi.request.RequestState;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;
Expand All @@ -25,13 +25,13 @@ public class OCCIApplication extends Application {
private IdentityPlugin identityPlugin;
private ComputePlugin computePlugin;
private Map<String, List<String>> userToRequestIds;
private Map<String, Request> requestIdToRequestUnit;
private Map<String, Request> requestIdToRequest;

private static final Logger LOGGER = Logger.getLogger(OCCIApplication.class);

public OCCIApplication() {
this.userToRequestIds = new ConcurrentHashMap<String, List<String>>();
this.requestIdToRequestUnit = new ConcurrentHashMap<String, Request>();
this.requestIdToRequest = new ConcurrentHashMap<String, Request>();
}

@Override
Expand All @@ -50,6 +50,7 @@ public ComputePlugin getComputePlugin() {
return computePlugin;
}

// TODO It is really needed?
public Map<String, List<String>> getUserToRequestIds() {
return userToRequestIds;
}
Expand All @@ -62,78 +63,89 @@ public IdentityPlugin getIdentityPlugin() {
return identityPlugin;
}

public Request getRequestDetails(String userToken, String requestId) {
checkUserToken(userToken);
checkRequestId(userToken, requestId);
return requestIdToRequestUnit.get(requestId);
public Request getRequestDetails(String authToken, String requestId) {
checkUserToken(authToken);
checkRequestId(authToken, requestId);
return requestIdToRequest.get(requestId);
}

public Request newRequest(String userToken, List<Category> categories,
public Request newRequest(String authToken, List<Category> categories,
Map<String, String> xOCCIAtt) {
checkUserToken(userToken);
checkUserToken(authToken);

String user = getIdentityPlugin().getUser(authToken);

if (userToRequestIds.get(userToken) == null) {
userToRequestIds.put(userToken, new ArrayList<String>());
if (userToRequestIds.get(user) == null) {
userToRequestIds.put(user, new ArrayList<String>());
}
String requestId = String.valueOf(UUID.randomUUID());
Request request = new Request(requestId, "", RequestState.OPEN,
categories, xOCCIAtt);
Request request = new Request(requestId, "", RequestState.OPEN, categories, xOCCIAtt);

userToRequestIds.get(userToken).add(request.getId());
requestIdToRequestUnit.put(request.getId(), request);
userToRequestIds.get(user).add(request.getId());
requestIdToRequest.put(request.getId(), request);

submitRequest(request, categories, xOCCIAtt);
submitRequest(authToken, request, categories, xOCCIAtt);

return request;
}

// FIXME Should req be an attribute of requestUnit?
private void submitRequest(Request request, List<Category> categories,
private void submitRequest(String authToken, Request request, List<Category> categories,
Map<String, String> xOCCIAtt) {
// TODO Choose if submit to local or remote cloud and submit
computePlugin.requestInstance(categories, xOCCIAtt);

// TODO remove fogbow attributes from xOCCIAtt
for (String keyAttributes : RequestAttribute.getValues()) {
xOCCIAtt.remove(keyAttributes);
}

computePlugin.requestInstance(authToken, categories, xOCCIAtt);
}

public List<Request> getRequestsFromUser(String userToken) {
checkUserToken(userToken);
public List<Request> getRequestsFromUser(String authToken) {
checkUserToken(authToken);
String user = getIdentityPlugin().getUser(authToken);

List<Request> requests = new ArrayList<Request>();
if (userToRequestIds.get(userToken) != null) {
for (String requestId : userToRequestIds.get(userToken)) {
requests.add(requestIdToRequestUnit.get(requestId));
if (userToRequestIds.get(user) != null) {
for (String requestId : userToRequestIds.get(user)) {
requests.add(requestIdToRequest.get(requestId));
}
}
return requests;
}

public void removeAllRequests(String userToken) {
checkUserToken(userToken);
public void removeAllRequests(String authToken) {
checkUserToken(authToken);
String user = getIdentityPlugin().getUser(authToken);

if (userToRequestIds.get(userToken) != null) {
for (String requestId : userToRequestIds.get(userToken)) {
requestIdToRequestUnit.remove(requestId);
if (userToRequestIds.get(user) != null) {
for (String requestId : userToRequestIds.get(user)) {
requestIdToRequest.remove(requestId);
}
userToRequestIds.remove(userToken);
userToRequestIds.remove(user);
}
}

public void removeRequest(String userToken, String requestId) {
checkUserToken(userToken);
checkRequestId(userToken, requestId);
public void removeRequest(String authToken, String requestId) {
checkUserToken(authToken);
checkRequestId(authToken, requestId);
String user = getIdentityPlugin().getUser(authToken);

userToRequestIds.get(userToken).remove(requestId);
requestIdToRequestUnit.get(requestId);
userToRequestIds.get(user).remove(requestId);
requestIdToRequest.remove(requestId);
}

private void checkRequestId(String userToken, String requestId) {
if (userToRequestIds.get(userToken) == null
|| !userToRequestIds.get(userToken).contains(requestId)) {
private void checkRequestId(String authToken, String requestId) {
String user = getIdentityPlugin().getUser(authToken);

if (userToRequestIds.get(user) == null || !userToRequestIds.get(user).contains(requestId)) {
throw new OCCIException(ErrorType.NOT_FOUND, ResponseConstants.NOT_FOUND);
}
}

private void checkUserToken(String userToken) {
if (!identityPlugin.isValidToken(userToken)) {
private void checkUserToken(String authToken) {
if (!identityPlugin.isValidToken(authToken)) {
throw new OCCIException(ErrorType.UNAUTHORIZED, ResponseConstants.UNAUTHORIZED);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
public class RequestServerResource extends ServerResource {

private static final Logger LOGGER = Logger.getLogger(RequestServerResource.class);

@Get
public String fetch() {
OCCIApplication application = (OCCIApplication) getApplication();
HttpRequest req = (HttpRequest) getRequest();
String userToken = HeaderUtils.getAuthToken(req.getHeaders());
String requestId = (String) getRequestAttributes().get("requestid");

if (requestId == null) {
LOGGER.info("Getting all requests of token :" + userToken);
return HeaderUtils.generateResponseId(application.getRequestsFromUser(userToken), req);
}
}

LOGGER.info("Getting request(" + requestId + ") of token :" + userToken);
Request request = application.getRequestDetails(userToken, requestId);
return request.toHttpMessageFormat();
Expand All @@ -56,7 +56,7 @@ public String remove() {
application.removeAllRequests(userToken);
return ResponseConstants.OK;
}

LOGGER.info("Removing request(" + requestId + ") of token :" + userToken);
application.removeRequest(userToken, requestId);
return ResponseConstants.OK;
Expand All @@ -69,39 +69,38 @@ public String post() {

List<Category> categories = HeaderUtils.getCategories(req.getHeaders());
HeaderUtils.checkCategories(categories, RequestConstants.TERM);
HeaderUtils.checkOCCIContentType(req.getHeaders());
HeaderUtils.checkOCCIContentType(req.getHeaders());

Map<String, String> xOCCIAtt = HeaderUtils.getXOCCIAtributes(req.getHeaders());
xOCCIAtt = normalizeXOCCIAtt(xOCCIAtt);

String authToken = HeaderUtils.getAuthToken(req.getHeaders());
Integer instanceCount = Integer.valueOf(xOCCIAtt.get(
RequestAttribute.INSTANCE_COUNT.getValue()));
Integer instanceCount = Integer.valueOf(xOCCIAtt.get(RequestAttribute.INSTANCE_COUNT
.getValue()));

LOGGER.info("Request " + instanceCount + " instances");

List<Request> currentRequestUnits = new ArrayList<Request>();
for (int i = 0; i < instanceCount; i++) {
currentRequestUnits.add(application.newRequest(authToken, categories, xOCCIAtt));
}
return HeaderUtils.generateResponseId(currentRequestUnits, req);
}

public static Map<String, String> normalizeXOCCIAtt(Map<String, String> xOCCIAtt) {
Map<String, String> defOCCIAtt = new HashMap<String, String>();
defOCCIAtt.put(RequestAttribute.TYPE.getValue(),
RequestConstants.DEFAULT_TYPE);
defOCCIAtt.put(RequestAttribute.INSTANCE_COUNT.getValue(),
defOCCIAtt.put(RequestAttribute.TYPE.getValue(), RequestConstants.DEFAULT_TYPE);
defOCCIAtt.put(RequestAttribute.INSTANCE_COUNT.getValue(),
RequestConstants.DEFAULT_INSTANCE_COUNT.toString());

defOCCIAtt.putAll(xOCCIAtt);
checkRequestType(defOCCIAtt.get(RequestAttribute.TYPE.getValue()));
HeaderUtils.checkDateValue(defOCCIAtt.get(RequestAttribute.VALID_FROM.getValue()));
HeaderUtils.checkDateValue(defOCCIAtt.get(RequestAttribute.VALID_UNTIL.getValue()));
HeaderUtils.checkIntegerValue(defOCCIAtt.get(RequestAttribute.INSTANCE_COUNT.getValue()));
List<Resource> requestResources = ResourceRepository.getAll();

checkRequestType(defOCCIAtt.get(RequestAttribute.TYPE.getValue()));
HeaderUtils.checkDateValue(defOCCIAtt.get(RequestAttribute.VALID_FROM.getValue()));
HeaderUtils.checkDateValue(defOCCIAtt.get(RequestAttribute.VALID_UNTIL.getValue()));
HeaderUtils.checkIntegerValue(defOCCIAtt.get(RequestAttribute.INSTANCE_COUNT.getValue()));

List<Resource> requestResources = ResourceRepository.getAll();
for (String attributeName : xOCCIAtt.keySet()) {
boolean supportedAtt = false;
for (Resource resource : requestResources) {
Expand All @@ -114,9 +113,9 @@ public static Map<String, String> normalizeXOCCIAtt(Map<String, String> xOCCIAtt
throw new OCCIException(ErrorType.BAD_REQUEST,
ResponseConstants.UNSUPPORTED_ATTRIBUTES);
}
}
return defOCCIAtt;
}

return defOCCIAtt;
}

public static void checkRequestType(String enumString) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.fogbowcloud.manager.occi.core;

//FIXME This class may be divided in two. ResourceCategory and RequestCategory
public class Category {

private String term;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum ErrorType {

UNAUTHORIZED, NOT_FOUND, BAD_REQUEST, OK;
UNAUTHORIZED, NOT_FOUND, BAD_REQUEST;

}
35 changes: 23 additions & 12 deletions src/main/java/org/fogbowcloud/manager/occi/core/HeaderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import java.util.List;
import java.util.Map;

import org.fogbowcloud.manager.occi.request.RequestConstants;
import org.fogbowcloud.manager.occi.request.RequestAttribute;
import org.fogbowcloud.manager.occi.request.RequestType;
import org.fogbowcloud.manager.occi.request.Request;
import org.restlet.engine.adapter.HttpRequest;
import org.restlet.engine.header.Header;
Expand All @@ -28,7 +25,19 @@ public static String generateResponseId(List<Request> requests, HttpRequest req)
}
if (response.equals("")) {
response = "Empty";
}
}
return response;
}

public static String generateResponseInstanceLocations(List<String> instances, HttpRequest req) {
String requestEndpoint = req.getHostRef() + req.getHttpCall().getRequestUri();
String response = "";
for (String location : instances) {
response += X_OCCI_LOCATION + requestEndpoint + "/" + location + "\n";
}
if (response.equals("")) {
response = "Empty";
}
return response;
}

Expand Down Expand Up @@ -82,13 +91,15 @@ public static List<Category> getCategories(Series<Header> headers) {
} else if (nameValue[0].trim().equals(OCCIHeaders.CLASS_CATEGORY)) {
catClass = nameValue[1].replace("\"", "").trim();
} else {
throw new OCCIException(ErrorType.BAD_REQUEST, ResponseConstants.IRREGULAR_SYNTAX);
throw new OCCIException(ErrorType.BAD_REQUEST,
ResponseConstants.IRREGULAR_SYNTAX);
}
}
try {
category = new Category(term, scheme, catClass);
} catch (IllegalArgumentException e) {
throw new OCCIException(ErrorType.BAD_REQUEST, ResponseConstants.IRREGULAR_SYNTAX);
throw new OCCIException(ErrorType.BAD_REQUEST,
ResponseConstants.IRREGULAR_SYNTAX);
}
listCategory.add(category);
} else {
Expand All @@ -107,21 +118,21 @@ public static String normalize(String headerName) {

public static void checkCategories(List<Category> categories, String mandatoryTerm) {
List<Resource> resources = ResourceRepository.get(categories);
if(resources.size() != categories.size()){
throw new OCCIException(ErrorType.BAD_REQUEST, ResponseConstants.IRREGULAR_SYNTAX);

if (resources.size() != categories.size()) {
throw new OCCIException(ErrorType.BAD_REQUEST, ResponseConstants.IRREGULAR_SYNTAX);
}
for (Category category : categories) {
if (category.getTerm().equals(mandatoryTerm)) {
Resource resource = ResourceRepository.get(mandatoryTerm);
if (resource == null || !resource.matches(category)) {
throw new OCCIException(ErrorType.BAD_REQUEST,
throw new OCCIException(ErrorType.BAD_REQUEST,
ResponseConstants.IRREGULAR_SYNTAX);
}
return;
}
}
throw new OCCIException(ErrorType.BAD_REQUEST, ResponseConstants.IRREGULAR_SYNTAX);
}
throw new OCCIException(ErrorType.BAD_REQUEST, ResponseConstants.IRREGULAR_SYNTAX);
}

public static void checkDateValue(String dataString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ private static int getStatusCode(ErrorType type) {
return HttpStatus.SC_NOT_FOUND;
case BAD_REQUEST:
return HttpStatus.SC_BAD_REQUEST;
case OK:
return HttpStatus.SC_OK;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class OCCIHeaders {
// header constants
public static final String CONTENT_TYPE = "Content-Type";
public static final String X_AUTH_TOKEN = "X-Auth-Token";
public static final String X_SUBJEC_TOKEN = "X-Subject-Token";

// occi constants
public static final String CATEGORY = "Category";
Expand Down
Loading

0 comments on commit 9c72b8e

Please sign in to comment.