use of org.btrplace.model.DefaultAttributes in project scheduler by btrplace.
the class AttributesConverter method fromJSON.
/**
* Decode attributes
*
* @param mo the model to rely on
* @param o the encoded attributes
* @return the resulting attributes
* @throws JSONConverterException if the conversion failed
*/
public static Attributes fromJSON(Model mo, JSONObject o) throws JSONConverterException {
Attributes attrs = new DefaultAttributes();
try {
JSONObject vms = (JSONObject) o.get("vms");
if (vms != null) {
for (Map.Entry<String, Object> e : vms.entrySet()) {
String el = e.getKey();
VM vm = getVM(mo, Integer.parseInt(el));
JSONObject entries = (JSONObject) e.getValue();
putAttributes(attrs, vm, entries);
}
}
JSONObject nodes = (JSONObject) o.get("nodes");
if (nodes != null) {
for (Map.Entry<String, Object> e : nodes.entrySet()) {
String el = e.getKey();
Node n = getNode(mo, Integer.parseInt(el));
JSONObject entries = (JSONObject) e.getValue();
putAttributes(attrs, n, entries);
}
}
} catch (ClassCastException ex) {
throw new JSONConverterException(ex);
}
return attrs;
}
use of org.btrplace.model.DefaultAttributes in project scheduler by btrplace.
the class AttributesConverterTest method testSimple.
@Test
public void testSimple() throws JSONConverterException {
Model mo = new DefaultModel();
Attributes attrs = new DefaultAttributes();
VM vm1 = mo.newVM();
VM vm3 = mo.newVM(3);
Node n1 = mo.newNode();
attrs.put(n1, "boot", 7);
attrs.put(vm1, "template", "xen");
attrs.put(vm1, "forge", 3);
attrs.put(vm3, "template", "kvm");
attrs.put(vm3, "clone", true);
attrs.put(vm3, "foo", 1.3);
JSONObject o = AttributesConverter.toJSON(attrs);
System.out.println(o);
Attributes attrs2 = AttributesConverter.fromJSON(mo, o);
Assert.assertTrue(attrs.equals(attrs2));
}
Aggregations