blob: c625f3fc7dc5ef289173193d69533793948332be [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;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08008import java.util.Collection;
Toshio Koide3738ee52014-02-12 14:57:39 -08009import java.util.Iterator;
Toshio Koide3738ee52014-02-12 14:57:39 -080010import net.onrc.onos.datagrid.IDatagridService;
11import net.onrc.onos.intent.ConstrainedShortestPathIntent;
Toshio Koide3738ee52014-02-12 14:57:39 -080012import net.onrc.onos.intent.ShortestPathIntent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080013import net.onrc.onos.intent.IntentOperation;
14import net.onrc.onos.intent.IntentMap;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080015import net.onrc.onos.intent.Intent;
16import net.onrc.onos.intent.runtime.IPathCalcRuntimeService;
17import net.onrc.onos.intent.IntentOperationList;
Toshio Koide3738ee52014-02-12 14:57:39 -080018import 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 Karanatsios5fcf93e2014-02-15 14:13:55 -080025import java.util.HashMap;
Nick Karanatsios87e8be72014-02-21 23:45:37 -080026import java.util.LinkedList;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080027import java.util.Map;
28import org.codehaus.jackson.node.ArrayNode;
Nick Karanatsios88948d32014-02-18 15:14:30 -080029import org.codehaus.jackson.node.ObjectNode;
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080030import org.restlet.resource.Delete;
Nick Karanatsios88948d32014-02-18 15:14:30 -080031import org.restlet.resource.Get;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080034
35/**
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080036 *
Toshio Koide3738ee52014-02-12 14:57:39 -080037 * @author nickkaranatsios
38 */
39public class IntentResource extends ServerResource {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080040 private final static Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080041 // TODO need to assign proper application id.
42 private final String APPLN_ID = "1";
Toshio Koide3738ee52014-02-12 14:57:39 -080043
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080044 private class IntentStatus {
45 String intentId;
Nick Karanatsios88948d32014-02-18 15:14:30 -080046 String status;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080047
48 public IntentStatus() {}
49
Nick Karanatsios88948d32014-02-18 15:14:30 -080050 public IntentStatus(String intentId, String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080051 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
Nick Karanatsios88948d32014-02-18 15:14:30 -080063 public String getStatus() {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080064 return status;
65 }
66
Nick Karanatsios88948d32014-02-18 15:14:30 -080067 public void setStatus(String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080068 this.status = status;
69 }
70 }
71
Toshio Koide3738ee52014-02-12 14:57:39 -080072 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080073 public String store(String jsonIntent) throws IOException {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080074 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext()
75 .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
76 if (pathRuntime == null) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080077 log.warn("Failed to get path calc runtime");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080078 return "";
79 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080080 String reply = "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080081 ObjectMapper mapper = new ObjectMapper();
82 JsonNode jNode = null;
83 try {
Nick Karanatsios88948d32014-02-18 15:14:30 -080084 jNode = mapper.readValue(jsonIntent, JsonNode.class);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080085 } catch (JsonGenerationException ex) {
86 log.error("JsonGeneration exception ", ex);
87 } catch (JsonMappingException ex) {
88 log.error("JsonMappingException occurred", ex);
89 } catch (IOException ex) {
90 log.error("IOException occurred", ex);
91 }
92
93 if (jNode != null) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080094 reply = parseJsonNode(jNode.getElements(), pathRuntime);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080095 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080096 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -080097 }
Nick Karanatsios1f4defb2014-02-23 19:34:47 -080098
99 @Delete("json")
100 public String store() {
101 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext().
102 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
103 pathRuntime.purgeIntents();
104 // TODO no reply yet from the purge intents call
105 return "";
106
107 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800108
Nick Karanatsios88948d32014-02-18 15:14:30 -0800109 @Get("json")
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800110 public String retrieve() throws IOException {
111 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext().
112 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800113
114 String intentCategory = (String) getRequestAttributes().get("category");
115 IntentMap intentMap = null;
116 if (intentCategory.equals("high")) {
117 intentMap = pathRuntime.getHighLevelIntents();
118 } else {
119 intentMap = pathRuntime.getPathIntents();
120 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800121 ObjectMapper mapper = new ObjectMapper();
122 String restStr = "";
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800123
124 String intentId = (String) getRequestAttributes().get("intent_id");
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800125 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800126 Collection<Intent> intents = intentMap.getAllIntents();
127 if (!intents.isEmpty()) {
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800128 if ((intentId != null )) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800129 String applnIntentId = APPLN_ID + ":" + intentId;
130 Intent intent = intentMap.getIntent(applnIntentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800131 if (intent != null) {
132 ObjectNode node = mapper.createObjectNode();
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800133 // TODO refactor/remove duplicate code.
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800134 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800135 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800136 LinkedList<String> logs = intent.getLogs();
137 ArrayNode logNode = mapper.createArrayNode();
138 for (String intentLog :logs) {
139 logNode.add(intentLog);
140 }
141 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800142 arrayNode.add(node);
143 }
144 } else {
145 for (Intent intent : intents) {
146 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800147 String applnIntentId = intent.getId();
148 intentId = applnIntentId.split(":")[1];
149 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800150 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800151 LinkedList<String> logs = intent.getLogs();
152 ArrayNode logNode = mapper.createArrayNode();
153 for (String intentLog :logs) {
154 logNode.add(intentLog);
155 }
156 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800157 arrayNode.add(node);
158 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800159 }
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800160 restStr = mapper.writeValueAsString(arrayNode);
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800161 }
162 return restStr;
Nick Karanatsios88948d32014-02-18 15:14:30 -0800163 }
164
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800165 private String parseJsonNode(Iterator<JsonNode> nodes,
166 IPathCalcRuntimeService pathRuntime) throws IOException {
167 IntentOperationList operations = new IntentOperationList();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800168 ObjectMapper mapper = new ObjectMapper();
169 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800170 while (nodes.hasNext()) {
171 JsonNode node = nodes.next();
172 if (node.isObject()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800173 JsonNode data;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800174 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800175 Map<String, Object> fields = new HashMap<>();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800176 while (fieldNames.hasNext()) {
177 String fieldName = fieldNames.next();
178 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800179 parseFields(data, fieldName, fields);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800180 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800181 Intent intent = processIntent(fields, operations);
182 appendIntentStatus(intent, (String)fields.get("intent_id"), mapper, arrayNode);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800183 }
184 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800185 pathRuntime.executeIntentOperations(operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800186 return mapper.writeValueAsString(arrayNode);
Toshio Koide3738ee52014-02-12 14:57:39 -0800187 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800188
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800189 private void appendIntentStatus(Intent intent, final String intentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800190 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
Nick Karanatsios88948d32014-02-18 15:14:30 -0800191 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800192 node.put("intent_id", intentId);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800193 node.put("status", intent.getState().toString());
194 LinkedList<String> logs = intent.getLogs();
195 ArrayNode logNode = mapper.createArrayNode();
196 for (String intentLog : logs) {
197 logNode.add(intentLog);
198 }
199 node.put("log", logNode);
Nick Karanatsios88948d32014-02-18 15:14:30 -0800200 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800201 }
202
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800203 private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800204 String intentType = (String)fields.get("intent_type");
Nick Karanatsios88948d32014-02-18 15:14:30 -0800205 String intentOp = (String)fields.get("intent_op");
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800206 Intent intent;
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800207 String applnIntentId = APPLN_ID + ":" + (String)fields.get("intent_id");
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800208
Nick Karanatsios88948d32014-02-18 15:14:30 -0800209 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
210 if ((intentOp.equals("remove"))) {
211 operation = IntentOperation.Operator.REMOVE;
212 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800213 if (intentType.equals("shortest_intent_type")) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800214 ShortestPathIntent spi = new ShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800215 Long.decode((String) fields.get("srcSwitch")),
216 (long) fields.get("srcPort"),
217 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
218 Long.decode((String) fields.get("dstSwitch")),
219 (long) fields.get("dstPort"),
220 MACAddress.valueOf((String) fields.get("dstMac")).toLong());
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800221 operations.add(operation, spi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800222 intent = spi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800223 } else {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800224 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800225 Long.decode((String) fields.get("srcSwitch")),
226 (long) fields.get("srcPort"),
227 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
228 Long.decode((String) fields.get("dstSwitch")),
229 (long) fields.get("dstPort"),
230 MACAddress.valueOf((String) fields.get("dstMac")).toLong(),
231 (double) fields.get("bandwidth"));
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800232 operations.add(operation, cspi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800233 intent = cspi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800234 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800235 return intent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800236 }
237
238 private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) {
239 if ((node.isTextual())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800240 fields.put(fieldName, node.getTextValue());
241 } else if ((node.isInt())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800242 fields.put(fieldName, (long)node.getIntValue());
243 } else if (node.isDouble()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800244 fields.put(fieldName, node.getDoubleValue());
245 } else if ((node.isLong())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800246 fields.put(fieldName, node.getLongValue());
247 }
248 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800249}