diff --git a/src/main/java/net/floodlightcontroller/firewall/FirewallDisableResource.java b/src/main/java/net/floodlightcontroller/firewall/FirewallDisableResource.java new file mode 100644 index 0000000000000000000000000000000000000000..6f51ebf74d863112a48378ede05a2985f66336a5 --- /dev/null +++ b/src/main/java/net/floodlightcontroller/firewall/FirewallDisableResource.java @@ -0,0 +1,52 @@ +/** + * Copyright 2011, Big Switch Networks, Inc. + * Originally created by Amer Tahir + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + **/ + +package net.floodlightcontroller.firewall; + +import org.restlet.resource.Post; +import org.restlet.resource.Get; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/* + * Rest API endpoint to disable the firewall + * + * Contrary to best practices it changes the state on both GET and POST + * We should disable this behavior for GET as soon as we can be sure + * that no clients depend on this behavior. + */ +public class FirewallDisableResource extends FirewallResourceBase { + private static final Logger log = LoggerFactory.getLogger(FirewallDisableResource.class); + + @Get("json") + public Object handleRequest() { + log.warn("REST call to FirewallDisableResource with method GET is depreciated. Use POST: "); + + return handlePost(); + } + + @Post("json") + public Object handlePost() { + IFirewallService firewall = getFirewallService(); + + firewall.enableFirewall(false); + + return "{\"status\" : \"success\", \"details\" : \"firewall stopped\"}"; + } +} diff --git a/src/main/java/net/floodlightcontroller/firewall/FirewallEnableResource.java b/src/main/java/net/floodlightcontroller/firewall/FirewallEnableResource.java new file mode 100644 index 0000000000000000000000000000000000000000..4355db1b17ac4c7804d80322960c170ba913a7ca --- /dev/null +++ b/src/main/java/net/floodlightcontroller/firewall/FirewallEnableResource.java @@ -0,0 +1,53 @@ +/** + * Copyright 2011, Big Switch Networks, Inc. + * Originally created by Amer Tahir + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + **/ + +package net.floodlightcontroller.firewall; + +import org.restlet.resource.Post; +import org.restlet.resource.Get; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/* + * Rest API endpoint to enable the firewall + * + * Contrary to best practices it changes the state on both GET and POST + * We should disable this behavior for GET as soon as we can be sure + * that no clients depend on this behavior. + */ +public class FirewallEnableResource extends FirewallResourceBase { + private static final Logger log = LoggerFactory.getLogger(FirewallEnableResource.class); + + @Get("json") + public Object handleRequest() { + log.warn("REST call to FirewallEnableResource with method GET is depreciated. Use POST: "); + + return handlePost(); + } + + @Post("json") + public Object handlePost() { + IFirewallService firewall = getFirewallService(); + + firewall.enableFirewall(true); + + return "{\"status\" : \"success\", \"details\" : \"firewall running\"}"; + } +} + diff --git a/src/main/java/net/floodlightcontroller/firewall/FirewallResourceBase.java b/src/main/java/net/floodlightcontroller/firewall/FirewallResourceBase.java new file mode 100644 index 0000000000000000000000000000000000000000..4bff1f9f896e2d9cd96033ff58fc97ab37138a05 --- /dev/null +++ b/src/main/java/net/floodlightcontroller/firewall/FirewallResourceBase.java @@ -0,0 +1,32 @@ +/** + * Copyright 2011, Big Switch Networks, Inc. + * Originally created by Amer Tahir + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + **/ + +package net.floodlightcontroller.firewall; + +import org.restlet.resource.ServerResource; + + +/* + * Base class for Firewall REST API endpoints. + * Provides a convenience method to retrieve the firewall service + */ +class FirewallResourceBase extends ServerResource { + IFirewallService getFirewallService() { + return (IFirewallService)getContext().getAttributes(). + get(IFirewallService.class.getCanonicalName()); + } +} diff --git a/src/main/java/net/floodlightcontroller/firewall/FirewallStatusResource.java b/src/main/java/net/floodlightcontroller/firewall/FirewallStatusResource.java new file mode 100644 index 0000000000000000000000000000000000000000..47bfc6b23908e9864e8b096f13fcaabe24c3517d --- /dev/null +++ b/src/main/java/net/floodlightcontroller/firewall/FirewallStatusResource.java @@ -0,0 +1,36 @@ +/** + * Copyright 2011, Big Switch Networks, Inc. + * Originally created by Amer Tahir + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + **/ + +package net.floodlightcontroller.firewall; + +import org.restlet.resource.Get; + + +/* + * REST API for retrieving the status of the firewall + */ +public class FirewallStatusResource extends FirewallResourceBase { + @Get("json") + public Object handleRequest() { + IFirewallService firewall = this.getFirewallService(); + + if (firewall.isEnabled()) + return "{\"result\" : \"firewall enabled\"}"; + else + return "{\"result\" : \"firewall disabled\"}"; + } +} diff --git a/src/main/java/net/floodlightcontroller/firewall/FirewallStorageRulesResource.java b/src/main/java/net/floodlightcontroller/firewall/FirewallStorageRulesResource.java new file mode 100644 index 0000000000000000000000000000000000000000..b795fa4d89cf3a30ea3d04b6496bccdbee175884 --- /dev/null +++ b/src/main/java/net/floodlightcontroller/firewall/FirewallStorageRulesResource.java @@ -0,0 +1,38 @@ +/** + * Copyright 2011, Big Switch Networks, Inc. + * Originally created by Amer Tahir + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + **/ + +package net.floodlightcontroller.firewall; + +import java.io.IOException; + +import org.restlet.resource.Get; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class FirewallStorageRulesResource extends FirewallResourceBase { + // REST API for retrieving rules from storage + + private static final Logger log = LoggerFactory.getLogger(FirewallStorageRulesResource.class); + + @Get("json") + public Object handleRequest() { + IFirewallService firewall = getFirewallService(); + + return firewall.getStorageRules(); + } +} diff --git a/src/main/java/net/floodlightcontroller/firewall/FirewallResource.java b/src/main/java/net/floodlightcontroller/firewall/FirewallSubnetMaskResource.java similarity index 59% rename from src/main/java/net/floodlightcontroller/firewall/FirewallResource.java rename to src/main/java/net/floodlightcontroller/firewall/FirewallSubnetMaskResource.java index 773a450e12480cbe2f845b13dee89d98b1d1fc53..f76dd78404c8a6c510f51dae9ff40b97a0b83d80 100644 --- a/src/main/java/net/floodlightcontroller/firewall/FirewallResource.java +++ b/src/main/java/net/floodlightcontroller/firewall/FirewallSubnetMaskResource.java @@ -29,63 +29,24 @@ import org.restlet.resource.ServerResource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class FirewallResource extends ServerResource { - protected static Logger log = LoggerFactory.getLogger(FirewallResource.class); - @Get("json") - public Object handleRequest() { - IFirewallService firewall = - (IFirewallService)getContext().getAttributes(). - get(IFirewallService.class.getCanonicalName()); - - String op = (String) getRequestAttributes().get("op"); - - // REST API check status - if (op.equalsIgnoreCase("status")) { - if (firewall.isEnabled()) - return "{\"result\" : \"firewall enabled\"}"; - else - return "{\"result\" : \"firewall disabled\"}"; - } - - // REST API enable firewall - if (op.equalsIgnoreCase("enable")) { - firewall.enableFirewall(true); - return "{\"status\" : \"success\", \"details\" : \"firewall running\"}"; - } - - // REST API disable firewall - if (op.equalsIgnoreCase("disable")) { - firewall.enableFirewall(false); - return "{\"status\" : \"success\", \"details\" : \"firewall stopped\"}"; - } +public class FirewallSubnetMaskResource extends FirewallResourceBase { + // REST API to get or set local subnet mask -- this only makes sense for one subnet + // will remove later - // REST API retrieving rules from storage - // currently equivalent to /wm/firewall/rules/json - if (op.equalsIgnoreCase("storageRules")) { - return firewall.getStorageRules(); - } + private static final Logger log = LoggerFactory.getLogger(FirewallSubnetMaskResource.class); - // REST API set local subnet mask -- this only makes sense for one subnet - // will remove later - if (op.equalsIgnoreCase("subnet-mask")) { - return "{\"subnet-mask\":\"" + firewall.getSubnetMask() + "\"}"; - } + @Get("json") + public Object handleRequest() { + IFirewallService firewall = getFirewallService(); - // no known options found - return "{\"status\" : \"failure\", \"details\" : \"invalid operation\"}"; + return "{\"subnet-mask\":\"" + firewall.getSubnetMask() + "\"}"; } - /** - * Allows setting of subnet mask - * @param fmJson The Subnet Mask in JSON format. - * @return A string status message - */ + @Post public String handlePost(String fmJson) { - IFirewallService firewall = - (IFirewallService)getContext().getAttributes(). - get(IFirewallService.class.getCanonicalName()); + IFirewallService firewall = getFirewallService(); String newMask; try { diff --git a/src/main/java/net/floodlightcontroller/firewall/FirewallWebRoutable.java b/src/main/java/net/floodlightcontroller/firewall/FirewallWebRoutable.java index c2d1c8fdb053e019eedecadc476c7cec7101fc79..fdc1d70c2b33141cbe82209e6c6af32a43056dfe 100644 --- a/src/main/java/net/floodlightcontroller/firewall/FirewallWebRoutable.java +++ b/src/main/java/net/floodlightcontroller/firewall/FirewallWebRoutable.java @@ -28,8 +28,14 @@ public class FirewallWebRoutable implements RestletRoutable { @Override public Router getRestlet(Context context) { Router router = new Router(context); - router.attach("/module/{op}/json", FirewallResource.class); - router.attach("/rules/json", FirewallRulesResource.class); + router.attach("/module/status/json", FirewallStatusResource.class); + router.attach("/module/enable/json", FirewallEnableResource.class); + router.attach("/module/disable/json", FirewallDisableResource.class); + router.attach("/module/subnet-mask/json", FirewallSubnetMaskResource.class); + router.attach("/module/storageRules/json", FirewallStorageRulesResource.class); + + router.attach("/rules/json", FirewallRulesResource.class); + return router; }