blob: 416ab76958d28c0e21b9dfa7535e84a9e64b53d6 [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 Karanatsios1a4a2002014-02-14 04:35:39 -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;
13import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
14import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
15import net.onrc.onos.registry.controller.IControllerRegistryService;
16import net.onrc.onos.registry.controller.IdBlock;
17import org.codehaus.jackson.JsonGenerationException;
18import org.codehaus.jackson.JsonNode;
19import org.codehaus.jackson.map.JsonMappingException;
20import org.restlet.resource.Post;
21import org.restlet.resource.ServerResource;
22import org.codehaus.jackson.map.ObjectMapper;
23import org.slf4j.LoggerFactory;
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080024import net.floodlightcontroller.util.MACAddress;
25import net.onrc.onos.ofcontroller.networkgraph.Port;
26import net.onrc.onos.ofcontroller.networkgraph.Switch;
27import com.esotericsoftware.kryo.Kryo;
28import com.esotericsoftware.kryo.io.Input;
29import com.esotericsoftware.kryo.io.Output;
Toshio Koide3738ee52014-02-12 14:57:39 -080030
31/**
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080032 *
Toshio Koide3738ee52014-02-12 14:57:39 -080033 * @author nickkaranatsios
34 */
35public class IntentResource extends ServerResource {
36
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080037 private final static org.slf4j.Logger log = LoggerFactory
38 .getLogger(IntentResource.class);
39 private final String sep = ":";
Toshio Koide3738ee52014-02-12 14:57:39 -080040 private IdBlock idBlock = null;
41 private long nextIdBlock = 0;
42
43 @Post("json")
44 public void store(String jsonFlowIntent) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080045 IDatagridService datagridService = (IDatagridService) getContext()
46 .getAttributes().get(IDatagridService.class.getCanonicalName());
47 if (datagridService == null) {
48 log.debug("FlowIntentResource ONOS Datagrid Service not found");
49 return;
50 }
51 INetworkGraphService networkGraphService = (INetworkGraphService) getContext()
52 .getAttributes().get(
53 INetworkGraphService.class.getCanonicalName());
54 NetworkGraph graph = networkGraphService.getNetworkGraph();
Toshio Koide3738ee52014-02-12 14:57:39 -080055
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080056 ObjectMapper mapper = new ObjectMapper();
57 JsonNode jNode = null;
58 try {
59 System.out.println("json string " + jsonFlowIntent);
60 jNode = mapper.readValue(jsonFlowIntent, JsonNode.class);
61 } catch (JsonGenerationException ex) {
62 log.error("JsonGeneration exception ", ex);
63 } catch (JsonMappingException ex) {
64 log.error("JsonMappingException occurred", ex);
65 } catch (IOException ex) {
66 log.error("IOException occurred", ex);
67 }
68
69 if (jNode != null) {
70 Kryo kryo = new Kryo();
71 parseJsonNode(kryo, jNode.getElements(), datagridService);
72 // datagridService.registerIntent(intents);
73 }
Toshio Koide3738ee52014-02-12 14:57:39 -080074 }
75
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080076 private void parseJsonNode(Kryo kryo, Iterator<JsonNode> nodes,
77 IDatagridService datagridService) {
78 StringBuilder sb = new StringBuilder();
79 sb.ensureCapacity(256);
Toshio Koide3738ee52014-02-12 14:57:39 -080080
Nick Karanatsios1a4a2002014-02-14 04:35:39 -080081 while (nodes.hasNext()) {
82 JsonNode node = nodes.next();
83 if (node.isObject()) {
84 JsonNode data = null;
85 Iterator<String> fieldNames = node.getFieldNames();
86 String intentId = null;
87 String pathTypeName = null;
88 long srcSwitch = 0, dstSwitch = 0;
89 String srcMac = null, dstMac = null;
90 long srcPort = 0, dstPort = 0;
91 Double bandwidth = null;
92 while (fieldNames.hasNext()) {
93 String fieldName = fieldNames.next();
94 data = node.get(fieldName);
95 if (fieldName.equals("type")) {
96 if (data != null) {
97 System.out.println("type is not null "
98 + data.getTextValue());
99 // uuid = setPathIntentId();
100 pathTypeName = data.getTextValue();
101 setPathIntentType(pathTypeName, sb);
102 }
103 } else if (fieldName.equals("intentId")) {
104 intentId = data.getTextValue();
105 } else if (fieldName.equals("srcSwitch")) {
106 srcSwitch = Long.decode(data.getTextValue());
107 } else if (fieldName.equals("dstSwitch")) {
108 dstSwitch = Long.decode(data.getTextValue());
109 } else if (fieldName.equals("srcMac")) {
110 srcMac = data.getTextValue();
111 } else if (fieldName.equals("dstMac")) {
112 dstMac = data.getTextValue();
113 } else if (fieldName.equals("srcPort")) {
114 srcPort = data.getLongValue();
115 } else if (fieldName.equals("dstPort")) {
116 dstPort = data.getLongValue();
117 } else if (fieldName.equals("bandwidth")) {
118 bandwidth = data.getDoubleValue();
119 }
120 }
121 if (pathTypeName.equals("shortest-path")) {
122 ShortestPathIntent spi = new ShortestPathIntent(intentId,
123 srcSwitch,
124 srcPort,
125 MACAddress.valueOf(srcMac).toLong(),
126 dstSwitch,
127 dstPort,
128 MACAddress.valueOf(dstMac).toLong());
129 sb.append(toBytes(kryo, spi));
130
131 } else {
132 ConstrainedShortestPathIntent cspi = new ConstrainedShortestPathIntent(intentId,
133 srcSwitch,
134 srcPort,
135 MACAddress.valueOf(srcMac).toLong(), dstSwitch,
136 dstPort, MACAddress.valueOf(dstMac).toLong(),
137 bandwidth);
138 sb.append(toBytes(kryo, cspi));
139
140 }
141 System.out.println("constructed node " + sb.toString());
142 // datagridService.registerIntent(Long.toString(uuid),
143 // sb.toString().getBytes());
144 sb.delete(0, sb.length());
145 }
146 }
Toshio Koide3738ee52014-02-12 14:57:39 -0800147 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800148
149 private long setPathIntentId() {
150 long uuid = 0;
151 if (idBlock == null || nextIdBlock + 1 == idBlock.getSize()) {
152 IControllerRegistryService controllerRegistry = getControllerRegistry();
153 if (controllerRegistry != null) {
154 idBlock = controllerRegistry.allocateUniqueIdBlock();
155 nextIdBlock = idBlock.getStart();
156 System.out.println("start block " + nextIdBlock + " end block "
157 + idBlock.getEnd() + " size " + idBlock.getSize());
158 }
159 }
160 if (idBlock != null) {
161 uuid = nextIdBlock;
162 nextIdBlock++;
163 }
164 return uuid;
Toshio Koide3738ee52014-02-12 14:57:39 -0800165 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800166
Toshio Koide3738ee52014-02-12 14:57:39 -0800167 private void setPathIntentType(final String pathIntentType, StringBuilder sb) {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800168 String canonicalName = null;
169 if (pathIntentType.equals("shortest-path")) {
170 canonicalName = ShortestPathIntent.class.getCanonicalName();
171 sb.append(ShortestPathIntent.class.getCanonicalName());
172 } else if (pathIntentType.equals("constrained-shortest-path")) {
173 canonicalName = ShortestPathIntent.class.getCanonicalName();
174 }
175 sb.append(canonicalName);
176 sb.append(sep);
Toshio Koide3738ee52014-02-12 14:57:39 -0800177 }
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800178
Toshio Koide3738ee52014-02-12 14:57:39 -0800179 private IControllerRegistryService getControllerRegistry() {
Nick Karanatsios1a4a2002014-02-14 04:35:39 -0800180 return (IControllerRegistryService) getContext().getAttributes().get(
181 IControllerRegistryService.class.getCanonicalName());
182 }
183
184 private byte[] toBytes(Kryo kryo, Object value) {
185 Output output = new Output(1024);
186 kryo.writeObject(output, value);
187 output.close();
188 return output.toBytes();
Toshio Koide3738ee52014-02-12 14:57:39 -0800189 }
190}