Skip to content
Snippets Groups Projects
Commit b33aed7c authored by Jay Cox's avatar Jay Cox
Browse files

Refactored firewall REST api. Allow POST for firewall enable/disable.

Allow enabling and disabling of firewall via POST method as well as GET.
(I recommend disabling GET when all clients have been updated)

Refactored FirewallResource class into multiple smaller classes to make
URL routing clearer.
parent 821ab996
No related branches found
No related tags found
No related merge requests found
/**
* 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\"}";
}
}
/**
* 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\"}";
}
}
/**
* 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());
}
}
/**
* 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\"}";
}
}
/**
* 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();
}
}
...@@ -29,63 +29,24 @@ import org.restlet.resource.ServerResource; ...@@ -29,63 +29,24 @@ import org.restlet.resource.ServerResource;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class FirewallResource extends ServerResource {
protected static Logger log = LoggerFactory.getLogger(FirewallResource.class);
@Get("json") public class FirewallSubnetMaskResource extends FirewallResourceBase {
public Object handleRequest() { // REST API to get or set local subnet mask -- this only makes sense for one subnet
IFirewallService firewall = // will remove later
(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\"}";
}
// REST API retrieving rules from storage private static final Logger log = LoggerFactory.getLogger(FirewallSubnetMaskResource.class);
// currently equivalent to /wm/firewall/rules/json
if (op.equalsIgnoreCase("storageRules")) {
return firewall.getStorageRules();
}
// REST API set local subnet mask -- this only makes sense for one subnet @Get("json")
// will remove later public Object handleRequest() {
if (op.equalsIgnoreCase("subnet-mask")) { IFirewallService firewall = getFirewallService();
return "{\"subnet-mask\":\"" + firewall.getSubnetMask() + "\"}";
}
// no known options found return "{\"subnet-mask\":\"" + firewall.getSubnetMask() + "\"}";
return "{\"status\" : \"failure\", \"details\" : \"invalid operation\"}";
} }
/**
* Allows setting of subnet mask
* @param fmJson The Subnet Mask in JSON format.
* @return A string status message
*/
@Post @Post
public String handlePost(String fmJson) { public String handlePost(String fmJson) {
IFirewallService firewall = IFirewallService firewall = getFirewallService();
(IFirewallService)getContext().getAttributes().
get(IFirewallService.class.getCanonicalName());
String newMask; String newMask;
try { try {
......
...@@ -28,8 +28,14 @@ public class FirewallWebRoutable implements RestletRoutable { ...@@ -28,8 +28,14 @@ public class FirewallWebRoutable implements RestletRoutable {
@Override @Override
public Router getRestlet(Context context) { public Router getRestlet(Context context) {
Router router = new Router(context); Router router = new Router(context);
router.attach("/module/{op}/json", FirewallResource.class); router.attach("/module/status/json", FirewallStatusResource.class);
router.attach("/rules/json", FirewallRulesResource.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; return router;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment