/* (non-Javadoc)
   * @see entropy.monitoring.ConfigurationAdapter#extractConfiguration()
   */
  @Override
  public Configuration extractConfiguration() {

    Configuration config = new SimpleConfiguration();

    for (ServerType server : Utils.getAllServers(currentFit4Green)) {
      Node node = getNode(server);

      if (server.getStatus() == ServerStatusType.ON
          || server.getStatus()
              == ServerStatusType
                  .POWERING_ON) { // POWERING_ON nodes are seen as ON by entropy as they will be
        // soon on. This avoids ping-pong effect on the state.
        config.addOnline(node);

        for (VirtualMachineType VM : Utils.getVMs(server)) {
          VirtualMachine vm = getVM(node, VM);
          config.setRunOn(vm, node);
        }
      } else { // OFF, POWERING_OFF
        config.addOffline(node);
      }
    }
    return config;
  }
  public void test() {

    Configuration cfg = new SimpleConfiguration();
    Node n1 = new SimpleNode("N1", 1, 2, 3);
    Node n2 = new SimpleNode("N2", 1, 2, 3);
    Node n3 = new SimpleNode("N3", 1, 2, 3);
    Node n4 = new SimpleNode("N4", 1, 2, 3);
    cfg.addOnline(n1);
    cfg.addOnline(n2);
    cfg.addOnline(n3);
    cfg.addOffline(n4);

    VirtualMachine vm1 = new SimpleVirtualMachine("VM1", 1, 2, 3);
    VirtualMachine vm2 = new SimpleVirtualMachine("VM2", 1, 2, 3);
    VirtualMachine vm3 = new SimpleVirtualMachine("VM3", 1, 2, 3);
    VirtualMachine vm4 = new SimpleVirtualMachine("VM4", 1, 2, 3);
    VirtualMachine vm5 = new SimpleVirtualMachine("VM5", 1, 2, 3);
    VirtualMachine vm6 = new SimpleVirtualMachine("VM6", 1, 2, 3);
    VirtualMachine vm7 = new SimpleVirtualMachine("VM7", 1, 2, 3);

    cfg.setRunOn(vm1, n1);
    cfg.setRunOn(vm2, n1);
    cfg.setSleepOn(vm3, n2);
    cfg.setRunOn(vm4, n2);
    cfg.addWaiting(vm5);
    cfg.setRunOn(vm6, n3);
    cfg.setRunOn(vm7, n3);

    TimedReconfigurationPlan p = new DefaultTimedReconfigurationPlan(cfg);
    Assert.assertTrue(p.add(new Migration(vm1, n1, n2, 0, 5)));
    Assert.assertTrue(p.add(new Startup(n4, 0, 3)));
    Assert.assertTrue(p.add(new Migration(vm6, n3, n2, 2, 5)));
    Assert.assertTrue(p.add(new Suspend(vm7, n3, n4, 7, 10)));
    Assert.assertTrue(p.add(new Run(vm5, n4, 0, 1)));
    // Assert.assertTrue(p.add(new Pause(vm2, n1, 0, 5)));
    // Assert.assertTrue(p.add(new UnPause(vm3, n1, 10, 15)));
    Assert.assertTrue(p.add(new Stop(vm4, n2, 1, 2)));
    Assert.assertTrue(p.add(new Shutdown(n3, 10, 15)));

    ProtobufTimedReconfigurationPlanSerializer s =
        ProtobufTimedReconfigurationPlanSerializer.getInstance();
    File tmpF = null;
    try {
      tmpF = File.createTempFile("out", "out");
      s.write(p, tmpF.getAbsolutePath());
      TimedReconfigurationPlan r = s.read(tmpF.getAbsolutePath());
      Assert.assertEquals(r, p);
    } catch (IOException e) {
      Assert.fail(e.getMessage(), e);
    } catch (TimedReconfigurationPlanSerializerException e) {
      Assert.fail(e.getMessage(), e);
    } finally {
      if (tmpF != null && tmpF.exists()) {
        tmpF.delete();
      }
    }
  }
 /** Test serialization/unserialization */
 public void test() {
   Configuration cfg = new SimpleConfiguration();
   for (int i = 1; i <= 10; i++) {
     Node n = new SimpleNode("N" + i, i, i + 1, i + 2);
     if (i % 3 == 0) {
       cfg.addOffline(n);
     } else {
       cfg.addOnline(n);
     }
   }
   Random rnd = new Random();
   for (int i = 1; i <= 20; i++) {
     VirtualMachine vm = new SimpleVirtualMachine("VM" + i, i, i + 1, i + 2);
     vm.setCPUDemand(i + 3);
     vm.setCPUMax(i + 7);
     Node n = cfg.getOnlines().get(rnd.nextInt(cfg.getOnlines().size()));
     if (i % 3 == 0) {
       cfg.setSleepOn(vm, n);
     } else if (i % 5 == 0) {
       cfg.addWaiting(vm);
     } else {
       cfg.setRunOn(vm, n);
     }
   }
   FileConfigurationSerializer s = PlainTextConfigurationSerializer.getInstance();
   File tmpF;
   try {
     tmpF = File.createTempFile("out", "out");
     tmpF.deleteOnExit();
     s.write(cfg, tmpF.getAbsolutePath());
     Configuration r = s.read(tmpF.getAbsolutePath());
     Assert.assertEquals(r, cfg);
   } catch (IOException e) {
     Assert.fail(e.getMessage(), e);
   } catch (ConfigurationSerializerException e) {
     Assert.fail(e.getMessage(), e);
   }
 }