use of org.btrplace.json.JSONConverterException in project scheduler by btrplace.
the class Replay method get.
@Override
public TestCase get() {
try {
String json = in.readLine();
if (json == null) {
return null;
}
TestCase tc = TestCase.fromJSON(constraints, json);
if (restriction.size() == 1) {
if (restriction.contains(Restriction.CONTINUOUS) && !tc.impl().setContinuous(true)) {
throw new IllegalArgumentException("Cannot be CONTINUOUS");
} else if (!tc.impl().setContinuous(false)) {
throw new IllegalArgumentException("Cannot be DISCRETE");
}
}
return tc;
} catch (IOException | ParseException | JSONConverterException e) {
throw new IllegalArgumentException(e);
}
}
use of org.btrplace.json.JSONConverterException in project scheduler by btrplace.
the class ModelViewsConverter method fromJSON.
/**
* Convert a json-encoded view.
*
* @param mo the model to rely on
* @param in the view to decode
* @return the resulting view
* @throws JSONConverterException if the conversion failed
*/
public ModelView fromJSON(Model mo, JSONObject in) throws JSONConverterException {
checkKeys(in, ModelViewConverter.IDENTIFIER);
Object id = in.get(ModelViewConverter.IDENTIFIER);
ModelViewConverter<? extends ModelView> c = json2java.get(id.toString());
if (c == null) {
throw new JSONConverterException("No converter available for a view having id '" + id + "'");
}
return c.fromJSON(mo, in);
}
use of org.btrplace.json.JSONConverterException 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.json.JSONConverterException in project scheduler by btrplace.
the class ShareableResourceConverter method parseNodes.
private static void parseNodes(Model mo, ShareableResource rc, Object o) throws JSONConverterException {
if (o != null) {
try {
JSONObject values = (JSONObject) o;
for (Map.Entry<String, Object> e : values.entrySet()) {
String k = e.getKey();
Node u = getNode(mo, Integer.parseInt(k));
int v = Integer.parseInt(e.getValue().toString());
rc.setCapacity(u, v);
}
} catch (ClassCastException cc) {
throw new JSONConverterException("Unable to read the nodes at key '" + NODES_LABEL + "'. Expect a JSONObject but got a '" + o.getClass().getName() + "'", cc);
}
}
}
use of org.btrplace.json.JSONConverterException in project scheduler by btrplace.
the class ModelConverter method newNodes.
/**
* Build nodes from a key.
*
* @param mo the model to build
* @param o the object that contains the node
* @param key the key associated to the nodes
* @return the resulting set of nodes
* @throws JSONConverterException if at least one of the parsed node already exists
*/
private static Set<Node> newNodes(Model mo, JSONObject o, String key) throws JSONConverterException {
checkKeys(o, key);
Object x = o.get(key);
if (!(x instanceof JSONArray)) {
throw new JSONConverterException("array expected at key '" + key + "'");
}
Set<Node> s = new HashSet<>(((JSONArray) x).size());
for (Object i : (JSONArray) x) {
int id = (Integer) i;
Node n = mo.newNode(id);
if (n == null) {
throw JSONConverterException.nodeAlreadyDeclared(id);
}
s.add(n);
}
return s;
}
Aggregations