blob: 164601215a7b7f0b984746e1a1869b18072e6cac [file] [log] [blame]
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -08001package net.floodlightcontroller.flowcache.web;
2
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08003import java.io.IOException;
4
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -08005import net.floodlightcontroller.flowcache.IFlowService;
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -08006import net.floodlightcontroller.util.FlowId;
7import net.floodlightcontroller.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;
12import org.codehaus.jackson.map.ObjectMapper;
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080013import org.restlet.resource.Get;
14import org.restlet.resource.ServerResource;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
18public class AddFlowResource extends ServerResource {
19
20 protected static Logger log = LoggerFactory.getLogger(AddFlowResource.class);
21
22 @Get("json")
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -080023 public FlowId retrieve() {
24 FlowId result = new FlowId();
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080025
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 Radoslavov2013cbb2013-02-26 10:15:18 -080035 //
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080036 // Extract the arguments
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080037 // NOTE: The "flow" is specified in JSON format.
38 //
39 ObjectMapper mapper = new ObjectMapper();
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080040 String flowPathStr = (String) getRequestAttributes().get("flow");
Pavlin Radoslavovb6f53542013-03-01 16:02:14 -080041 // TODO: Remove it later
42 // String flowPathStr = "{\"flowId\":{\"value\":\"5\"},\"installerId\":{\"value\":\"FOOBAR\"},\"dataPath\":{\"srcPort\":{\"dpid\":{\"value\":\"00:00:00:00:00:00:00:01\"},\"port\":{\"value\":0}},\"dstPort\":{\"dpid\":{\"value\":\"00:00:00:00:00:00:00:02\"},\"port\":{\"value\":0}},\"flowEntries\":[{\"flowEntryId\":null,\"flowEntryMatch\":null,\"flowEntryActions\":null,\"dpid\":{\"value\":\"00:00:00:00:00:00:00:01\"},\"inPort\":{\"value\":0},\"outPort\":{\"value\":1},\"flowEntryUserState\":\"FE_USER_UNKNOWN\",\"flowEntrySwitchState\":\"FE_SWITCH_UNKNOWN\",\"flowEntryErrorState\":null},{\"flowEntryId\":null,\"flowEntryMatch\":null,\"flowEntryActions\":null,\"dpid\":{\"value\":\"00:00:00:00:00:00:00:02\"},\"inPort\":{\"value\":9},\"outPort\":{\"value\":0},\"flowEntryUserState\":\"FE_USER_UNKNOWN\",\"flowEntrySwitchState\":\"FE_SWITCH_UNKNOWN\",\"flowEntryErrorState\":null}]}}";
43
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080044 FlowPath flowPath = null;
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080045 log.debug("Add Flow Path: " + flowPathStr);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080046 try {
47 flowPath = mapper.readValue(flowPathStr, FlowPath.class);
48 } catch (JsonGenerationException e) {
49 e.printStackTrace();
50 } catch (JsonMappingException e) {
51 e.printStackTrace();
52 } catch (IOException e) {
53 e.printStackTrace();
54 }
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080055
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080056 // Process the request
57 if (flowPath != null) {
58 if (flowService.addFlow(flowPath, result) != true) {
59 result = new FlowId(); // Error: Return empty Flow Id
60 }
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -080061 }
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080062
63 return result;
64 }
65}