blob: 0d50865d2892c9f9986323772dc0aa1d485b776e [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;
Toshio Koide3738ee52014-02-12 14:57:39 -08008import java.util.Iterator;
Toshio Koide3738ee52014-02-12 14:57:39 -08009import net.onrc.onos.datagrid.IDatagridService;
10import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koide3738ee52014-02-12 14:57:39 -080011import net.onrc.onos.intent.ShortestPathIntent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080012import net.onrc.onos.intent.IntentOperation;
13import net.onrc.onos.intent.IntentMap;
Toshio Koide3738ee52014-02-12 14:57:39 -080014import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
15import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
16import net.onrc.onos.registry.controller.IControllerRegistryService;
17import net.onrc.onos.registry.controller.IdBlock;
18import org.codehaus.jackson.JsonGenerationException;
19import org.codehaus.jackson.JsonNode;
20import org.codehaus.jackson.map.JsonMappingException;
21import org.restlet.resource.Post;
22import org.restlet.resource.ServerResource;
23import org.codehaus.jackson.map.ObjectMapper;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080024import net.floodlightcontroller.util.MACAddress;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080025import com.esotericsoftware.kryo.Kryo;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080026import com.esotericsoftware.kryo.io.Output;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080027import java.util.HashMap;
28import java.util.LinkedList;
29import java.util.Map;
30import org.codehaus.jackson.node.ArrayNode;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080033
34/**
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080035 *
Toshio Koide3738ee52014-02-12 14:57:39 -080036 * @author nickkaranatsios
37 */
38public class IntentResource extends ServerResource {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080039 private final static Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080040 private final String sep = ":";
Toshio Koide3738ee52014-02-12 14:57:39 -080041 private IdBlock idBlock = null;
42 private long nextIdBlock = 0;
43
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080044 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 Koide3738ee52014-02-12 14:57:39 -080072 @Post("json")
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080073 public String store(String jsonFlowIntent) throws IOException {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080074 IDatagridService datagridService = (IDatagridService) getContext()
75 .getAttributes().get(IDatagridService.class.getCanonicalName());
76 if (datagridService == null) {
77 log.debug("FlowIntentResource ONOS Datagrid Service not found");
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080078 return "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080079 }
80 INetworkGraphService networkGraphService = (INetworkGraphService) getContext()
81 .getAttributes().get(
82 INetworkGraphService.class.getCanonicalName());
83 NetworkGraph graph = networkGraphService.getNetworkGraph();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080084 String reply = "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080085 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 Karanatsios5fcf93e2014-02-15 14:13:55 -0800100 reply = parseJsonNode(kryo, jNode.getElements(), datagridService);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800101 // datagridService.registerIntent(intents);
102 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800103 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -0800104 }
105
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800106 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 Karanatsios1a4a2002014-02-14 04:35:39 -0800111 while (nodes.hasNext()) {
112 JsonNode node = nodes.next();
113 if (node.isObject()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800114 JsonNode data;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800115 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800116 Map<String, Object> fields = new HashMap<>();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800117 while (fieldNames.hasNext()) {
118 String fieldName = fieldNames.next();
119 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800120 parseFields(data, fieldName, fields);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800121 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800122 System.out.println("recv fields " + fields);
123 boolean status = processIntent(fields, operations);
124 appendIntentStatus(status, (String)fields.get("intent_id"), mapper, arrayNode);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800125 // datagridService.registerIntent(Long.toString(uuid),
126 // sb.toString().getBytes());
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800127 }
128 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800129 IntentMap intents = new IntentMap();
130 intents.executeOperations(operations);
131 return mapper.writeValueAsString(arrayNode);
132
Toshio Koide3738ee52014-02-12 14:57:39 -0800133 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800134
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800135 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 Karanatsios1a4a2002014-02-14 04:35:39 -0800193 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 Koide3738ee52014-02-12 14:57:39 -0800209 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800210
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800211 @Deprecated
212 private void setIntentType(final String intentType, StringBuilder sb) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800213 String canonicalName = null;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800214 if (intentType.equals("shortest-path")) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800215 canonicalName = ShortestPathIntent.class.getCanonicalName();
216 sb.append(ShortestPathIntent.class.getCanonicalName());
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800217 } else if (intentType.equals("constrained-shortest-path")) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800218 canonicalName = ShortestPathIntent.class.getCanonicalName();
219 }
220 sb.append(canonicalName);
221 sb.append(sep);
Toshio Koide3738ee52014-02-12 14:57:39 -0800222 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800223
Toshio Koide3738ee52014-02-12 14:57:39 -0800224 private IControllerRegistryService getControllerRegistry() {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800225 return (IControllerRegistryService) getContext().getAttributes().get(
226 IControllerRegistryService.class.getCanonicalName());
227 }
228
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800229 @Deprecated
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800230 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 Koide3738ee52014-02-12 14:57:39 -0800235 }
236}