blob: 72a39551f88d5265d6b57c9902ce0a0744216488 [file] [log] [blame]
Pavlin Radoslavovb743a202013-03-04 13:54:57 -08001package net.floodlightcontroller.flowcache.web;
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -08002
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08003import java.io.IOException;
4
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -08005import net.floodlightcontroller.util.FlowId;
6import net.floodlightcontroller.util.FlowPath;
HIGUCHI Yutaedf81d72013-06-12 12:06:57 -07007import net.onrc.onos.ofcontroller.flowcache.IFlowService;
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 Radoslavov4d4fc4e2013-03-04 13:49:04 -080013import org.restlet.resource.Post;
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080014import 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
Pavlin Radoslavov4d4fc4e2013-03-04 13:49:04 -080022 @Post("json")
23 public FlowId store(String flowJson) {
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -080024 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 Radoslavov4d4fc4e2013-03-04 13:49:04 -080040 String flowPathStr = flowJson;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080041 FlowPath flowPath = null;
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080042 log.debug("Add Flow Path: " + flowPathStr);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080043 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 Radoslavov5ab68512013-02-18 09:59:33 -080052
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080053 // Process the request
54 if (flowPath != null) {
Pavlin Radoslavovdbaaf2e2013-03-29 04:25:55 -070055 if (flowService.addFlow(flowPath, result, null) != true) {
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080056 result = new FlowId(); // Error: Return empty Flow Id
57 }
Pavlin Radoslavova10a9a82013-02-22 11:47:54 -080058 }
Pavlin Radoslavov5ab68512013-02-18 09:59:33 -080059
60 return result;
61 }
62}