Search in sources :

Example 1 with BtrpElement

use of org.btrplace.btrpsl.element.BtrpElement in project scheduler by btrplace.

the class ScriptTest method testNodeAddition.

public void testNodeAddition() {
    Script v = new Script();
    Model mo = new DefaultModel();
    BtrpElement n1 = new BtrpElement(BtrpOperand.Type.NODE, "@N1", mo.newNode());
    BtrpElement n2 = new BtrpElement(BtrpOperand.Type.NODE, "@N2", mo.newNode());
    v.add(n1);
    v.add(n2);
    Assert.assertEquals(v.getNodes().size(), 2);
    Assert.assertTrue(v.getNodes().contains(n1.getElement()) && v.getNodes().contains(n2.getElement()));
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) BtrpElement(org.btrplace.btrpsl.element.BtrpElement)

Example 2 with BtrpElement

use of org.btrplace.btrpsl.element.BtrpElement in project scheduler by btrplace.

the class ScriptTest method testVMsAddition.

public void testVMsAddition() {
    Script v = new Script();
    Model mo = new DefaultModel();
    BtrpElement vm1 = new BtrpElement(BtrpOperand.Type.VM, "VM1", mo.newVM());
    BtrpElement vm2 = new BtrpElement(BtrpOperand.Type.VM, "VM2", mo.newVM());
    BtrpElement vm3 = new BtrpElement(BtrpOperand.Type.VM, "VM3", mo.newVM());
    v.add(vm1);
    v.add(Arrays.asList(vm2, vm3));
    Assert.assertEquals(v.getVMs().size(), 3);
    Assert.assertTrue(v.getVMs().contains(vm1.getElement()) && v.getVMs().contains(vm2.getElement()) && v.getVMs().contains(vm3.getElement()));
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) BtrpElement(org.btrplace.btrpsl.element.BtrpElement)

Example 3 with BtrpElement

use of org.btrplace.btrpsl.element.BtrpElement in project scheduler by btrplace.

the class ScriptTest method testExported.

public void testExported() {
    Script v = new Script();
    v.setFullyQualifiedName("testScript");
    Model mo = new DefaultModel();
    BtrpOperand o1 = new BtrpElement(BtrpOperand.Type.VM, "vm1", mo.newVM());
    v.addExportable("$s", o1, Collections.singleton("*"));
    // No restrictions, so every can access the exported variable
    Assert.assertEquals(v.getImportable("$s"), o1);
    Assert.assertTrue(v.canImport("$s", "foo"));
    Assert.assertTrue(v.canImport("$s", "bar.toto"));
    // Explicit no restriction
    BtrpOperand o2 = new BtrpElement(BtrpOperand.Type.VM, "vm2", mo.newVM());
    v.addExportable("$x", o2, Collections.singleton("*"));
    Assert.assertEquals(v.getImportable("$x"), o2);
    Assert.assertTrue(v.canImport("$x", "foo"));
    Assert.assertTrue(v.canImport("$x", "bar.toto"));
    // Unknown export
    Assert.assertFalse(v.canImport("$z", "foo"));
    Assert.assertFalse(v.canImport("$z", "bar.toto"));
    Set<String> valid = new HashSet<>();
    valid.add("foo");
    valid.add("foo.*");
    valid.add("bar");
    BtrpOperand o3 = new BtrpElement(BtrpOperand.Type.VM, "vm2", mo.newVM());
    v.addExportable("$y", o3, valid);
    Assert.assertNull(v.getImportable("$y"));
    Assert.assertEquals(v.getImportable("$y", "foo"), o3);
    Assert.assertNull(v.getImportable("$y", "zog"));
    Assert.assertEquals(v.getImportable("$y", "foo.bar.fii"), o3);
    Assert.assertEquals(v.getImportable("$y", "bar"), o3);
    Assert.assertEquals(v.getExported().size(), 3);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) BtrpOperand(org.btrplace.btrpsl.element.BtrpOperand) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) BtrpElement(org.btrplace.btrpsl.element.BtrpElement) HashSet(java.util.HashSet)

Example 4 with BtrpElement

use of org.btrplace.btrpsl.element.BtrpElement in project scheduler by btrplace.

the class ElementTree method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    String lbl = getText();
    Element el;
    BtrpElement btrpEl;
    switch(token.getType()) {
        case ANTLRBtrplaceSL2Parser.NODE_NAME:
            String ref = lbl.substring(1, lbl.length());
            el = namingServiceNodes.resolve(lbl);
            if (el == null) {
                return ignoreError("Unknown node '" + ref + "'");
            }
            btrpEl = new BtrpElement(BtrpOperand.Type.NODE, lbl, el);
            break;
        case ANTLRBtrplaceSL2Parser.IDENTIFIER:
            /**
             * Switch to Fully Qualified name before getting the VM
             */
            String fqn = script.id() + '.' + lbl;
            el = namingServiceVMs.resolve(fqn);
            if (el == null) {
                return ignoreError("Unknown VM '" + lbl + "'");
            }
            btrpEl = new BtrpElement(BtrpOperand.Type.VM, fqn, el);
            break;
        default:
            return ignoreError("Unexpected type: " + ANTLRBtrplaceSL2Parser.tokenNames[token.getType()]);
    }
    return btrpEl;
}
Also used : Element(org.btrplace.model.Element) BtrpElement(org.btrplace.btrpsl.element.BtrpElement) BtrpElement(org.btrplace.btrpsl.element.BtrpElement)

Example 5 with BtrpElement

use of org.btrplace.btrpsl.element.BtrpElement in project scheduler by btrplace.

the class TemplateAssignment method addNode.

private void addNode(String tplName, String id, Map<String, String> opts) {
    try {
        Element el = namingServiceNodes.resolve(id);
        if (el == null) {
            Node n = mo.newNode();
            mo.getMapping().addOfflineNode(n);
            if (n == null) {
                ignoreError("No UUID to create node '" + id + "'");
            } else {
                namingServiceNodes.register(n, id);
                el = n;
            }
        }
        tpls.check(script, tplName, el, opts);
        if (!script.add(new BtrpElement(BtrpOperand.Type.NODE, id, el))) {
            ignoreError("Node '" + id + "' already created");
        }
    } catch (ElementBuilderException ex) {
        ignoreError(ex);
    }
}
Also used : ElementBuilderException(org.btrplace.btrpsl.template.ElementBuilderException) Element(org.btrplace.model.Element) BtrpElement(org.btrplace.btrpsl.element.BtrpElement) Node(org.btrplace.model.Node) BtrpElement(org.btrplace.btrpsl.element.BtrpElement)

Aggregations

BtrpElement (org.btrplace.btrpsl.element.BtrpElement)6 DefaultModel (org.btrplace.model.DefaultModel)3 Element (org.btrplace.model.Element)3 Model (org.btrplace.model.Model)3 ElementBuilderException (org.btrplace.btrpsl.template.ElementBuilderException)2 HashSet (java.util.HashSet)1 BtrpOperand (org.btrplace.btrpsl.element.BtrpOperand)1 Node (org.btrplace.model.Node)1 VM (org.btrplace.model.VM)1