Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.firewall; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import org.codehaus.jackson.JsonParseException; |
| 6 | import org.codehaus.jackson.JsonParser; |
| 7 | import org.codehaus.jackson.JsonToken; |
| 8 | import org.codehaus.jackson.map.MappingJsonFactory; |
| 9 | import org.restlet.resource.Post; |
| 10 | import org.restlet.resource.Get; |
| 11 | import org.restlet.resource.ServerResource; |
| 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
| 15 | public class FirewallResource extends ServerResource { |
| 16 | protected static Logger log = LoggerFactory.getLogger(FirewallResource.class); |
| 17 | |
| 18 | @Get("json") |
| 19 | public Object handleRequest() { |
| 20 | IFirewallService firewall = |
| 21 | (IFirewallService)getContext().getAttributes(). |
| 22 | get(IFirewallService.class.getCanonicalName()); |
| 23 | |
| 24 | String op = (String) getRequestAttributes().get("op"); |
| 25 | |
| 26 | // REST API check status |
| 27 | if (op.equalsIgnoreCase("status")) { |
| 28 | if (firewall.isEnabled()) |
| 29 | return "{\"result\" : \"firewall enabled\"}"; |
| 30 | else |
| 31 | return "{\"result\" : \"firewall disabled\"}"; |
| 32 | } |
| 33 | |
| 34 | // REST API enable firewall |
| 35 | if (op.equalsIgnoreCase("enable")) { |
| 36 | firewall.enableFirewall(true); |
| 37 | return "{\"status\" : \"success\", \"details\" : \"firewall running\"}"; |
| 38 | } |
| 39 | |
| 40 | // REST API disable firewall |
| 41 | if (op.equalsIgnoreCase("disable")) { |
| 42 | firewall.enableFirewall(false); |
| 43 | return "{\"status\" : \"success\", \"details\" : \"firewall stopped\"}"; |
| 44 | } |
| 45 | |
| 46 | // REST API retrieving rules from storage |
| 47 | // currently equivalent to /wm/firewall/rules/json |
| 48 | if (op.equalsIgnoreCase("storageRules")) { |
| 49 | return firewall.getStorageRules(); |
| 50 | } |
| 51 | |
| 52 | // REST API set local subnet mask -- this only makes sense for one subnet |
| 53 | // will remove later |
| 54 | if (op.equalsIgnoreCase("subnet-mask")) { |
| 55 | return firewall.getSubnetMask(); |
| 56 | } |
| 57 | |
| 58 | // no known options found |
| 59 | return "{\"status\" : \"failure\", \"details\" : \"invalid operation\"}"; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Allows setting of subnet mask |
| 64 | * @param fmJson The Subnet Mask in JSON format. |
| 65 | * @return A string status message |
| 66 | */ |
| 67 | @Post |
| 68 | public String handlePost(String fmJson) { |
| 69 | IFirewallService firewall = |
| 70 | (IFirewallService)getContext().getAttributes(). |
| 71 | get(IFirewallService.class.getCanonicalName()); |
| 72 | |
| 73 | String newMask; |
| 74 | try { |
| 75 | newMask = jsonExtractSubnetMask(fmJson); |
| 76 | } catch (IOException e) { |
| 77 | log.error("Error parsing new subnet mask: " + fmJson, e); |
| 78 | e.printStackTrace(); |
| 79 | return "{\"status\" : \"Error! Could not parse new subnet mask, see log for details.\"}"; |
| 80 | } |
| 81 | firewall.setSubnetMask(newMask); |
| 82 | return ("{\"status\" : \"subnet mask set\"}"); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Extracts subnet mask from a JSON string |
| 87 | * @param fmJson The JSON formatted string |
| 88 | * @return The subnet mask |
| 89 | * @throws IOException If there was an error parsing the JSON |
| 90 | */ |
| 91 | public static String jsonExtractSubnetMask(String fmJson) throws IOException { |
| 92 | String subnet_mask = ""; |
| 93 | MappingJsonFactory f = new MappingJsonFactory(); |
| 94 | JsonParser jp; |
| 95 | |
| 96 | try { |
| 97 | jp = f.createJsonParser(fmJson); |
| 98 | } catch (JsonParseException e) { |
| 99 | throw new IOException(e); |
| 100 | } |
| 101 | |
| 102 | jp.nextToken(); |
| 103 | if (jp.getCurrentToken() != JsonToken.START_OBJECT) { |
| 104 | throw new IOException("Expected START_OBJECT"); |
| 105 | } |
| 106 | |
| 107 | while (jp.nextToken() != JsonToken.END_OBJECT) { |
| 108 | if (jp.getCurrentToken() != JsonToken.FIELD_NAME) { |
| 109 | throw new IOException("Expected FIELD_NAME"); |
| 110 | } |
| 111 | |
| 112 | String n = jp.getCurrentName(); |
| 113 | jp.nextToken(); |
| 114 | if (jp.getText().equals("")) |
| 115 | continue; |
| 116 | |
| 117 | if (n == "subnet-mask") { |
| 118 | subnet_mask = jp.getText(); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return subnet_mask; |
| 124 | } |
| 125 | } |