blob: 1445e4b7f59f20b605b7cb321fd102d1ec93ed4f [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;
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -070022import net.onrc.onos.core.util.Dpid;
Jonathan Hartaa380972014-04-03 10:24:46 -070023
Toshio Koide3738ee52014-02-12 14:57:39 -080024import org.codehaus.jackson.JsonGenerationException;
25import org.codehaus.jackson.JsonNode;
26import org.codehaus.jackson.map.JsonMappingException;
Toshio Koide3738ee52014-02-12 14:57:39 -080027import org.codehaus.jackson.map.ObjectMapper;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080028import org.codehaus.jackson.node.ArrayNode;
Nick Karanatsios88948d32014-02-18 15:14:30 -080029import org.codehaus.jackson.node.ObjectNode;
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080030import org.restlet.resource.Delete;
Nick Karanatsios88948d32014-02-18 15:14:30 -080031import org.restlet.resource.Get;
Jonathan Harta99ec672014-04-03 11:30:34 -070032import org.restlet.resource.Post;
33import org.restlet.resource.ServerResource;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080036
37/**
Toshio Koide3738ee52014-02-12 14:57:39 -080038 * @author nickkaranatsios
39 */
40public class IntentResource extends ServerResource {
Ray Milkeyec838942014-04-09 11:28:43 -070041 private static final Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080042 // TODO need to assign proper application id.
Ray Milkey2476cac2014-04-08 11:03:21 -070043 private static final String APPLN_ID = "1";
Ray Milkey9c8a2132014-04-02 15:16:42 -070044
Toshio Koide3738ee52014-02-12 14:57:39 -080045 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080046 public String store(String jsonIntent) throws IOException {
Ray Milkey9c8a2132014-04-02 15:16:42 -070047 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext()
Nick Karanatsios8abe7172014-02-19 20:31:48 -080048 .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
49 if (pathRuntime == null) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080050 log.warn("Failed to get path calc runtime");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080051 return "";
52 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080053 String reply = "";
Ray Milkey9c8a2132014-04-02 15:16:42 -070054 ObjectMapper mapper = new ObjectMapper();
55 JsonNode jNode = null;
56 try {
57 jNode = mapper.readValue(jsonIntent, JsonNode.class);
58 } catch (JsonGenerationException ex) {
59 log.error("JsonGeneration exception ", ex);
60 } catch (JsonMappingException ex) {
61 log.error("JsonMappingException occurred", ex);
62 } catch (IOException ex) {
63 log.error("IOException occurred", ex);
64 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080065
Ray Milkey9c8a2132014-04-02 15:16:42 -070066 if (jNode != null) {
67 reply = parseJsonNode(jNode.getElements(), pathRuntime);
68 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080069 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -080070 }
Ray Milkey9c8a2132014-04-02 15:16:42 -070071
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080072 @Delete("json")
73 public String store() {
Ray Milkey9c8a2132014-04-02 15:16:42 -070074 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080075 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
76 pathRuntime.purgeIntents();
77 // TODO no reply yet from the purge intents call
78 return "";
Ray Milkey9c8a2132014-04-02 15:16:42 -070079
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080080 }
Toshio Koide3738ee52014-02-12 14:57:39 -080081
Nick Karanatsios88948d32014-02-18 15:14:30 -080082 @Get("json")
Nick Karanatsios8abe7172014-02-19 20:31:48 -080083 public String retrieve() throws IOException {
Ray Milkey9c8a2132014-04-02 15:16:42 -070084 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
Nick Karanatsios8abe7172014-02-19 20:31:48 -080085 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
Ray Milkey9c8a2132014-04-02 15:16:42 -070086
Nick Karanatsios87e8be72014-02-21 23:45:37 -080087 String intentCategory = (String) getRequestAttributes().get("category");
88 IntentMap intentMap = null;
89 if (intentCategory.equals("high")) {
90 intentMap = pathRuntime.getHighLevelIntents();
91 } else {
92 intentMap = pathRuntime.getPathIntents();
93 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -080094 ObjectMapper mapper = new ObjectMapper();
95 String restStr = "";
Nick Karanatsiosed645df2014-02-20 23:22:29 -080096
97 String intentId = (String) getRequestAttributes().get("intent_id");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080098 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios8abe7172014-02-19 20:31:48 -080099 Collection<Intent> intents = intentMap.getAllIntents();
100 if (!intents.isEmpty()) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700101 if ((intentId != null)) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800102 String applnIntentId = APPLN_ID + ":" + intentId;
103 Intent intent = intentMap.getIntent(applnIntentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800104 if (intent != null) {
105 ObjectNode node = mapper.createObjectNode();
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800106 // TODO refactor/remove duplicate code.
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800107 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800108 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800109 LinkedList<String> logs = intent.getLogs();
110 ArrayNode logNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700111 for (String intentLog : logs) {
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800112 logNode.add(intentLog);
113 }
114 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800115 arrayNode.add(node);
116 }
117 } else {
118 for (Intent intent : intents) {
119 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800120 String applnIntentId = intent.getId();
121 intentId = applnIntentId.split(":")[1];
122 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800123 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800124 LinkedList<String> logs = intent.getLogs();
125 ArrayNode logNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700126 for (String intentLog : logs) {
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800127 logNode.add(intentLog);
128 }
129 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800130 arrayNode.add(node);
131 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800132 }
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800133 restStr = mapper.writeValueAsString(arrayNode);
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800134 }
135 return restStr;
Nick Karanatsios88948d32014-02-18 15:14:30 -0800136 }
Ray Milkey9c8a2132014-04-02 15:16:42 -0700137
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800138 private String parseJsonNode(Iterator<JsonNode> nodes,
Ray Milkey9c8a2132014-04-02 15:16:42 -0700139 IPathCalcRuntimeService pathRuntime) throws IOException {
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800140 IntentOperationList operations = new IntentOperationList();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800141 ObjectMapper mapper = new ObjectMapper();
142 ArrayNode arrayNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700143 while (nodes.hasNext()) {
144 JsonNode node = nodes.next();
145 if (node.isObject()) {
146 JsonNode data;
147 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800148 Map<String, Object> fields = new HashMap<>();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700149 while (fieldNames.hasNext()) {
150 String fieldName = fieldNames.next();
151 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800152 parseFields(data, fieldName, fields);
Ray Milkey9c8a2132014-04-02 15:16:42 -0700153 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800154 Intent intent = processIntent(fields, operations);
Ray Milkey9c8a2132014-04-02 15:16:42 -0700155 appendIntentStatus(intent, (String) fields.get("intent_id"), mapper, arrayNode);
156 }
157 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800158 pathRuntime.executeIntentOperations(operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800159 return mapper.writeValueAsString(arrayNode);
Toshio Koide3738ee52014-02-12 14:57:39 -0800160 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800161
Ray Milkey9c8a2132014-04-02 15:16:42 -0700162 private void appendIntentStatus(Intent intent, final String intentId,
163 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
Nick Karanatsios88948d32014-02-18 15:14:30 -0800164 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800165 node.put("intent_id", intentId);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800166 node.put("status", intent.getState().toString());
167 LinkedList<String> logs = intent.getLogs();
168 ArrayNode logNode = mapper.createArrayNode();
169 for (String intentLog : logs) {
170 logNode.add(intentLog);
171 }
172 node.put("log", logNode);
Nick Karanatsios88948d32014-02-18 15:14:30 -0800173 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800174 }
Ray Milkey9c8a2132014-04-02 15:16:42 -0700175
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800176 private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700177 String intentType = (String) fields.get("intent_type");
178 String intentOp = (String) fields.get("intent_op");
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800179 Intent intent;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700180 String intentId = (String) fields.get("intent_id");
Toshio Koide565d6dd2014-03-27 11:22:25 -0700181 boolean pathFrozen = false;
182 if (intentId.startsWith("F")) { // TODO define REST API for frozen intents
183 pathFrozen = true;
184 intentId = intentId.substring(1);
185 }
186 String applnIntentId = APPLN_ID + ":" + intentId;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700187
Nick Karanatsios88948d32014-02-18 15:14:30 -0800188 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
189 if ((intentOp.equals("remove"))) {
190 operation = IntentOperation.Operator.REMOVE;
191 }
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700192 Dpid srcSwitchDpid = new Dpid((String) fields.get("srcSwitch"));
193 Dpid dstSwitchDpid = new Dpid((String) fields.get("dstSwitch"));
194
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800195 if (intentType.equals("shortest_intent_type")) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800196 ShortestPathIntent spi = new ShortestPathIntent(applnIntentId,
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700197 srcSwitchDpid.value(),
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800198 (long) fields.get("srcPort"),
199 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700200 dstSwitchDpid.value(),
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800201 (long) fields.get("dstPort"),
202 MACAddress.valueOf((String) fields.get("dstMac")).toLong());
Toshio Koide565d6dd2014-03-27 11:22:25 -0700203 spi.setPathFrozen(pathFrozen);
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800204 operations.add(operation, spi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800205 intent = spi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800206 } else {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800207 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800208 Long.decode((String) fields.get("srcSwitch")),
209 (long) fields.get("srcPort"),
210 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
211 Long.decode((String) fields.get("dstSwitch")),
212 (long) fields.get("dstPort"),
213 MACAddress.valueOf((String) fields.get("dstMac")).toLong(),
214 (double) fields.get("bandwidth"));
Toshio Koide565d6dd2014-03-27 11:22:25 -0700215 cspi.setPathFrozen(pathFrozen);
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800216 operations.add(operation, cspi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800217 intent = cspi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800218 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800219 return intent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800220 }
221
222 private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) {
223 if ((node.isTextual())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800224 fields.put(fieldName, node.getTextValue());
225 } else if ((node.isInt())) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700226 fields.put(fieldName, (long) node.getIntValue());
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800227 } else if (node.isDouble()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800228 fields.put(fieldName, node.getDoubleValue());
229 } else if ((node.isLong())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800230 fields.put(fieldName, node.getLongValue());
231 }
232 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800233}