Pavlin Radoslavov | b743a20 | 2013-03-04 13:54:57 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.flowcache.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 | |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 5 | import net.floodlightcontroller.flowcache.IFlowService; |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 6 | import net.floodlightcontroller.util.FlowId; |
| 7 | import net.floodlightcontroller.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; |
| 12 | import org.codehaus.jackson.map.ObjectMapper; |
Pavlin Radoslavov | 4d4fc4e | 2013-03-04 13:49:04 -0800 | [diff] [blame] | 13 | import org.restlet.resource.Post; |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 14 | import org.restlet.resource.ServerResource; |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | public class AddFlowResource extends ServerResource { |
| 19 | |
| 20 | protected static Logger log = LoggerFactory.getLogger(AddFlowResource.class); |
| 21 | |
Pavlin Radoslavov | 4d4fc4e | 2013-03-04 13:49:04 -0800 | [diff] [blame] | 22 | @Post("json") |
| 23 | public FlowId store(String flowJson) { |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 24 | FlowId result = new FlowId(); |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 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 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 35 | // |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 36 | // Extract the arguments |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 37 | // NOTE: The "flow" is specified in JSON format. |
| 38 | // |
| 39 | ObjectMapper mapper = new ObjectMapper(); |
Pavlin Radoslavov | 4d4fc4e | 2013-03-04 13:49:04 -0800 | [diff] [blame] | 40 | String flowPathStr = flowJson; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 41 | FlowPath flowPath = null; |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 42 | log.debug("Add Flow Path: " + flowPathStr); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 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 | } |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 52 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 53 | // Process the request |
| 54 | if (flowPath != null) { |
Pavlin Radoslavov | dbaaf2e | 2013-03-29 04:25:55 -0700 | [diff] [blame] | 55 | if (flowService.addFlow(flowPath, result, null) != true) { |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 56 | result = new FlowId(); // Error: Return empty Flow Id |
| 57 | } |
Pavlin Radoslavov | a10a9a8 | 2013-02-22 11:47:54 -0800 | [diff] [blame] | 58 | } |
Pavlin Radoslavov | 5ab6851 | 2013-02-18 09:59:33 -0800 | [diff] [blame] | 59 | |
| 60 | return result; |
| 61 | } |
| 62 | } |