blob: 48e5e1590a6540bcd59539502cd4dbedbf50e01f [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;
30import org.restlet.resource.Get;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080031import 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 Karanatsiosda9f36f2014-02-21 01:27:02 -080040 // TODO need to assign proper application id.
41 private final String APPLN_ID = "1";
Toshio Koide3738ee52014-02-12 14:57:39 -080042
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080043 private class IntentStatus {
44 String intentId;
Nick Karanatsios88948d32014-02-18 15:14:30 -080045 String status;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080046
47 public IntentStatus() {}
48
Nick Karanatsios88948d32014-02-18 15:14:30 -080049 public IntentStatus(String intentId, String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080050 this.intentId = intentId;
51 this.status = status;
52 }
53
54 public String getIntentId() {
55 return intentId;
56 }
57
58 public void setIntentId(String intentId) {
59 this.intentId = intentId;
60 }
61
Nick Karanatsios88948d32014-02-18 15:14:30 -080062 public String getStatus() {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080063 return status;
64 }
65
Nick Karanatsios88948d32014-02-18 15:14:30 -080066 public void setStatus(String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080067 this.status = status;
68 }
69 }
70
Toshio Koide3738ee52014-02-12 14:57:39 -080071 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080072 public String store(String jsonIntent) throws IOException {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080073 IDatagridService datagridService = (IDatagridService) getContext()
74 .getAttributes().get(IDatagridService.class.getCanonicalName());
75 if (datagridService == null) {
76 log.debug("FlowIntentResource ONOS Datagrid Service not found");
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080077 return "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080078 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -080079 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext()
80 .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
81 if (pathRuntime == null) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -080082 log.warn("Failed to get path calc runtime");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080083 return "";
84 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080085 String reply = "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080086 ObjectMapper mapper = new ObjectMapper();
87 JsonNode jNode = null;
88 try {
Nick Karanatsios88948d32014-02-18 15:14:30 -080089 jNode = mapper.readValue(jsonIntent, JsonNode.class);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080090 } 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) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080099 reply = parseJsonNode(jNode.getElements(), pathRuntime);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800100 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800101 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -0800102 }
103
Nick Karanatsios88948d32014-02-18 15:14:30 -0800104 @Get("json")
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800105 public String retrieve() throws IOException {
106 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext().
107 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800108
109 String intentCategory = (String) getRequestAttributes().get("category");
110 IntentMap intentMap = null;
111 if (intentCategory.equals("high")) {
112 intentMap = pathRuntime.getHighLevelIntents();
113 } else {
114 intentMap = pathRuntime.getPathIntents();
115 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800116 ObjectMapper mapper = new ObjectMapper();
117 String restStr = "";
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800118
119 String intentId = (String) getRequestAttributes().get("intent_id");
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800120 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800121 Collection<Intent> intents = intentMap.getAllIntents();
122 if (!intents.isEmpty()) {
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800123 if ((intentId != null )) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800124 String applnIntentId = APPLN_ID + ":" + intentId;
125 Intent intent = intentMap.getIntent(applnIntentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800126 if (intent != null) {
127 ObjectNode node = mapper.createObjectNode();
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800128 // TODO refactor/remove duplicate code.
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800129 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800130 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800131 LinkedList<String> logs = intent.getLogs();
132 ArrayNode logNode = mapper.createArrayNode();
133 for (String intentLog :logs) {
134 logNode.add(intentLog);
135 }
136 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800137 arrayNode.add(node);
138 }
139 } else {
140 for (Intent intent : intents) {
141 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800142 String applnIntentId = intent.getId();
143 intentId = applnIntentId.split(":")[1];
144 node.put("intent_id", intentId);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800145 node.put("status", intent.getState().toString());
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800146 LinkedList<String> logs = intent.getLogs();
147 ArrayNode logNode = mapper.createArrayNode();
148 for (String intentLog :logs) {
149 logNode.add(intentLog);
150 }
151 node.put("log", logNode);
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800152 arrayNode.add(node);
153 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800154 }
Nick Karanatsiosed645df2014-02-20 23:22:29 -0800155 restStr = mapper.writeValueAsString(arrayNode);
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800156 }
157 return restStr;
Nick Karanatsios88948d32014-02-18 15:14:30 -0800158 }
159
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800160 private String parseJsonNode(Iterator<JsonNode> nodes,
161 IPathCalcRuntimeService pathRuntime) throws IOException {
162 IntentOperationList operations = new IntentOperationList();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800163 ObjectMapper mapper = new ObjectMapper();
164 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800165 while (nodes.hasNext()) {
166 JsonNode node = nodes.next();
167 if (node.isObject()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800168 JsonNode data;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800169 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800170 Map<String, Object> fields = new HashMap<>();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800171 while (fieldNames.hasNext()) {
172 String fieldName = fieldNames.next();
173 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800174 parseFields(data, fieldName, fields);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800175 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800176 Intent intent = processIntent(fields, operations);
177 appendIntentStatus(intent, (String)fields.get("intent_id"), mapper, arrayNode);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800178 }
179 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800180 pathRuntime.executeIntentOperations(operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800181 return mapper.writeValueAsString(arrayNode);
Toshio Koide3738ee52014-02-12 14:57:39 -0800182 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800183
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800184 private void appendIntentStatus(Intent intent, final String intentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800185 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
Nick Karanatsios88948d32014-02-18 15:14:30 -0800186 ObjectNode node = mapper.createObjectNode();
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800187 node.put("intent_id", intentId);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800188 node.put("status", intent.getState().toString());
189 LinkedList<String> logs = intent.getLogs();
190 ArrayNode logNode = mapper.createArrayNode();
191 for (String intentLog : logs) {
192 logNode.add(intentLog);
193 }
194 node.put("log", logNode);
Nick Karanatsios88948d32014-02-18 15:14:30 -0800195 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800196 }
197
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800198 private Intent processIntent(Map<String, Object> fields, IntentOperationList operations) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800199 String intentType = (String)fields.get("intent_type");
Nick Karanatsios88948d32014-02-18 15:14:30 -0800200 String intentOp = (String)fields.get("intent_op");
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800201 Intent intent;
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800202 String applnIntentId = APPLN_ID + ":" + (String)fields.get("intent_id");
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800203
Nick Karanatsios88948d32014-02-18 15:14:30 -0800204 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
205 if ((intentOp.equals("remove"))) {
206 operation = IntentOperation.Operator.REMOVE;
207 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800208 if (intentType.equals("shortest_intent_type")) {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800209 ShortestPathIntent spi = new ShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800210 Long.decode((String) fields.get("srcSwitch")),
211 (long) fields.get("srcPort"),
212 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
213 Long.decode((String) fields.get("dstSwitch")),
214 (long) fields.get("dstPort"),
215 MACAddress.valueOf((String) fields.get("dstMac")).toLong());
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800216 operations.add(operation, spi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800217 intent = spi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800218 } else {
Nick Karanatsiosda9f36f2014-02-21 01:27:02 -0800219 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800220 Long.decode((String) fields.get("srcSwitch")),
221 (long) fields.get("srcPort"),
222 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
223 Long.decode((String) fields.get("dstSwitch")),
224 (long) fields.get("dstPort"),
225 MACAddress.valueOf((String) fields.get("dstMac")).toLong(),
226 (double) fields.get("bandwidth"));
Nick Karanatsiosa1bad352014-02-22 14:16:34 -0800227 operations.add(operation, cspi);
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800228 intent = cspi;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800229 }
Nick Karanatsios87e8be72014-02-21 23:45:37 -0800230 return intent;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800231 }
232
233 private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) {
234 if ((node.isTextual())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800235 fields.put(fieldName, node.getTextValue());
236 } else if ((node.isInt())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800237 fields.put(fieldName, (long)node.getIntValue());
238 } else if (node.isDouble()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800239 fields.put(fieldName, node.getDoubleValue());
240 } else if ((node.isLong())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800241 fields.put(fieldName, node.getLongValue());
242 }
243 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800244}