blob: b07be6b6d8e8e66fb428356c83741e8ae43c769a [file] [log] [blame]
Toshio Koide3738ee52014-02-12 14:57:39 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.datagrid.web;
6
7import java.io.IOException;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08008import java.util.Collection;
Toshio Koide3738ee52014-02-12 14:57:39 -08009import java.util.Iterator;
Toshio Koide3738ee52014-02-12 14:57:39 -080010import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koide3738ee52014-02-12 14:57:39 -080011import net.onrc.onos.intent.ShortestPathIntent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080012import net.onrc.onos.intent.IntentOperation;
13import net.onrc.onos.intent.IntentMap;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080014import net.onrc.onos.intent.Intent;
15import net.onrc.onos.intent.runtime.IPathCalcRuntimeService;
16import net.onrc.onos.intent.IntentOperationList;
Toshio Koide3738ee52014-02-12 14:57:39 -080017import org.codehaus.jackson.JsonGenerationException;
18import org.codehaus.jackson.JsonNode;
19import org.codehaus.jackson.map.JsonMappingException;
20import org.restlet.resource.Post;
21import org.restlet.resource.ServerResource;
22import org.codehaus.jackson.map.ObjectMapper;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080023import net.floodlightcontroller.util.MACAddress;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080024import java.util.HashMap;
Nick Karanatsios87e8be72014-02-21 23:45:37 -080025import java.util.LinkedList;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080026import java.util.Map;
27import org.codehaus.jackson.node.ArrayNode;
Nick Karanatsios88948d32014-02-18 15:14:30 -080028import org.codehaus.jackson.node.ObjectNode;
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080029import org.restlet.resource.Delete;
Nick Karanatsios88948d32014-02-18 15:14:30 -080030import org.restlet.resource.Get;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080033
34/**
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080035 *
Toshio Koide3738ee52014-02-12 14:57:39 -080036 * @author nickkaranatsios
37 */
38public class IntentResource extends ServerResource {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080039 private final static Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080040 // TODO need to assign proper application id.
41 private final String APPLN_ID = "1";
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080042
Toshio Koide3738ee52014-02-12 14:57:39 -080043 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080044 public String store(String jsonIntent) throws IOException {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080045 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext()
46 .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
47 if (pathRuntime == null) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080048 log.warn("Failed to get path calc runtime");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080049 return "";
50 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080051 String reply = "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080052 ObjectMapper mapper = new ObjectMapper();
53 JsonNode jNode = null;
54 try {
Nick Karanatsios88948d32014-02-18 15:14:30 -080055 jNode = mapper.readValue(jsonIntent, JsonNode.class);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080056 } catch (JsonGenerationException ex) {
57 log.error("JsonGeneration exception ", ex);
58 } catch (JsonMappingException ex) {
59 log.error("JsonMappingException occurred", ex);
60 } catch (IOException ex) {
61 log.error("IOException occurred", ex);
62 }
63
64 if (jNode != null) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080065 reply = parseJsonNode(jNode.getElements(), pathRuntime);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080066 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080067 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -080068 }
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080069
70 @Delete("json")
71 public String store() {
72 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext().
73 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
74 pathRuntime.purgeIntents();
75 // TODO no reply yet from the purge intents call
76 return "";
77
78 }
Toshio Koide3738ee52014-02-12 14:57:39 -080079
Nick Karanatsios88948d32014-02-18 15:14:30 -080080 @Get("json")
Nick Karanatsios8abe7172014-02-19 20:31:48 -080081 public String retrieve() throws IOException {
82 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext().
83 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
Nick Karanatsios87e8be72014-02-21 23:45:37 -080084
85 String intentCategory = (String) getRequestAttributes().get("category");
86 IntentMap intentMap = null;
87 if (intentCategory.equals("high")) {
88 intentMap = pathRuntime.getHighLevelIntents();
89 } else {
90 intentMap = pathRuntime.getPathIntents();
91 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -080092 ObjectMapper mapper = new ObjectMapper();
93 String restStr = "";
Nick Karanatsiosed645df2014-02-20 23:22:29 -080094
95 String intentId = (String) getRequestAttributes().get("intent_id");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080096 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios8abe7172014-02-19 20:31:48 -080097 Collection<Intent> intents = intentMap.getAllIntents();
98 if (!intents.isEmpty()) {
Nick Karanatsiosed645df2014-02-20 23:22:29 -080099 if ((intentId != null )) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800100 String applnIntentId = APPLN_ID + ":" + intentId;
101 Intent intent = intentMap.getIntent(applnIntentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800102 if (intent != null) {
103 ObjectNode node = mapper.createObjectNode();
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800104 // TODO refactor/remove duplicate code.
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800105 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800106 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800107 LinkedList<String> logs = intent.getLogs();
108 ArrayNode logNode = mapper.createArrayNode();
109 for (String intentLog :logs) {
110 logNode.add(intentLog);
111 }
112 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800113 arrayNode.add(node);
114 }
115 } else {
116 for (Intent intent : intents) {
117 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800118 String applnIntentId = intent.getId();
119 intentId = applnIntentId.split(":")[1];
120 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800121 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800122 LinkedList<String> logs = intent.getLogs();
123 ArrayNode logNode = mapper.createArrayNode();
124 for (String intentLog :logs) {
125 logNode.add(intentLog);
126 }
127 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800128 arrayNode.add(node);
129 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800130 }
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800131 restStr = mapper.writeValueAsString(arrayNode);
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800132 }
133 return restStr;
Nick Karanatsios88948d32014-02-18 15:14:30 -0800134 }
135
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800136 private String parseJsonNode(Iterator<JsonNode> nodes,
137 IPathCalcRuntimeService pathRuntime) throws IOException {
138 IntentOperationList operations = new IntentOperationList();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800139 ObjectMapper mapper = new ObjectMapper();
140 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800141 while (nodes.hasNext()) {
142 JsonNode node = nodes.next();
143 if (node.isObject()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800144 JsonNode data;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800145 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800146 Map<String, Object> fields = new HashMap<>();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800147 while (fieldNames.hasNext()) {
148 String fieldName = fieldNames.next();
149 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800150 parseFields(data, fieldName, fields);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800151 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800152 Intent intent = processIntent(fields, operations);
153 appendIntentStatus(intent, (String)fields.get("intent_id"), mapper, arrayNode);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800154 }
155 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800156 pathRuntime.executeIntentOperations(operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800157 return mapper.writeValueAsString(arrayNode);
Toshio Koide3738ee52014-02-12 14:57:39 -0800158 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800159
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800160 private void appendIntentStatus(Intent intent, final String intentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800161 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
Nick Karanatsios88948d32014-02-18 15:14:30 -0800162 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800163 node.put("intent_id", intentId);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800164 node.put("status", intent.getState().toString());
165 LinkedList<String> logs = intent.getLogs();
166 ArrayNode logNode = mapper.createArrayNode();
167 for (String intentLog : logs) {
168 logNode.add(intentLog);
169 }
170 node.put("log", logNode);
Nick Karanatsios88948d32014-02-18 15:14:30 -0800171 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800172 }
173
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800174 private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800175 String intentType = (String)fields.get("intent_type");
Nick Karanatsios88948d32014-02-18 15:14:30 -0800176 String intentOp = (String)fields.get("intent_op");
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800177 Intent intent;
Toshio Koide565d6dd2014-03-27 11:22:25 -0700178 String intentId = (String)fields.get("intent_id");
179 boolean pathFrozen = false;
180 if (intentId.startsWith("F")) { // TODO define REST API for frozen intents
181 pathFrozen = true;
182 intentId = intentId.substring(1);
183 }
184 String applnIntentId = APPLN_ID + ":" + intentId;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800185
Nick Karanatsios88948d32014-02-18 15:14:30 -0800186 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
187 if ((intentOp.equals("remove"))) {
188 operation = IntentOperation.Operator.REMOVE;
189 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800190 if (intentType.equals("shortest_intent_type")) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800191 ShortestPathIntent spi = new ShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800192 Long.decode((String) fields.get("srcSwitch")),
193 (long) fields.get("srcPort"),
194 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
195 Long.decode((String) fields.get("dstSwitch")),
196 (long) fields.get("dstPort"),
197 MACAddress.valueOf((String) fields.get("dstMac")).toLong());
Toshio Koide565d6dd2014-03-27 11:22:25 -0700198 spi.setPathFrozen(pathFrozen);
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800199 operations.add(operation, spi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800200 intent = spi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800201 } else {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800202 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800203 Long.decode((String) fields.get("srcSwitch")),
204 (long) fields.get("srcPort"),
205 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
206 Long.decode((String) fields.get("dstSwitch")),
207 (long) fields.get("dstPort"),
208 MACAddress.valueOf((String) fields.get("dstMac")).toLong(),
209 (double) fields.get("bandwidth"));
Toshio Koide565d6dd2014-03-27 11:22:25 -0700210 cspi.setPathFrozen(pathFrozen);
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800211 operations.add(operation, cspi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800212 intent = cspi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800213 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800214 return intent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800215 }
216
217 private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) {
218 if ((node.isTextual())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800219 fields.put(fieldName, node.getTextValue());
220 } else if ((node.isInt())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800221 fields.put(fieldName, (long)node.getIntValue());
222 } else if (node.isDouble()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800223 fields.put(fieldName, node.getDoubleValue());
224 } else if ((node.isLong())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800225 fields.put(fieldName, node.getLongValue());
226 }
227 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800228}