Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public void handle(Request request, Response response) {

String ruleId = (String) request.getAttributes().get("ruleid");

if ( manager.getFirewallStorage().getFirewallEntryTable().getFirewallEntry(ruleId) != null ) {
manager.deleteRule( Integer.parseInt(ruleId) );
if (manager.getFirewallStorage().getFirewallEntryTable().getFirewallEntry(ruleId) != null) {
manager.deleteRule(Integer.parseInt(ruleId));
status = "Rule deleted";
} else {
logger.error("Error! Can't delete, a rule with ID {} doesn't exist.", ruleId);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public RestSwitchApi(OFMStaticFlowEntryManager manager) {
public void handle(Request request, Response response) {
Method method = request.getMethod();

if(method == Method.GET) {
if (method == Method.GET) {
handleGet(request, response);
} else if (method == Method.POST) {
handlePost(request, response);
Expand All @@ -46,6 +46,36 @@ public void handle(Request request, Response response) {
}
}


/**
* Make json result for response.
*
* @param response {@link Response} to write status code.
* @param status {@link Status} Status code.
* @param message Message for this response.
*/
private void makeResult(Response response, Status status, String message) {
StringWriter sWriter = new StringWriter();
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jsonGenerator;

try {
jsonGenerator = jsonFactory.createJsonGenerator(sWriter);
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("result");
jsonGenerator.writeString(message);
jsonGenerator.writeEndObject();
jsonGenerator.close();

String r = sWriter.toString();
response.setEntity(r, MediaType.APPLICATION_JSON);
response.setStatus(status);
} catch (Exception e) {
e.printStackTrace();
}

}

/**
* List entries.
*
Expand All @@ -54,12 +84,12 @@ public void handle(Request request, Response response) {
*/
private void handleGet(Request request, Response response) {
String dpid = (String) request.getAttributes().get("dpid");
Set<String> flows = new HashSet<String>();

if (! (dpid.toLowerCase().equals("all") || manager.isSwitchExists(dpid))) {
response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
if (!(dpid.toLowerCase().equals("all") || manager.isSwitchExists(dpid))) {
makeResult(response,
Status.CLIENT_ERROR_NOT_FOUND,
"That switch doesn't exists.");
return;
// FIXME: give reason why.
}

ObjectMapper om = new ObjectMapper();
Expand All @@ -83,82 +113,79 @@ private void handleGet(Request request, Response response) {
*/
private void handlePost(Request request, Response response) {
String dpid = (String) request.getAttributes().get("dpid");
String name = (String) request.getAttributes().get("name");

if (name == null) {
// Post to /wm/staticflowentry/00:00~~00:00/json is not allowed.
makeResult(response, Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, "");
return;
}

if (!manager.isSwitchExists(dpid)) {
response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
makeResult(response,
Status.CLIENT_ERROR_NOT_FOUND,
"That switch doesn't exists.");
return;
// FIXME: give reason why.
}

String jsonString = request.getEntityAsText();
StringWriter sWriter = new StringWriter();
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jsonGenerator;
String status;

Map<String, Object> entry;
Object flowName;

try {
entry = StaticFlowEntry.jsonToStaticFlowEntry(jsonString);
entry = StaticFlowEntry.jsonToStaticFlowEntry(dpid, name, jsonString);
if (entry == null) {
throw new Exception("The input is null");
makeResult(response, Status.CLIENT_ERROR_BAD_REQUEST, "The input was null");
return;
}

flowName = entry.get("name");
if (flowName != null) {
StaticFlowEntry.checkInputField(entry.keySet());
StaticFlowEntry.checkMatchPrerequisite(entry);
manager.addFlow((String) flowName, entry, dpid);
status = "Entry pushed: " + flowName;

String message = "Entry pushed: " + flowName;
makeResult(response, Status.SUCCESS_CREATED, message);
} else {
status = "The name field is indispensable";
makeResult(response,
Status.CLIENT_ERROR_BAD_REQUEST,
"The name field is indispensable");
}
response.setStatus(Status.SUCCESS_CREATED);
} catch (UnsupportedOperationException e) {
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
status = "Fail to push entry: Wrong version for the switch";
makeResult(response,
Status.CLIENT_ERROR_BAD_REQUEST,
"Fail to push entry: Wrong version for the switch");
} catch (StaticFlowEntryException e) {
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
status = e.getReason();
makeResult(response,
Status.CLIENT_ERROR_BAD_REQUEST,
e.getReason());
} catch (IOException e) {
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
status = "Fail to parse JSON format";
makeResult(response,
Status.CLIENT_ERROR_BAD_REQUEST,
"Fail to parse JSON format");
e.printStackTrace();
} catch (Exception e) {
response.setStatus(Status.SERVER_ERROR_INTERNAL);
makeResult(response,
Status.SERVER_ERROR_INTERNAL,
"Fail to insert entry");
e.printStackTrace();
status = "Fail to insert entry";
}

try {
jsonGenerator = jsonFactory.createJsonGenerator(sWriter);
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("result");
jsonGenerator.writeString(status);
jsonGenerator.writeEndObject();
jsonGenerator.close();
} catch (Exception e) {
e.printStackTrace();
}

String r = sWriter.toString();
response.setEntity(r, MediaType.APPLICATION_JSON);
}

/**
* Reload entries.
*
* @param request
* @param response
*/
private void handlePut(Request request, Response response) {
String dpid = (String) request.getAttributes().get("dpid");

StringWriter sWriter = new StringWriter();
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jsonGenerator;
String status;

if (!(dpid.toLowerCase().equals("all") || manager.isSwitchExists(dpid))) {
response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return;
Expand All @@ -169,61 +196,57 @@ private void handlePut(Request request, Response response) {
if (dpid.toLowerCase().equals("all")) {
if (!manager.getStaticFlowEntryStorage().getFlowModNameToDpidIndex().isEmpty()) {
manager.reloadAllFlowsToSwitch();
status = "All entries are reloaded to switches.";
}
else {
status = "There is no entry";
makeResult(response,
Status.SUCCESS_OK,
"All entries are reloaded to switches.");
} else {
makeResult(response,
Status.CLIENT_ERROR_NOT_FOUND,
"There is no entry");
}
}
else {
} else {
if (!manager.getStaticFlowEntryStorage().getDpidToFlowModNameIndex().isEmpty()) {
manager.reloadFlowsToSwitch(dpid);
status = "Entries are reloaded to switch: " + dpid + ".";
}
else {
status = "There is no entry";
makeResult(response,
Status.SUCCESS_OK,
"Entries are reloaded to switch: " + dpid + ".");
} else {
makeResult(response,
Status.CLIENT_ERROR_NOT_FOUND,
"There is no entry");
}
}
}
catch (UnsupportedOperationException e) {
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
status = "Fail to reload entry: Wrong version for the switch";
}
catch (Exception e) {
e.printStackTrace();
response.setStatus(Status.SERVER_ERROR_INTERNAL);
status = "Fail to reload entries to switches.";
}

try {
jsonGenerator = jsonFactory.createJsonGenerator(sWriter);
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("result");
jsonGenerator.writeString(status);
jsonGenerator.writeEndObject();
jsonGenerator.close();
}
catch (Exception e) {
} catch (UnsupportedOperationException e) {
makeResult(response,
Status.CLIENT_ERROR_BAD_REQUEST,
"Fail to reload entry: Wrong version for the switch");
} catch (Exception e) {
e.printStackTrace();
makeResult(response,
Status.SERVER_ERROR_INTERNAL,
"Fail to reload entries to switches.");
}

String r = sWriter.toString();
response.setEntity(r, MediaType.APPLICATION_JSON);
}

/**
* Clear switch.
*
* @param request
* @param response
*/
private void handleDelete(Request request, Response response) {
String dpid = (String) request.getAttributes().get("dpid");
String name = (String) request.getAttributes().get("name");

StringWriter sWriter = new StringWriter();
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jsonGenerator;
StringBuilder status = new StringBuilder();
if (name != null) {
clearRules(response, dpid);
} else {
deleteRule(response, dpid, name);
}

}

private void clearRules(Response response, String dpid) {
if (!(dpid.toLowerCase().equals("all") || manager.isSwitchExists(dpid))) {
response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return;
Expand Down Expand Up @@ -265,27 +288,29 @@ private void handleDelete(Request request, Response response) {
}

if (ret) {
status.append("All entries are cleared.");
makeResult(response, Status.SUCCESS_OK, "All entries are cleared.");
} else {
status.append("Failure clearing entries: ");
status.append(exceptionlist);
makeResult(response,
Status.SERVER_ERROR_INTERNAL,
"Failure clearing entries: " + exceptionlist);
}
} else {
status.append("There is no entry.");
makeResult(response, Status.CLIENT_ERROR_BAD_REQUEST, "There is no entry.");
}
}

private void deleteRule(Response response, String dpid, String name) {
if (!manager.getAllFlows().keySet().contains(name)) {
makeResult(response, Status.CLIENT_ERROR_NOT_FOUND, "There is no entry");
return;
}

try {
jsonGenerator = jsonFactory.createJsonGenerator(sWriter);
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("result");
jsonGenerator.writeString(status.toString());
jsonGenerator.writeEndObject();
jsonGenerator.close();
} catch (Exception e) {
manager.deleteFlow(name);
makeResult(response, Status.SUCCESS_OK, "Entry deleted: " + name);
} catch (StaticFlowEntryException e) {
makeResult(response, Status.SERVER_ERROR_INTERNAL, e.getMessage());
e.printStackTrace();
}

String r = sWriter.toString();
response.setEntity(r, MediaType.APPLICATION_JSON);
}
}
Loading