package net.floodlightcontroller.flowcache.web;

import java.io.IOException;

import net.floodlightcontroller.flowcache.IFlowService;
import net.floodlightcontroller.util.FlowId;
import net.floodlightcontroller.util.FlowPath;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AddFlowResource extends ServerResource {

    protected static Logger log = LoggerFactory.getLogger(AddFlowResource.class);

    @Get("json")
    public FlowId retrieve() {
	FlowId result = new FlowId();

        IFlowService flowService =
                (IFlowService)getContext().getAttributes().
                get(IFlowService.class.getCanonicalName());

        if (flowService == null) {
	    log.debug("ONOS Flow Service not found");
            return result;
	}

	//
	// Extract the arguments
	// NOTE: The "flow" is specified in JSON format.
	//
	ObjectMapper mapper = new ObjectMapper();
	String flowPathStr = (String) getRequestAttributes().get("flow");
	// TODO: Remove it later
	// 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}]}}";

	FlowPath flowPath = null;
	log.debug("Add Flow Path: " + flowPathStr);
	try {
	    flowPath = mapper.readValue(flowPathStr, FlowPath.class);
	} catch (JsonGenerationException e) {
	    e.printStackTrace();
	} catch (JsonMappingException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}

	// Process the request
	if (flowPath != null) {
	    if (flowService.addFlow(flowPath, result) != true) {
		result = new FlowId();		// Error: Return empty Flow Id
	    }
	}

        return result;
    }
}

@@ -1,8 +1,14 @@
+flowId
 package 
@@ -47,16 +47,16 @@
 he.web;%0A
-
 %0Aimport 
@@ -453,18 +453,19 @@
 esource.
-Ge
+Pos
 t;%0Aimpor
@@ -704,18 +704,19 @@
 ;%0A%0A    @
-Ge
+Pos
 t(%22json%22
@@ -739,17 +739,29 @@
 wId 
-retrieve(
+store(String flowJson
 ) %7B%0A
@@ -1214,966 +1214,17 @@
 r = 
-(String) getRequestAttributes().get(%22flow%22);%0A%09// TODO: Remove it later%0A%09// String flowPathStr = %22%7B%5C%22flowId%5C%22:%7B%5C%22value%5C%22:%5C%225%5C%22%7D,%5C%22installerId%5C%22:%7B%5C%22value%5C%22:%5C%22FOOBAR%5C%22%7D,%5C%22dataPath%5C%22:%7B%5C%22srcPort%5C%22:%7B%5C%22dpid%5C%22:%7B%5C%22value%5C%22:%5C%2200:00:00:00:00:00:00:01%5C%22%7D,%5C%22port%5C%22:%7B%5C%22value%5C%22:0%7D%7D,%5C%22dstPort%5C%22:%7B%5C%22dpid%5C%22:%7B%5C%22value%5C%22:%5C%2200:00:00:00:00:00:00:02%5C%22%7D,%5C%22port%5C%22:%7B%5C%22value%5C%22:0%7D%7D,%5C%22flowEntries%5C%22:%5B%7B%5C%22flowEntryId%5C%22:null,%5C%22flowEntryMatch%5C%22:null,%5C%22flowEntryActions%5C%22:null,%5C%22dpid%5C%22:%7B%5C%22value%5C%22:%5C%2200:00:00:00:00:00:00:01%5C%22%7D,%5C%22inPort%5C%22:%7B%5C%22value%5C%22:0%7D,%5C%22outPort%5C%22:%7B%5C%22value%5C%22:1%7D,%5C%22flowEntryUserState%5C%22:%5C%22FE_USER_UNKNOWN%5C%22,%5C%22flowEntrySwitchState%5C%22:%5C%22FE_SWITCH_UNKNOWN%5C%22,%5C%22flowEntryErrorState%5C%22:null%7D,%7B%5C%22flowEntryId%5C%22:null,%5C%22flowEntryMatch%5C%22:null,%5C%22flowEntryActions%5C%22:null,%5C%22dpid%5C%22:%7B%5C%22value%5C%22:%5C%2200:00:00:00:00:00:00:02%5C%22%7D,%5C%22inPort%5C%22:%7B%5C%22value%5C%22:9%7D,%5C%22outPort%5C%22:%7B%5C%22value%5C%22:0%7D,%5C%22flowEntryUserState%5C%22:%5C%22FE_USER_UNKNOWN%5C%22,%5C%22flowEntrySwitchState%5C%22:%5C%22FE_SWITCH_UNKNOWN%5C%22,%5C%22flowEntryErrorState%5C%22:null%7D%5D%7D%7D%22;%0A
+flowJson;
 %0A%09Fl

Use "POST" instead of "GET" to install a flow via the REST API.