Thomas Vachuska

Hooking up GUI server & client via web-socket.

Change-Id: If522a5f46de528f28bf09a985af40b140ef5abaa
......@@ -15,8 +15,18 @@
*/
package org.onlab.onos.gui;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.eclipse.jetty.websocket.WebSocket;
import org.onlab.onos.event.Event;
import org.onlab.onos.net.Annotations;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.device.DeviceEvent;
import org.onlab.onos.net.device.DeviceService;
import org.onlab.onos.net.link.LinkEvent;
import org.onlab.onos.net.topology.Topology;
import org.onlab.onos.net.topology.TopologyEdge;
import org.onlab.onos.net.topology.TopologyEvent;
......@@ -28,6 +38,11 @@ import org.onlab.osgi.ServiceDirectory;
import java.io.IOException;
import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED;
import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
import static org.onlab.onos.net.link.LinkEvent.Type.LINK_REMOVED;
/**
* Web socket capable of interacting with the GUI topology view.
*/
......@@ -37,6 +52,8 @@ public class TopologyWebSocket implements WebSocket.OnTextMessage, TopologyListe
private final TopologyService topologyService;
private final DeviceService deviceService;
private final ObjectMapper mapper = new ObjectMapper();
private Connection connection;
/**
......@@ -58,22 +75,19 @@ public class TopologyWebSocket implements WebSocket.OnTextMessage, TopologyListe
if (topologyService != null && deviceService != null) {
topologyService.addListener(this);
sendMessage("Yo!!!");
Topology topology = topologyService.currentTopology();
TopologyGraph graph = topologyService.getGraph(topology);
for (TopologyVertex vertex : graph.getVertexes()) {
sendMessage(deviceService.getDevice(vertex.deviceId()).toString());
sendMessage(message(new DeviceEvent(DEVICE_ADDED,
deviceService.getDevice(vertex.deviceId()))));
}
for (TopologyEdge edge : graph.getEdges()) {
sendMessage(edge.link().toString());
sendMessage(message(new LinkEvent(LINK_ADDED, edge.link())));
}
sendMessage("That's what we're starting with...");
} else {
sendMessage("No topology service!!!");
sendMessage(message("error", "No topology service!!!"));
}
}
......@@ -90,7 +104,7 @@ public class TopologyWebSocket implements WebSocket.OnTextMessage, TopologyListe
System.out.println("Received: " + data);
}
public void sendMessage(String data) {
private void sendMessage(String data) {
try {
connection.sendMessage(data);
} catch (IOException e) {
......@@ -98,9 +112,80 @@ public class TopologyWebSocket implements WebSocket.OnTextMessage, TopologyListe
}
}
// Produces a link event message to the client.
private String message(DeviceEvent event) {
Device device = event.subject();
ObjectNode payload = mapper.createObjectNode()
.put("id", device.id().toString())
.put("type", device.type().toString().toLowerCase())
.put("online", deviceService.isAvailable(device.id()));
// Generate labels: id, chassis id, no-label, optional-name
ArrayNode labels = mapper.createArrayNode();
labels.add(device.id().toString());
labels.add(device.chassisId().toString());
labels.add(" "); // compact no-label view
labels.add(device.annotations().value("name"));
// Add labels, props and stuff the payload into envelope.
payload.set("labels", labels);
payload.set("props", props(device.annotations()));
payload.set("metaUi", mapper.createObjectNode());
String type = (event.type() == DEVICE_ADDED) ? "addDevice" :
((event.type() == DEVICE_REMOVED) ? "removeDevice" : "updateDevice");
return envelope(type, payload);
}
// Produces a link event message to the client.
private String message(LinkEvent event) {
Link link = event.subject();
ObjectNode payload = mapper.createObjectNode()
.put("type", link.type().toString().toLowerCase())
.put("linkWidth", 2)
.put("src", link.src().deviceId().toString())
.put("srcPort", link.src().port().toString())
.put("dst", link.dst().deviceId().toString())
.put("dstPort", link.dst().port().toString());
String type = (event.type() == LINK_ADDED) ? "addLink" :
((event.type() == LINK_REMOVED) ? "removeLink" : "removeLink");
return envelope(type, payload);
}
// Produces JSON structure from annotations.
private JsonNode props(Annotations annotations) {
ObjectNode props = mapper.createObjectNode();
for (String key : annotations.keys()) {
props.put(key, annotations.value(key));
}
return props;
}
// Produces a log message event bound to the client.
private String message(String severity, String message) {
return envelope("message",
mapper.createObjectNode()
.put("severity", severity)
.put("message", message));
}
// Puts the payload into an envelope and returns it.
private String envelope(String type, ObjectNode payload) {
ObjectNode event = mapper.createObjectNode();
event.put("event", type);
event.set("payload", payload);
return event.toString();
}
@Override
public void event(TopologyEvent event) {
sendMessage(event.toString());
for (Event reason : event.reasons()) {
if (reason instanceof DeviceEvent) {
sendMessage(message((DeviceEvent) reason));
} else if (reason instanceof LinkEvent) {
sendMessage(message((LinkEvent) reason));
}
}
}
}
......
......@@ -604,6 +604,7 @@
webSock.ws.onmessage = function(m) {
if (m.data) {
console.log(m.data);
handleServerEvent(JSON.parse(m.data));
}
};
......