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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations