blob: 9d2e0c91106a97fa2bb6767efcb527d5cc5d5e7e [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
17public class AddShortestPathFlowResource extends ServerResource {
18
19 protected static Logger log = LoggerFactory.getLogger(AddShortestPathFlowResource.class);
20
21 @Post("json")
22 public FlowId store(String flowJson) {
23 FlowId result = new FlowId();
24
25 IFlowService flowService =
26 (IFlowService)getContext().getAttributes().
27 get(IFlowService.class.getCanonicalName());
28
29 if (flowService == null) {
30 log.debug("ONOS Flow Service not found");
31 return result;
32 }
33
34 //
35 // Extract the arguments
36 // NOTE: The "flow" is specified in JSON format.
37 //
38 ObjectMapper mapper = new ObjectMapper();
39 String flowPathStr = flowJson;
40 FlowPath flowPath = null;
41 log.debug("Add Shortest Path Flow Path: " + flowPathStr);
42 try {
43 flowPath = mapper.readValue(flowPathStr, FlowPath.class);
44 } catch (JsonGenerationException e) {
45 e.printStackTrace();
46 } catch (JsonMappingException e) {
47 e.printStackTrace();
48 } catch (IOException e) {
49 e.printStackTrace();
50 }
51
52 // Process the request
53 if (flowPath != null) {
Pavlin Radoslavove0575292013-03-28 05:35:25 -070054 FlowPath addedFlowPath =
55 flowService.addAndMaintainShortestPathFlow(flowPath);
56 if (addedFlowPath == null)
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070057 result = new FlowId(); // Error: Return empty Flow Id
Pavlin Radoslavove0575292013-03-28 05:35:25 -070058 else
59 result = addedFlowPath.flowId();
Pavlin Radoslavovb9fe6b42013-03-27 16:25:05 -070060 }
61
62 return result;
63 }
64}