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.datagrid.IDatagridService; |
| 11 | import net.onrc.onos.intent.ConstrainedShortestPathIntent; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 12 | import net.onrc.onos.intent.ShortestPathIntent; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 13 | import net.onrc.onos.intent.IntentOperation; |
| 14 | import net.onrc.onos.intent.IntentMap; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 15 | import net.onrc.onos.intent.Intent; |
| 16 | import net.onrc.onos.intent.runtime.IPathCalcRuntimeService; |
| 17 | import net.onrc.onos.intent.IntentOperationList; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 18 | import org.codehaus.jackson.JsonGenerationException; |
| 19 | import org.codehaus.jackson.JsonNode; |
| 20 | import org.codehaus.jackson.map.JsonMappingException; |
| 21 | import org.restlet.resource.Post; |
| 22 | import org.restlet.resource.ServerResource; |
| 23 | import org.codehaus.jackson.map.ObjectMapper; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 24 | import net.floodlightcontroller.util.MACAddress; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 25 | import java.util.HashMap; |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 26 | import java.util.LinkedList; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 27 | import java.util.Map; |
| 28 | import org.codehaus.jackson.node.ArrayNode; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 29 | import org.codehaus.jackson.node.ObjectNode; |
Nick Karanatsios | 1f4defb | 2014-02-23 19:34:47 -0800 | [diff] [blame^] | 30 | import org.restlet.resource.Delete; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 31 | import org.restlet.resource.Get; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 36 | * |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 37 | * @author nickkaranatsios |
| 38 | */ |
| 39 | public class IntentResource extends ServerResource { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 40 | private final static 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. |
| 42 | private final String APPLN_ID = "1"; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 43 | |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 44 | private class IntentStatus { |
| 45 | String intentId; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 46 | String status; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 47 | |
| 48 | public IntentStatus() {} |
| 49 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 50 | public IntentStatus(String intentId, String status) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 51 | this.intentId = intentId; |
| 52 | this.status = status; |
| 53 | } |
| 54 | |
| 55 | public String getIntentId() { |
| 56 | return intentId; |
| 57 | } |
| 58 | |
| 59 | public void setIntentId(String intentId) { |
| 60 | this.intentId = intentId; |
| 61 | } |
| 62 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 63 | public String getStatus() { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 64 | return status; |
| 65 | } |
| 66 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 67 | public void setStatus(String status) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 68 | this.status = status; |
| 69 | } |
| 70 | } |
| 71 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 72 | @Post("json") |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 73 | public String store(String jsonIntent) throws IOException { |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 74 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext() |
| 75 | .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 76 | if (pathRuntime == null) { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 77 | log.warn("Failed to get path calc runtime"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 78 | return ""; |
| 79 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 80 | String reply = ""; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 81 | ObjectMapper mapper = new ObjectMapper(); |
| 82 | JsonNode jNode = null; |
| 83 | try { |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 84 | jNode = mapper.readValue(jsonIntent, JsonNode.class); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 85 | } catch (JsonGenerationException ex) { |
| 86 | log.error("JsonGeneration exception ", ex); |
| 87 | } catch (JsonMappingException ex) { |
| 88 | log.error("JsonMappingException occurred", ex); |
| 89 | } catch (IOException ex) { |
| 90 | log.error("IOException occurred", ex); |
| 91 | } |
| 92 | |
| 93 | if (jNode != null) { |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 94 | reply = parseJsonNode(jNode.getElements(), pathRuntime); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 95 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 96 | return reply; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 97 | } |
Nick Karanatsios | 1f4defb | 2014-02-23 19:34:47 -0800 | [diff] [blame^] | 98 | |
| 99 | @Delete("json") |
| 100 | public String store() { |
| 101 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext(). |
| 102 | getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 103 | pathRuntime.purgeIntents(); |
| 104 | // TODO no reply yet from the purge intents call |
| 105 | return ""; |
| 106 | |
| 107 | } |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 108 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 109 | @Get("json") |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 110 | public String retrieve() throws IOException { |
| 111 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext(). |
| 112 | getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 113 | |
| 114 | String intentCategory = (String) getRequestAttributes().get("category"); |
| 115 | IntentMap intentMap = null; |
| 116 | if (intentCategory.equals("high")) { |
| 117 | intentMap = pathRuntime.getHighLevelIntents(); |
| 118 | } else { |
| 119 | intentMap = pathRuntime.getPathIntents(); |
| 120 | } |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 121 | ObjectMapper mapper = new ObjectMapper(); |
| 122 | String restStr = ""; |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 123 | |
| 124 | String intentId = (String) getRequestAttributes().get("intent_id"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 125 | ArrayNode arrayNode = mapper.createArrayNode(); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 126 | Collection<Intent> intents = intentMap.getAllIntents(); |
| 127 | if (!intents.isEmpty()) { |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 128 | if ((intentId != null )) { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 129 | String applnIntentId = APPLN_ID + ":" + intentId; |
| 130 | Intent intent = intentMap.getIntent(applnIntentId); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 131 | if (intent != null) { |
| 132 | ObjectNode node = mapper.createObjectNode(); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 133 | // TODO refactor/remove duplicate code. |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 134 | node.put("intent_id", intentId); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 135 | node.put("status", intent.getState().toString()); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 136 | LinkedList<String> logs = intent.getLogs(); |
| 137 | ArrayNode logNode = mapper.createArrayNode(); |
| 138 | for (String intentLog :logs) { |
| 139 | logNode.add(intentLog); |
| 140 | } |
| 141 | node.put("log", logNode); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 142 | arrayNode.add(node); |
| 143 | } |
| 144 | } else { |
| 145 | for (Intent intent : intents) { |
| 146 | ObjectNode node = mapper.createObjectNode(); |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 147 | String applnIntentId = intent.getId(); |
| 148 | intentId = applnIntentId.split(":")[1]; |
| 149 | node.put("intent_id", intentId); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 150 | node.put("status", intent.getState().toString()); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 151 | LinkedList<String> logs = intent.getLogs(); |
| 152 | ArrayNode logNode = mapper.createArrayNode(); |
| 153 | for (String intentLog :logs) { |
| 154 | logNode.add(intentLog); |
| 155 | } |
| 156 | node.put("log", logNode); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 157 | arrayNode.add(node); |
| 158 | } |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 159 | } |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 160 | restStr = mapper.writeValueAsString(arrayNode); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 161 | } |
| 162 | return restStr; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 165 | private String parseJsonNode(Iterator<JsonNode> nodes, |
| 166 | IPathCalcRuntimeService pathRuntime) throws IOException { |
| 167 | IntentOperationList operations = new IntentOperationList(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 168 | ObjectMapper mapper = new ObjectMapper(); |
| 169 | ArrayNode arrayNode = mapper.createArrayNode(); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 170 | while (nodes.hasNext()) { |
| 171 | JsonNode node = nodes.next(); |
| 172 | if (node.isObject()) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 173 | JsonNode data; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 174 | Iterator<String> fieldNames = node.getFieldNames(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 175 | Map<String, Object> fields = new HashMap<>(); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 176 | while (fieldNames.hasNext()) { |
| 177 | String fieldName = fieldNames.next(); |
| 178 | data = node.get(fieldName); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 179 | parseFields(data, fieldName, fields); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 180 | } |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 181 | Intent intent = processIntent(fields, operations); |
| 182 | appendIntentStatus(intent, (String)fields.get("intent_id"), mapper, arrayNode); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 185 | pathRuntime.executeIntentOperations(operations); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 186 | return mapper.writeValueAsString(arrayNode); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 187 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 188 | |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 189 | private void appendIntentStatus(Intent intent, final String intentId, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 190 | ObjectMapper mapper, ArrayNode arrayNode) throws IOException { |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 191 | ObjectNode node = mapper.createObjectNode(); |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 192 | node.put("intent_id", intentId); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 193 | node.put("status", intent.getState().toString()); |
| 194 | LinkedList<String> logs = intent.getLogs(); |
| 195 | ArrayNode logNode = mapper.createArrayNode(); |
| 196 | for (String intentLog : logs) { |
| 197 | logNode.add(intentLog); |
| 198 | } |
| 199 | node.put("log", logNode); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 200 | arrayNode.add(node); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 203 | private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 204 | String intentType = (String)fields.get("intent_type"); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 205 | String intentOp = (String)fields.get("intent_op"); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 206 | Intent intent; |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 207 | String applnIntentId = APPLN_ID + ":" + (String)fields.get("intent_id"); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 208 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 209 | IntentOperation.Operator operation = IntentOperation.Operator.ADD; |
| 210 | if ((intentOp.equals("remove"))) { |
| 211 | operation = IntentOperation.Operator.REMOVE; |
| 212 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 213 | if (intentType.equals("shortest_intent_type")) { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 214 | ShortestPathIntent spi = new ShortestPathIntent(applnIntentId, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 215 | Long.decode((String) fields.get("srcSwitch")), |
| 216 | (long) fields.get("srcPort"), |
| 217 | MACAddress.valueOf((String) fields.get("srcMac")).toLong(), |
| 218 | Long.decode((String) fields.get("dstSwitch")), |
| 219 | (long) fields.get("dstPort"), |
| 220 | MACAddress.valueOf((String) fields.get("dstMac")).toLong()); |
Nick Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 221 | operations.add(operation, spi); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 222 | intent = spi; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 223 | } else { |
Nick Karanatsios | da9f36f | 2014-02-21 01:27:02 -0800 | [diff] [blame] | 224 | ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(applnIntentId, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 225 | Long.decode((String) fields.get("srcSwitch")), |
| 226 | (long) fields.get("srcPort"), |
| 227 | MACAddress.valueOf((String) fields.get("srcMac")).toLong(), |
| 228 | Long.decode((String) fields.get("dstSwitch")), |
| 229 | (long) fields.get("dstPort"), |
| 230 | MACAddress.valueOf((String) fields.get("dstMac")).toLong(), |
| 231 | (double) fields.get("bandwidth")); |
Nick Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 232 | operations.add(operation, cspi); |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 233 | intent = cspi; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 234 | } |
Nick Karanatsios | 87e8be7 | 2014-02-21 23:45:37 -0800 | [diff] [blame] | 235 | return intent; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) { |
| 239 | if ((node.isTextual())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 240 | fields.put(fieldName, node.getTextValue()); |
| 241 | } else if ((node.isInt())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 242 | fields.put(fieldName, (long)node.getIntValue()); |
| 243 | } else if (node.isDouble()) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 244 | fields.put(fieldName, node.getDoubleValue()); |
| 245 | } else if ((node.isLong())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 246 | fields.put(fieldName, node.getLongValue()); |
| 247 | } |
| 248 | } |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 249 | } |