use of org.btrplace.model.view.NamingService in project scheduler by btrplace.
the class NamingServiceConverterTest method test.
@Test
public void test() throws JSONConverterException {
NamingService<VM> ns = NamingService.newVMNS();
Model mo = new DefaultModel();
for (int i = 0; i < 10; i++) {
VM v = mo.newVM();
ns.register(v, "VM " + i);
}
NamingServiceConverter nsc = new NamingServiceConverter();
JSONObject o = nsc.toJSON(ns);
System.out.println(o);
@SuppressWarnings("unchecked") NamingService<VM> ns2 = (NamingService<VM>) nsc.fromJSON(mo, o);
Assert.assertEquals(ns, ns2);
}
use of org.btrplace.model.view.NamingService in project scheduler by btrplace.
the class NamingServiceConverter method fromJSON.
@Override
public NamingService<? extends Element> fromJSON(Model mo, JSONObject o) throws JSONConverterException {
String id = requiredString(o, ModelViewConverter.IDENTIFIER);
if (!id.equals(getJSONId())) {
return null;
}
NamingService ns;
String type = requiredString(o, "type");
switch(type) {
case VM.TYPE:
ns = NamingService.newVMNS();
break;
case Node.TYPE:
ns = NamingService.newNodeNS();
break;
default:
throw new JSONConverterException("Unsupported type of element '" + type + "'");
}
checkKeys(o, "map");
JSONObject map = (JSONObject) o.get("map");
for (Map.Entry<String, Object> e : map.entrySet()) {
String n = e.getKey();
int v = Integer.parseInt(e.getValue().toString());
Element el = VM.TYPE.equals(type) ? getVM(mo, v) : getNode(mo, v);
if (!ns.register(el, n)) {
throw new JSONConverterException("Duplicated name '" + n + "'");
}
}
return ns;
}
Aggregations