blob: 1dc05a278e4c588cb2923b190ba3de2735b359f5 [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datagrid.web;
Toshio Koide3738ee52014-02-12 14:57:39 -08002
3import java.io.IOException;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08004import java.util.Collection;
Jonathan Harta99ec672014-04-03 11:30:34 -07005import java.util.HashMap;
Toshio Koide3738ee52014-02-12 14:57:39 -08006import java.util.Iterator;
Jonathan Harta99ec672014-04-03 11:30:34 -07007import java.util.LinkedList;
8import java.util.Map;
Ray Milkey9c8a2132014-04-02 15:16:42 -07009
Jonathan Harta99ec672014-04-03 11:30:34 -070010import net.floodlightcontroller.util.MACAddress;
Jonathan Hartaa380972014-04-03 10:24:46 -070011import net.onrc.onos.core.intent.ConstrainedShortestPathIntent;
12import net.onrc.onos.core.intent.Intent;
13import net.onrc.onos.core.intent.IntentMap;
14import net.onrc.onos.core.intent.IntentOperation;
15import net.onrc.onos.core.intent.IntentOperationList;
16import net.onrc.onos.core.intent.ShortestPathIntent;
17import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService;
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -070018import net.onrc.onos.core.util.Dpid;
Jonathan Hartaa380972014-04-03 10:24:46 -070019
Toshio Koide3738ee52014-02-12 14:57:39 -080020import org.codehaus.jackson.JsonGenerationException;
21import org.codehaus.jackson.JsonNode;
22import org.codehaus.jackson.map.JsonMappingException;
Toshio Koide3738ee52014-02-12 14:57:39 -080023import org.codehaus.jackson.map.ObjectMapper;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080024import org.codehaus.jackson.node.ArrayNode;
Nick Karanatsios88948d32014-02-18 15:14:30 -080025import org.codehaus.jackson.node.ObjectNode;
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080026import org.restlet.resource.Delete;
Nick Karanatsios88948d32014-02-18 15:14:30 -080027import org.restlet.resource.Get;
Jonathan Harta99ec672014-04-03 11:30:34 -070028import org.restlet.resource.Post;
29import org.restlet.resource.ServerResource;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080032
33/**
Yuta HIGUCHId4acc802014-06-19 22:30:31 -070034 * old REST API implementation for the Intents.
Toshio Koide3738ee52014-02-12 14:57:39 -080035 */
36public class IntentResource extends ServerResource {
Ray Milkeyec838942014-04-09 11:28:43 -070037 private static final Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080038 // TODO need to assign proper application id.
Ray Milkey2476cac2014-04-08 11:03:21 -070039 private static final String APPLN_ID = "1";
Ray Milkey9c8a2132014-04-02 15:16:42 -070040
Toshio Koide3738ee52014-02-12 14:57:39 -080041 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080042 public String store(String jsonIntent) throws IOException {
Ray Milkey9c8a2132014-04-02 15:16:42 -070043 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext()
Nick Karanatsios8abe7172014-02-19 20:31:48 -080044 .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
45 if (pathRuntime == null) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080046 log.warn("Failed to get path calc runtime");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080047 return "";
48 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080049 String reply = "";
Ray Milkey9c8a2132014-04-02 15:16:42 -070050 ObjectMapper mapper = new ObjectMapper();
51 JsonNode jNode = null;
52 try {
53 jNode = mapper.readValue(jsonIntent, JsonNode.class);
54 } catch (JsonGenerationException ex) {
55 log.error("JsonGeneration exception ", ex);
56 } catch (JsonMappingException ex) {
57 log.error("JsonMappingException occurred", ex);
58 } catch (IOException ex) {
59 log.error("IOException occurred", ex);
60 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080061
Ray Milkey9c8a2132014-04-02 15:16:42 -070062 if (jNode != null) {
63 reply = parseJsonNode(jNode.getElements(), pathRuntime);
64 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080065 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -080066 }
Ray Milkey9c8a2132014-04-02 15:16:42 -070067
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080068 @Delete("json")
69 public String store() {
Ray Milkey9c8a2132014-04-02 15:16:42 -070070 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080071 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
72 pathRuntime.purgeIntents();
73 // TODO no reply yet from the purge intents call
74 return "";
Ray Milkey9c8a2132014-04-02 15:16:42 -070075
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080076 }
Toshio Koide3738ee52014-02-12 14:57:39 -080077
Nick Karanatsios88948d32014-02-18 15:14:30 -080078 @Get("json")
Nick Karanatsios8abe7172014-02-19 20:31:48 -080079 public String retrieve() throws IOException {
Ray Milkey9c8a2132014-04-02 15:16:42 -070080 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext().
Nick Karanatsios8abe7172014-02-19 20:31:48 -080081 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
Ray Milkey9c8a2132014-04-02 15:16:42 -070082
Nick Karanatsios87e8be72014-02-21 23:45:37 -080083 String intentCategory = (String) getRequestAttributes().get("category");
84 IntentMap intentMap = null;
85 if (intentCategory.equals("high")) {
86 intentMap = pathRuntime.getHighLevelIntents();
87 } else {
88 intentMap = pathRuntime.getPathIntents();
89 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -080090 ObjectMapper mapper = new ObjectMapper();
91 String restStr = "";
Nick Karanatsiosed645df2014-02-20 23:22:29 -080092
93 String intentId = (String) getRequestAttributes().get("intent_id");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080094 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios8abe7172014-02-19 20:31:48 -080095 Collection<Intent> intents = intentMap.getAllIntents();
96 if (!intents.isEmpty()) {
Ray Milkey9c8a2132014-04-02 15:16:42 -070097 if ((intentId != null)) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080098 String applnIntentId = APPLN_ID + ":" + intentId;
99 Intent intent = intentMap.getIntent(applnIntentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800100 if (intent != null) {
101 ObjectNode node = mapper.createObjectNode();
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800102 // TODO refactor/remove duplicate code.
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800103 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800104 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800105 LinkedList<String> logs = intent.getLogs();
106 ArrayNode logNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700107 for (String intentLog : logs) {
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800108 logNode.add(intentLog);
109 }
110 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800111 arrayNode.add(node);
112 }
113 } else {
114 for (Intent intent : intents) {
115 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800116 String applnIntentId = intent.getId();
117 intentId = applnIntentId.split(":")[1];
118 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800119 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800120 LinkedList<String> logs = intent.getLogs();
121 ArrayNode logNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700122 for (String intentLog : logs) {
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800123 logNode.add(intentLog);
124 }
125 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800126 arrayNode.add(node);
127 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800128 }
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800129 restStr = mapper.writeValueAsString(arrayNode);
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800130 }
131 return restStr;
Nick Karanatsios88948d32014-02-18 15:14:30 -0800132 }
Ray Milkey9c8a2132014-04-02 15:16:42 -0700133
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800134 private String parseJsonNode(Iterator<JsonNode> nodes,
Ray Milkey9c8a2132014-04-02 15:16:42 -0700135 IPathCalcRuntimeService pathRuntime) throws IOException {
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800136 IntentOperationList operations = new IntentOperationList();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800137 ObjectMapper mapper = new ObjectMapper();
138 ArrayNode arrayNode = mapper.createArrayNode();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700139 while (nodes.hasNext()) {
140 JsonNode node = nodes.next();
141 if (node.isObject()) {
142 JsonNode data;
143 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800144 Map<String, Object> fields = new HashMap<>();
Ray Milkey9c8a2132014-04-02 15:16:42 -0700145 while (fieldNames.hasNext()) {
146 String fieldName = fieldNames.next();
147 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800148 parseFields(data, fieldName, fields);
Ray Milkey9c8a2132014-04-02 15:16:42 -0700149 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800150 Intent intent = processIntent(fields, operations);
Ray Milkey9c8a2132014-04-02 15:16:42 -0700151 appendIntentStatus(intent, (String) fields.get("intent_id"), mapper, arrayNode);
152 }
153 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800154 pathRuntime.executeIntentOperations(operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800155 return mapper.writeValueAsString(arrayNode);
Toshio Koide3738ee52014-02-12 14:57:39 -0800156 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800157
Ray Milkey9c8a2132014-04-02 15:16:42 -0700158 private void appendIntentStatus(Intent intent, final String intentId,
159 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
Nick Karanatsios88948d32014-02-18 15:14:30 -0800160 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800161 node.put("intent_id", intentId);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800162 node.put("status", intent.getState().toString());
163 LinkedList<String> logs = intent.getLogs();
164 ArrayNode logNode = mapper.createArrayNode();
165 for (String intentLog : logs) {
166 logNode.add(intentLog);
167 }
168 node.put("log", logNode);
Nick Karanatsios88948d32014-02-18 15:14:30 -0800169 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800170 }
Ray Milkey9c8a2132014-04-02 15:16:42 -0700171
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800172 private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) {
Ray Milkey9c8a2132014-04-02 15:16:42 -0700173 String intentType = (String) fields.get("intent_type");
174 String intentOp = (String) fields.get("intent_op");
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800175 Intent intent;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700176 String intentId = (String) fields.get("intent_id");
Toshio Koide565d6dd2014-03-27 11:22:25 -0700177 boolean pathFrozen = false;
178 if (intentId.startsWith("F")) { // TODO define REST API for frozen intents
179 pathFrozen = true;
180 intentId = intentId.substring(1);
181 }
182 String applnIntentId = APPLN_ID + ":" + intentId;
Ray Milkey9c8a2132014-04-02 15:16:42 -0700183
Nick Karanatsios88948d32014-02-18 15:14:30 -0800184 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
185 if ((intentOp.equals("remove"))) {
186 operation = IntentOperation.Operator.REMOVE;
187 }
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700188 Dpid srcSwitchDpid = new Dpid((String) fields.get("srcSwitch"));
189 Dpid dstSwitchDpid = new Dpid((String) fields.get("dstSwitch"));
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,
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700193 srcSwitchDpid.value(),
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800194 (long) fields.get("srcPort"),
195 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
Pavlin Radoslavov2fa80f42014-04-18 13:10:38 -0700196 dstSwitchDpid.value(),
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800197 (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}