Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 5 | package net.onrc.onos.core.datagrid.web; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 6 | |
| 7 | import java.io.IOException; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 8 | import java.util.Collection; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 9 | import java.util.HashMap; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 10 | import java.util.Iterator; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 11 | import java.util.LinkedList; |
| 12 | import java.util.Map; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 13 | |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 14 | import net.floodlightcontroller.util.MACAddress; |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.intent.ConstrainedShortestPathIntent; |
| 16 | import net.onrc.onos.core.intent.Intent; |
| 17 | import net.onrc.onos.core.intent.IntentMap; |
| 18 | import net.onrc.onos.core.intent.IntentOperation; |
| 19 | import net.onrc.onos.core.intent.IntentOperationList; |
| 20 | import net.onrc.onos.core.intent.ShortestPathIntent; |
| 21 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
| 22 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 23 | import org.codehaus.jackson.JsonGenerationException; |
| 24 | import org.codehaus.jackson.JsonNode; |
| 25 | import org.codehaus.jackson.map.JsonMappingException; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 26 | import org.codehaus.jackson.map.ObjectMapper; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 27 | import org.codehaus.jackson.node.ArrayNode; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 28 | import org.codehaus.jackson.node.ObjectNode; |
Nick Karanatsios | 1f4defb | 2014-02-23 19:34:47 -0800 | [diff] [blame] | 29 | import org.restlet.resource.Delete; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 30 | import org.restlet.resource.Get; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 31 | import org.restlet.resource.Post; |
| 32 | import org.restlet.resource.ServerResource; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 37 | * @author nickkaranatsios |
| 38 | */ |
| 39 | public class IntentResource extends ServerResource { |
Ray Milkey | ec83894 | 2014-04-09 11:28:43 -0700 | [diff] [blame] | 40 | private static final Logger log = LoggerFactory.getLogger(IntentResource.class); |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 41 | // TODO need to assign proper application id. |
Ray Milkey | 2476cac | 2014-04-08 11:03:21 -0700 | [diff] [blame] | 42 | private static final String APPLN_ID = "1"; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 43 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 44 | @Post("json") |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 45 | public String store(String jsonIntent) throws IOException { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 46 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext() |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 47 | .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 48 | if (pathRuntime == null) { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 49 | log.warn("Failed to get path calc runtime"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 50 | return ""; |
| 51 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 52 | String reply = ""; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 53 | 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 Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 64 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 65 | if (jNode != null) { |
| 66 | reply = parseJsonNode(jNode.getElements(), pathRuntime); |
| 67 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 68 | return reply; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 69 | } |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 70 | |
Nick Karanatsios | 1f4defb | 2014-02-23 19:34:47 -0800 | [diff] [blame] | 71 | @Delete("json") |
| 72 | public String store() { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 73 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext(). |
Nick Karanatsios | 1f4defb | 2014-02-23 19:34:47 -0800 | [diff] [blame] | 74 | getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 75 | pathRuntime.purgeIntents(); |
| 76 | // TODO no reply yet from the purge intents call |
| 77 | return ""; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 78 | |
Nick Karanatsios | 1f4defb | 2014-02-23 19:34:47 -0800 | [diff] [blame] | 79 | } |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 80 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 81 | @Get("json") |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 82 | public String retrieve() throws IOException { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 83 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext(). |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 84 | getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 85 | |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 86 | 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 Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 93 | ObjectMapper mapper = new ObjectMapper(); |
| 94 | String restStr = ""; |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 95 | |
| 96 | String intentId = (String) getRequestAttributes().get("intent_id"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 97 | ArrayNode arrayNode = mapper.createArrayNode(); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 98 | Collection<Intent> intents = intentMap.getAllIntents(); |
| 99 | if (!intents.isEmpty()) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 100 | if ((intentId != null)) { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 101 | String applnIntentId = APPLN_ID + ":" + intentId; |
| 102 | Intent intent = intentMap.getIntent(applnIntentId); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 103 | if (intent != null) { |
| 104 | ObjectNode node = mapper.createObjectNode(); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 105 | // TODO refactor/remove duplicate code. |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 106 | node.put("intent_id", intentId); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 107 | node.put("status", intent.getState().toString()); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 108 | LinkedList<String> logs = intent.getLogs(); |
| 109 | ArrayNode logNode = mapper.createArrayNode(); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 110 | for (String intentLog : logs) { |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 111 | logNode.add(intentLog); |
| 112 | } |
| 113 | node.put("log", logNode); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 114 | arrayNode.add(node); |
| 115 | } |
| 116 | } else { |
| 117 | for (Intent intent : intents) { |
| 118 | ObjectNode node = mapper.createObjectNode(); |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 119 | String applnIntentId = intent.getId(); |
| 120 | intentId = applnIntentId.split(":")[1]; |
| 121 | node.put("intent_id", intentId); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 122 | node.put("status", intent.getState().toString()); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 123 | LinkedList<String> logs = intent.getLogs(); |
| 124 | ArrayNode logNode = mapper.createArrayNode(); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 125 | for (String intentLog : logs) { |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 126 | logNode.add(intentLog); |
| 127 | } |
| 128 | node.put("log", logNode); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 129 | arrayNode.add(node); |
| 130 | } |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 131 | } |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 132 | restStr = mapper.writeValueAsString(arrayNode); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 133 | } |
| 134 | return restStr; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 135 | } |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 136 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 137 | private String parseJsonNode(Iterator<JsonNode> nodes, |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 138 | IPathCalcRuntimeService pathRuntime) throws IOException { |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 139 | IntentOperationList operations = new IntentOperationList(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 140 | ObjectMapper mapper = new ObjectMapper(); |
| 141 | ArrayNode arrayNode = mapper.createArrayNode(); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 142 | while (nodes.hasNext()) { |
| 143 | JsonNode node = nodes.next(); |
| 144 | if (node.isObject()) { |
| 145 | JsonNode data; |
| 146 | Iterator<String> fieldNames = node.getFieldNames(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 147 | Map<String, Object> fields = new HashMap<>(); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 148 | while (fieldNames.hasNext()) { |
| 149 | String fieldName = fieldNames.next(); |
| 150 | data = node.get(fieldName); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 151 | parseFields(data, fieldName, fields); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 152 | } |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 153 | Intent intent = processIntent(fields, operations); |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 154 | appendIntentStatus(intent, (String) fields.get("intent_id"), mapper, arrayNode); |
| 155 | } |
| 156 | } |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 157 | pathRuntime.executeIntentOperations(operations); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 158 | return mapper.writeValueAsString(arrayNode); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 159 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 160 | |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 161 | private void appendIntentStatus(Intent intent, final String intentId, |
| 162 | ObjectMapper mapper, ArrayNode arrayNode) throws IOException { |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 163 | ObjectNode node = mapper.createObjectNode(); |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 164 | node.put("intent_id", intentId); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 165 | 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 Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 172 | arrayNode.add(node); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 173 | } |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 174 | |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 175 | private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 176 | String intentType = (String) fields.get("intent_type"); |
| 177 | String intentOp = (String) fields.get("intent_op"); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 178 | Intent intent; |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 179 | String intentId = (String) fields.get("intent_id"); |
Toshio Koide | 565d6dd | 2014-03-27 11:22:25 -0700 | [diff] [blame] | 180 | 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 Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 186 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 187 | IntentOperation.Operator operation = IntentOperation.Operator.ADD; |
| 188 | if ((intentOp.equals("remove"))) { |
| 189 | operation = IntentOperation.Operator.REMOVE; |
| 190 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 191 | if (intentType.equals("shortest_intent_type")) { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 192 | ShortestPathIntent spi = new ShortestPathIntent(applnIntentId, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 193 | 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 Koide | 565d6dd | 2014-03-27 11:22:25 -0700 | [diff] [blame] | 199 | spi.setPathFrozen(pathFrozen); |
Nick Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 200 | operations.add(operation, spi); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 201 | intent = spi; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 202 | } else { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 203 | ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(applnIntentId, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 204 | 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 Koide | 565d6dd | 2014-03-27 11:22:25 -0700 | [diff] [blame] | 211 | cspi.setPathFrozen(pathFrozen); |
Nick Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 212 | operations.add(operation, cspi); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 213 | intent = cspi; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 214 | } |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 215 | return intent; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) { |
| 219 | if ((node.isTextual())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 220 | fields.put(fieldName, node.getTextValue()); |
| 221 | } else if ((node.isInt())) { |
Ray Milkey | 9c8a213 | 2014-04-02 15:16:42 -0700 | [diff] [blame] | 222 | fields.put(fieldName, (long) node.getIntValue()); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 223 | } else if (node.isDouble()) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 224 | fields.put(fieldName, node.getDoubleValue()); |
| 225 | } else if ((node.isLong())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 226 | fields.put(fieldName, node.getLongValue()); |
| 227 | } |
| 228 | } |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 229 | } |