use of org.btrplace.model.view.network.StaticRouting in project scheduler by btrplace.
the class NetworkTest method staticRoutingTest.
/**
* Test the static routing implementation.
*/
@Test
public void staticRoutingTest() {
Model mo = new DefaultModel();
Network net = new Network(new StaticRouting());
Switch s = net.newSwitch(1000);
Node n1 = mo.newNode();
Node n2 = mo.newNode();
net.connect(2000, s, n1, n2);
Map<Link, Boolean> route = new LinkedHashMap<>();
route.put(net.getConnectedLinks(n1).get(0), true);
route.put(net.getConnectedLinks(n2).get(0), false);
((StaticRouting) net.getRouting()).setStaticRoute(new StaticRouting.NodesMap(n1, n2), route);
mo.attach(net);
Assert.assertTrue(net.getRouting().getPath(n1, n2).size() == 2);
Assert.assertTrue(net.getRouting().getPath(n1, n2).containsAll(net.getLinks()));
}
use of org.btrplace.model.view.network.StaticRouting in project scheduler by btrplace.
the class StaticRoutingConverter method toJSON.
/**
* Convert a Routing implementation into a JSON object
*
* @param routing the routing implementation to convert
* @return the JSON formatted routing object
*/
@Override
public JSONObject toJSON(StaticRouting routing) {
JSONObject o = new JSONObject();
o.put("type", getJSONId());
JSONArray a = new JSONArray();
Map<StaticRouting.NodesMap, Map<Link, Boolean>> routes = routing.getStaticRoutes();
for (Map.Entry<StaticRouting.NodesMap, Map<Link, Boolean>> e : routes.entrySet()) {
StaticRouting.NodesMap nm = e.getKey();
JSONObject ao = new JSONObject();
ao.put("nodes_map", nodesMapToJSON(nm));
JSONArray links = new JSONArray();
Map<Link, Boolean> v = e.getValue();
for (Link l : v.keySet()) {
JSONObject lo = new JSONObject();
lo.put("link", l.id());
lo.put("direction", routes.get(nm).get(l).toString());
links.add(lo);
}
ao.put("links", links);
a.add(ao);
}
o.put(ROUTES_LABEL, a);
return o;
}
use of org.btrplace.model.view.network.StaticRouting in project scheduler by btrplace.
the class StaticRoutingConverter method fromJSON.
@Override
public StaticRouting fromJSON(Model mo, JSONObject o) throws JSONConverterException {
Network v = Network.get(mo);
TIntObjectMap<Link> idToLink = new TIntObjectHashMap<>();
for (Link l : v.getLinks()) {
idToLink.put(l.id(), l);
}
StaticRouting r = new StaticRouting();
checkKeys(o, ROUTES_LABEL);
JSONArray a = (JSONArray) o.get(ROUTES_LABEL);
for (Object ao : a) {
StaticRouting.NodesMap nm = nodesMapFromJSON(mo, (JSONObject) ((JSONObject) ao).get("nodes_map"));
Map<Link, Boolean> links = new LinkedHashMap<>();
JSONArray aoa = (JSONArray) ((JSONObject) ao).get("links");
for (Object aoao : aoa) {
links.put(idToLink.get(requiredInt((JSONObject) aoao, "link")), Boolean.valueOf(requiredString((JSONObject) aoao, "direction")));
}
r.setStaticRoute(nm, links);
}
return r;
}
use of org.btrplace.model.view.network.StaticRouting in project scheduler by btrplace.
the class NetworkConverterTest method staticRoutingTest.
@Test
public void staticRoutingTest() throws JSONConverterException {
Model mo = new DefaultModel();
Network net = new Network(new StaticRouting());
Switch s = net.newSwitch(1000);
Node n1 = mo.newNode();
Node n2 = mo.newNode();
mo.getMapping().addOnlineNode(n1);
mo.getMapping().addOnlineNode(n2);
net.connect(1000, s, n1, n2);
Map<Link, Boolean> route = new LinkedHashMap<>();
route.put(net.getConnectedLinks(n1).get(0), true);
route.put(net.getConnectedLinks(n2).get(0), false);
((StaticRouting) net.getRouting()).setStaticRoute(new StaticRouting.NodesMap(n1, n2), route);
mo.attach(net);
ModelConverter mc = new ModelConverter();
JSONObject jo = mc.toJSON(mo);
System.err.println(jo);
Model mo2 = mc.fromJSON(jo);
Network net2 = Network.get(mo2);
Assert.assertTrue(net.getSwitches().equals(net2.getSwitches()));
Assert.assertTrue(net.getLinks().equals(net2.getLinks()));
Assert.assertTrue(net.getConnectedNodes().equals(net2.getConnectedNodes()));
Map<StaticRouting.NodesMap, Map<Link, Boolean>> routes = ((StaticRouting) net.getRouting()).getStaticRoutes();
Map<StaticRouting.NodesMap, Map<Link, Boolean>> routes2 = ((StaticRouting) net2.getRouting()).getStaticRoutes();
for (StaticRouting.NodesMap nm : routes.keySet()) {
for (StaticRouting.NodesMap nm2 : routes2.keySet()) {
Assert.assertTrue(nm.equals(nm2));
Assert.assertTrue(routes.get(nm).equals(routes2.get(nm2)));
}
}
}
Aggregations