HIGUCHI Yuta | 60a1014 | 2013-06-14 15:50:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager.web; |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 2 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 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 | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 8 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 9 | import org.codehaus.jackson.JsonGenerationException; |
| 10 | import org.codehaus.jackson.map.ObjectMapper; |
| 11 | import org.codehaus.jackson.map.JsonMappingException; |
Pavlin Radoslavov | 4d4fc4e | 2013-03-04 13:49:04 -0800 | [diff] [blame] | 12 | import org.restlet.resource.Post; |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 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 with the Flow |
admin | 944ef4f | 2013-10-08 17:48:37 -0700 | [diff] [blame] | 19 | * Entries: |
| 20 | * |
| 21 | * POST /wm/flow/add/json |
| 22 | */ |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 23 | public class AddFlowResource extends ServerResource { |
| 24 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 25 | protected final static Logger log = LoggerFactory.getLogger(AddFlowResource.class); |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [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 | 4d4fc4e | 2013-03-04 13:49:04 -0800 | [diff] [blame] | 34 | @Post("json") |
| 35 | public FlowId store(String flowJson) { |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 36 | FlowId result = new FlowId(); |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 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 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 47 | // |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 48 | // Extract the arguments |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 49 | // NOTE: The "flow" is specified in JSON format. |
| 50 | // |
| 51 | ObjectMapper mapper = new ObjectMapper(); |
Pavlin Radoslavov | 4d4fc4e | 2013-03-04 13:49:04 -0800 | [diff] [blame] | 52 | String flowPathStr = flowJson; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 53 | FlowPath flowPath = null; |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 54 | log.debug("Add Flow Path: " + flowPathStr); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 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 | } |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 64 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 65 | // Process the request |
| 66 | if (flowPath != null) { |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 67 | if (flowService.addFlow(flowPath, result, null) != true) { |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 68 | result = new FlowId(); // Error: Return empty Flow Id |
| 69 | } |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 70 | } |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 71 | |
| 72 | return result; |
| 73 | } |
| 74 | } |