Search in sources :

Example 6 with JSONConverterException

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);
    }
}
Also used : TestCase(org.btrplace.safeplace.testing.TestCase) IOException(java.io.IOException) ParseException(net.minidev.json.parser.ParseException) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 7 with JSONConverterException

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);
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 8 with JSONConverterException

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;
}
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 9 with JSONConverterException

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

Example 10 with JSONConverterException

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;
}
Also used : JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) JSONConverterException(org.btrplace.json.JSONConverterException) HashSet(java.util.HashSet)

Aggregations

JSONConverterException (org.btrplace.json.JSONConverterException)11 JSONObject (net.minidev.json.JSONObject)9 Map (java.util.Map)4 JSONs.requiredString (org.btrplace.json.JSONs.requiredString)4 JSONArray (net.minidev.json.JSONArray)3 HashSet (java.util.HashSet)2 ParseException (net.minidev.json.parser.ParseException)2 JSONs.getNode (org.btrplace.json.JSONs.getNode)2 JSONs.getVM (org.btrplace.json.JSONs.getVM)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 JSONParser (net.minidev.json.parser.JSONParser)1 InstanceConverter (org.btrplace.json.model.InstanceConverter)1 ReconfigurationPlanConverter (org.btrplace.json.plan.ReconfigurationPlanConverter)1