Search in sources :

Example 1 with NamingService

use of org.btrplace.model.view.NamingService in project scheduler by btrplace.

the class NamingServiceConverterTest method test.

@Test
public void test() throws JSONConverterException {
    NamingService<VM> ns = NamingService.newVMNS();
    Model mo = new DefaultModel();
    for (int i = 0; i < 10; i++) {
        VM v = mo.newVM();
        ns.register(v, "VM " + i);
    }
    NamingServiceConverter nsc = new NamingServiceConverter();
    JSONObject o = nsc.toJSON(ns);
    System.out.println(o);
    @SuppressWarnings("unchecked") NamingService<VM> ns2 = (NamingService<VM>) nsc.fromJSON(mo, o);
    Assert.assertEquals(ns, ns2);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) JSONObject(net.minidev.json.JSONObject) NamingService(org.btrplace.model.view.NamingService) VM(org.btrplace.model.VM) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Test(org.testng.annotations.Test)

Example 2 with NamingService

use of org.btrplace.model.view.NamingService 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)

Aggregations

JSONObject (net.minidev.json.JSONObject)2 NamingService (org.btrplace.model.view.NamingService)2 Map (java.util.Map)1 JSONConverterException (org.btrplace.json.JSONConverterException)1 JSONs.requiredString (org.btrplace.json.JSONs.requiredString)1 DefaultModel (org.btrplace.model.DefaultModel)1 Element (org.btrplace.model.Element)1 Model (org.btrplace.model.Model)1 VM (org.btrplace.model.VM)1 Test (org.testng.annotations.Test)1