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 | 1a4a200 | 2014-02-14 04:35:39 -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; |
| 13 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
| 14 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 15 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 16 | import net.onrc.onos.registry.controller.IdBlock; |
| 17 | import org.codehaus.jackson.JsonGenerationException; |
| 18 | import org.codehaus.jackson.JsonNode; |
| 19 | import org.codehaus.jackson.map.JsonMappingException; |
| 20 | import org.restlet.resource.Post; |
| 21 | import org.restlet.resource.ServerResource; |
| 22 | import org.codehaus.jackson.map.ObjectMapper; |
| 23 | import org.slf4j.LoggerFactory; |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 24 | import net.floodlightcontroller.util.MACAddress; |
| 25 | import net.onrc.onos.ofcontroller.networkgraph.Port; |
| 26 | import net.onrc.onos.ofcontroller.networkgraph.Switch; |
| 27 | import com.esotericsoftware.kryo.Kryo; |
| 28 | import com.esotericsoftware.kryo.io.Input; |
| 29 | import com.esotericsoftware.kryo.io.Output; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 30 | |
| 31 | /** |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 32 | * |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 33 | * @author nickkaranatsios |
| 34 | */ |
| 35 | public class IntentResource extends ServerResource { |
| 36 | |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 37 | private final static org.slf4j.Logger log = LoggerFactory |
| 38 | .getLogger(IntentResource.class); |
| 39 | private final String sep = ":"; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 40 | private IdBlock idBlock = null; |
| 41 | private long nextIdBlock = 0; |
| 42 | |
| 43 | @Post("json") |
| 44 | public void store(String jsonFlowIntent) { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 45 | IDatagridService datagridService = (IDatagridService) getContext() |
| 46 | .getAttributes().get(IDatagridService.class.getCanonicalName()); |
| 47 | if (datagridService == null) { |
| 48 | log.debug("FlowIntentResource ONOS Datagrid Service not found"); |
| 49 | return; |
| 50 | } |
| 51 | INetworkGraphService networkGraphService = (INetworkGraphService) getContext() |
| 52 | .getAttributes().get( |
| 53 | INetworkGraphService.class.getCanonicalName()); |
| 54 | NetworkGraph graph = networkGraphService.getNetworkGraph(); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 55 | |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 56 | ObjectMapper mapper = new ObjectMapper(); |
| 57 | JsonNode jNode = null; |
| 58 | try { |
| 59 | System.out.println("json string " + jsonFlowIntent); |
| 60 | jNode = mapper.readValue(jsonFlowIntent, JsonNode.class); |
| 61 | } catch (JsonGenerationException ex) { |
| 62 | log.error("JsonGeneration exception ", ex); |
| 63 | } catch (JsonMappingException ex) { |
| 64 | log.error("JsonMappingException occurred", ex); |
| 65 | } catch (IOException ex) { |
| 66 | log.error("IOException occurred", ex); |
| 67 | } |
| 68 | |
| 69 | if (jNode != null) { |
| 70 | Kryo kryo = new Kryo(); |
| 71 | parseJsonNode(kryo, jNode.getElements(), datagridService); |
| 72 | // datagridService.registerIntent(intents); |
| 73 | } |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 76 | private void parseJsonNode(Kryo kryo, Iterator<JsonNode> nodes, |
| 77 | IDatagridService datagridService) { |
| 78 | StringBuilder sb = new StringBuilder(); |
| 79 | sb.ensureCapacity(256); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 80 | |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 81 | while (nodes.hasNext()) { |
| 82 | JsonNode node = nodes.next(); |
| 83 | if (node.isObject()) { |
| 84 | JsonNode data = null; |
| 85 | Iterator<String> fieldNames = node.getFieldNames(); |
| 86 | String intentId = null; |
| 87 | String pathTypeName = null; |
| 88 | long srcSwitch = 0, dstSwitch = 0; |
| 89 | String srcMac = null, dstMac = null; |
| 90 | long srcPort = 0, dstPort = 0; |
| 91 | Double bandwidth = null; |
| 92 | while (fieldNames.hasNext()) { |
| 93 | String fieldName = fieldNames.next(); |
| 94 | data = node.get(fieldName); |
| 95 | if (fieldName.equals("type")) { |
| 96 | if (data != null) { |
| 97 | System.out.println("type is not null " |
| 98 | + data.getTextValue()); |
| 99 | // uuid = setPathIntentId(); |
| 100 | pathTypeName = data.getTextValue(); |
| 101 | setPathIntentType(pathTypeName, sb); |
| 102 | } |
| 103 | } else if (fieldName.equals("intentId")) { |
| 104 | intentId = data.getTextValue(); |
| 105 | } else if (fieldName.equals("srcSwitch")) { |
| 106 | srcSwitch = Long.decode(data.getTextValue()); |
| 107 | } else if (fieldName.equals("dstSwitch")) { |
| 108 | dstSwitch = Long.decode(data.getTextValue()); |
| 109 | } else if (fieldName.equals("srcMac")) { |
| 110 | srcMac = data.getTextValue(); |
| 111 | } else if (fieldName.equals("dstMac")) { |
| 112 | dstMac = data.getTextValue(); |
| 113 | } else if (fieldName.equals("srcPort")) { |
| 114 | srcPort = data.getLongValue(); |
| 115 | } else if (fieldName.equals("dstPort")) { |
| 116 | dstPort = data.getLongValue(); |
| 117 | } else if (fieldName.equals("bandwidth")) { |
| 118 | bandwidth = data.getDoubleValue(); |
| 119 | } |
| 120 | } |
| 121 | if (pathTypeName.equals("shortest-path")) { |
| 122 | ShortestPathIntent spi = new ShortestPathIntent(intentId, |
| 123 | srcSwitch, |
| 124 | srcPort, |
| 125 | MACAddress.valueOf(srcMac).toLong(), |
| 126 | dstSwitch, |
| 127 | dstPort, |
| 128 | MACAddress.valueOf(dstMac).toLong()); |
| 129 | sb.append(toBytes(kryo, spi)); |
| 130 | |
| 131 | } else { |
| 132 | ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(intentId, |
| 133 | srcSwitch, |
| 134 | srcPort, |
| 135 | MACAddress.valueOf(srcMac).toLong(), dstSwitch, |
| 136 | dstPort, MACAddress.valueOf(dstMac).toLong(), |
| 137 | bandwidth); |
| 138 | sb.append(toBytes(kryo, cspi)); |
| 139 | |
| 140 | } |
| 141 | System.out.println("constructed node " + sb.toString()); |
| 142 | // datagridService.registerIntent(Long.toString(uuid), |
| 143 | // sb.toString().getBytes()); |
| 144 | sb.delete(0, sb.length()); |
| 145 | } |
| 146 | } |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 147 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 148 | |
| 149 | private long setPathIntentId() { |
| 150 | long uuid = 0; |
| 151 | if (idBlock == null || nextIdBlock + 1 == idBlock.getSize()) { |
| 152 | IControllerRegistryService controllerRegistry = getControllerRegistry(); |
| 153 | if (controllerRegistry != null) { |
| 154 | idBlock = controllerRegistry.allocateUniqueIdBlock(); |
| 155 | nextIdBlock = idBlock.getStart(); |
| 156 | System.out.println("start block " + nextIdBlock + " end block " |
| 157 | + idBlock.getEnd() + " size " + idBlock.getSize()); |
| 158 | } |
| 159 | } |
| 160 | if (idBlock != null) { |
| 161 | uuid = nextIdBlock; |
| 162 | nextIdBlock++; |
| 163 | } |
| 164 | return uuid; |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 165 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 166 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 167 | private void setPathIntentType(final String pathIntentType, StringBuilder sb) { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 168 | String canonicalName = null; |
| 169 | if (pathIntentType.equals("shortest-path")) { |
| 170 | canonicalName = ShortestPathIntent.class.getCanonicalName(); |
| 171 | sb.append(ShortestPathIntent.class.getCanonicalName()); |
| 172 | } else if (pathIntentType.equals("constrained-shortest-path")) { |
| 173 | canonicalName = ShortestPathIntent.class.getCanonicalName(); |
| 174 | } |
| 175 | sb.append(canonicalName); |
| 176 | sb.append(sep); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 177 | } |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 178 | |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 179 | private IControllerRegistryService getControllerRegistry() { |
Nick Karanatsios | 1a4a200 | 2014-02-14 04:35:39 -0800 | [diff] [blame^] | 180 | return (IControllerRegistryService) getContext().getAttributes().get( |
| 181 | IControllerRegistryService.class.getCanonicalName()); |
| 182 | } |
| 183 | |
| 184 | private byte[] toBytes(Kryo kryo, Object value) { |
| 185 | Output output = new Output(1024); |
| 186 | kryo.writeObject(output, value); |
| 187 | output.close(); |
| 188 | return output.toBytes(); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 189 | } |
| 190 | } |