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