blob: 0f552d34bc6e59908acd3c8c9f17233f19dab2ee [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;
Nick Karanatsios88948d32014-02-18 15:14:30 -080014//import net.onrc.onos.intent.Intent.IntentState;
Toshio Koide3738ee52014-02-12 14:57:39 -080015import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
16import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
17import net.onrc.onos.registry.controller.IControllerRegistryService;
18import net.onrc.onos.registry.controller.IdBlock;
19import org.codehaus.jackson.JsonGenerationException;
20import org.codehaus.jackson.JsonNode;
21import org.codehaus.jackson.map.JsonMappingException;
22import org.restlet.resource.Post;
23import org.restlet.resource.ServerResource;
24import org.codehaus.jackson.map.ObjectMapper;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080025import net.floodlightcontroller.util.MACAddress;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080026import com.esotericsoftware.kryo.Kryo;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080027import com.esotericsoftware.kryo.io.Output;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080028import java.util.HashMap;
29import java.util.LinkedList;
30import java.util.Map;
31import org.codehaus.jackson.node.ArrayNode;
Nick Karanatsios88948d32014-02-18 15:14:30 -080032import org.codehaus.jackson.node.ObjectNode;
33import org.restlet.resource.Get;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Toshio Koide3738ee52014-02-12 14:57:39 -080036
37/**
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080038 *
Toshio Koide3738ee52014-02-12 14:57:39 -080039 * @author nickkaranatsios
40 */
41public class IntentResource extends ServerResource {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080042 private final static Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080043 private final String sep = ":";
Toshio Koide3738ee52014-02-12 14:57:39 -080044 private IdBlock idBlock = null;
45 private long nextIdBlock = 0;
46
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080047 private class IntentStatus {
48 String intentId;
Nick Karanatsios88948d32014-02-18 15:14:30 -080049 String status;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080050
51 public IntentStatus() {}
52
Nick Karanatsios88948d32014-02-18 15:14:30 -080053 public IntentStatus(String intentId, String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080054 this.intentId = intentId;
55 this.status = status;
56 }
57
58 public String getIntentId() {
59 return intentId;
60 }
61
62 public void setIntentId(String intentId) {
63 this.intentId = intentId;
64 }
65
Nick Karanatsios88948d32014-02-18 15:14:30 -080066 public String getStatus() {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080067 return status;
68 }
69
Nick Karanatsios88948d32014-02-18 15:14:30 -080070 public void setStatus(String status) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080071 this.status = status;
72 }
73 }
74
Toshio Koide3738ee52014-02-12 14:57:39 -080075 @Post("json")
Nick Karanatsios88948d32014-02-18 15:14:30 -080076 public String store(String jsonIntent) throws IOException {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080077 IDatagridService datagridService = (IDatagridService) getContext()
78 .getAttributes().get(IDatagridService.class.getCanonicalName());
79 if (datagridService == null) {
80 log.debug("FlowIntentResource ONOS Datagrid Service not found");
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080081 return "";
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080082 }
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) {
97 Kryo kryo = new Kryo();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -080098 reply = parseJsonNode(kryo, jNode.getElements(), datagridService);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080099 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800100 return reply;
Toshio Koide3738ee52014-02-12 14:57:39 -0800101 }
102
Nick Karanatsios88948d32014-02-18 15:14:30 -0800103 @Get("json")
104 public String retrieve() {
105 return "123";
106 }
107
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800108 private String parseJsonNode(Kryo kryo, Iterator<JsonNode> nodes,
109 IDatagridService datagridService) throws IOException {
110 LinkedList<IntentOperation> operations = new LinkedList<>();
111 ObjectMapper mapper = new ObjectMapper();
112 ArrayNode arrayNode = mapper.createArrayNode();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800113 while (nodes.hasNext()) {
114 JsonNode node = nodes.next();
115 if (node.isObject()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800116 JsonNode data;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800117 Iterator<String> fieldNames = node.getFieldNames();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800118 Map<String, Object> fields = new HashMap<>();
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800119 while (fieldNames.hasNext()) {
120 String fieldName = fieldNames.next();
121 data = node.get(fieldName);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800122 parseFields(data, fieldName, fields);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800123 }
Nick Karanatsios88948d32014-02-18 15:14:30 -0800124 String status = processIntent(fields, operations);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800125 appendIntentStatus(status, (String)fields.get("intent_id"), mapper, arrayNode);
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800126 // datagridService.registerIntent(Long.toString(uuid),
127 // sb.toString().getBytes());
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800128 }
129 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800130 IntentMap intents = new IntentMap();
131 intents.executeOperations(operations);
132 return mapper.writeValueAsString(arrayNode);
133
Toshio Koide3738ee52014-02-12 14:57:39 -0800134 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800135
Nick Karanatsios88948d32014-02-18 15:14:30 -0800136 private void appendIntentStatus(String status, final String applnIntentId,
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800137 ObjectMapper mapper, ArrayNode arrayNode) throws IOException {
Nick Karanatsios88948d32014-02-18 15:14:30 -0800138 System.out.println("status " + status);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800139 String intentId = applnIntentId.split(":")[1];
Nick Karanatsios88948d32014-02-18 15:14:30 -0800140 ObjectNode node = mapper.createObjectNode();
141 node.put("intent_id", intentId);
142 node.put("status", status);
143 arrayNode.add(node);
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800144 }
145
Nick Karanatsios88948d32014-02-18 15:14:30 -0800146 private String processIntent(Map<String, Object> fields, LinkedList<IntentOperation> operations) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800147 String intentType = (String)fields.get("intent_type");
Nick Karanatsios88948d32014-02-18 15:14:30 -0800148 String intentOp = (String)fields.get("intent_op");
149 String status = null;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800150
Nick Karanatsios88948d32014-02-18 15:14:30 -0800151 IntentOperation.Operator operation = IntentOperation.Operator.ADD;
152 if ((intentOp.equals("remove"))) {
153 operation = IntentOperation.Operator.REMOVE;
154 }
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800155 if (intentType.equals("shortest_intent_type")) {
156 ShortestPathIntent spi = new ShortestPathIntent((String) fields.get("intent_id"),
157 Long.decode((String) fields.get("srcSwitch")),
158 (long) fields.get("srcPort"),
159 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
160 Long.decode((String) fields.get("dstSwitch")),
161 (long) fields.get("dstPort"),
162 MACAddress.valueOf((String) fields.get("dstMac")).toLong());
Nick Karanatsios88948d32014-02-18 15:14:30 -0800163 operations.add(new IntentOperation(operation, spi));
164 System.out.println("intent operation " + operation.toString());
165 status = (spi.getState()).toString();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800166 } else {
167 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent((String) fields.get("intent_id"),
168 Long.decode((String) fields.get("srcSwitch")),
169 (long) fields.get("srcPort"),
170 MACAddress.valueOf((String) fields.get("srcMac")).toLong(),
171 Long.decode((String) fields.get("dstSwitch")),
172 (long) fields.get("dstPort"),
173 MACAddress.valueOf((String) fields.get("dstMac")).toLong(),
174 (double) fields.get("bandwidth"));
Nick Karanatsios88948d32014-02-18 15:14:30 -0800175 operations.add(new IntentOperation(operation, cspi));
176 status = (cspi.getState()).toString();
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800177 }
178 return status;
179 }
180
181 private void parseFields(JsonNode node, String fieldName, Map<String, Object> fields) {
182 if ((node.isTextual())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800183 fields.put(fieldName, node.getTextValue());
184 } else if ((node.isInt())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800185 fields.put(fieldName, (long)node.getIntValue());
186 } else if (node.isDouble()) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800187 fields.put(fieldName, node.getDoubleValue());
188 } else if ((node.isLong())) {
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800189 fields.put(fieldName, node.getLongValue());
190 }
191 }
192
193 @Deprecated
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800194 private long setPathIntentId() {
195 long uuid = 0;
196 if (idBlock == null || nextIdBlock + 1 == idBlock.getSize()) {
197 IControllerRegistryService controllerRegistry = getControllerRegistry();
198 if (controllerRegistry != null) {
199 idBlock = controllerRegistry.allocateUniqueIdBlock();
200 nextIdBlock = idBlock.getStart();
201 System.out.println("start block " + nextIdBlock + " end block "
202 + idBlock.getEnd() + " size " + idBlock.getSize());
203 }
204 }
205 if (idBlock != null) {
206 uuid = nextIdBlock;
207 nextIdBlock++;
208 }
209 return uuid;
Toshio Koide3738ee52014-02-12 14:57:39 -0800210 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800211
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800212 @Deprecated
213 private void setIntentType(final String intentType, StringBuilder sb) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800214 String canonicalName = null;
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800215 if (intentType.equals("shortest-path")) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800216 canonicalName = ShortestPathIntent.class.getCanonicalName();
217 sb.append(ShortestPathIntent.class.getCanonicalName());
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800218 } else if (intentType.equals("constrained-shortest-path")) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800219 canonicalName = ShortestPathIntent.class.getCanonicalName();
220 }
221 sb.append(canonicalName);
222 sb.append(sep);
Toshio Koide3738ee52014-02-12 14:57:39 -0800223 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800224
Toshio Koide3738ee52014-02-12 14:57:39 -0800225 private IControllerRegistryService getControllerRegistry() {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800226 return (IControllerRegistryService) getContext().getAttributes().get(
227 IControllerRegistryService.class.getCanonicalName());
228 }
229
Nick Karanatsios5fcf93e2014-02-15 14:13:55 -0800230 @Deprecated
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800231 private byte[] toBytes(Kryo kryo, Object value) {
232 Output output = new Output(1024);
233 kryo.writeObject(output, value);
234 output.close();
235 return output.toBytes();
Toshio Koide3738ee52014-02-12 14:57:39 -0800236 }
237}