/**
   * VirtualImage Factory construction XXX is not stored on DB. (not an HB because it's not useful
   * the ''category''/''repository''...)
   *
   * @throws PersistenceException
   * @param name
   * @param cpu
   * @param ram
   * @param hd
   * @return a reference to the newly created VirtualimageHB persistent object
   * @throws PersistenceException
   */
  public VirtualimageHB createVirtualImage(String name, int cpu, int ram, long hd)
      throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    VirtualImageDAO daoVI = factorytest.getVirtualImageDAO();

    factorytest.beginConnection();

    VirtualimageHB vImage = new VirtualimageHB();

    vImage.setName(name);
    vImage.setDescription("test");

    vImage.setCpuRequired(cpu);
    vImage.setHdRequired(hd);
    vImage.setRamRequired(ram);

    vImage.setPathName("test");

    vImage.setIcon(icon);
    vImage.setCategory(category);
    vImage.setType(imageType);
    vImage.setRepository(repository);

    daoVI.makePersistent(vImage);

    vImagesCreated.add(vImage);

    factorytest.endConnection();

    return vImage;
  }
  /** TODO VirtualmachineHB vMachine (can be null) */
  private NodeVirtualImageHB createNodeVirtualImage(
      VirtualimageHB vImage, VirtualmachineHB vMachine) throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    NodeVirtualImageDAO daoNVI = factorytest.getNodeVirtualImageDAO();

    factorytest.beginConnection();

    NodeVirtualImageHB nVi = new NodeVirtualImageHB();
    // XXX nVi.setIdNode(idNode);
    nVi.setIdVirtualApp(vApp.getIdVirtualApp());
    nVi.setVirtualImageHB(vImage);
    nVi.setVirtualMachineHB(vMachine);

    nVi.setType(NodeTypeEnum.VIRTUAL_IMAGE);

    nVi.setName("test");

    nVi.setPosX(10);
    nVi.setPosY(10);

    daoNVI.makePersistent(nVi);

    factorytest.endConnection();

    return nVi;
  }
  /**
   * Creates a {@link AbicloudNetworkHB} object
   *
   * @return AbicloudNetworkHB object created
   * @throws PersistenceException
   */
  private NetworkHB createNetwork() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    NetworkDAO networkDAO = factorytest.getNetworkDAO();
    VlanNetworkDAO vlanNetworkDAO = factorytest.getVlanNetworkDAO();
    NetworkConfigurationDAO netConfDAO = factorytest.getNetworkConfigurationDAO();
    IpPoolManagementDAO hostDAO = factorytest.getIpPoolManagementDAO();

    factorytest.beginConnection();

    NetworkConfigurationHB netConf = new NetworkConfigurationHB();
    netConf.setMask(24);
    netConf.setNetmask("255.255.255.0");
    netConf.setNetworkAddress("192.168.1.0");
    netConfDAO.makePersistent(netConf);

    VlanNetworkHB vlanHB = new VlanNetworkHB();
    vlanHB.setDefaultNetwork(Boolean.TRUE);
    vlanHB.setNetworkName("Test VLAN");
    vlanHB.setConfiguration(netConf);
    vlanNetworkDAO.makePersistent(vlanHB);

    List<VlanNetworkHB> listOfVlans = new ArrayList<VlanNetworkHB>();
    listOfVlans.add(vlanHB);

    NetworkHB network = new NetworkHB();
    network.setUuid(UUID.randomUUID().toString());
    network.setNetworks(listOfVlans);
    networkDAO.makePersistent(network);

    return network;
  }
  /**
   * Creates nodes that correspond to the Virtualapp for the this object
   *
   * @throws PersistenceException
   */
  public void createNodes() throws PersistenceException {

    VirtualMachine virtualMachine = new VirtualMachine();
    factorytest = HibernateDAOFactory.instance();
    HyperVisorDAO dao = factorytest.getHyperVisorDAO();

    factorytest.beginConnection();

    virtualMachine.setAssignedTo(dao.findById(hypervisor.getIdHyper()).toPojo());
    virtualMachine.setHd(1);
    virtualMachine.setHighDisponibility(true);
    virtualMachine.setDescription("test-virtual machine");
    virtualMachine.setCpu(2);
    virtualMachine.setRam(256);
    virtualMachine.setUUID("apophis");
    State state = new State(StateEnum.NOT_DEPLOYED);
    virtualMachine.setState(state);
    virtualMachine.setVdrpIP("vdrpIP");
    virtualMachine.setVdrpPort(5050);

    VirtualimageHB virtualImageHB = createVirtualImage("image", 4, 256, 2);
    virtualMachine.setVirtualImage(virtualImageHB.toPojo());

    createVirtualImageNode(virtualImageHB, virtualMachine);

    factorytest.endConnection();
  }
  /**
   * Persist on database the input PhysicalMachine (also its DataCenter, Rack and So).
   *
   * @throws PersistenceException
   */
  protected PhysicalmachineHB createPhysicalMachine(
      PhysicalmachineHB machine, RackHB rack, HypervisorType htype) throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    PhysicalMachineDAO pmDAO = factorytest.getPhysicalMachineDAO();
    HyperVisorDAO hyperDAO = factorytest.getHyperVisorDAO();

    factorytest.beginConnection();

    HypervisorHB hyper = createHypervisor(htype);

    machine.setRack(rack);
    machine.setDataCenter(rack.getDatacenter());

    machine.setHypervisor(hyper);

    pmDAO.makePersistent(machine);

    log.debug("Added machine [{}]", machine.getName());

    hyper.setPhysicalMachine(machine);
    hyperDAO.makePersistent(hyper);

    factorytest.endConnection();

    return machine;
  }
  /** Gets the default enterprise (abiquo with id 1) */
  private EnterpriseHB getDefaultEnterprise() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    EnterpriseDAO daoEnt = factorytest.getEnterpriseDAO();

    factorytest.beginConnection();

    EnterpriseHB entHB = daoEnt.findById(1);

    factorytest.endConnection();

    return entHB;
  }
  /**
   * Gets the default repository (main id =1)
   *
   * @throws PersistenceException
   */
  private RepositoryHB getDefaultRepository() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    RepositoryDAO daoRep = factorytest.getRepositoryDAO();

    factorytest.beginConnection();

    RepositoryHB repHB = daoRep.findById(1);

    factorytest.endConnection();

    return repHB;
  }
  /**
   * Gets the default virtual image category (Others with id=1)
   *
   * @throws PersistenceException
   */
  protected CategoryHB getDefaultCategory() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    CategoryDAO daoCategory = factorytest.getCategoryDAO();

    factorytest.beginConnection();

    CategoryHB catHB = daoCategory.findById(1);

    factorytest.endConnection();

    return catHB;
  }
  /** Gets the default user (named user ) */
  protected UserHB getDefaultUser() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    UserDAO daoUser = factorytest.getUserDAO();

    factorytest.beginConnection();

    UserHB userHB = daoUser.findById(2);

    factorytest.endConnection();

    return userHB;
  }
  /**
   * Retrieves the default hypervisor and returns pojo encapsulating this information
   *
   * @return a {@link HypervisorHB} object representing the default Hypervisor stored in the DB
   * @throws PersistenceException
   */
  public HypervisorHB getDefaultHypervisor() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    HyperVisorDAO dao = factorytest.getHyperVisorDAO();

    factorytest.beginConnection();

    HypervisorHB hb = dao.findById(1);

    factorytest.endConnection();

    return hb;
  }
  private IconHB createIcon() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    IconDAO daoIcon = factorytest.getIconDAO();

    factorytest.beginConnection();

    IconHB icon = new IconHB();
    icon.setName("test");
    icon.setPath("test");
    daoIcon.makePersistent(icon);

    factorytest.endConnection();

    return icon;
  }
  /**
   * Save the virtual machine. XXX it do not use Crudable because the ''session.saveOrUpdate'' fails
   * caused by ''Batch update returned unexpected row count from update [0]; actual row count: 0;
   * expected: 1'' TODO fix
   */
  private VirtualmachineHB createVirtualMachine(VirtualmachineHB vMachine)
      throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    VirtualMachineDAO vmDAO = factorytest.getVirtualMachineDAO();

    factorytest.beginConnection();
    try {
      vmDAO.makePersistent(vMachine);
      factorytest.endConnection();
    } catch (HibernateException e) {
      factorytest.rollbackConnection();
      throw new PersistenceException("Hibernate exception", e);
    }

    return vMachine;
  }
  /**
   * Deletes entries in the rasd and rasd-management tables
   *
   * @throws PersistenceException
   */
  private void cleanRasd() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    ResourceManagementDAO resourceManagementHBDao = factorytest.getResourceManagementDAO();
    ResourceAllocationSettingDataDAO rasdDao = factorytest.getResourceAllocationSettingDataDAO();

    factorytest.beginConnection();
    for (ResourceManagementHB rasdManagementHB : resourceManagementHBs) {
      resourceManagementHBDao.makeTransient(rasdManagementHB);
    }

    for (ResourceAllocationSettingData rasd : rasdHBs) {
      rasdDao.makeTransient(rasd);
    }
    factorytest.endConnection();

    log.info("Deleted rasd and rasd-management entries");
  }
  private DatacenterHB createDataCenter() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    DataCenterDAO daoDC = factorytest.getDataCenterDAO();

    factorytest.beginConnection();
    DatacenterHB data = new DatacenterHB();

    data.setName(DATACENTER_TEST); // XXX
    data.setSituation("test");

    // XXX data.setRacks(racks);

    daoDC.makePersistent(data);
    factorytest.endConnection();

    return data;
  }
  /**
   * Rack Factory construction
   *
   * @throws PersistenceException
   */
  private RackHB createRack(DatacenterHB dc) throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    RackDAO daoRack = factorytest.getRackDAO();

    factorytest.beginConnection();

    RackHB rack = new RackHB();
    rack.setDatacenter(dc);
    rack.setName("test");
    rack.setShortDescription("test");
    rack.setLargeDescription("test");
    daoRack.makePersistent(rack);

    factorytest.endConnection();

    return rack;
  }
  /**
   * VirtualDataCenter Factory construction
   *
   * @throws PersistenceException
   */
  private VirtualDataCenterHB createVirtualDataCenter(
      DatacenterHB dc, EnterpriseHB enterprise, HypervisorType htype) throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    VirtualDataCenterDAO daoVdc = factorytest.getVirtualDataCenterDAO();

    factorytest.beginConnection();

    VirtualDataCenterHB vdc = new VirtualDataCenterHB();
    vdc.setEnterpriseHB(enterprise);
    vdc.setIdDataCenter(dc.getIdDataCenter());
    vdc.setName("test");
    vdc.setHypervisorType(htype);
    vdc.setNetwork(createNetwork());
    daoVdc.makePersistent(vdc);

    factorytest.endConnection();

    return vdc;
  }
  /**
   * VirtualAppliance Factory construction
   *
   * @throws PersistenceException
   */
  private VirtualappHB createVirtualApp(
      VirtualDataCenterHB vdc, EnterpriseHB enterprise, StateEnum state)
      throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    VirtualApplianceDAO daoApp = factorytest.getVirtualApplianceDAO();

    factorytest.beginConnection();

    VirtualappHB vApp = new VirtualappHB();
    vApp.setEnterpriseHB(enterprise);
    vApp.setState(state);
    vApp.setVirtualDataCenterHB(vdc);
    vApp.setError(0);
    vApp.setHighDisponibility(1);
    vApp.setName("test");
    daoApp.makePersistent(vApp);

    factorytest.endConnection();

    return vApp;
  }
  /**
   * Remove from DB the PhyisicalMachines from initPhysical implementation (actually remove
   * DataCenter @see dataCenter, for cascading delete physical machines disapear) Clean from DB the
   * scheduler test virtual datacenter, virtual application, icon and virtual images.
   *
   * @throws PersistenceException
   */
  public void clearCreatedDomainObjects() throws PersistenceException {
    if (dc == null) {
      return;
    }

    factorytest = HibernateDAOFactory.instance();
    DataCenterDAO daoDc = factorytest.getDataCenterDAO();
    VirtualDataCenterDAO daoVdc = factorytest.getVirtualDataCenterDAO();
    IconDAO daoIco = factorytest.getIconDAO();
    VirtualImageDAO daoVi = factorytest.getVirtualImageDAO();

    factorytest.beginConnection();

    log.debug("Cleaning all the created objects ");

    daoDc.makeTransient(dc);
    daoVdc.makeTransient(vdc);

    daoIco.makeTransient(icon);

    // TODO: create the speciphic method by description??
    // Criterion descriptionTest = Restrictions.eq("description", "test");
    // for (VirtualimageHB vi : daoVi.findByCriteria(descriptionTest))
    // {
    // HibernateUtil.getSession().delete(vi); // XXX use DAO
    // }

    factorytest.endConnection();
  }
  /** Log all PhysicalMachines on the DataBase */
  public void listPhysicalMachines() throws PersistenceException {
    factorytest = HibernateDAOFactory.instance();
    PhysicalMachineDAO daoPM = factorytest.getPhysicalMachineDAO();

    factorytest.beginConnection();

    List<PhysicalmachineHB> machines;

    machines = daoPM.findAll();

    log.debug("########## All PhysicalMachiene ##########");
    for (PhysicalmachineHB pm : machines) {
      log.debug(
          "PhysicalMachine name:"
              + pm.getName()
              + " dc:"
              + pm.getRack().getDatacenter().getName()
              + "\t cpu:"
              + pm.getCpu()
              + "("
              + pm.getCpuUsed()
              + ")"
              + "\t ram:"
              + pm.getRam()
              + "("
              + pm.getRamUsed()
              + ")"
              + "\t hd:"
              + pm.getHd()
              + "("
              + pm.getHdUsed()
              + ")");
    }

    factorytest.endConnection();
  }