Added support for grid coordinates in the legacy topology view.

Change-Id: I48533f24eded919673af92a59cc5e2edefef46b3
This commit is contained in:
Thomas Vachuska 2018-03-06 14:37:45 -08:00
parent 3b9644f6c1
commit c67a9912cc
5 changed files with 58 additions and 13 deletions

View File

@ -15,6 +15,8 @@
*/
package org.onosproject.net.config.basics;
import java.util.Objects;
/**
* Basic configuration for network elements, e.g. devices, hosts. Such elements
* can have a friendly name, geo-coordinates (or grid-coordinates),
@ -202,16 +204,12 @@ public abstract class BasicElementConfig<S> extends AllowedEntityConfig<S> {
/**
* Returns true if the grid coordinates (gridY and gridX) are set on
* this element; false otherwise.
* <p>
* It is assumed that elements will not be placed at {@code (0,0)}.
* If you really need to position the element there, consider setting the
* coordinates to something like {@code (0.000001, 0.000001)} instead.
* this element, i.e. if locType is set to 'grid'; false otherwise.
*
* @return true if grid coordinates are set; false otherwise.
*/
public boolean gridCoordsSet() {
return !doubleIsZero(gridY()) || !doubleIsZero(gridX());
return Objects.equals(locType(), LOC_TYPE_GRID);
}
/**

View File

@ -117,7 +117,7 @@ public class BasicElementConfigTest {
@Test
public void someGridCoords() {
cfg.gridX(35.0).gridY(49.7);
cfg.gridX(35.0).gridY(49.7).locType(GRID);
print(cfg);
assertTrue("grid at origin?", cfg.gridCoordsSet());
assertEquals("gridx", 35.0, cfg.gridX(), ZERO_THRESHOLD);

View File

@ -61,15 +61,22 @@ function sim {
echo "$@" >> $CMDS
}
function y {
let p="${3:-300} * ($1 - 1) - (${3:-300} * ($2 - 1)) / 2"
echo $p
}
# Create spines
for spine in $(seq 1 $spines); do
sim "null-create-device switch Spine-${spine} ${spinePorts}"
sim "null-create-device switch Spine-${spine} ${spinePorts} 0 $(y $spine $spines) grid"
done
# Create 2 leaf pairs with dual links to the spines and a link between the pair
for pair in $serviceLeafGroups; do
sim "null-create-device switch Leaf-${pair}1 ${leafPorts}"
sim "null-create-device switch Leaf-${pair}2 ${leafPorts}"
[ $pair = A ] && l1=1 || l1=3
[ $pair = A ] && l2=2 || l2=4
sim "null-create-device switch Leaf-${pair}1 ${leafPorts} -200 $(y $l1 4) grid"
sim "null-create-device switch Leaf-${pair}2 ${leafPorts} -200 $(y $l2 4) grid"
sim "null-create-link direct Leaf-${pair}1 Leaf-${pair}2"
for spine in $(seq 1 $spines); do
@ -88,7 +95,7 @@ done
# Create single access leafs with dual links to the spines
for access in $(seq $accessLeaves); do
sim "null-create-device switch Access-${access} ${accessPorts}"
sim "null-create-device switch Access-${access} ${accessPorts} 200 $(y $access $accessLeaves) grid"
for spine in $(seq 1 $spines); do
for link in $(seq 1 $spineLinks); do

View File

@ -308,6 +308,7 @@ public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
payload.set("labels", labels("", name, device.id().toString()));
payload.set("props", props(device.annotations()));
addGeoLocation(device, payload);
addGridLocation(device, payload);
addMetaUi(device.id().toString(), payload);
String type = DEVICE_EVENT.get(event.type());
@ -354,6 +355,7 @@ public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
payload.set("labels", labels(nameForHost(host), ip, host.mac().toString(), ""));
payload.set("props", props(host.annotations()));
addGeoLocation(host, payload);
addGridLocation(host, payload);
addMetaUi(host.id().toString(), payload);
String type = HOST_EVENT.get(event.type());
@ -428,6 +430,30 @@ public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
}
}
// Adds a grid location JSON to the specified payload object.
private void addGridLocation(Annotated annotated, ObjectNode payload) {
Annotations annotations = annotated.annotations();
if (annotations == null) {
return;
}
String xs = annotations.value(AnnotationKeys.GRID_X);
String ys = annotations.value(AnnotationKeys.GRID_Y);
if (xs != null && ys != null) {
try {
double x = Double.parseDouble(xs);
double y = Double.parseDouble(ys);
ObjectNode loc = objectNode()
.put("locType", "grid")
.put("latOrY", y)
.put("longOrX", x);
payload.set("location", loc);
} catch (NumberFormatException e) {
log.warn("Invalid grid data: x={}, y={}", xs, ys);
}
}
}
// Updates meta UI information for the specified object.
protected void updateMetaUi(ObjectNode payload) {
metaUi.put(JsonUtils.string(payload, "id"),

View File

@ -55,6 +55,20 @@
return p ? p.invert(coord) : [0, 0];
}
function coordFromXY(loc) {
var bgWidth = 1000,
bgHeight = 1000;
var scale = 1000 / bgWidth,
yOffset = (1000 - (bgHeight * scale)) / 2;
// 1000 is a hardcoded HTML value of the SVG element (topo2.html)
var x = scale * loc.longOrX,
y = (scale * loc.latOrY) + yOffset;
return [x, y];
}
function positionNode(node, forUpdate) {
var meta = node.metaUi,
x = meta && meta.x,
@ -114,8 +128,8 @@
var loc = node.location,
coord;
if (loc && loc.locType === 'geo') {
coord = coordFromLngLat(loc);
if (loc) {
coord = loc.locType === 'geo' ? coordFromLngLat(loc) : coordFromXY(loc);
node.fixed = true;
node.px = node.x = coord[0];
node.py = node.y = coord[1];