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 = ""; |
| 108 | ArrayNode arrayNode = mapper.createArrayNode(); |
| 109 | IntentMap intentMap = pathRuntime.getHighLevelIntents(); |
| 110 | Collection<Intent> intents = intentMap.getAllIntents(); |
| 111 | if (!intents.isEmpty()) { |
| 112 | for (Intent intent : intents) { |
| 113 | ObjectNode node = mapper.createObjectNode(); |
| 114 | node.put("intent_id", intent.getId()); |
| 115 | node.put("status", intent.getState().toString()); |
| 116 | arrayNode.add(node); |
| 117 | restStr = mapper.writeValueAsString(arrayNode); |
| 118 | } |
| 119 | } |
| 120 | return restStr; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 123 | private String parseJsonNode(Iterator<JsonNode> nodes, |
| 124 | IPathCalcRuntimeService pathRuntime) throws IOException { |
| 125 | IntentOperationList operations = new IntentOperationList(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 126 | ObjectMapper mapper = new ObjectMapper(); |
| 127 | ArrayNode arrayNode = mapper.createArrayNode(); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 128 | while (nodes.hasNext()) { |
| 129 | JsonNode node = nodes.next(); |
| 130 | if (node.isObject()) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 131 | JsonNode data; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 132 | Iterator<String> fieldNames = node.getFieldNames(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 133 | Map<String, Object> fields = new HashMap<>(); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 134 | while (fieldNames.hasNext()) { |
| 135 | String fieldName = fieldNames.next(); |
| 136 | data = node.get(fieldName); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 137 | parseFields(data, fieldName, fields); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 138 | } |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 139 | String status = processIntent(fields, operations); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 140 | appendIntentStatus(status, (String)fields.get("intent_id"), mapper, arrayNode); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 143 | pathRuntime.executeIntentOperations(operations); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 144 | return mapper.writeValueAsString(arrayNode); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 145 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 146 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 147 | private void appendIntentStatus(String status, final String applnIntentId, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 148 | ObjectMapper mapper, ArrayNode arrayNode) throws IOException { |
| 149 | String intentId = applnIntentId.split(":")[1]; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 150 | ObjectNode node = mapper.createObjectNode(); |
| 151 | node.put("intent_id", intentId); |
| 152 | node.put("status", status); |
| 153 | arrayNode.add(node); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 156 | private String processIntent(Map<String, Object> fields, IntentOperationList operations) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 157 | String intentType = (String)fields.get("intent_type"); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 158 | String intentOp = (String)fields.get("intent_op"); |
| 159 | String status = null; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 160 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 161 | IntentOperation.Operator operation = IntentOperation.Operator.ADD; |
| 162 | if ((intentOp.equals("remove"))) { |
| 163 | operation = IntentOperation.Operator.REMOVE; |
| 164 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 165 | if (intentType.equals("shortest_intent_type")) { |
| 166 | ShortestPathIntent spi = new ShortestPathIntent((String) fields.get("intent_id"), |
| 167 | Long.decode((String) fields.get("srcSwitch")), |
| 168 | (long) fields.get("srcPort"), |
| 169 | MACAddress.valueOf((String) fields.get("srcMac")).toLong(), |
| 170 | Long.decode((String) fields.get("dstSwitch")), |
| 171 | (long) fields.get("dstPort"), |
| 172 | MACAddress.valueOf((String) fields.get("dstMac")).toLong()); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 173 | operations.add(new IntentOperation(operation, spi)); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 174 | status = (spi.getState()).toString(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 175 | } else { |
| 176 | ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent((String) fields.get("intent_id"), |
| 177 | Long.decode((String) fields.get("srcSwitch")), |
| 178 | (long) fields.get("srcPort"), |
| 179 | MACAddress.valueOf((String) fields.get("srcMac")).toLong(), |
| 180 | Long.decode((String) fields.get("dstSwitch")), |
| 181 | (long) fields.get("dstPort"), |
| 182 | MACAddress.valueOf((String) fields.get("dstMac")).toLong(), |
| 183 | (double) fields.get("bandwidth")); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame] | 184 | operations.add(new IntentOperation(operation, cspi)); |
| 185 | status = (cspi.getState()).toString(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 186 | } |
| 187 | return status; |
| 188 | } |
| 189 | |
| 190 | private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) { |
| 191 | if ((node.isTextual())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 192 | fields.put(fieldName, node.getTextValue()); |
| 193 | } else if ((node.isInt())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 194 | fields.put(fieldName, (long)node.getIntValue()); |
| 195 | } else if (node.isDouble()) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 196 | fields.put(fieldName, node.getDoubleValue()); |
| 197 | } else if ((node.isLong())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 198 | fields.put(fieldName, node.getLongValue()); |
| 199 | } |
| 200 | } |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 201 | } |