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