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