blob: daf786f7b5bb037fd9c8ef31b36a07b266087dab [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 Karanatsios5fcf93e2014-02-15 14:13:55 -080026import java.util.Map;
27import org.codehaus.jackson.node.ArrayNode;
Nick Karanatsios88948d32014-02-18 15:14:30 -080028import org.codehaus.jackson.node.ObjectNode;
29import org.restlet.resource.Get;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080032
33/**
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080034 *
Toshio Koide3738ee52014-02-12 14:57:39 -080035 * @author nickkaranatsios
36 */
37public class IntentResource extends ServerResource {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080038 private final static Logger log = LoggerFactory.getLogger(IntentResource.class);
Toshio Koide3738ee52014-02-12 14:57:39 -080039
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080040 private class IntentStatus {
41 String intentId;
Nick Karanatsios88948d32014-02-18 15:14:30 -080042 String status;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080043
44 public IntentStatus() {}
45
Nick Karanatsios88948d32014-02-18 15:14:30 -080046 public IntentStatus(String intentId, String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080047 this.intentId = intentId;
48 this.status = status;
49 }
50
51 public String getIntentId() {
52 return intentId;
53 }
54
55 public void setIntentId(String intentId) {
56 this.intentId = intentId;
57 }
58
Nick Karanatsios88948d32014-02-18 15:14:30 -080059 public String getStatus() {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080060 return status;
61 }
62
Nick Karanatsios88948d32014-02-18 15:14:30 -080063 public void setStatus(String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080064 this.status = status;
65 }
66 }
67
Toshio Koide3738ee52014-02-12 14:57:39 -080068 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080069 public String store(String jsonIntent) throws IOException {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080070 IDatagridService datagridService = (IDatagridService) getContext()
71 .getAttributes().get(IDatagridService.class.getCanonicalName());
72 if (datagridService == null) {
73 log.debug("FlowIntentResource ONOS Datagrid Service not found");
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080074 return "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080075 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -080076 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext()
77 .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
78 if (pathRuntime == null) {
79 log.debug("Failed to get path calc runtime");
80 System.out.println("Failed to get path calc runtime");
81 return "";
82 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080083 String reply = "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080084 ObjectMapper mapper = new ObjectMapper();
85 JsonNode jNode = null;
86 try {
Nick Karanatsios88948d32014-02-18 15:14:30 -080087 jNode = mapper.readValue(jsonIntent, JsonNode.class);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080088 } catch (JsonGenerationException ex) {
89 log.error("JsonGeneration exception ", ex);
90 } catch (JsonMappingException ex) {
91 log.error("JsonMappingException occurred", ex);
92 } catch (IOException ex) {
93 log.error("IOException occurred", ex);
94 }
95
96 if (jNode != null) {
Nick Karanatsios8abe7172014-02-19 20:31:48 -080097 reply = parseJsonNode(jNode.getElements(), pathRuntime);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080098 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080099 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -0800100 }
101
Nick Karanatsios88948d32014-02-18 15:14:30 -0800102 @Get("json")
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800103 public String retrieve() throws IOException {
104 IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService)getContext().
105 getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName());
106 ObjectMapper mapper = new ObjectMapper();
107 String restStr = "";
108 ArrayNode arrayNode = mapper.createArrayNode();
109 IntentMap intentMap = pathRuntime.getHighLevelIntents();
110 Collection<Intent> intents = intentMap.getAllIntents();
111 if (!intents.isEmpty()) {
112 for (Intent intent : intents) {
113 ObjectNode node = mapper.createObjectNode();
114 node.put("intent_id", intent.getId());
115 node.put("status", intent.getState().toString());
116 arrayNode.add(node);
117 restStr = mapper.writeValueAsString(arrayNode);
118 }
119 }
120 return restStr;
Nick Karanatsios88948d32014-02-18 15:14:30 -0800121 }
122
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800123 private String parseJsonNode(Iterator<JsonNode> nodes,
124 IPathCalcRuntimeService pathRuntime) throws IOException {
125 IntentOperationList operations = new IntentOperationList();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800126 ObjectMapper mapper = new ObjectMapper();
127 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800128 while (nodes.hasNext()) {
129 JsonNode node = nodes.next();
130 if (node.isObject()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800131 JsonNode data;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800132 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800133 Map<String, Object> fields = new HashMap<>();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800134 while (fieldNames.hasNext()) {
135 String fieldName = fieldNames.next();
136 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800137 parseFields(data, fieldName, fields);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800138 }
Nick Karanatsios88948d32014-02-18 15:14:30 -0800139 String status = processIntent(fields, operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800140 appendIntentStatus(status, (String)fields.get("intent_id"), mapper, arrayNode);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800141 }
142 }
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800143 pathRuntime.executeIntentOperations(operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800144 return mapper.writeValueAsString(arrayNode);
Toshio Koide3738ee52014-02-12 14:57:39 -0800145 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800146
Nick Karanatsios88948d32014-02-18 15:14:30 -0800147 private void appendIntentStatus(String status, final String applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800148 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
149 String intentId = applnIntentId.split(":")[1];
Nick Karanatsios88948d32014-02-18 15:14:30 -0800150 ObjectNode node = mapper.createObjectNode();
151 node.put("intent_id", intentId);
152 node.put("status", status);
153 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800154 }
155
Nick Karanatsios8abe7172014-02-19 20:31:48 -0800156 private String processIntent(Map<String, Object> fields, IntentOperationList operations) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800157 String intentType = (String)fields.get("intent_type");
Nick Karanatsios88948d32014-02-18 15:14:30 -0800158 String intentOp = (String)fields.get("intent_op");
159 String status = null;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800160
Nick Karanatsios88948d32014-02-18 15:14:30 -0800161 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
162 if ((intentOp.equals("remove"))) {
163 operation = IntentOperation.Operator.REMOVE;
164 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800165 if (intentType.equals("shortest_intent_type")) {
166 ShortestPathIntent spi = new ShortestPathIntent((String) fields.get("intent_id"),
167 Long.decode((String) fields.get("srcSwitch")),
168 (long) fields.get("srcPort"),
169 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
170 Long.decode((String) fields.get("dstSwitch")),
171 (long) fields.get("dstPort"),
172 MACAddress.valueOf((String) fields.get("dstMac")).toLong());
Nick Karanatsios88948d32014-02-18 15:14:30 -0800173 operations.add(new IntentOperation(operation, spi));
Nick Karanatsios88948d32014-02-18 15:14:30 -0800174 status = (spi.getState()).toString();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800175 } else {
176 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent((String) fields.get("intent_id"),
177 Long.decode((String) fields.get("srcSwitch")),
178 (long) fields.get("srcPort"),
179 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
180 Long.decode((String) fields.get("dstSwitch")),
181 (long) fields.get("dstPort"),
182 MACAddress.valueOf((String) fields.get("dstMac")).toLong(),
183 (double) fields.get("bandwidth"));
Nick Karanatsios88948d32014-02-18 15:14:30 -0800184 operations.add(new IntentOperation(operation, cspi));
185 status = (cspi.getState()).toString();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800186 }
187 return status;
188 }
189
190 private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) {
191 if ((node.isTextual())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800192 fields.put(fieldName, node.getTextValue());
193 } else if ((node.isInt())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800194 fields.put(fieldName, (long)node.getIntValue());
195 } else if (node.isDouble()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800196 fields.put(fieldName, node.getDoubleValue());
197 } else if ((node.isLong())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800198 fields.put(fieldName, node.getLongValue());
199 }
200 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800201}