HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager.web; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | |
HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 5 | import net.onrc.onos.ofcontroller.flowmanager.IFlowService; |
HIGUCHI Yuta | 356086e | 2013-06-12 15:21:19 -0700 | [diff] [blame] | 6 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 7 | import net.onrc.onos.ofcontroller.util.FlowPath; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 8 | |
| 9 | import org.codehaus.jackson.JsonGenerationException; |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 10 | import org.codehaus.jackson.map.JsonMappingException; |
| 11 | import org.codehaus.jackson.map.ObjectMapper; |
| 12 | import org.restlet.resource.Post; |
| 13 | import org.restlet.resource.ServerResource; |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 17 | /** |
HIGUCHI Yuta | eb567aa | 2013-10-08 19:27:35 -0700 | [diff] [blame] | 18 | * Flow Manager REST API implementation: Add a Flow by delegating |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 19 | * the Shortest Path computation to ONOS: |
| 20 | * |
| 21 | * POST /wm/flow/add-shortest-path/json |
| 22 | */ |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 23 | public class AddShortestPathFlowResource extends ServerResource { |
| 24 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 25 | protected final static Logger log = LoggerFactory.getLogger(AddShortestPathFlowResource.class); |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 26 | |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Implement the API. |
| 29 | * |
| 30 | * @param flowJson a string with the JSON representation of the Flow to |
| 31 | * add. |
| 32 | * @return the Flow ID of the added flow. |
| 33 | */ |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 34 | @Post("json") |
| 35 | public FlowId store(String flowJson) { |
| 36 | FlowId result = new FlowId(); |
| 37 | |
| 38 | IFlowService flowService = |
| 39 | (IFlowService)getContext().getAttributes(). |
| 40 | get(IFlowService.class.getCanonicalName()); |
| 41 | |
| 42 | if (flowService == null) { |
| 43 | log.debug("ONOS Flow Service not found"); |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | // |
| 48 | // Extract the arguments |
| 49 | // NOTE: The "flow" is specified in JSON format. |
| 50 | // |
| 51 | ObjectMapper mapper = new ObjectMapper(); |
| 52 | String flowPathStr = flowJson; |
| 53 | FlowPath flowPath = null; |
| 54 | log.debug("Add Shortest Path Flow Path: " + flowPathStr); |
| 55 | try { |
| 56 | flowPath = mapper.readValue(flowPathStr, FlowPath.class); |
| 57 | } catch (JsonGenerationException e) { |
| 58 | e.printStackTrace(); |
| 59 | } catch (JsonMappingException e) { |
| 60 | e.printStackTrace(); |
| 61 | } catch (IOException e) { |
| 62 | e.printStackTrace(); |
| 63 | } |
| 64 | |
| 65 | // Process the request |
| 66 | if (flowPath != null) { |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 67 | FlowPath addedFlowPath = |
| 68 | flowService.addAndMaintainShortestPathFlow(flowPath); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 69 | if (addedFlowPath == null) { |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 70 | result = new FlowId(); // Error: Return empty Flow Id |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 71 | } else { |
Pavlin Radoslavov | e057529 | 2013-03-28 05:35:25 -0700 | [diff] [blame] | 72 | result = addedFlowPath.flowId(); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 73 | } |
Pavlin Radoslavov | b9fe6b4 | 2013-03-27 16:25:05 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return result; |
| 77 | } |
| 78 | } |