blob: ee1c704f7ff54a9023dfaabdccae70224245a221 [file] [log] [blame]
Toshio Koide3738ee52014-02-12 14:57:39 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.datagrid.web;
6
7import java.io.IOException;
8import java.util.ArrayList;
9import java.util.Iterator;
10import java.util.List;
11import net.onrc.onos.datagrid.IDatagridService;
12import net.onrc.onos.intent.ConstrainedShortestPathIntent;
13import net.onrc.onos.intent.Intent;
14import net.onrc.onos.intent.IntentDeserializer;
15import net.onrc.onos.intent.ShortestPathIntent;
16import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
17import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
18import net.onrc.onos.registry.controller.IControllerRegistryService;
19import net.onrc.onos.registry.controller.IdBlock;
20import org.codehaus.jackson.JsonGenerationException;
21import org.codehaus.jackson.JsonNode;
22import org.codehaus.jackson.map.JsonMappingException;
23import org.restlet.resource.Post;
24import org.restlet.resource.ServerResource;
25import org.codehaus.jackson.map.ObjectMapper;
26import org.slf4j.LoggerFactory;
27
28/**
29 *
30 * @author nickkaranatsios
31 */
32public 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());
104 intentDesializer = new IntentDeserializer(graph, sb.toString().getBytes());
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}