Search in sources :

Example 11 with JSONConverterException

use of org.btrplace.json.JSONConverterException in project scheduler by btrplace.

the class ModelConverter method newVMs.

/**
 * Build VMs from a key.
 *
 * @param mo  the model to build
 * @param o   the object that contains the vm
 * @param key the key associated to the VMs
 * @return the resulting set of VMs
 * @throws JSONConverterException if at least one of the parsed VM already exists
 */
private static Set<VM> newVMs(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<VM> s = new HashSet<>(((JSONArray) x).size());
    for (Object i : (JSONArray) x) {
        int id = (Integer) i;
        VM vm = mo.newVM(id);
        if (vm == null) {
            throw JSONConverterException.vmAlreadyDeclared(id);
        }
        s.add(vm);
    }
    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