Search in sources :

Example 1 with DefaultAttributes

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;
}
Also used : JSONObject(net.minidev.json.JSONObject) VM(org.btrplace.model.VM) JSONs.getVM(org.btrplace.json.JSONs.getVM) Node(org.btrplace.model.Node) JSONs.getNode(org.btrplace.json.JSONs.getNode) Attributes(org.btrplace.model.Attributes) DefaultAttributes(org.btrplace.model.DefaultAttributes) JSONObject(net.minidev.json.JSONObject) DefaultAttributes(org.btrplace.model.DefaultAttributes) Map(java.util.Map) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 2 with DefaultAttributes

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));
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) JSONObject(net.minidev.json.JSONObject) VM(org.btrplace.model.VM) Node(org.btrplace.model.Node) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Attributes(org.btrplace.model.Attributes) DefaultAttributes(org.btrplace.model.DefaultAttributes) DefaultAttributes(org.btrplace.model.DefaultAttributes) Test(org.testng.annotations.Test)

Aggregations

JSONObject (net.minidev.json.JSONObject)2 Attributes (org.btrplace.model.Attributes)2 DefaultAttributes (org.btrplace.model.DefaultAttributes)2 Node (org.btrplace.model.Node)2 VM (org.btrplace.model.VM)2 Map (java.util.Map)1 JSONConverterException (org.btrplace.json.JSONConverterException)1 JSONs.getNode (org.btrplace.json.JSONs.getNode)1 JSONs.getVM (org.btrplace.json.JSONs.getVM)1 DefaultModel (org.btrplace.model.DefaultModel)1 Model (org.btrplace.model.Model)1 Test (org.testng.annotations.Test)1