use of org.btrplace.model.Attributes in project scheduler by btrplace.
the class ModelConverterTest method testConversion.
@Test
public void testConversion() throws JSONConverterException {
ModelConverter conv = new ModelConverter();
Model mo = new DefaultModel();
Mapping m = mo.getMapping();
Node n1 = mo.newNode();
VM vm1 = mo.newVM();
m.addOnlineNode(n1);
m.addReadyVM(vm1);
Attributes attrs = mo.getAttributes();
attrs.put(vm1, "boot", 5);
attrs.put(n1, "type", "xen");
ShareableResource rc = new ShareableResource("cpu");
rc.setConsumption(vm1, 5);
rc.setCapacity(n1, 10);
mo.attach(rc);
String jo = conv.toJSONString(mo);
System.out.println(jo);
Model res = conv.fromJSON(jo);
Assert.assertEquals(res, mo);
Assert.assertTrue(res.contains(n1));
Assert.assertTrue(res.contains(vm1));
}
use of org.btrplace.model.Attributes in project scheduler by btrplace.
the class ModelCustomization method run.
@Override
public void run() {
Model model = makeModel();
List<SatConstraint> cstrs = makeConstraints(model);
// Set attributes for the VMs
Attributes attrs = model.getAttributes();
for (VM vm : model.getMapping().getAllVMs()) {
attrs.put(vm, "template", vm.id() % 2 == 0 ? "small" : "large");
attrs.put(vm, "clone", true);
attrs.put(vm, "forge", vm.id() % 2 == 0 ? 2 : 10);
// forge == 2 && template == small for vm0, vm2, vm4, vm6, vm8
// forge == 10 && template == large for vm1, vm3, vm5, vm7, vm9
}
// Change the duration evaluator for MigrateVM action
ChocoScheduler cra = new DefaultChocoScheduler();
DurationEvaluators dev = cra.getDurationEvaluators();
dev.register(MigrateVM.class, new LinearToAResourceActionDuration<VM>("mem", 2, 3));
dev.register(BootVM.class, new ConstantActionDuration<>(1));
dev.register(ShutdownVM.class, new ConstantActionDuration<>(1));
// Relocate VM4:
// using a migration: (2 * mem + 3) = (2 * 2 + 3) = 7 sec.
// using a re-instantiation: forge + boot + shutdown = 2 + 1 + 1 = 4 sec.
// Relocate VM5:
// using a migration: (2 * mem + 3) = (2 * 3 + 3) = 9 sec.
// using a re-instantiation: forge + boot + shutdown = 10 + 1 + 1 = 12 sec.
cra.doOptimize(true);
ReconfigurationPlan plan = cra.solve(model, cstrs);
System.out.println(plan);
}
use of org.btrplace.model.Attributes 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.Attributes in project scheduler by btrplace.
the class DurationFromOptionalAttributeTest method test.
@Test
public void test() {
Model mo = new DefaultModel();
Attributes attrs = mo.getAttributes();
ActionDurationEvaluator<VM> parent = new ConstantActionDuration<>(15);
VM vm1 = mo.newVM();
ActionDurationFromOptionalAttribute<VM> dev = new ActionDurationFromOptionalAttribute<>("boot", parent);
Assert.assertEquals(parent, dev.getParent());
Assert.assertEquals("boot", dev.getAttributeKey());
Assert.assertEquals(15, dev.evaluate(mo, vm1));
attrs.put(vm1, "boot", 7);
Assert.assertEquals(7, dev.evaluate(mo, vm1));
parent = new ConstantActionDuration<>(2);
dev.setParent(parent);
attrs.clear();
Assert.assertEquals(2, dev.evaluate(mo, vm1));
Assert.assertEquals(parent, dev.getParent());
Assert.assertFalse(dev.toString().contains("null"));
}
use of org.btrplace.model.Attributes 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