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; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 8 | import java.util.Iterator; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 9 | import net.onrc.onos.datagrid.IDatagridService; |
| 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 | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 14 | //import net.onrc.onos.intent.Intent.IntentState; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 15 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
| 16 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 17 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 18 | import net.onrc.onos.registry.controller.IdBlock; |
| 19 | import org.codehaus.jackson.JsonGenerationException; |
| 20 | import org.codehaus.jackson.JsonNode; |
| 21 | import org.codehaus.jackson.map.JsonMappingException; |
| 22 | import org.restlet.resource.Post; |
| 23 | import org.restlet.resource.ServerResource; |
| 24 | import org.codehaus.jackson.map.ObjectMapper; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 25 | import net.floodlightcontroller.util.MACAddress; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 26 | import com.esotericsoftware.kryo.Kryo; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 27 | import com.esotericsoftware.kryo.io.Output; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.LinkedList; |
| 30 | import java.util.Map; |
| 31 | import org.codehaus.jackson.node.ArrayNode; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 32 | import org.codehaus.jackson.node.ObjectNode; |
| 33 | import org.restlet.resource.Get; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 38 | * |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 39 | * @author nickkaranatsios |
| 40 | */ |
| 41 | public class IntentResource extends ServerResource { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 42 | private final static Logger log = LoggerFactory.getLogger(IntentResource.class); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 43 | private final String sep = ":"; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 44 | private IdBlock idBlock = null; |
| 45 | private long nextIdBlock = 0; |
| 46 | |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 47 | private class IntentStatus { |
| 48 | String intentId; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 49 | String status; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 50 | |
| 51 | public IntentStatus() {} |
| 52 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 53 | public IntentStatus(String intentId, String status) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 54 | this.intentId = intentId; |
| 55 | this.status = status; |
| 56 | } |
| 57 | |
| 58 | public String getIntentId() { |
| 59 | return intentId; |
| 60 | } |
| 61 | |
| 62 | public void setIntentId(String intentId) { |
| 63 | this.intentId = intentId; |
| 64 | } |
| 65 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 66 | public String getStatus() { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 67 | return status; |
| 68 | } |
| 69 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 70 | public void setStatus(String status) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 71 | this.status = status; |
| 72 | } |
| 73 | } |
| 74 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 75 | @Post("json") |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 76 | public String store(String jsonIntent) throws IOException { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 77 | IDatagridService datagridService = (IDatagridService) getContext() |
| 78 | .getAttributes().get(IDatagridService.class.getCanonicalName()); |
| 79 | if (datagridService == null) { |
| 80 | log.debug("FlowIntentResource ONOS Datagrid Service not found"); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 81 | return ""; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 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) { |
| 97 | Kryo kryo = new Kryo(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 98 | reply = parseJsonNode(kryo, jNode.getElements(), datagridService); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 99 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 100 | return reply; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 103 | @Get("json") |
| 104 | public String retrieve() { |
| 105 | return "123"; |
| 106 | } |
| 107 | |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 108 | private String parseJsonNode(Kryo kryo, Iterator<JsonNode> nodes, |
| 109 | IDatagridService datagridService) throws IOException { |
| 110 | LinkedList<IntentOperation> operations = new LinkedList<>(); |
| 111 | ObjectMapper mapper = new ObjectMapper(); |
| 112 | ArrayNode arrayNode = mapper.createArrayNode(); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 113 | while (nodes.hasNext()) { |
| 114 | JsonNode node = nodes.next(); |
| 115 | if (node.isObject()) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 116 | JsonNode data; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 117 | Iterator<String> fieldNames = node.getFieldNames(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 118 | Map<String, Object> fields = new HashMap<>(); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 119 | while (fieldNames.hasNext()) { |
| 120 | String fieldName = fieldNames.next(); |
| 121 | data = node.get(fieldName); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 122 | parseFields(data, fieldName, fields); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 123 | } |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 124 | String status = processIntent(fields, operations); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 125 | appendIntentStatus(status, (String)fields.get("intent_id"), mapper, arrayNode); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 126 | // datagridService.registerIntent(Long.toString(uuid), |
| 127 | // sb.toString().getBytes()); |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 128 | } |
| 129 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 130 | IntentMap intents = new IntentMap(); |
| 131 | intents.executeOperations(operations); |
| 132 | return mapper.writeValueAsString(arrayNode); |
| 133 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 134 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 135 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 136 | private void appendIntentStatus(String status, final String applnIntentId, |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 137 | ObjectMapper mapper, ArrayNode arrayNode) throws IOException { |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 138 | System.out.println("status " + status); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 139 | String intentId = applnIntentId.split(":")[1]; |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 140 | ObjectNode node = mapper.createObjectNode(); |
| 141 | node.put("intent_id", intentId); |
| 142 | node.put("status", status); |
| 143 | arrayNode.add(node); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 146 | private String processIntent(Map<String, Object> fields, LinkedList<IntentOperation> operations) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 147 | String intentType = (String)fields.get("intent_type"); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 148 | String intentOp = (String)fields.get("intent_op"); |
| 149 | String status = null; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 150 | |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 151 | IntentOperation.Operator operation = IntentOperation.Operator.ADD; |
| 152 | if ((intentOp.equals("remove"))) { |
| 153 | operation = IntentOperation.Operator.REMOVE; |
| 154 | } |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 155 | if (intentType.equals("shortest_intent_type")) { |
| 156 | ShortestPathIntent spi = new ShortestPathIntent((String) fields.get("intent_id"), |
| 157 | Long.decode((String) fields.get("srcSwitch")), |
| 158 | (long) fields.get("srcPort"), |
| 159 | MACAddress.valueOf((String) fields.get("srcMac")).toLong(), |
| 160 | Long.decode((String) fields.get("dstSwitch")), |
| 161 | (long) fields.get("dstPort"), |
| 162 | MACAddress.valueOf((String) fields.get("dstMac")).toLong()); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 163 | operations.add(new IntentOperation(operation, spi)); |
| 164 | System.out.println("intent operation " + operation.toString()); |
| 165 | status = (spi.getState()).toString(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 166 | } else { |
| 167 | ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent((String) fields.get("intent_id"), |
| 168 | Long.decode((String) fields.get("srcSwitch")), |
| 169 | (long) fields.get("srcPort"), |
| 170 | MACAddress.valueOf((String) fields.get("srcMac")).toLong(), |
| 171 | Long.decode((String) fields.get("dstSwitch")), |
| 172 | (long) fields.get("dstPort"), |
| 173 | MACAddress.valueOf((String) fields.get("dstMac")).toLong(), |
| 174 | (double) fields.get("bandwidth")); |
Nick Karanatsios | 88948d3 | 2014-02-18 15:14:30 -0800 | [diff] [blame^] | 175 | operations.add(new IntentOperation(operation, cspi)); |
| 176 | status = (cspi.getState()).toString(); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 177 | } |
| 178 | return status; |
| 179 | } |
| 180 | |
| 181 | private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) { |
| 182 | if ((node.isTextual())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 183 | fields.put(fieldName, node.getTextValue()); |
| 184 | } else if ((node.isInt())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 185 | fields.put(fieldName, (long)node.getIntValue()); |
| 186 | } else if (node.isDouble()) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 187 | fields.put(fieldName, node.getDoubleValue()); |
| 188 | } else if ((node.isLong())) { |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 189 | fields.put(fieldName, node.getLongValue()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Deprecated |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 194 | private long setPathIntentId() { |
| 195 | long uuid = 0; |
| 196 | if (idBlock == null || nextIdBlock + 1 == idBlock.getSize()) { |
| 197 | IControllerRegistryService controllerRegistry = getControllerRegistry(); |
| 198 | if (controllerRegistry != null) { |
| 199 | idBlock = controllerRegistry.allocateUniqueIdBlock(); |
| 200 | nextIdBlock = idBlock.getStart(); |
| 201 | System.out.println("start block " + nextIdBlock + " end block " |
| 202 | + idBlock.getEnd() + " size " + idBlock.getSize()); |
| 203 | } |
| 204 | } |
| 205 | if (idBlock != null) { |
| 206 | uuid = nextIdBlock; |
| 207 | nextIdBlock++; |
| 208 | } |
| 209 | return uuid; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 210 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 211 | |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 212 | @Deprecated |
| 213 | private void setIntentType(final String intentType, StringBuilder sb) { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 214 | String canonicalName = null; |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 215 | if (intentType.equals("shortest-path")) { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 216 | canonicalName = ShortestPathIntent.class.getCanonicalName(); |
| 217 | sb.append(ShortestPathIntent.class.getCanonicalName()); |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 218 | } else if (intentType.equals("constrained-shortest-path")) { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 219 | canonicalName = ShortestPathIntent.class.getCanonicalName(); |
| 220 | } |
| 221 | sb.append(canonicalName); |
| 222 | sb.append(sep); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 223 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 224 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 225 | private IControllerRegistryService getControllerRegistry() { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 226 | return (IControllerRegistryService) getContext().getAttributes().get( |
| 227 | IControllerRegistryService.class.getCanonicalName()); |
| 228 | } |
| 229 | |
Nick Karanatsios | 5fcf93e | 2014-02-15 14:13:55 -0800 | [diff] [blame] | 230 | @Deprecated |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame] | 231 | private byte[] toBytes(Kryo kryo, Object value) { |
| 232 | Output output = new Output(1024); |
| 233 | kryo.writeObject(output, value); |
| 234 | output.close(); |
| 235 | return output.toBytes(); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 236 | } |
| 237 | } |