blob: c454b0f3615d15011c66ee1c0628d1dbb3943d3f [file] [log] [blame]
HIGUCHI Yuta60a10142013-06-14 15:50:10 -07001package net.onrc.onos.ofcontroller.flowmanager.web;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07002
3import java.io.IOException;
4
HIGUCHI Yuta60a10142013-06-14 15:50:10 -07005import net.onrc.onos.ofcontroller.flowmanager.IFlowService;
HIGUCHI Yuta356086e2013-06-12 15:21:19 -07006import net.onrc.onos.ofcontroller.util.FlowId;
7import net.onrc.onos.ofcontroller.util.FlowPath;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -07008
9import org.codehaus.jackson.JsonGenerationException;
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070010import org.codehaus.jackson.map.JsonMappingException;
11import org.codehaus.jackson.map.ObjectMapper;
12import org.restlet.resource.Post;
13import org.restlet.resource.ServerResource;
14import org.slf4j.Logger;
15import org.slf4j.LoggerFactory;
16
admin944ef4f2013-10-08 17:48:37 -070017/**
HIGUCHI Yutaeb567aa2013-10-08 19:27:35 -070018 * Flow Manager REST API implementation: Add a Flow by delegating
admin944ef4f2013-10-08 17:48:37 -070019 * the Shortest Path computation to ONOS:
20 *
21 * POST /wm/flow/add-shortest-path/json
22 */
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070023public class AddShortestPathFlowResource extends ServerResource {
24
25 protected static Logger log = LoggerFactory.getLogger(AddShortestPathFlowResource.class);
26
admin944ef4f2013-10-08 17:48:37 -070027 /**
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 Radoslavovb9fe6b42013-03-27 16:25:05 -070034 @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 Radoslavove0575292013-03-28 05:35:25 -070067 FlowPath addedFlowPath =
68 flowService.addAndMaintainShortestPathFlow(flowPath);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070069 if (addedFlowPath == null) {
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070070 result = new FlowId(); // Error: Return empty Flow Id
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070071 } else {
Pavlin Radoslavove0575292013-03-28 05:35:25 -070072 result = addedFlowPath.flowId();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070073 }
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070074 }
75
76 return result;
77 }
78}