Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 1 | package net.floodlightcontroller.flowcache.web; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import net.floodlightcontroller.flowcache.IFlowService; |
| 6 | import net.floodlightcontroller.util.FlowId; |
| 7 | import net.floodlightcontroller.util.FlowPath; |
| 8 | |
| 9 | import org.codehaus.jackson.JsonGenerationException; |
| 10 | import org.codehaus.jackson.map.ObjectMapper; |
| 11 | import org.codehaus.jackson.map.JsonMappingException; |
| 12 | import org.codehaus.jackson.map.ObjectMapper; |
| 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 AddShortestPathFlowResource extends ServerResource { |
| 19 | |
| 20 | protected static Logger log = LoggerFactory.getLogger(AddShortestPathFlowResource.class); |
| 21 | |
| 22 | @Post("json") |
| 23 | public FlowId store(String flowJson) { |
| 24 | FlowId result = new FlowId(); |
| 25 | |
| 26 | IFlowService flowService = |
| 27 | (IFlowService)getContext().getAttributes(). |
| 28 | get(IFlowService.class.getCanonicalName()); |
| 29 | |
| 30 | if (flowService == null) { |
| 31 | log.debug("ONOS Flow Service not found"); |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | // |
| 36 | // Extract the arguments |
| 37 | // NOTE: The "flow" is specified in JSON format. |
| 38 | // |
| 39 | ObjectMapper mapper = new ObjectMapper(); |
| 40 | String flowPathStr = flowJson; |
| 41 | FlowPath flowPath = null; |
| 42 | log.debug("Add Shortest Path Flow Path: " + flowPathStr); |
| 43 | try { |
| 44 | flowPath = mapper.readValue(flowPathStr, FlowPath.class); |
| 45 | } catch (JsonGenerationException e) { |
| 46 | e.printStackTrace(); |
| 47 | } catch (JsonMappingException e) { |
| 48 | e.printStackTrace(); |
| 49 | } catch (IOException e) { |
| 50 | e.printStackTrace(); |
| 51 | } |
| 52 | |
| 53 | // Process the request |
| 54 | if (flowPath != null) { |
| 55 | if (flowService.addAndMaintainShortestPathFlow(flowPath, result) |
| 56 | != true) { |
| 57 | result = new FlowId(); // Error: Return empty Flow Id |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return result; |
| 62 | } |
| 63 | } |