1 // Copyright (c) 2018 Maen Artimy
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 const tabObj = Tabs('topology');
20 // Define the div for the tooltip
21 const div = d3.select("body").append("div")
22 .attr("class", "tooltip")
25 function add_prefix(obj) {
26 return String(obj).replace(/^0+/, "Switch_");
29 function trim_zeros(obj) {
30 return String(obj).replace(/^0+/, "");
33 // Takes JSON data and convert to a graph format that D3 understands:
36 // {"id": "0000000000000001", "type": "switch"},
37 // {"id": "0000000000000002", "type": "switch"},
38 // {"id": "16:18:83:a8:4d:1c", "type": "host"},
42 // {"source": "0000000000000002", "target": "0000000000000003",
43 // "port": {"source": "00000003", "target": "00000002"}, "value": 4},
46 function toGraph(top) {
50 var lst = top.switches;
51 for (var i = 0; i < lst.length; i++) {
52 nodes.push({ "id": lst[i].dpid, "type": "switch" });
55 if (top.links.length > 0) {
57 for (var i = 0; i < lst.length; i++) {
58 if (lst[i].src.dpid < lst[i].dst.dpid) { // prevent duplicate links
60 "source": lst[i].src.dpid, "target": lst[i].dst.dpid, "value": 4,
61 "port": { "source": lst[i].src.port_no, "target": lst[i].dst.port_no }
65 } else if (top.switches.length > 1) { // represent the network with a cloud
66 nodes.push({ "id": 0, "type": "cloud" });
67 for (var i = 0; i < lst.length; i++) {
69 "source": 0, "target": lst[i].dpid, "value": 4,
70 "port": { "source": 0, "target": 0 }
76 for (var i = 0; i < lst.length; i++) {
77 nodes.push({ "id": lst[i].mac, "type": "host" });
79 "source": lst[i].port.dpid, "target": lst[i].mac, "value": 2,
80 "port": { "source": lst[i].port.port_no, "target": 0 }
84 return { "nodes": nodes, "links": links };
87 // Plot the topology using D3.js
88 // Many online tutorials explain how this works. Example: www.puzzlr.org/force-graphs-with-d3
89 function plotGraph(graph) {
90 var svg = d3.select("svg");
91 var width = +svg.attr("width");
92 var height = +svg.attr("height");
94 //custom force to put everything in a box
95 function box_force() {
97 for (var i = 0, n = graph.nodes.length; i < n; ++i) {
98 curr_node = graph.nodes[i];
99 curr_node.x = Math.max(radius, Math.min(width - radius, curr_node.x));
100 curr_node.y = Math.max(radius, Math.min(height - radius, curr_node.y));
104 // Create a force layout simulation
106 // Link: attraction force
107 // chanrge: repulsion force
108 // x: attracts the nodes to the horizontal centre
109 // y: attracts the hosts the bottom while other nodes are attrcated to the top
110 // centre: atracts to the centre
111 // collision: avoids node collision
112 // box: keeps all nodes inside
113 var simulation = d3.forceSimulation()
115 .force("link", d3.forceLink(graph.links).id(function (d) { return d.id; }).distance(size * 2))
116 .force("charge", d3.forceManyBody().strength(-size * 30))
117 .force("x", d3.forceX(width / 2))
118 .force("y", d3.forceY(function (d) {
119 if (d.type === "host") {
120 return 3 * height / 4
122 return 1 * height / 4
125 .force("centre", d3.forceCenter(width / 2, height / 2))
126 .force("collision", d3.forceCollide().radius(35))
127 .force("box", box_force);
129 // Create nodes with image and text
130 var node = svg.append("g")
131 .attr("class", "nodes")
135 // .attr("id", function (d) { return "N" + d.id; })
136 .attr("class", "node");
139 .attr("xlink:href", function (d) {
140 if (d.type === "switch") {
141 return "/home/img/switch.svg"
142 } else if (d.type === "cloud") {
143 return "/home/img/cloud.svg"
145 return "/home/img/pc.svg"
148 .on("mouseover", handleMouseOver)
149 .on("mouseout", handleMouseOut)
152 .attr("class", "label")
153 .attr("dy", size + 14)
154 .text(function (d) { return d.id; });
155 // .text(function (d) { return d.id.replace(/^0+/, ''); });
158 // Create links with lines, circles, and text
159 var link = svg.append("g")
160 .attr("class", "links")
164 .attr("class", "link");
167 .attr("stroke-width", function (d) { return d.value; });
169 link.append("circle")
170 .attr("class", "start")
173 link.append("circle")
174 .attr("class", "end")
178 .attr("class", "start")
179 .text(function (d) { return trim_zeros(d.port.source); })
182 .attr("class", "end")
183 .text(function (d) { return trim_zeros(d.port.target); })
186 function tickActions() {
188 return Math.sqrt((d.target.x - d.source.x) ** 2 + (d.target.y - d.source.y) ** 2);
192 .attr("transform", function (d) { return "translate(" + (d.x - size / 2) + "," + (d.y - size / 2) + ")"; });
194 .attr("transform", function (d) { return "translate(" + d.source.x + "," + d.source.y + ")"; });
196 link.selectAll("line")
197 .attr("x1", function (d) { return (d.target.x - d.source.x) * size / 2 / norm(d); })
198 .attr("y1", function (d) { return (d.target.y - d.source.y) * size / 2 / norm(d); })
199 .attr("x2", function (d) { return (d.target.x - d.source.x) * (1 - size / 2 / norm(d)); })
200 .attr("y2", function (d) { return (d.target.y - d.source.y) * (1 - size / 2 / norm(d)); })
202 // position of the link start port
203 link.selectAll("circle.start")
204 .attr("cx", function (d) { return (d.target.x - d.source.x) * size / 2 / norm(d); })
205 .attr("cy", function (d) { return (d.target.y - d.source.y) * size / 2 / norm(d); })
207 // psotion of the link end port
208 link.selectAll("circle.end")
209 .attr("cx", function (d) { return (d.target.x - d.source.x) * (1 - size / 2 / norm(d)); })
210 .attr("cy", function (d) { return (d.target.y - d.source.y) * (1 - size / 2 / norm(d)); })
212 link.selectAll("text.start")
213 .attr("dx", function (d) { return (d.target.x - d.source.x) * size / 2 / norm(d); })
214 .attr("dy", function (d) { return (d.target.y - d.source.y) * size / 2 / norm(d); })
216 link.selectAll("text.end")
217 .attr("dx", function (d) { return (d.target.x - d.source.x) * (1 - size / 2 / norm(d)); })
218 .attr("dy", function (d) { return (d.target.y - d.source.y) * (1 - size / 2 / norm(d)); })
222 // Handling mouse drag
224 .on("start", drag_start)
225 .on("drag", drag_drag)
226 .on("end", drag_end));
228 function drag_start(d) {
229 if (!d3.event.active) simulation.alphaTarget(0.3).restart();
234 function drag_drag(d) {
239 function drag_end(d) {
240 if (!d3.event.active) simulation.alphaTarget(0);
245 // Handling mouse over
246 function handleMouseOver(d) {
249 .style("opacity", .9);
250 div.html(d.type + ": " + d.id)
251 .style("left", (d3.event.pageX) + "px")
252 .style("top", (d3.event.pageY - 28) + "px");
255 function handleMouseOut(d) {
258 .style("opacity", 0);
261 // drag_handler(node);
263 // run tickActions in every simulation step
264 simulation.on("tick", tickActions);
268 // Display the raw topology data
269 function listTopology(network) {
270 data = "<h1>Switches</h1>" + JSON.stringify(network.switches) + "<br>";
271 data += "<h1>Links</h1>" + JSON.stringify(network.links) + "<br>";
272 data += "<h1>Hosts</h1>" + JSON.stringify(network.hosts) + "<br>";
273 $('#data').html(data);
276 function getTopology() {
277 tabObj.buildTabs("#main", ["Graph", "Tables"], "Nothing to show!");
278 var $svg = $('<svg width="1116" height="600"></svg>');
279 var $data = $('<div id="data"></div>');
280 tabObj.buildContent('Graph', $svg);
281 tabObj.buildContent('Tables', $data);
289 const url = "http://"+location.hostname+":8080/topology"
290 let xhr = new XMLHttpRequest();
291 xhr.open('GET', url , true);
292 //console.log(xhr); //para ver en la consola
293 xhr.onload = function() {
294 if (xhr.status == 200) { //can use this.status instead
295 //console.log(xhr.responseText);// para ver en la consola
296 data = JSON.parse(xhr.responseText)
298 plotGraph(toGraph(data));
310 // d3.json(location.hostname+":8080/topology").then(function (data) {
311 // listTopology(data)
312 // plotGraph(toGraph(data));
317 // When the refresh button is clicked, clear the page and start over
318 $('.refresh').on('click', function () {