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