Jonathan Hart | 0210289 | 2013-04-10 12:55:50 -0700 | [diff] [blame] | 1 | package net.floodlightcontroller.core.web; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 9 | import net.floodlightcontroller.core.IOFSwitch; |
| 10 | |
| 11 | import org.codehaus.jackson.map.ObjectMapper; |
| 12 | import org.openflow.util.HexString; |
| 13 | import org.restlet.resource.Post; |
| 14 | import org.restlet.resource.ServerResource; |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | public class ClearFlowTableResource extends ServerResource { |
| 19 | static Logger log = LoggerFactory.getLogger(ClearFlowTableResource.class); |
| 20 | |
| 21 | @Post("json") |
| 22 | public List<String> ClearFlowTable(String jsonData){ |
| 23 | IFloodlightProviderService floodlightProvider = |
| 24 | (IFloodlightProviderService) getContext().getAttributes() |
| 25 | .get(IFloodlightProviderService.class.getCanonicalName()); |
| 26 | |
| 27 | Map<Long, IOFSwitch> switches = floodlightProvider.getSwitches(); |
| 28 | |
| 29 | List<String> response = new ArrayList<String>(); |
| 30 | ObjectMapper mapper = new ObjectMapper(); |
| 31 | String[] dpids = null; |
| 32 | try { |
| 33 | dpids = mapper.readValue(jsonData, String[].class); |
| 34 | } catch (IOException e) { |
| 35 | log.debug("Error parsing switch dpid array: {}", e.getMessage()); |
| 36 | response.add("Error parsing input"); |
| 37 | return response; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | for (String dpid : dpids){ |
| 42 | IOFSwitch sw = switches.get(HexString.toLong(dpid)); |
| 43 | if (sw != null){ |
| 44 | sw.clearAllFlowMods(); |
| 45 | response.add(dpid + " cleared"); |
| 46 | } |
| 47 | else { |
| 48 | response.add(dpid + " not found"); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return response; |
| 53 | } |
| 54 | |
| 55 | } |