Search in sources :

Example 56 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class AmongConverter method toJSON.

@Override
public JSONObject toJSON(Among o) {
    JSONObject c = new JSONObject();
    c.put("id", getJSONId());
    c.put("vms", vmsToJSON(o.getInvolvedVMs()));
    JSONArray a = new JSONArray();
    for (Collection<Node> grp : o.getGroupsOfNodes()) {
        a.add(nodesToJSON(grp));
    }
    c.put("parts", a);
    c.put("continuous", o.isContinuous());
    return c;
}
Also used : JSONObject(net.minidev.json.JSONObject) Node(org.btrplace.model.Node) JSONArray(net.minidev.json.JSONArray)

Example 57 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class TestCase method fromJSON.

public static TestCase fromJSON(List<Constraint> cstrs, String c) throws ParseException, JSONConverterException {
    JSONParser p = new JSONParser(JSONParser.MODE_RFC4627);
    JSONObject o = (JSONObject) p.parse(new StringReader(c));
    String cId = o.getAsString("constraint");
    Optional<Constraint> opt = cstrs.stream().filter(x -> x.id().equals(cId)).findFirst();
    if (!opt.isPresent()) {
        throw new IllegalArgumentException("Unknown constraint '" + cId + "'");
    }
    Constraint cstr = opt.get();
    InstanceConverter ic = new InstanceConverter();
    ic.getConstraintsConverter().register(new ScheduleConverter());
    ReconfigurationPlanConverter rc = new ReconfigurationPlanConverter();
    Instance i = ic.fromJSON(o.getAsString("instance"));
    ReconfigurationPlan plan = rc.fromJSON(o.getAsString("plan"));
    TestCase tc = new TestCase(i, plan, cstr);
    List<Constant> l = new ArrayList<>();
    for (Object x : (JSONArray) o.get("args")) {
        l.add(Constant.fromJSON((JSONObject) x));
    }
    tc.args(l);
    if (cstr.isSatConstraint()) {
        tc.impl(cstr.instantiate(l.stream().map(x -> x.eval(null)).collect(Collectors.toList())));
    }
    if (tc.impl() != null) {
        tc.impl().setContinuous((Boolean) o.get("continuous"));
    }
    return tc;
}
Also used : Constant(org.btrplace.safeplace.spec.term.Constant) ModelView(org.btrplace.model.view.ModelView) JSONConverterException(org.btrplace.json.JSONConverterException) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) Collectors(java.util.stream.Collectors) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONParser(net.minidev.json.parser.JSONParser) Objects(java.util.Objects) Constraint(org.btrplace.safeplace.spec.Constraint) List(java.util.List) StringReader(java.io.StringReader) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) ParseException(net.minidev.json.parser.ParseException) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Optional(java.util.Optional) Instance(org.btrplace.model.Instance) Collections(java.util.Collections) SatConstraint(org.btrplace.model.constraint.SatConstraint) ScheduleConverter(org.btrplace.safeplace.testing.verification.btrplace.ScheduleConverter) Constraint(org.btrplace.safeplace.spec.Constraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) Instance(org.btrplace.model.Instance) Constant(org.btrplace.safeplace.spec.term.Constant) InstanceConverter(org.btrplace.json.model.InstanceConverter) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) StringReader(java.io.StringReader) ReconfigurationPlanConverter(org.btrplace.json.plan.ReconfigurationPlanConverter) JSONParser(net.minidev.json.parser.JSONParser) JSONObject(net.minidev.json.JSONObject)

Example 58 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class StaticRoutingConverter method fromJSON.

@Override
public StaticRouting fromJSON(Model mo, JSONObject o) throws JSONConverterException {
    Network v = Network.get(mo);
    TIntObjectMap<Link> idToLink = new TIntObjectHashMap<>();
    for (Link l : v.getLinks()) {
        idToLink.put(l.id(), l);
    }
    StaticRouting r = new StaticRouting();
    checkKeys(o, ROUTES_LABEL);
    JSONArray a = (JSONArray) o.get(ROUTES_LABEL);
    for (Object ao : a) {
        StaticRouting.NodesMap nm = nodesMapFromJSON(mo, (JSONObject) ((JSONObject) ao).get("nodes_map"));
        Map<Link, Boolean> links = new LinkedHashMap<>();
        JSONArray aoa = (JSONArray) ((JSONObject) ao).get("links");
        for (Object aoao : aoa) {
            links.put(idToLink.get(requiredInt((JSONObject) aoao, "link")), Boolean.valueOf(requiredString((JSONObject) aoao, "direction")));
        }
        r.setStaticRoute(nm, links);
    }
    return r;
}
Also used : JSONObject(net.minidev.json.JSONObject) StaticRouting(org.btrplace.model.view.network.StaticRouting) Network(org.btrplace.model.view.network.Network) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Link(org.btrplace.model.view.network.Link) LinkedHashMap(java.util.LinkedHashMap)

Example 59 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class StaticRoutingConverter method toJSON.

/**
 * Convert a Routing implementation into a JSON object
 *
 * @param routing the routing implementation to convert
 * @return the JSON formatted routing object
 */
@Override
public JSONObject toJSON(StaticRouting routing) {
    JSONObject o = new JSONObject();
    o.put("type", getJSONId());
    JSONArray a = new JSONArray();
    Map<StaticRouting.NodesMap, Map<Link, Boolean>> routes = routing.getStaticRoutes();
    for (Map.Entry<StaticRouting.NodesMap, Map<Link, Boolean>> e : routes.entrySet()) {
        StaticRouting.NodesMap nm = e.getKey();
        JSONObject ao = new JSONObject();
        ao.put("nodes_map", nodesMapToJSON(nm));
        JSONArray links = new JSONArray();
        Map<Link, Boolean> v = e.getValue();
        for (Link l : v.keySet()) {
            JSONObject lo = new JSONObject();
            lo.put("link", l.id());
            lo.put("direction", routes.get(nm).get(l).toString());
            links.add(lo);
        }
        ao.put("links", links);
        a.add(ao);
    }
    o.put(ROUTES_LABEL, a);
    return o;
}
Also used : JSONObject(net.minidev.json.JSONObject) StaticRouting(org.btrplace.model.view.network.StaticRouting) JSONArray(net.minidev.json.JSONArray) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) TIntObjectMap(gnu.trove.map.TIntObjectMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Link(org.btrplace.model.view.network.Link)

Example 60 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class SplitConverter method toJSON.

@Override
public JSONObject toJSON(Split o) {
    JSONObject c = new JSONObject();
    c.put("id", getJSONId());
    JSONArray a = new JSONArray();
    for (Collection<VM> grp : o.getSets()) {
        a.add(vmsToJSON(grp));
    }
    c.put("parts", a);
    c.put("continuous", o.isContinuous());
    return c;
}
Also used : JSONObject(net.minidev.json.JSONObject) VM(org.btrplace.model.VM) JSONArray(net.minidev.json.JSONArray)

Aggregations

JSONArray (net.minidev.json.JSONArray)71 JSONObject (net.minidev.json.JSONObject)52 Test (org.junit.Test)13 HashMap (java.util.HashMap)10 JSONParser (net.minidev.json.parser.JSONParser)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 HashSet (java.util.HashSet)6 List (java.util.List)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 JSONConverterException (org.btrplace.json.JSONConverterException)5 Test (org.testng.annotations.Test)5 DocumentContext (com.jayway.jsonpath.DocumentContext)3 JsonPath (com.jayway.jsonpath.JsonPath)3 Option (com.jayway.jsonpath.Option)3 ContentType (ddf.catalog.data.ContentType)3 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)3 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)3 SourceDescriptor (ddf.catalog.source.SourceDescriptor)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3