blob: 909ec82a5ab0961f878cecb17c7aaa62bc284b93 [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 */
Jonathan Hart6df90172014-04-03 10:13:11 -07005package net.onrc.onos.core.datagrid.web;
Toshio Koide3738ee52014-02-12 14:57:39 -08006
7import java.io.IOException;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08008import java.util.Collection;
Jonathan Harta99ec672014-04-03 11:30:34 -07009import java.util.HashMap;
Toshio Koide3738ee52014-02-12 14:57:39 -080010import java.util.Iterator;
Jonathan Harta99ec672014-04-03 11:30:34 -070011import java.util.LinkedList;
12import java.util.Map;
Ray Milkey9c8a2132014-04-02 15:16:42 -070013
Jonathan Harta99ec672014-04-03 11:30:34 -070014import net.floodlightcontroller.util.MACAddress;
Jonathan Hartaa380972014-04-03 10:24:46 -070015import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
16import net.onrc.onos.core.intent.Intent;
17import net.onrc.onos.core.intent.IntentMap;
18import net.onrc.onos.core.intent.IntentOperation;
19import net.onrc.onos.core.intent.IntentOperationList;
20import net.onrc.onos.core.intent.ShortestPathIntent;
21import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
22
Toshio Koide3738ee52014-02-12 14:57:39 -080023import org.codehaus.jackson.JsonGenerationException;
24import org.codehaus.jackson.JsonNode;
25import org.codehaus.jackson.map.JsonMappingException;
Toshio Koide3738ee52014-02-12 14:57:39 -080026import org.codehaus.jackson.map.ObjectMapper;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080027import 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;
Jonathan Harta99ec672014-04-03 11:30:34 -070031import org.restlet.resource.Post;
32import org.restlet.resource.ServerResource;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080035
36/**
Toshio Koide3738ee52014-02-12 14:57:39 -080037 * @author nickkaranatsios
38 */
39public class IntentResource extends ServerResource {
Ray Milkeyec838942014-04-09 11:28:43 -070040 private static final Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080041 // TODO need to assign proper application id.
Ray Milkey2476cac2014-04-08 11:03:21 -070042 private static final String APPLN_ID = "1";
Ray Milkey9c8a2132014-04-02 15:16:42 -070043
Toshio Koide3738ee52014-02-12 14:57:39 -080044 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080045 public String store(String jsonIntent) throws IOException {
Ray Milkey9c8a2132014-04-02 15:16:42 -070046 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext()
Nick Karanatsios8abe7172014-02-19 20:31:48 -080047 .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
48 if (pathRuntime == null) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080049 log.warn("Failed to get path calc runtime");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080050 return "";
51 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080052 String reply = "";
Ray Milkey9c8a2132014-04-02 15:16:42 -070053 ObjectMapper mapper = new ObjectMapper();
54 JsonNode jNode = null;
55 try {
56 jNode = mapper.readValue(jsonIntent, JsonNode.class);
57 } catch (JsonGenerationException ex) {
58 log.error("JsonGeneration exception ", ex);
59 } catch (JsonMappingException ex) {
60 log.error("JsonMappingException occurred", ex);
61 } catch (IOException ex) {
62 log.error("IOException occurred", ex);
63 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080064
Ray Milkey9c8a2132014-04-02 15:16:42 -070065 if (jNode != null) {
66 reply = parseJsonNode(jNode.getElements(), pathRuntime);
67 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080068 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -080069 }
Ray Milkey9c8a2132014-04-02 15:16:42 -070070
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080071 @Delete("json")
72 public String store() {
Ray Milkey9c8a2132014-04-02 15:16:42 -070073 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080074 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
75 pathRuntime.purgeIntents();
76 // TODO no reply yet from the purge intents call
77 return "";
Ray Milkey9c8a2132014-04-02 15:16:42 -070078
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080079 }
Toshio Koide3738ee52014-02-12 14:57:39 -080080
Nick Karanatsios88948d32014-02-18 15:14:30 -080081 @Get("json")
Nick Karanatsios8abe7172014-02-19 20:31:48 -080082 public String retrieve() throws IOException {
Ray Milkey9c8a2132014-04-02 15:16:42 -070083 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
Nick Karanatsios8abe7172014-02-19 20:31:48 -080084 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
Ray Milkey9c8a2132014-04-02 15:16:42 -070085
Nick Karanatsios87e8be72014-02-21 23:45:37 -080086 String intentCategory = (String) getRequestAttributes().get("category");
87 IntentMap intentMap = null;
88 if (intentCategory.equals("high")) {
89 intentMap = pathRuntime.getHighLevelIntents();
90 } else {
91 intentMap = pathRuntime.getPathIntents();
92 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -080093 ObjectMapper mapper = new ObjectMapper();
94 String restStr = "";
Nick Karanatsiosed645df2014-02-20 23:22:29 -080095
96 String intentId = (String) getRequestAttributes().get("intent_id");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080097 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios8abe7172014-02-19 20:31:48 -080098 Collection<Intent> intents = intentMap.getAllIntents();
99 if (!intents.isEmpty()) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700100 if ((intentId != null)) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800101 String applnIntentId = APPLN_ID + ":" + intentId;
102 Intent intent = intentMap.getIntent(applnIntentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800103 if (intent != null) {
104 ObjectNode node = mapper.createObjectNode();
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800105 // TODO refactor/remove duplicate code.
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800106 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800107 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800108 LinkedList<String> logs = intent.getLogs();
109 ArrayNode logNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700110 for (String intentLog : logs) {
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800111 logNode.add(intentLog);
112 }
113 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800114 arrayNode.add(node);
115 }
116 } else {
117 for (Intent intent : intents) {
118 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800119 String applnIntentId = intent.getId();
120 intentId = applnIntentId.split(":")[1];
121 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800122 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800123 LinkedList<String> logs = intent.getLogs();
124 ArrayNode logNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700125 for (String intentLog : logs) {
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800126 logNode.add(intentLog);
127 }
128 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800129 arrayNode.add(node);
130 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800131 }
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800132 restStr = mapper.writeValueAsString(arrayNode);
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800133 }
134 return restStr;
Nick Karanatsios88948d32014-02-18 15:14:30 -0800135 }
Ray Milkey9c8a2132014-04-02 15:16:42 -0700136
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800137 private String parseJsonNode(Iterator<JsonNode> nodes,
Ray Milkey9c8a2132014-04-02 15:16:42 -0700138 IPathCalcRuntimeService pathRuntime) throws IOException {
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800139 IntentOperationList operations = new IntentOperationList();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800140 ObjectMapper mapper = new ObjectMapper();
141 ArrayNode arrayNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700142 while (nodes.hasNext()) {
143 JsonNode node = nodes.next();
144 if (node.isObject()) {
145 JsonNode data;
146 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800147 Map<String, Object> fields = new HashMap<>();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700148 while (fieldNames.hasNext()) {
149 String fieldName = fieldNames.next();
150 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800151 parseFields(data, fieldName, fields);
Ray Milkey9c8a2132014-04-02 15:16:42 -0700152 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800153 Intent intent = processIntent(fields, operations);
Ray Milkey9c8a2132014-04-02 15:16:42 -0700154 appendIntentStatus(intent, (String) fields.get("intent_id"), mapper, arrayNode);
155 }
156 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800157 pathRuntime.executeIntentOperations(operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800158 return mapper.writeValueAsString(arrayNode);
Toshio Koide3738ee52014-02-12 14:57:39 -0800159 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800160
Ray Milkey9c8a2132014-04-02 15:16:42 -0700161 private void appendIntentStatus(Intent intent, final String intentId,
162 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
Nick Karanatsios88948d32014-02-18 15:14:30 -0800163 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800164 node.put("intent_id", intentId);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800165 node.put("status", intent.getState().toString());
166 LinkedList<String> logs = intent.getLogs();
167 ArrayNode logNode = mapper.createArrayNode();
168 for (String intentLog : logs) {
169 logNode.add(intentLog);
170 }
171 node.put("log", logNode);
Nick Karanatsios88948d32014-02-18 15:14:30 -0800172 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800173 }
Ray Milkey9c8a2132014-04-02 15:16:42 -0700174
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800175 private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700176 String intentType = (String) fields.get("intent_type");
177 String intentOp = (String) fields.get("intent_op");
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800178 Intent intent;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700179 String intentId = (String) fields.get("intent_id");
Toshio Koide565d6dd2014-03-27 11:22:25 -0700180 boolean pathFrozen = false;
181 if (intentId.startsWith("F")) { // TODO define REST API for frozen intents
182 pathFrozen = true;
183 intentId = intentId.substring(1);
184 }
185 String applnIntentId = APPLN_ID + ":" + intentId;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700186
Nick Karanatsios88948d32014-02-18 15:14:30 -0800187 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
188 if ((intentOp.equals("remove"))) {
189 operation = IntentOperation.Operator.REMOVE;
190 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800191 if (intentType.equals("shortest_intent_type")) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800192 ShortestPathIntent spi = new ShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800193 Long.decode((String) fields.get("srcSwitch")),
194 (long) fields.get("srcPort"),
195 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
196 Long.decode((String) fields.get("dstSwitch")),
197 (long) fields.get("dstPort"),
198 MACAddress.valueOf((String) fields.get("dstMac")).toLong());
Toshio Koide565d6dd2014-03-27 11:22:25 -0700199 spi.setPathFrozen(pathFrozen);
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800200 operations.add(operation, spi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800201 intent = spi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800202 } else {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800203 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800204 Long.decode((String) fields.get("srcSwitch")),
205 (long) fields.get("srcPort"),
206 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
207 Long.decode((String) fields.get("dstSwitch")),
208 (long) fields.get("dstPort"),
209 MACAddress.valueOf((String) fields.get("dstMac")).toLong(),
210 (double) fields.get("bandwidth"));
Toshio Koide565d6dd2014-03-27 11:22:25 -0700211 cspi.setPathFrozen(pathFrozen);
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800212 operations.add(operation, cspi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800213 intent = cspi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800214 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800215 return intent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800216 }
217
218 private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) {
219 if ((node.isTextual())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800220 fields.put(fieldName, node.getTextValue());
221 } else if ((node.isInt())) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700222 fields.put(fieldName, (long) node.getIntValue());
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800223 } else if (node.isDouble()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800224 fields.put(fieldName, node.getDoubleValue());
225 } else if ((node.isLong())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800226 fields.put(fieldName, node.getLongValue());
227 }
228 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800229}