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; |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.Iterator; |
| 10 | import java.util.List; |
| 11 | import net.onrc.onos.datagrid.IDatagridService; |
| 12 | import net.onrc.onos.intent.ConstrainedShortestPathIntent; |
| 13 | import net.onrc.onos.intent.Intent; |
| 14 | import net.onrc.onos.intent.IntentDeserializer; |
| 15 | import net.onrc.onos.intent.ShortestPathIntent; |
| 16 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
| 17 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 18 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 19 | import net.onrc.onos.registry.controller.IdBlock; |
| 20 | import org.codehaus.jackson.JsonGenerationException; |
| 21 | import org.codehaus.jackson.JsonNode; |
| 22 | import org.codehaus.jackson.map.JsonMappingException; |
| 23 | import org.restlet.resource.Post; |
| 24 | import org.restlet.resource.ServerResource; |
| 25 | import org.codehaus.jackson.map.ObjectMapper; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @author nickkaranatsios |
| 31 | */ |
| 32 | public class IntentResource extends ServerResource { |
| 33 | |
| 34 | private final static org.slf4j.Logger log = LoggerFactory.getLogger(IntentResource.class); |
| 35 | private IdBlock idBlock = null; |
| 36 | private long nextIdBlock = 0; |
| 37 | |
| 38 | @Post("json") |
| 39 | public void store(String jsonFlowIntent) { |
| 40 | IDatagridService datagridService = |
| 41 | (IDatagridService) getContext().getAttributes(). |
| 42 | get(IDatagridService.class.getCanonicalName()); |
| 43 | if (datagridService == null) { |
| 44 | log.debug("FlowIntentResource ONOS Datagrid Service not found"); |
| 45 | return; |
| 46 | } |
| 47 | INetworkGraphService networkGraphService = (INetworkGraphService)getContext().getAttributes(). |
| 48 | get(INetworkGraphService.class.getCanonicalName()); |
| 49 | NetworkGraph graph = networkGraphService.getNetworkGraph(); |
| 50 | |
| 51 | ObjectMapper mapper = new ObjectMapper(); |
| 52 | JsonNode jNode = null; |
| 53 | try { |
| 54 | System.out.println("json string " + jsonFlowIntent); |
| 55 | jNode = mapper.readValue(jsonFlowIntent, JsonNode.class); |
| 56 | } catch (JsonGenerationException ex) { |
| 57 | log.error("JsonGeneration exception ", ex); |
| 58 | } catch (JsonMappingException ex) { |
| 59 | log.error("JsonMappingException occurred", ex); |
| 60 | } catch (IOException ex) { |
| 61 | log.error("IOException occurred", ex); |
| 62 | } |
| 63 | |
| 64 | List<Intent> intents = new ArrayList<>(); |
| 65 | if (jNode != null) { |
| 66 | parseJsonNode(graph, jNode.getElements(), intents); |
| 67 | // datagridService.registerIntent(intents); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private void parseJsonNode(NetworkGraph graph, Iterator<JsonNode> nodes, List<Intent> intents) { |
| 72 | StringBuilder sb = new StringBuilder(); |
| 73 | sb.ensureCapacity(256); |
| 74 | IntentDeserializer intentDesializer = null; |
| 75 | |
| 76 | while (nodes.hasNext()) { |
| 77 | JsonNode node = nodes.next(); |
| 78 | if (node.isObject()) { |
| 79 | JsonNode data = null; |
| 80 | Iterator<String> fieldNames = node.getFieldNames(); |
| 81 | while (fieldNames.hasNext()) { |
| 82 | String fieldName = fieldNames.next(); |
| 83 | data = node.get(fieldName); |
| 84 | if (fieldName.equals("type")) { |
| 85 | if (data != null) { |
| 86 | System.out.println("type is not null " + data.getTextValue()); |
| 87 | setPathIntentId(sb); |
| 88 | setPathIntentType(data.getTextValue(), sb); |
| 89 | } |
| 90 | } else { |
| 91 | if (data.isTextual()) { |
| 92 | sb.append(data.getTextValue()); |
| 93 | } else if (data.isDouble()) { |
| 94 | Double bandwidth = data.getDoubleValue(); |
| 95 | sb.append(bandwidth); |
| 96 | } else if (data.isNumber()) { |
| 97 | Integer number = data.getIntValue(); |
| 98 | sb.append(number); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | System.out.println("constructed node " + sb.toString()); |
| 103 | sb.delete(0, sb.length()); |
Toshio Koide | 0e4d8d2 | 2014-02-14 10:56:10 -0800 | [diff] [blame] | 104 | intentDesializer = new IntentDeserializer(sb.toString().getBytes()); |
Toshio Koide | 3738ee5 | 2014-02-12 14:57:39 -0800 | [diff] [blame] | 105 | Intent intent = intentDesializer.getIntent(); |
| 106 | intents.add(intent); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | private void setPathIntentId(StringBuilder sb) { |
| 113 | if (idBlock == null || nextIdBlock + 1 == idBlock.getSize()) { |
| 114 | IControllerRegistryService controllerRegistry = getControllerRegistry(); |
| 115 | if (controllerRegistry != null) { |
| 116 | idBlock = controllerRegistry.allocateUniqueIdBlock(); |
| 117 | nextIdBlock = idBlock.getStart(); |
| 118 | System.out.println("start block " + nextIdBlock + " end block " + idBlock.getEnd() + " size " + idBlock.getSize()); |
| 119 | } |
| 120 | } |
| 121 | if (idBlock != null) { |
| 122 | sb.append(nextIdBlock); |
| 123 | nextIdBlock++; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private void setPathIntentType(final String pathIntentType, StringBuilder sb) { |
| 128 | if (pathIntentType.equals("shortest-path")) { |
| 129 | sb.append(ShortestPathIntent.class.getCanonicalName()); |
| 130 | } else if (pathIntentType.equals("constrainted-shortest-path")) { |
| 131 | sb.append(ConstrainedShortestPathIntent.class.getCanonicalName()); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | private IControllerRegistryService getControllerRegistry() { |
| 136 | return (IControllerRegistryService) getContext().getAttributes().get(IControllerRegistryService.class.getCanonicalName()); |
| 137 | } |
| 138 | } |