blob: 4f4c1e4631bdf60d08a6dc2a7c79384a7e42f2eb [file] [log] [blame]
HIGUCHI Yuta60a10142013-06-14 15:50:10 -07001package net.onrc.onos.ofcontroller.flowmanager.web;
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -08002
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08003import 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 Radoslavov5ab68512013-02-18 09:59:33 -08008
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08009import org.codehaus.jackson.JsonGenerationException;
10import org.codehaus.jackson.map.ObjectMapper;
11import org.codehaus.jackson.map.JsonMappingException;
Pavlin Radoslavov4d4fc4e2013-03-04 13:49:04 -080012import org.restlet.resource.Post;
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080013import 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 with the Flow
admin944ef4f2013-10-08 17:48:37 -070019 * Entries:
20 *
Pavlin Radoslavov598635c2013-12-18 19:43:12 -080021 * POST /wm/onos/flows/add/json
admin944ef4f2013-10-08 17:48:37 -070022 */
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080023public class AddFlowResource extends ServerResource {
24
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070025 protected final static Logger log = LoggerFactory.getLogger(AddFlowResource.class);
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080026
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 Radoslavov4d4fc4e2013-03-04 13:49:04 -080034 @Post("json")
35 public FlowId store(String flowJson) {
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -080036 FlowId result = new FlowId();
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080037
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 Radoslavov2013cbb2013-02-26 10:15:18 -080047 //
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080048 // Extract the arguments
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080049 // NOTE: The "flow" is specified in JSON format.
50 //
51 ObjectMapper mapper = new ObjectMapper();
Pavlin Radoslavov4d4fc4e2013-03-04 13:49:04 -080052 String flowPathStr = flowJson;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080053 FlowPath flowPath = null;
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -080054 log.debug("Add Flow Path: {}", flowPathStr);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080055 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 Radoslavov5ab68512013-02-18 09:59:33 -080064
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080065 // Process the request
66 if (flowPath != null) {
Pavlin Radoslavov051abb42013-12-05 17:24:50 -080067 FlowId addedFlowId = flowService.addFlow(flowPath);
68 if (addedFlowId != null)
69 result = addedFlowId;
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -080070 }
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080071
72 return result;
73 }
74}