diff --git a/Torpedo/src/etri/sdn/controller/module/firewall/RESTDeleteByIDApi.java b/Torpedo/src/etri/sdn/controller/module/firewall/RESTDeleteByIDApi.java index 8a919302..a15a26a2 100644 --- a/Torpedo/src/etri/sdn/controller/module/firewall/RESTDeleteByIDApi.java +++ b/Torpedo/src/etri/sdn/controller/module/firewall/RESTDeleteByIDApi.java @@ -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); diff --git a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/RESTDeleteByNameApi.java b/Torpedo/src/etri/sdn/controller/module/staticentrymanager/RESTDeleteByNameApi.java deleted file mode 100644 index e798850c..00000000 --- a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/RESTDeleteByNameApi.java +++ /dev/null @@ -1,54 +0,0 @@ -package etri.sdn.controller.module.staticentrymanager; - -import java.io.StringWriter; - -import org.codehaus.jackson.JsonFactory; -import org.codehaus.jackson.JsonGenerator; -import org.restlet.Request; -import org.restlet.Response; -import org.restlet.Restlet; -import org.restlet.data.MediaType; - -public class RESTDeleteByNameApi extends Restlet { - - private OFMStaticFlowEntryManager manager; - - public RESTDeleteByNameApi(OFMStaticFlowEntryManager manager) { - this.manager = manager; - } - - @Override - public void handle(Request request, Response response) { - StringWriter sWriter = new StringWriter(); - JsonFactory f = new JsonFactory(); - JsonGenerator g; - String status = null; - - String flowName = (String) request.getAttributes().get("name"); - - if ( !manager.getAllFlows().keySet().contains(flowName) ) { - status = "There is no entry"; - } else { - try { - manager.deleteFlow(flowName); - status = "Entry deleted: " + flowName; - } catch (Exception e){ - e.printStackTrace(); - } - } - - try { - g = f.createJsonGenerator(sWriter); - g.writeStartObject(); - g.writeFieldName("result"); - g.writeString(status); - g.writeEndObject(); - g.close(); - } catch (Exception e){ - e.printStackTrace(); - } - - String r = sWriter.toString(); - response.setEntity(r, MediaType.APPLICATION_JSON); - } -} diff --git a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/RestSwitchApi.java b/Torpedo/src/etri/sdn/controller/module/staticentrymanager/RestSwitchApi.java index 4c62871a..a2b93af0 100644 --- a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/RestSwitchApi.java +++ b/Torpedo/src/etri/sdn/controller/module/staticentrymanager/RestSwitchApi.java @@ -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); @@ -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. * @@ -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 flows = new HashSet(); - 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(); @@ -83,26 +113,31 @@ 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 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"); @@ -110,55 +145,47 @@ private void handlePost(Request request, Response response) { 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; @@ -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; @@ -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); } } diff --git a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntry.java b/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntry.java index c0490525..e6799440 100644 --- a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntry.java +++ b/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntry.java @@ -949,7 +949,7 @@ private static List> jsonToActions(String jsontext) throws I * * @throws IOException */ - public static Map jsonToStaticFlowEntry(String jsontext) throws IOException { + public static Map jsonToStaticFlowEntry(String sw, String name, String jsontext) throws IOException { Map entry = new LinkedHashMap(); MappingJsonFactory f = new MappingJsonFactory(); ObjectMapper mapper = new ObjectMapper(f); @@ -970,6 +970,9 @@ else if (field.getKey() == "actions") { } } + entry.put("switch", sw); + entry.put("name", name); + // if (Main.debug){ // System.out.println("input entry : " + jsontext); // System.out.println("converted entry: " + entry); diff --git a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntryStorage.java b/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntryStorage.java index a40932d5..c915646d 100644 --- a/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntryStorage.java +++ b/Torpedo/src/etri/sdn/controller/module/staticentrymanager/StaticFlowEntryStorage.java @@ -65,17 +65,19 @@ private void initRestApis() { * Array of RESTApi objects. * Each objects represent a REST call handler routine bound to a specific URI. */ + + RestSwitchApi restSwitchApi = new RestSwitchApi(manager); + RESTApi[] tmp = { /* - * LIST example + * LIST example * OF1.0,1.3: curl http://{controller_ip}:{rest_api_port}/wm/staticflowentry/all/json * curl http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:01/json * * ADD example - * OF1.0,1.3: curl http://{controller_ip}:{rest_api_port}/wm/staticflowentry/all/json - * curl -d '{"name":"s1","priority":"101","eth_type":"0x0800","ipv4_src":"10.0.0.1","ipv4_dst":"10.0.0.2","active":"true","instructions":[{"apply_actions":[{"output":"2"}]}]}' http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:01/json - * curl -d '{"name":"s20","priority":"1001","eth_type":"0x0806","ipv4_dst":"10.0.0.4","active":"true","instructions":[{"apply_actions":[{"output":"3"}]}]}' http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:02/json - * curl -d '{"name":"s1","priority":"1001","eth_type":"0x0800","ipv4_dst":"10.0.0.3","active":"true","instructions":[{"apply_actions":[{"set_field":{"ipv4_dst":"10.0.0.2"}},{"output":"2"}]}]}' http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:01/json + * OF1.0,1.3: curl http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:01/s1/json -d '{"priority":"101","eth_type":"0x0800","ipv4_src":"10.0.0.1","ipv4_dst":"10.0.0.2","active":"true","instructions":[{"apply_actions":[{"output":"2"}]}]}' + * curl http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:02/s20/json -d '{"priority":"1001","eth_type":"0x0806","ipv4_dst":"10.0.0.4","active":"true","instructions":[{"apply_actions":[{"output":"3"}]}]}' + * curl http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:01/s1/json -d '{"priority":"1001","eth_type":"0x0800","ipv4_dst":"10.0.0.3","active":"true","instructions":[{"apply_actions":[{"set_field":{"ipv4_dst":"10.0.0.2"}},{"output":"2"}]}]}' * * CLEAR example * OF1.0,1.3: curl -X DELETE http://{controller_ip}:{rest_api_port}/wm/staticflowentry/all/json @@ -85,21 +87,17 @@ private void initRestApis() { * OF1.0,1.3: curl -X PUT http://{controller_ip}:{rest_api_port}/wm/staticflowentry/all/json * curl -X PUT http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:01/json * - */ - new RESTApi("/wm/staticflowentry/{dpid}/json", - new RestSwitchApi(manager)), - - /* * DELETE by name example - * OF1.0,1.3: curl -X DELETE -d '{"name":"s1"}' http://{controller_ip}:{rest_api_port}/wm/staticflowentry/json - * + * OF1.0,1.3: curl -X DELETE http://{controller_ip}:{rest_api_port}/wm/staticflowentry/00:00:00:00:00:00:00:01/s1/json + * * This object is an additional implements REST handler routines (i.e. RFC2616). * According to RFC2616, DELETE method cannot have data fields. */ - new RESTApi( - "/wm/staticflowentry/{dpid}/{name}/json", - new RESTDeleteByNameApi(manager) - ) + new RESTApi("/wm/staticflowentry/{dpid}/json", + restSwitchApi), + new RESTApi("/wm/staticflowentry/{dpid}/{name}/json", + restSwitchApi), + }; this.apis = tmp;