Search in sources :

Example 1 with JSONConverterException

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

the class ReconfigurationPlanConverter method fromJSON.

@Override
public ReconfigurationPlan fromJSON(JSONObject ob) throws JSONConverterException {
    JSONs.checkKeys(ob, ORIGIN_LABEL, ACTIONS_LABEL);
    final Model m = mc.fromJSON((JSONObject) ob.get(ORIGIN_LABEL));
    final ReconfigurationPlan plan = new DefaultReconfigurationPlan(m);
    for (final JSONObject json : (List<JSONObject>) ob.get(ACTIONS_LABEL)) {
        final String id = json.getAsString(ActionConverter.ID_LABEL);
        ActionConverter<? extends Action> ac = json3java.get(id);
        if (ac == null) {
            throw new JSONConverterException("No converter for action '" + id + "'");
        }
        final Action action = ac.fromJSON(m, json);
        eventsFromJSON(json, m, action);
        plan.add(action);
    }
    return plan;
}
Also used : DefaultReconfigurationPlan(org.btrplace.plan.DefaultReconfigurationPlan) Action(org.btrplace.plan.event.Action) JSONObject(net.minidev.json.JSONObject) DefaultReconfigurationPlan(org.btrplace.plan.DefaultReconfigurationPlan) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) Model(org.btrplace.model.Model) List(java.util.List) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 2 with JSONConverterException

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

the class ReconfigurationPlanConverter method eventFromJSON.

private Event eventFromJSON(final Model mo, final Object o) throws JSONConverterException {
    final JSONObject json = (JSONObject) o;
    JSONs.checkKeys(json, ActionConverter.ID_LABEL);
    final String id = json.get(ActionConverter.ID_LABEL).toString();
    final EventConverter<? extends Event> ec = json2java.get(id);
    if (ec == null) {
        throw new JSONConverterException("No converter available for a event having id '" + id + "'");
    }
    return ec.fromJSON(mo, json);
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 3 with JSONConverterException

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

the class AttributesConverter method fromJSON.

/**
 * Decode attributes
 *
 * @param mo the model to rely on
 * @param o  the encoded attributes
 * @return the resulting attributes
 * @throws JSONConverterException if the conversion failed
 */
public static Attributes fromJSON(Model mo, JSONObject o) throws JSONConverterException {
    Attributes attrs = new DefaultAttributes();
    try {
        JSONObject vms = (JSONObject) o.get("vms");
        if (vms != null) {
            for (Map.Entry<String, Object> e : vms.entrySet()) {
                String el = e.getKey();
                VM vm = getVM(mo, Integer.parseInt(el));
                JSONObject entries = (JSONObject) e.getValue();
                putAttributes(attrs, vm, entries);
            }
        }
        JSONObject nodes = (JSONObject) o.get("nodes");
        if (nodes != null) {
            for (Map.Entry<String, Object> e : nodes.entrySet()) {
                String el = e.getKey();
                Node n = getNode(mo, Integer.parseInt(el));
                JSONObject entries = (JSONObject) e.getValue();
                putAttributes(attrs, n, entries);
            }
        }
    } catch (ClassCastException ex) {
        throw new JSONConverterException(ex);
    }
    return attrs;
}
Also used : JSONObject(net.minidev.json.JSONObject) VM(org.btrplace.model.VM) JSONs.getVM(org.btrplace.json.JSONs.getVM) Node(org.btrplace.model.Node) JSONs.getNode(org.btrplace.json.JSONs.getNode) Attributes(org.btrplace.model.Attributes) DefaultAttributes(org.btrplace.model.DefaultAttributes) JSONObject(net.minidev.json.JSONObject) DefaultAttributes(org.btrplace.model.DefaultAttributes) Map(java.util.Map) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 4 with JSONConverterException

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

the class ConstraintsConverter method fromJSON.

/**
 * Convert a json-encoded constraint.
 *
 * @param mo the model to rely on
 * @param in the constraint to decode
 * @return the resulting constraint
 * @throws JSONConverterException if the conversion failed
 */
public Constraint fromJSON(Model mo, JSONObject in) throws JSONConverterException {
    checkKeys(in, "id");
    Object id = in.get("id");
    ConstraintConverter<? extends Constraint> c = json2java.get(id.toString());
    if (c == null) {
        throw new JSONConverterException("No converter available for a constraint having id '" + id + "'");
    }
    return c.fromJSON(mo, in);
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 5 with JSONConverterException

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

the class NetworkConverter method routingFromJSON.

/**
 * Convert a JSON routing object into the corresponding java Routing implementation.
 *
 * @param mo the model we focus on
 * @param o the JSON object to convert
 * @return the resulting {@link Routing} object
 * @throws JSONConverterException if the Routing implementation is not known
 */
public Routing routingFromJSON(Model mo, JSONObject o) throws JSONConverterException {
    String type = requiredString(o, "type");
    RoutingConverter<? extends Routing> c = json2java.get(type);
    if (c == null) {
        throw new JSONConverterException("No converter available for a routing of type '" + type + "'");
    }
    return c.fromJSON(mo, o);
}
Also used : JSONs.requiredString(org.btrplace.json.JSONs.requiredString) JSONConverterException(org.btrplace.json.JSONConverterException)

Aggregations

JSONConverterException (org.btrplace.json.JSONConverterException)15 JSONObject (net.minidev.json.JSONObject)13 JSONArray (net.minidev.json.JSONArray)5 Map (java.util.Map)4 Node (org.btrplace.model.Node)3 VM (org.btrplace.model.VM)3 HashSet (java.util.HashSet)2 List (java.util.List)2 ParseException (net.minidev.json.parser.ParseException)2 JSONs.requiredString (org.btrplace.json.JSONs.requiredString)2 Model (org.btrplace.model.Model)2 ReconfigurationPlan (org.btrplace.plan.ReconfigurationPlan)2 Action (org.btrplace.plan.event.Action)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1