List of usage examples for org.hibernate Session get
Object get(String entityName, Serializable id);
From source file:com.abiquo.abiserver.abicloudws.RemoteServiceUtils.java
License:Open Source License
/** * Private helper to get the NodeCollecto address from the datacenter id. Requires a nested * transaction/*from w w w . j ava2s . c o m*/ * * @param datacenterId the datacenter ID * @param the hibernate session * @return the address destination * @throws PersistenceException * @throws RemoteServiceException */ @Deprecated // use RemoteServiceDAO public static String getNodeCollectorFromDatacenter(final Integer datacenterId, final Session session) throws PersistenceException, RemoteServiceException { String destination = null; DatacenterHB myDatacenter = (DatacenterHB) session.get(DatacenterHB.class, datacenterId); Set<RemoteServiceHB> remoteServices = myDatacenter.getRemoteServicesHB(); for (RemoteServiceHB remoteServiceHB : remoteServices) { if (remoteServiceHB .getRemoteServiceType() == com.abiquo.abiserver.business.hibernate.pojohb.service.RemoteServiceType.NODE_COLLECTOR) { destination = remoteServiceHB.getUri(); break; } } if (destination == null) { throw new RemoteServiceException( "There is no NodeCollector remote service configured for this datacenter." + " Check datacenter configuration in Infrastructure view."); } return destination; }
From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java
License:Open Source License
@Override @SuppressWarnings("unchecked") @Deprecated//from w ww . java2 s .c o m public DataResult<ArrayList<InfrastructureElement>> getInfrastructureByDataCenter(final DataCenter dataCenter) { DataResult<ArrayList<InfrastructureElement>> dataResult = new DataResult<ArrayList<InfrastructureElement>>(); ArrayList<InfrastructureElement> infrastructures = null; DatacenterHB datacenterPojo = null; Session session = HibernateUtil.getSession(); Transaction transaction = null; try { transaction = session.beginTransaction(); infrastructures = new ArrayList<InfrastructureElement>(); datacenterPojo = (DatacenterHB) session.get(DatacenterHB.class, dataCenter.getId()); // Adding the racks Set<RackHB> racks = datacenterPojo.getRacks(); for (RackHB rackPojo : racks) { Rack rack = rackPojo.toPojo(); rack.setDataCenter(dataCenter); // Adding to the infrastructure list infrastructures.add(rack); // Adding the physicalMachines Set<PhysicalmachineHB> phyMachines = rackPojo.getPhysicalmachines(); for (PhysicalmachineHB phyMachinePojo : phyMachines) { if (phyMachinePojo.getHypervisor() != null) { PhysicalMachine phyMachine = phyMachinePojo.toPojo(); phyMachine.setAssignedTo(rack); infrastructures.add(phyMachine); // Adding the HyperVisor HypervisorHB hypervisorPojo = phyMachinePojo.getHypervisor(); HyperVisor hypervisor = hypervisorPojo.toPojo(); hypervisor.setAssignedTo(phyMachine); infrastructures.add(hypervisor); // Adding the VirtualMachines Set<VirtualmachineHB> virtualMachines = hypervisorPojo.getVirtualmachines(); for (VirtualmachineHB virtualMachinePojo : virtualMachines) { VirtualMachine virtualMachine = virtualMachinePojo.toPojo(); virtualMachine.setAssignedTo(hypervisor); infrastructures.add(virtualMachine); } } } } // Adding the physical machines in this Data Center, without a rack Conjunction conjunction = Restrictions.conjunction(); conjunction.add(Restrictions.isNull("rack")); conjunction.add(Restrictions.eq("dataCenter", datacenterPojo)); ArrayList<PhysicalmachineHB> physicalMachinesWORack = (ArrayList<PhysicalmachineHB>) session .createCriteria(PhysicalmachineHB.class).add(conjunction).list(); for (PhysicalmachineHB physicalMachineHB : physicalMachinesWORack) { infrastructures.add(physicalMachineHB.toPojo()); } // We are done! transaction.commit(); dataResult.setSuccess(true); dataResult.setData(infrastructures); } catch (HibernateException e) { if (transaction != null) { transaction.rollback(); } logger.trace("Unexpected database error when refreshing infrastructure data", e); } return dataResult; }
From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java
License:Open Source License
@Override public DataResult<PhysicalMachineCreation> createPhysicalMachine(final UserSession userSession, final PhysicalMachineCreation physicalMachineCreation) throws InfrastructureCommandException { DataResult<PhysicalMachineCreation> dataResult; dataResult = new DataResult<PhysicalMachineCreation>(); Session session = null; Transaction transaction = null;/* w ww .ja v a 2 s . c om*/ PhysicalMachine pm = physicalMachineCreation.getPhysicalMachine(); try { if (pm.getName() != null && pm.getName().trim().length() == 0) { dataResult.setSuccess(false); errorManager.reportError(InfrastructureCommandImpl.resourceManager, dataResult, "createPhysicalMachine_noname"); // Log the event traceLog(SeverityType.MINOR, ComponentType.MACHINE, EventType.MACHINE_CREATE, userSession, pm.getDataCenter(), null, "Physical machine without name", null, (Rack) pm.getAssignedTo(), pm, null, null); return dataResult; } } catch (Exception e) { errorManager.reportError(InfrastructureCommandImpl.resourceManager, dataResult, "createPhysicalMachine_noname", e); // Log the event traceLog(SeverityType.MINOR, ComponentType.MACHINE, EventType.MACHINE_CREATE, userSession, pm.getDataCenter(), null, e.getMessage(), null, (Rack) pm.getAssignedTo(), pm, null, null); } String virtualSystemMonitorAddress = null; try { virtualSystemMonitorAddress = RemoteServiceUtils.getVirtualSystemMonitorFromPhysicalMachine(pm); } catch (Exception e) { errorManager.reportError(InfrastructureCommandImpl.resourceManager, dataResult, "createPhysicalMachine", e); PhysicalMachine physicalMachine = physicalMachineCreation.getPhysicalMachine(); // Log the event traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_CREATE, userSession, physicalMachine.getDataCenter(), null, e.getMessage(), null, (Rack) physicalMachine.getAssignedTo(), physicalMachine, null, null); } HyperVisor hypervisor = physicalMachineCreation.getHypervisors().get(0); String user = hypervisor.getUser(); String password = hypervisor.getPassword(); String virtualSystemAddress = "http://" + hypervisor.getIp() + ":" + hypervisor.getPort() + "/"; HypervisorType hypervisorType = hypervisor.toPojoHB().getType(); // try // { // EventingSupport.monitorPhysicalMachine(virtualSystemAddress, hypervisorType, // virtualSystemMonitorAddress, user, password); // } // catch (EventingException e) // { // errorManager.reportError(InfrastructureCommandImpl.resourceManager, dataResult, // "createPhysicalMachine", e); // return dataResult; // } try { PhysicalMachine physicalMachine = physicalMachineCreation.getPhysicalMachine(); // Checks non-zero values in PhysicalMachine data checkPhysicalMachineData(physicalMachine); session = HibernateUtil.getSessionFactory().getCurrentSession(); transaction = session.beginTransaction(); // Creating the PhysicalMachineHB object to save the PhysicalMachine // in the Data Base PhysicalmachineHB physicalMachineHB = physicalMachine.toPojoHB(); physicalMachineHB.setHypervisor(null); // Save the datastores of the physicalmachine first for (DatastoreHB datastore : physicalMachineHB.getDatastoresHB()) { if (datastore.getRootPath().length() <= Datastore.ROOT_PATH_LENGTH_MAX) { session.save(datastore); } else { if (transaction != null && transaction.isActive()) { transaction.rollback(); } dataResult.setSuccess(false); errorManager.reportError(InfrastructureCommandImpl.resourceManager, dataResult, "createPhysicalMachine_longname"); // Log the event traceLog(SeverityType.MINOR, ComponentType.MACHINE, EventType.MACHINE_CREATE, userSession, pm.getDataCenter(), null, "Datastore name size is too long", null, (Rack) pm.getAssignedTo(), pm, null, null); return dataResult; } } session.save(physicalMachineHB); // Creating the hypervisors, if there is any ArrayList<HyperVisor> hypervisorList = physicalMachineCreation.getHypervisors(); ArrayList<HyperVisor> createdHypervisorList = new ArrayList<HyperVisor>(); HypervisorHB hypervisorHBToCreate; for (HyperVisor hypervisorToCreate : hypervisorList) { hypervisorHBToCreate = hypervisorToCreate.toPojoHB(); hypervisorHBToCreate.setPhysicalMachine(physicalMachineHB); session.save(hypervisorHBToCreate); createdHypervisorList.add(hypervisorHBToCreate.toPojo()); } // Returning the PhysicalMachine and the Hypervisors created to the // client PhysicalmachineHB physicalMachineHBCreated = (PhysicalmachineHB) session.get(PhysicalmachineHB.class, physicalMachineHB.getIdPhysicalMachine()); PhysicalMachine physicalMachineCreated = physicalMachineHBCreated.toPojo(); transaction.commit(); physicalMachineCreation.setPhysicalMachine(physicalMachineCreated); physicalMachineCreation.setHypervisors(createdHypervisorList); dataResult.setData(physicalMachineCreation); dataResult.setSuccess(true); dataResult.setMessage( InfrastructureCommandImpl.resourceManager.getMessage("createPhysicalMachine.success")); // Log the event String hyperName = "NULL"; if (physicalMachineCreation.getHypervisors().get(0) != null) { hyperName = physicalMachineCreation.getHypervisors().get(0).getName(); } traceLog(SeverityType.INFO, ComponentType.MACHINE, EventType.MACHINE_CREATE, userSession, physicalMachine.getDataCenter(), null, "Physical machine '" + physicalMachine.getName() + "' has been created [" + physicalMachine.getCpu() + "CPUs, " + physicalMachine.getRam() + " RAM, " + physicalMachine.getHd() + " HD, " + hyperName + " hypervisor]", null, (Rack) physicalMachine.getAssignedTo(), physicalMachine, null, null); } catch (Exception e) { if (transaction != null && transaction.isActive()) { transaction.rollback(); } errorManager.reportError(InfrastructureCommandImpl.resourceManager, dataResult, "createPhysicalMachine", e); PhysicalMachine physicalMachine = physicalMachineCreation.getPhysicalMachine(); // Log the event traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_CREATE, userSession, physicalMachine.getDataCenter(), null, e.getMessage(), null, (Rack) physicalMachine.getAssignedTo(), physicalMachine, null, null); // try // { // EventingSupport.unMonitorPhysicalMachine(virtualSystemAddress, hypervisorType, // virtualSystemMonitorAddress, user, password); // } // catch (EventingException e1) // { // errorManager.reportError(InfrastructureCommandImpl.resourceManager, dataResult, // "createPhysicalMachine", e1); // } } return dataResult; }
From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java
License:Open Source License
@Override public DataResult<ArrayList<HyperVisor>> editPhysicalMachine(final UserSession userSession, final PhysicalMachineCreation physicalMachineCreation) throws InfrastructureCommandException { DataResult<ArrayList<HyperVisor>> dataResult = new DataResult<ArrayList<HyperVisor>>(); Session session = null; Transaction transaction = null;//w w w .java 2 s. com try { PhysicalMachine pm = physicalMachineCreation.getPhysicalMachine(); checkPhysicalMachineData(pm); session = HibernateUtil.getSession(); transaction = session.beginTransaction(); PhysicalmachineHB physicalMachineHb = (PhysicalmachineHB) session.get(PhysicalmachineHB.class, pm.getId()); PhysicalMachine physicalMachineAux = physicalMachineHb.toPojo(); if (pm.getAssignedTo() instanceof UcsRack && !pm.getName().equalsIgnoreCase(physicalMachineHb.getName())) { dataResult.setSuccess(false); dataResult.setMessage("The Machine is managed and its name cannot change"); // Log the event traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession, physicalMachineCreation.getPhysicalMachine().getDataCenter(), null, "The Machine is managed and its name cannot change", null, (Rack) physicalMachineCreation.getPhysicalMachine().getAssignedTo(), physicalMachineHb.toPojo(), null, null); return dataResult; } if (pm.getAssignedTo() instanceof UcsRack) { dataResult.setSuccess(false); dataResult.setMessage("The Machine is managed and its name cannot change"); // Log the event traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession, physicalMachineCreation.getPhysicalMachine().getDataCenter(), null, "The Machine is managed and its name cannot change", null, (Rack) physicalMachineCreation.getPhysicalMachine().getAssignedTo(), physicalMachineHb.toPojo(), null, null); return dataResult; } final String ipService = pm.getHypervisor().getIpService(); // Updating the other attributes physicalMachineHb.setName(pm.getName()); physicalMachineHb.setDescription(pm.getDescription()); physicalMachineHb.setCpu(pm.getCpu()); physicalMachineHb.setRam(pm.getRam()); physicalMachineHb.setIdState(pm.getIdState()); physicalMachineHb.getHypervisor().setIpService(ipService); physicalMachineHb.setVswitchName(pm.getVswitchName()); physicalMachineHb.setIpmiIp(pm.getIpmiIp()); physicalMachineHb.setIpmiPort(pm.getIpmiPort()); physicalMachineHb.setIpmiUser(pm.getIpmiUser()); physicalMachineHb.setIpmiPassword(pm.getIpmiPassword()); // Updating virtual machines PhysicalMachineDAO pmDAO = factory.getPhysicalMachineDAO(); List<VirtualmachineHB> vmList = pmDAO .getDeployedVirtualMachines(physicalMachineHb.getIdPhysicalMachine()); if (vmList != null && !vmList.isEmpty()) { for (VirtualmachineHB vm : vmList) { if (StringUtils.hasText(vm.getVdrpIp())) { vm.setVdrpIp(ipService); } } } session.update(physicalMachineHb); dataResult.setSuccess(true); dataResult.setMessage("Physical Machine edited successfully"); dataResult.setData(physicalMachineCreation.getHypervisors()); transaction.commit(); traceLog(SeverityType.INFO, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession, physicalMachineAux.getDataCenter(), null, "Physical machine '" + physicalMachineAux.getName() + "' has been modified [Name: " + physicalMachineHb.getName() + ", " + +physicalMachineHb.getCpu() + "CPUs, " + physicalMachineHb.getRam() + " RAM, " + physicalMachineHb.getHypervisor().getType().getValue() + " hypervisor]", null, (Rack) physicalMachineAux.getAssignedTo(), physicalMachineAux, null, null); } catch (HibernateException e) { if (transaction != null && transaction.isActive()) { transaction.rollback(); } errorManager.reportError(resourceManager, dataResult, "editPhysicalMachine", e); PhysicalmachineHB physicalMachineHb = (PhysicalmachineHB) session.get(PhysicalmachineHB.class, physicalMachineCreation.getPhysicalMachine().getId()); // Log the event traceLog(SeverityType.CRITICAL, ComponentType.MACHINE, EventType.MACHINE_MODIFY, userSession, physicalMachineCreation.getPhysicalMachine().getDataCenter(), null, e.getMessage(), null, (Rack) physicalMachineCreation.getPhysicalMachine().getAssignedTo(), physicalMachineHb.toPojo(), null, null); } return dataResult; }
From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java
License:Open Source License
@Override public BasicResult editHypervisor(final UserSession userSession, final HyperVisor hypervisor) { BasicResult basicResult;/*w w w .j av a2s . com*/ basicResult = new BasicResult(); Session session = null; Transaction transaction = null; try { session = HibernateUtil.getSession(); transaction = session.beginTransaction(); HypervisorHB hypervisorHB = (HypervisorHB) session.get(HypervisorHB.class, hypervisor.getId()); PhysicalmachineHB physicalMachineHB = (PhysicalmachineHB) session.get(PhysicalmachineHB.class, hypervisor.getAssignedTo().getId()); // Updating the Hypervisor hypervisorHB.setType(HypervisorType.fromValue(hypervisor.getType().getName())); hypervisorHB.setIp(hypervisor.getIp()); hypervisorHB.setIpService(hypervisor.getIpService()); hypervisorHB.setPort(hypervisor.getPort()); hypervisorHB.setPhysicalMachine(physicalMachineHB); session.update(hypervisorHB); transaction.commit(); } catch (HibernateException e) { if (transaction != null && transaction.isActive()) { transaction.rollback(); } errorManager.reportError(resourceManager, basicResult, "editHypervisor", e); } basicResult.setMessage(InfrastructureCommandImpl.resourceManager.getMessage("editHypervisor.success")); basicResult.setSuccess(true); return basicResult; }
From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java
License:Open Source License
@Override public BasicResult deleteHypervisor(final HyperVisor hypervisor) { BasicResult basicResult;//from ww w .j a v a2 s . com basicResult = new BasicResult(); Session session = null; Transaction transaction = null; try { session = HibernateUtil.getSession(); transaction = session.beginTransaction(); HypervisorHB hypervisorHB = (HypervisorHB) session.get(HypervisorHB.class, hypervisor.getId()); session.delete(hypervisorHB); // TODO Do we have to delete Virtual Infrastructure when an // Hypervisor has been deleted? transaction.commit(); } catch (Exception e) { if (transaction != null && transaction.isActive()) { transaction.rollback(); } errorManager.reportError(resourceManager, basicResult, "deleteHypervisor", e); } basicResult.setSuccess(true); return basicResult; }
From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java
License:Open Source License
@Override public BasicResult deleteVirtualMachine(final UserSession userSession, final VirtualMachine virtualMachine) { // TODO Connect with database BasicResult basicResult = null;/*ww w . j ava2 s . com*/ VirtualmachineHB virtualMachinePojo = null; Transaction transaction = null; try { Session session = HibernateUtil.getSession(); transaction = session.beginTransaction(); virtualMachinePojo = (VirtualmachineHB) session.get(VirtualmachineHB.class, virtualMachine.getId()); session.delete(virtualMachinePojo); basicResult = getInfrastructureWS().deleteVirtualMachine(virtualMachine); } catch (Exception e) { errorManager.reportError(resourceManager, basicResult, "deleteVirtualMachine", e); traceLog(SeverityType.CRITICAL, ComponentType.VIRTUAL_MACHINE, EventType.VM_DESTROY, userSession, null, null, e.getMessage(), null, null, null, null, null); } if (basicResult.getSuccess()) { transaction.commit(); traceLog(SeverityType.INFO, ComponentType.VIRTUAL_MACHINE, EventType.VM_DESTROY, userSession, null, null, null, null, null, null, null, null); } return basicResult; }
From source file:com.abiquo.abiserver.commands.impl.InfrastructureCommandImpl.java
License:Open Source License
/** * Checks if the state of a given virtual machine, is actually the last valid state in the Data * Base If it is the same, the state of the virtual machine will be updated to * State.IN_PROGRESS, and a boolean will be returned to true, to indicate that the virtual * machine can be manipulated Otherwise, the current state will be returned, and the boolean * will be set to false, indicating that the virtual machine can not be manipulated * //from ww w. j a v a2s .c o m * @param virtualMachine The virtual machine that will be checked * @return A DataResult object, containing a boolean that indicates if the virtual machine can * be manipulated and, in any case, it will contain the last valid state of the virtual * machine * @throws Exception An Exception is thrown if there was a problem connecting to the Data base */ private DataResult<State> checkVirtualMachineState(final VirtualMachine virtualMachine) throws Exception { Session session = null; Transaction transaction = null; DataResult<State> currentStateAndAllow = new DataResult<State>(); try { session = HibernateUtil.getSession(); transaction = session.beginTransaction(); // Getting the last state of the virtual machine VirtualmachineHB virtualMachineHB = (VirtualmachineHB) session.get(VirtualmachineHB.class, virtualMachine.getId()); if (virtualMachine.getState().toEnum() == virtualMachineHB.getState() && virtualMachineHB.getState() != StateEnum.LOCKED) { // The given virtual machine is up to date, and is not in // progress. // We set it now to IN_PROGRESS, and return that it is allowed // to manipulate it virtualMachineHB.setState(StateEnum.LOCKED); session.update(virtualMachineHB); // Generating the result currentStateAndAllow.setSuccess(true); currentStateAndAllow.setData(new State(StateEnum.LOCKED)); } else { // The given virtual machine is not up to date, or the virtual // machine // is already in the state State.IN_PROGRESS. Manipulating it is // not allowed // Generating the result currentStateAndAllow.setSuccess(false); currentStateAndAllow.setData(new State(virtualMachineHB.getState())); } transaction.commit(); } catch (Exception e) { if (transaction != null && transaction.isActive()) { transaction.rollback(); } throw e; } return currentStateAndAllow; }
From source file:com.abiquo.abiserver.commands.impl.UserCommandImpl.java
License:Open Source License
@Override @SuppressWarnings("unchecked") public BasicResult editEnterprise(final UserSession userSession, final Enterprise enterprise) { // Getting the enterprise that will be edited Session session = HibernateUtil.getSession(); Transaction transaction = session.beginTransaction(); EnterpriseHB enterpriseHB = (EnterpriseHB) session.get(EnterpriseHB.class, enterprise.getId()); // Checked in API // try//from w w w . j a v a 2 s.co m // { // checkEditLimits(enterpriseHB, enterprise, userSession); // } // catch (HardLimitExceededException e) // { // BasicResult basicResult = new BasicResult(); // basicResult.setSuccess(false); // basicResult.setMessage(resourceManager.getMessage("editEnterprise.limitExceeded")); // // return basicResult; // // } // finally // { // transaction.commit(); // } EnterprisesResourceStub proxy = getEnterpriseStubProxy(userSession); DataResult<Enterprise> result = new DataResult<Enterprise>(); result = proxy.editEnterprise(enterprise); if (result.getSuccess()) { // Building result result.setSuccess(true); result.setMessage(resourceManager.getMessage("editEnterprise.success")); // Log the event // traceLog(SeverityType.INFO, ComponentType.ENTERPRISE, EventType.ENTERPRISE_MODIFY, // userSession, null, null, "Enterprise '" + enterprise.getName() // + "' has been modified [Name: " + enterprise.getName() + "]", null, null, null, // null, enterprise.getName()); } else { // result.setSuccess(false); // result.setMessage(resourceManager.getMessage("editEnterprise.limitExceeded")); // // errorManager.reportError(resourceManager, result, "editEnterprise", // result.getMessage()); traceLog(SeverityType.CRITICAL, ComponentType.ENTERPRISE, EventType.ENTERPRISE_MODIFY, userSession, null, null, result.getMessage(), null, null, null, null, enterprise.getName()); } return result; }