Search in sources :

Example 1 with Element

use of org.btrplace.model.Element in project scheduler by btrplace.

the class EnumElement method go.

@Override
public BtrpOperand go(BtrPlaceTree parent) {
    String head = getChild(0).getText().substring(0, getChild(0).getText().length() - 1);
    String tail = getChild(getChildCount() - 1).getText().substring(1);
    BtrpSet res;
    switch(type) {
        case NODE:
            res = new BtrpSet(1, BtrpOperand.Type.NODE);
            break;
        case VM:
            res = new BtrpSet(1, BtrpOperand.Type.VM);
            break;
        default:
            return ignoreError("Unsupported enumeration type: '" + type + "'");
    }
    for (int i = 1; i < getChildCount() - 1; i++) {
        BtrpOperand op = getChild(i).go(this);
        if (op == IgnorableOperand.getInstance()) {
            return op;
        }
        BtrpSet s = (BtrpSet) op;
        for (BtrpOperand o : s.getValues()) {
            if (o == IgnorableOperand.getInstance()) {
                return o;
            }
            // Compose
            String id = head + o.toString() + tail;
            if (type == BtrpOperand.Type.NODE) {
                // TODO: 'id' does not contains "@" in the scheduler NamingService
                Element el = namingServiceNodes.resolve(id);
                if (el == null) {
                    // Should be fair as each getChild(i) is a range with at least on child. Prevent from a fake token
                    // with no line number
                    Token t = getChild(i).getChild(0).getToken();
                    if (t.getCharPositionInLine() == -1) {
                        t = parent.getToken();
                    }
                    return ignoreError(t, "Unknown node '" + id.substring(1) + "'");
                }
                res.getValues().add(new BtrpElement(BtrpOperand.Type.NODE, id, namingServiceNodes.resolve(id)));
            } else if (type == BtrpOperand.Type.VM) {
                String fqn = script.id() + '.' + id;
                Element el = namingServiceVMs.resolve(fqn);
                Token t = getChild(i).getChild(0).getToken();
                if (el == null) {
                    return ignoreError(t, "Unknown VM '" + id + "'");
                }
                res.getValues().add(new BtrpElement(BtrpOperand.Type.VM, fqn, namingServiceVMs.resolve(fqn)));
            } else {
                return ignoreError("Unsupported type '" + type + "' in enumeration");
            }
        }
    }
    return res;
}
Also used : Element(org.btrplace.model.Element) Token(org.antlr.runtime.Token)

Example 2 with Element

use of org.btrplace.model.Element 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;
}
Also used : JSONObject(net.minidev.json.JSONObject) NamingService(org.btrplace.model.view.NamingService) Element(org.btrplace.model.Element) JSONObject(net.minidev.json.JSONObject) JSONs.requiredString(org.btrplace.json.JSONs.requiredString) Map(java.util.Map) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 3 with Element

use of org.btrplace.model.Element in project scheduler by btrplace.

the class NamingServiceConverter method toJSON.

@Override
public JSONObject toJSON(NamingService rc) {
    JSONObject container = new JSONObject();
    container.put(ModelViewConverter.IDENTIFIER, getJSONId());
    container.put("type", rc.getElementIdentifier());
    JSONObject map = new JSONObject();
    for (Object o : rc.getNamedElements()) {
        Element e = (Element) o;
        map.put(rc.resolve(e), e.id());
    }
    container.put("map", map);
    return container;
}
Also used : JSONObject(net.minidev.json.JSONObject) Element(org.btrplace.model.Element) JSONObject(net.minidev.json.JSONObject)

Example 4 with Element

use of org.btrplace.model.Element 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 Element

use of org.btrplace.model.Element 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

Element (org.btrplace.model.Element)6 BtrpElement (org.btrplace.btrpsl.element.BtrpElement)3 JSONObject (net.minidev.json.JSONObject)2 ElementBuilderException (org.btrplace.btrpsl.template.ElementBuilderException)2 Map (java.util.Map)1 Token (org.antlr.runtime.Token)1 JSONConverterException (org.btrplace.json.JSONConverterException)1 JSONs.requiredString (org.btrplace.json.JSONs.requiredString)1 Node (org.btrplace.model.Node)1 VM (org.btrplace.model.VM)1 NamingService (org.btrplace.model.view.NamingService)1