@Override public void preRecoverDataVolume(VolumeInventory vol) { if (vol.getPrimaryStorageUuid() == null) { return; } SimpleQuery<PrimaryStorageVO> q = dbf.createQuery(PrimaryStorageVO.class); q.select(PrimaryStorageVO_.type); q.add(PrimaryStorageVO_.uuid, Op.EQ, vol.getPrimaryStorageUuid()); String type = q.findValue(); if (!LocalStorageConstants.LOCAL_STORAGE_TYPE.equals(type)) { return; } SimpleQuery<LocalStorageResourceRefVO> rq = dbf.createQuery(LocalStorageResourceRefVO.class); rq.add(LocalStorageResourceRefVO_.resourceUuid, Op.EQ, vol.getUuid()); rq.add(LocalStorageResourceRefVO_.resourceType, Op.EQ, VolumeVO.class.getSimpleName()); if (!rq.isExists()) { throw new OperationFailureException( errf.stringToOperationError( String.format( "the data volume[name:%s, uuid:%s] is on the local storage[uuid:%s]; however," + "the host on which the data volume is has been deleted. Unable to recover this volume", vol.getName(), vol.getUuid(), vol.getPrimaryStorageUuid()))); } }
@Override @Transactional(readOnly = true) public void preRecoverVm(VmInstanceInventory vm) { String rootVolUuid = vm.getRootVolumeUuid(); String sql = "select ps.uuid from PrimaryStorageVO ps, VolumeVO vol where ps.uuid = vol.primaryStorageUuid" + " and vol.uuid = :uuid and ps.type = :pstype"; TypedQuery<String> q = dbf.getEntityManager().createQuery(sql, String.class); q.setParameter("uuid", rootVolUuid); q.setParameter("pstype", LocalStorageConstants.LOCAL_STORAGE_TYPE); String psuuid = dbf.find(q); if (psuuid == null) { return; } sql = "select count(ref) from LocalStorageResourceRefVO ref where ref.resourceUuid = :uuid and ref.resourceType = :rtype"; TypedQuery<Long> rq = dbf.getEntityManager().createQuery(sql, Long.class); rq.setParameter("uuid", rootVolUuid); rq.setParameter("rtype", VolumeVO.class.getSimpleName()); long count = rq.getSingleResult(); if (count == 0) { throw new OperationFailureException( errf.stringToOperationError( String.format( "unable to recover the vm[uuid:%s, name:%s]. The vm's root volume is on the local" + " storage[uuid:%s]; however, the host on which the root volume is has been deleted", vm.getUuid(), vm.getName(), psuuid))); } }
@Override public void run(FlowTrigger trigger, Map data) { PrimaryStorageAllocationSpec spec = (PrimaryStorageAllocationSpec) data.get(AllocatorParams.SPEC); List<PrimaryStorageVO> candidates = (List<PrimaryStorageVO>) data.get(AllocatorParams.CANDIDATES); DebugUtils.Assert( candidates != null && !candidates.isEmpty(), "PrimaryStorageTagAllocatorFlow cannot be the first element in allocator chain"); List<SystemTagVO> tvos = null; if (spec.getVmInstanceUuid() != null) { SimpleQuery<SystemTagVO> q = dbf.createQuery(SystemTagVO.class); q.add(SystemTagVO_.resourceType, Op.EQ, VmInstanceVO.class.getSimpleName()); q.add(SystemTagVO_.resourceUuid, Op.EQ, spec.getVmInstanceUuid()); tvos = q.list(); } else if (spec.getDiskOfferingUuid() != null) { SimpleQuery<SystemTagVO> q = dbf.createQuery(SystemTagVO.class); q.add(SystemTagVO_.resourceType, Op.EQ, DiskOfferingVO.class.getSimpleName()); q.add(SystemTagVO_.resourceUuid, Op.EQ, spec.getDiskOfferingUuid()); tvos = q.list(); } if (tvos != null && !tvos.isEmpty()) { candidates = callTagExtensions(SystemTagInventory.valueOf(tvos), candidates); data.put(AllocatorParams.CANDIDATES, candidates); } trigger.next(); }
@Test public void test() throws ApiSenderException { VmInstanceInventory vm = deployer.vms.get("TestVm"); api.stopVmInstance(vm.getUuid()); PrimaryStorageInventory local = deployer.primaryStorages.get("local"); VolumeSnapshotInventory sp = api.createSnapshot(vm.getRootVolumeUuid()); Assert.assertFalse(kconfig.snapshotCmds.isEmpty()); LocalStorageResourceRefVO ref = dbf.findByUuid(sp.getUuid(), LocalStorageResourceRefVO.class); Assert.assertNotNull(ref); Assert.assertEquals(vm.getHostUuid(), ref.getHostUuid()); sp = api.createSnapshot(vm.getRootVolumeUuid()); ref = dbf.findByUuid(sp.getUuid(), LocalStorageResourceRefVO.class); Assert.assertNotNull(ref); Assert.assertEquals(vm.getHostUuid(), ref.getHostUuid()); sp = api.createSnapshot(vm.getRootVolumeUuid()); ref = dbf.findByUuid(sp.getUuid(), LocalStorageResourceRefVO.class); Assert.assertNotNull(ref); Assert.assertEquals(vm.getHostUuid(), ref.getHostUuid()); PrimaryStorageVO localvo = dbf.findByUuid(local.getUuid(), PrimaryStorageVO.class); long avail = localvo.getCapacity().getAvailableCapacity(); ImageInventory img = api.createTemplateFromSnapshot(sp.getUuid()); Assert.assertTrue(img.getSize() != 0); localvo = dbf.findByUuid(local.getUuid(), PrimaryStorageVO.class); Assert.assertEquals(avail, localvo.getCapacity().getAvailableCapacity()); }
@Override public NetworkServiceProviderType getTypeOfNetworkServiceProviderForService( String l3NetworkUuid, NetworkServiceType serviceType) { L3NetworkVO l3vo = dbf.findByUuid(l3NetworkUuid, L3NetworkVO.class); L3NetworkInventory l3inv = L3NetworkInventory.valueOf(l3vo); NetworkServiceL3NetworkRefInventory targetRef = null; for (NetworkServiceL3NetworkRefInventory ref : l3inv.getNetworkServices()) { if (ref.getNetworkServiceType().equals(serviceType.toString())) { targetRef = ref; break; } } if (targetRef == null) { throw new OperationFailureException( errf.stringToOperationError( String.format( "L3Network[uuid:%s] doesn't have network service[type:%s] enabled or no provider provides this network service", l3NetworkUuid, serviceType))); } SimpleQuery<NetworkServiceProviderVO> q = dbf.createQuery(NetworkServiceProviderVO.class); q.select(NetworkServiceProviderVO_.type); q.add(NetworkServiceProviderVO_.uuid, Op.EQ, targetRef.getNetworkServiceProviderUuid()); String providerType = q.findValue(); return NetworkServiceProviderType.valueOf(providerType); }
private void handle(APIDetachNetworkServiceProviderFromL2NetworkMsg msg) { NetworkServiceProviderVO vo = dbf.findByUuid(msg.getNetworkServiceProviderUuid(), NetworkServiceProviderVO.class); NetworkServiceProviderFactory factory = getProviderFactory(vo.getType()); NetworkServiceProvider provider = factory.getNetworkServiceProvider(vo); L2NetworkVO l2vo = dbf.findByUuid(msg.getL2NetworkUuid(), L2NetworkVO.class); APIDetachNetworkServiceProviderFromL2NetworkEvent evt = new APIDetachNetworkServiceProviderFromL2NetworkEvent(msg.getId()); try { provider.detachFromL2Network(L2NetworkInventory.valueOf(l2vo), msg); } catch (NetworkException e) { String err = String.format( "unable to detach network service provider[uuid:%s, name:%s, type:%s] to l2network[uuid:%s, name:%s, type:%s], %s", vo.getUuid(), vo.getName(), vo.getType(), l2vo.getUuid(), l2vo.getName(), l2vo.getType(), e.getMessage()); logger.warn(err, e); evt.setErrorCode( errf.instantiateErrorCode( NetworkServiceErrors.DETACH_NETWORK_SERVICE_PROVIDER_ERROR, err)); bus.publish(evt); return; } SimpleQuery<NetworkServiceProviderL2NetworkRefVO> query = dbf.createQuery(NetworkServiceProviderL2NetworkRefVO.class); query.select(NetworkServiceProviderL2NetworkRefVO_.id); query.add(NetworkServiceProviderL2NetworkRefVO_.l2NetworkUuid, Op.EQ, l2vo.getUuid()); query.add( NetworkServiceProviderL2NetworkRefVO_.networkServiceProviderUuid, Op.EQ, vo.getUuid()); Long id = query.findValue(); if (id != null) { dbf.removeByPrimaryKey(id, NetworkServiceProviderL2NetworkRefVO.class); } vo = dbf.findByUuid(vo.getUuid(), NetworkServiceProviderVO.class); evt.setInventory(NetworkServiceProviderInventory.valueOf(vo)); String info = String.format( "successfully detach network service provider[uuid:%s, name:%s, type:%s] to l2network[uuid:%s, name:%s, type:%s]", vo.getUuid(), vo.getName(), vo.getType(), l2vo.getUuid(), l2vo.getName(), l2vo.getType()); logger.debug(info); bus.publish(evt); }
@Override public void run(FlowTrigger trigger, Map data) { final String vrUuid = (String) data.get(Param.VR_UUID.toString()); SimpleQuery<VirtualRouterVipVO> q = dbf.createQuery(VirtualRouterVipVO.class); q.add(VirtualRouterVipVO_.virtualRouterVmUuid, Op.EQ, vrUuid); List<VirtualRouterVipVO> refs = q.list(); if (!refs.isEmpty()) { dbf.removeCollection(refs, VirtualRouterVipVO.class); } trigger.next(); }
@Override @Transactional public void delete(String uuid) { String sql = "delete from KeyValueVO vo where vo.uuid = :uuid"; Query q = dbf.getEntityManager().createQuery(sql); q.setParameter("uuid", uuid); q.executeUpdate(); sql = "delete from KeyValueBinaryVO vo where vo.uuid = :uuid"; q = dbf.getEntityManager().createQuery(sql); q.setParameter("uuid", uuid); q.executeUpdate(); }
@Transactional private void handle(TakePrimaryStorageCapacityMsg msg) { PrimaryStorageCapacityVO vo = dbf.getEntityManager() .find(PrimaryStorageCapacityVO.class, self.getUuid(), LockModeType.PESSIMISTIC_WRITE); vo.setAvailableCapacity(vo.getAvailableCapacity() - msg.getSize()); if (vo.getAvailableCapacity() < 0) { vo.setAvailableCapacity(0); } dbf.getEntityManager().merge(vo); TakePrimaryStorageCapacityReply reply = new TakePrimaryStorageCapacityReply(); bus.reply(msg, reply); }
@Transactional private void updateCapacity(long total, long avail) { PrimaryStorageCapacityVO cvo = dbf.getEntityManager() .find(PrimaryStorageCapacityVO.class, self.getUuid(), LockModeType.PESSIMISTIC_WRITE); DebugUtils.Assert( cvo != null, String.format("how can there is no PrimaryStorageCapacityVO[uuid:%s]", self.getUuid())); cvo.setTotalPhysicalCapacity(total); cvo.setAvailablePhysicalCapacity(avail); dbf.getEntityManager().merge(cvo); }
@Transactional public void updatePhysicalCapacityByKvmAgentResponse( String psUuid, String hostUuid, AgentResponse rsp) { LocalStorageHostRefVO ref = dbf.getEntityManager() .find(LocalStorageHostRefVO.class, hostUuid, LockModeType.PESSIMISTIC_WRITE); if (ref == null) { return; } if (ref.getAvailablePhysicalCapacity() == rsp.getAvailableCapacity() && ref.getTotalPhysicalCapacity() == rsp.getTotalCapacity()) { return; } long originalPhysicalTotal = ref.getTotalPhysicalCapacity(); long originalPhysicalAvailable = ref.getAvailablePhysicalCapacity(); ref.setTotalPhysicalCapacity(rsp.getTotalCapacity()); ref.setAvailablePhysicalCapacity(rsp.getAvailableCapacity()); dbf.getEntityManager().merge(ref); if (logger.isTraceEnabled()) { logger.trace( String.format( "[Local Storage Capacity] changed the physical capacity of the host[uuid:%s] of " + "the local primary storage[uuid:%s] as:\n" + "physical total: %s --> %s\n" + "physical available: %s --> %s\n", hostUuid, psUuid, originalPhysicalTotal, ref.getTotalPhysicalCapacity(), originalPhysicalAvailable, ref.getAvailablePhysicalCapacity())); } final long totalChange = rsp.getTotalCapacity() - ref.getTotalPhysicalCapacity(); final long availChange = rsp.getAvailableCapacity() - ref.getAvailablePhysicalCapacity(); new PrimaryStorageCapacityUpdater(psUuid) .run( new PrimaryStorageCapacityUpdaterRunnable() { @Override public PrimaryStorageCapacityVO call(PrimaryStorageCapacityVO cap) { cap.setTotalPhysicalCapacity(cap.getTotalPhysicalCapacity() + totalChange); cap.setAvailablePhysicalCapacity(cap.getAvailablePhysicalCapacity() + availChange); return cap; } }); }
@Transactional private void handle(ReturnBackupStorageMsg msg) { self = dbf.getEntityManager() .find(BackupStorageVO.class, self.getUuid(), LockModeType.PESSIMISTIC_WRITE); long availSize = self.getAvailableCapacity() + msg.getSize(); if (availSize > self.getTotalCapacity()) { availSize = self.getTotalCapacity(); } self.setAvailableCapacity(availSize); dbf.getEntityManager().merge(self); bus.reply(msg, new ReturnBackupStorageReply()); }
@Transactional private void handle(PrimaryStorageReportCapacityMsg msg) { PrimaryStorageCapacityVO vo = dbf.getEntityManager() .find(PrimaryStorageCapacityVO.class, self.getUuid(), LockModeType.PESSIMISTIC_WRITE); if (vo.getTotalCapacity() == 0) { vo.setTotalCapacity(msg.getTotalCapacity()); vo.setAvailableCapacity(msg.getAvailableCapacity()); dbf.getEntityManager().merge(vo); } PrimaryStorageReportCapacityReply reply = new PrimaryStorageReportCapacityReply(); bus.reply(msg, reply); }
private void handle(APIAttachNetworkServiceProviderToL2NetworkMsg msg) { NetworkServiceProviderVO vo = dbf.findByUuid(msg.getNetworkServiceProviderUuid(), NetworkServiceProviderVO.class); NetworkServiceProviderFactory factory = getProviderFactory(vo.getType()); NetworkServiceProvider provider = factory.getNetworkServiceProvider(vo); L2NetworkVO l2vo = dbf.findByUuid(msg.getL2NetworkUuid(), L2NetworkVO.class); APIAttachNetworkServiceProviderToL2NetworkEvent evt = new APIAttachNetworkServiceProviderToL2NetworkEvent(msg.getId()); try { provider.attachToL2Network(L2NetworkInventory.valueOf(l2vo), msg); } catch (NetworkException e) { String err = String.format( "unable to attach network service provider[uuid:%s, name:%s, type:%s] to l2network[uuid:%s, name:%s, type:%s], %s", vo.getUuid(), vo.getName(), vo.getType(), l2vo.getUuid(), l2vo.getName(), l2vo.getType(), e.getMessage()); logger.warn(err, e); evt.setErrorCode( errf.instantiateErrorCode( NetworkServiceErrors.ATTACH_NETWORK_SERVICE_PROVIDER_ERROR, err)); bus.publish(evt); return; } NetworkServiceProviderL2NetworkRefVO ref = new NetworkServiceProviderL2NetworkRefVO(); ref.setL2NetworkUuid(l2vo.getUuid()); ref.setNetworkServiceProviderUuid(vo.getUuid()); dbf.persist(ref); vo = dbf.findByUuid(vo.getUuid(), NetworkServiceProviderVO.class); evt.setInventory(NetworkServiceProviderInventory.valueOf(vo)); String info = String.format( "successfully attach network service provider[uuid:%s, name:%s, type:%s] to l2network[uuid:%s, name:%s, type:%s]", vo.getUuid(), vo.getName(), vo.getType(), l2vo.getUuid(), l2vo.getName(), l2vo.getType()); logger.debug(info); bus.publish(evt); }
@Override @Transactional(readOnly = true) public List<VmInstanceVO> returnAttachableVms( VolumeInventory vol, List<VmInstanceVO> candidates) { String sql = "select ref.hostUuid from LocalStorageResourceRefVO ref where ref.resourceUuid = :uuid" + " and ref.resourceType = :rtype"; TypedQuery<String> q = dbf.getEntityManager().createQuery(sql, String.class); q.setParameter("uuid", vol.getUuid()); q.setParameter("rtype", VolumeVO.class.getSimpleName()); List<String> ret = q.getResultList(); if (ret.isEmpty()) { return candidates; } String hostUuid = ret.get(0); List<String> vmRootVolumeUuids = CollectionUtils.transformToList( candidates, new Function<String, VmInstanceVO>() { @Override public String call(VmInstanceVO arg) { return arg.getRootVolumeUuid(); } }); sql = "select ref.resourceUuid from LocalStorageResourceRefVO ref where ref.hostUuid = :huuid" + " and ref.resourceUuid in (:rootVolumeUuids) and ref.resourceType = :rtype"; q = dbf.getEntityManager().createQuery(sql, String.class); q.setParameter("huuid", hostUuid); q.setParameter("rootVolumeUuids", vmRootVolumeUuids); q.setParameter("rtype", VolumeVO.class.getSimpleName()); final List<String> toInclude = q.getResultList(); candidates = CollectionUtils.transformToList( candidates, new Function<VmInstanceVO, VmInstanceVO>() { @Override public VmInstanceVO call(VmInstanceVO arg) { return toInclude.contains(arg.getRootVolumeUuid()) ? arg : null; } }); return candidates; }
protected void updateCapacity(Long totalCapacity, Long availableCapacity) { if (totalCapacity != null && availableCapacity != null) { self.setTotalCapacity(totalCapacity); self.setAvailableCapacity(availableCapacity); dbf.update(self); } }
@Test public void test() throws ApiSenderException, InterruptedException { long requiredSize = SizeUnit.GIGABYTE.toByte(10); SimulatorPrimaryStorageDetails sp = new SimulatorPrimaryStorageDetails(); sp.setTotalCapacity(SizeUnit.TERABYTE.toByte(10)); sp.setAvailableCapacity(sp.getTotalCapacity()); sp.setUrl("nfs://simulator/primary/"); PrimaryStorageInventory pinv = api.createSimulatoPrimaryStorage(1, sp).get(0); ZoneInventory zone = api.createZones(1).get(0); ClusterInventory cluster = api.createClusters(1, zone.getUuid()).get(0); HostInventory host = api.createHost(1, cluster.getUuid()).get(0); api.attachPrimaryStorage(cluster.getUuid(), pinv.getUuid()); AllocatePrimaryStorageMsg msg = new AllocatePrimaryStorageMsg(); msg.setRequiredHostUuid(host.getUuid()); msg.setSize(requiredSize); msg.setServiceId(bus.makeLocalServiceId(PrimaryStorageConstant.SERVICE_ID)); MessageReply reply = bus.call(msg); Assert.assertEquals(AllocatePrimaryStorageReply.class, reply.getClass()); AllocatePrimaryStorageReply ar = (AllocatePrimaryStorageReply) reply; Assert.assertEquals(pinv.getUuid(), ar.getPrimaryStorageInventory().getUuid()); ReturnPrimaryStorageCapacityMsg rmsg = new ReturnPrimaryStorageCapacityMsg(); rmsg.setDiskSize(requiredSize); rmsg.setPrimaryStorageUuid(pinv.getUuid()); rmsg.setServiceId(bus.makeLocalServiceId(PrimaryStorageConstant.SERVICE_ID)); bus.send(rmsg); Thread.sleep(2000); PrimaryStorageVO pvo = dbf.findByUuid(pinv.getUuid(), PrimaryStorageVO.class); Assert.assertEquals( pvo.getCapacity().getTotalCapacity(), pvo.getCapacity().getAvailableCapacity()); }
@Override @Transactional( readOnly = true, noRollbackForClassName = {"org.zstack.header.errorcode.OperationFailureException"}) public void preVmMigration(VmInstanceInventory vm) { List<String> volUuids = CollectionUtils.transformToList( vm.getAllVolumes(), new Function<String, VolumeInventory>() { @Override public String call(VolumeInventory arg) { return arg.getUuid(); } }); String sql = "select count(ps) from PrimaryStorageVO ps, VolumeVO vol where ps.uuid = vol.primaryStorageUuid and" + " vol.uuid in (:volUuids) and ps.type = :ptype"; TypedQuery<Long> q = dbf.getEntityManager().createQuery(sql, Long.class); q.setParameter("volUuids", volUuids); q.setParameter("ptype", LocalStorageConstants.LOCAL_STORAGE_TYPE); q.setMaxResults(1); Long count = q.getSingleResult(); if (count > 0) { throw new OperationFailureException( errf.stringToOperationError( String.format( "unable to live migrate with local storage. The vm[uuid:%s] has volumes on local storage," + "to protect your data, please stop the vm and do the volume migration", vm.getUuid()))); } }
private void checkNic(VmInstanceVO vm, List<DhcpInfo> info) { VmNicVO nic = vm.getVmNics().iterator().next(); DhcpInfo target = null; for (DhcpInfo i : info) { if (i.mac.equals(nic.getMac())) { target = i; break; } } Assert.assertNotNull(target); Assert.assertEquals(nic.getIp(), target.ip); Assert.assertEquals(nic.getNetmask(), target.netmask); Assert.assertEquals(nic.getGateway(), target.gateway); Assert.assertEquals(true, target.isDefaultL3Network); Assert.assertEquals( VmSystemTags.HOSTNAME.getTokenByResourceUuid(vm.getUuid(), VmSystemTags.HOSTNAME_TOKEN), target.hostname); L3NetworkVO l3 = dbf.findByUuid(nic.getL3NetworkUuid(), L3NetworkVO.class); Assert.assertEquals(l3.getDnsDomain(), target.dnsDomain); Assert.assertNotNull(target.dns); List<String> dns = CollectionUtils.transformToList( l3.getDns(), new Function<String, L3NetworkDnsVO>() { @Override public String call(L3NetworkDnsVO arg) { return arg.getDns(); } }); Assert.assertTrue(dns.containsAll(target.dns)); Assert.assertTrue(target.dns.containsAll(dns)); }
@Override public PrimaryStorageInventory createPrimaryStorage( PrimaryStorageVO vo, APIAddPrimaryStorageMsg msg) { vo.setMountPath(msg.getUrl()); vo = dbf.persistAndRefresh(vo); return PrimaryStorageInventory.valueOf(vo); }
@Test public void test() throws ApiSenderException, InterruptedException { final BackupStorageInventory sftp = deployer.backupStorages.get("sftp"); final ImageInventory img = new ImageInventory(); img.setName("image"); img.setPlatform(ImagePlatform.Linux.toString()); img.setMediaType(ImageMediaType.RootVolumeTemplate.toString()); img.setFormat("qcow2"); img.setUrl("http://test.img"); for (int i = 0; i < num; i++) { createVm(img, sftp.getUuid()); } latch.await(5, TimeUnit.MINUTES); Assert.assertEquals(num, vms.size()); fconfig.applyDhcpCmdList.clear(); HostInventory host = deployer.hosts.get("host1"); api.reconnectHost(host.getUuid()); Assert.assertFalse(fconfig.applyDhcpCmdList.isEmpty()); ApplyDhcpCmd cmd = fconfig.applyDhcpCmdList.get(0); Assert.assertEquals(num + 1, cmd.dhcp.size()); List<VmInstanceVO> vms = dbf.listAll(VmInstanceVO.class); for (VmInstanceVO vm : vms) { checkNic(vm, cmd.dhcp); } }
@Test public void test() throws ApiSenderException { EipInventory eip = deployer.eips.get("eip"); VmInstanceInventory vm = deployer.vms.get("TestVm"); VmNicInventory nic = vm.getVmNics().get(0); IpRangeInventory ipr = deployer.ipRanges.get("GuestIpRange"); String newIp = null; long s = NetworkUtils.ipv4StringToLong(ipr.getStartIp()); long e = NetworkUtils.ipv4StringToLong(ipr.getEndIp()); for (long l = s; s < e; s++) { String ip = NetworkUtils.longToIpv4String(l); if (!ip.equals(nic.getIp())) { newIp = ip; break; } } Assert.assertNotNull(newIp); api.stopVmInstance(vm.getUuid()); api.setStaticIp(vm.getUuid(), nic.getL3NetworkUuid(), newIp); EipVO eipvo = dbf.findByUuid(eip.getUuid(), EipVO.class); Assert.assertEquals(newIp, eipvo.getGuestIp()); }
private void fullSnapshot(VolumeSnapshotInventory inv, int distance) { Assert.assertEquals(VolumeSnapshotState.Enabled.toString(), inv.getState()); Assert.assertEquals(VolumeSnapshotStatus.Ready.toString(), inv.getStatus()); VolumeVO vol = dbf.findByUuid(inv.getVolumeUuid(), VolumeVO.class); VolumeSnapshotVO svo = dbf.findByUuid(inv.getUuid(), VolumeSnapshotVO.class); Assert.assertNotNull(svo); Assert.assertTrue(svo.isFullSnapshot()); Assert.assertTrue(svo.isLatest()); Assert.assertNull(svo.getParentUuid()); Assert.assertEquals(distance, svo.getDistance()); Assert.assertEquals(vol.getPrimaryStorageUuid(), svo.getPrimaryStorageUuid()); Assert.assertNotNull(svo.getPrimaryStorageInstallPath()); VolumeSnapshotTreeVO cvo = dbf.findByUuid(svo.getTreeUuid(), VolumeSnapshotTreeVO.class); Assert.assertNotNull(cvo); Assert.assertTrue(cvo.isCurrent()); }
@Transactional private void batchWrite(List lst) { for (Object obj : lst) { LogVO vo = (LogVO) obj; dbf.getEntityManager().persist(vo); } }
private void handle(final APIAttachDataVolumeToVmMsg msg) { self.setVmInstanceUuid(msg.getVmInstanceUuid()); self = dbf.updateAndRefresh(self); AttachDataVolumeToVmMsg amsg = new AttachDataVolumeToVmMsg(); amsg.setVolume(getSelfInventory()); amsg.setVmInstanceUuid(msg.getVmInstanceUuid()); bus.makeTargetServiceIdByResourceUuid( amsg, VmInstanceConstant.SERVICE_ID, amsg.getVmInstanceUuid()); bus.send( amsg, new CloudBusCallBack(msg) { @Override public void run(MessageReply reply) { final APIAttachDataVolumeToVmEvent evt = new APIAttachDataVolumeToVmEvent(msg.getId()); self = dbf.reload(self); if (reply.isSuccess()) { AttachDataVolumeToVmReply ar = reply.castReply(); self.setVmInstanceUuid(msg.getVmInstanceUuid()); self.setFormat( VolumeFormat.getVolumeFormatByMasterHypervisorType(ar.getHypervisorType()) .toString()); self = dbf.updateAndRefresh(self); evt.setInventory(getSelfInventory()); } else { self.setVmInstanceUuid(null); dbf.update(self); evt.setErrorCode(reply.getError()); } bus.publish(evt); } }); }
private void handle(final ConnectPrimaryStorageMsg msg) { final ConnectPrimaryStorageReply reply = new ConnectPrimaryStorageReply(); self.setStatus(PrimaryStorageStatus.Connecting); self = dbf.updateAndRefresh(self); connectHook( msg, new Completion(msg) { @Override public void success() { self = dbf.reload(self); self.setStatus(PrimaryStorageStatus.Connected); self = dbf.updateAndRefresh(self); reply.setConnected(true); logger.debug( String.format("successfully connected primary storage[uuid:%s]", self.getUuid())); bus.reply(msg, reply); } @Override public void fail(ErrorCode errorCode) { if (msg.isNewAdded()) { reply.setError(errorCode); } else { self.setStatus(PrimaryStorageStatus.Disconnected); self = dbf.updateAndRefresh(self); logger.debug( String.format( "failed to connect primary storage[uuid:%s], %s", self.getUuid(), errorCode)); reply.setConnected(false); } bus.reply(msg, reply); } }); }
private void handle(final CreateDataVolumeTemplateFromDataVolumeMsg msg) { final CreateTemplateFromVolumeOnPrimaryStorageMsg cmsg = new CreateTemplateFromVolumeOnPrimaryStorageMsg(); cmsg.setBackupStorageUuid(msg.getBackupStorageUuid()); cmsg.setImageInventory( ImageInventory.valueOf(dbf.findByUuid(msg.getImageUuid(), ImageVO.class))); cmsg.setVolumeInventory(getSelfInventory()); bus.makeTargetServiceIdByResourceUuid( cmsg, PrimaryStorageConstant.SERVICE_ID, self.getPrimaryStorageUuid()); bus.send( cmsg, new CloudBusCallBack(msg) { @Override public void run(MessageReply r) { CreateDataVolumeTemplateFromDataVolumeReply reply = new CreateDataVolumeTemplateFromDataVolumeReply(); if (!r.isSuccess()) { reply.setError(r.getError()); } else { CreateTemplateFromVolumeOnPrimaryStorageReply creply = r.castReply(); String backupStorageInstallPath = creply.getTemplateBackupStorageInstallPath(); reply.setFormat(creply.getFormat()); reply.setInstallPath(backupStorageInstallPath); reply.setMd5sum(null); reply.setBackupStorageUuid(msg.getBackupStorageUuid()); } bus.reply(msg, reply); } }); }
public boolean hasTag(String resourceUuid, Class resourceClass) { SimpleQuery<SystemTagVO> q = dbf.createQuery(SystemTagVO.class); q.add(SystemTagVO_.resourceType, Op.EQ, resourceClass.getSimpleName()); q.add(SystemTagVO_.resourceUuid, Op.EQ, resourceUuid); q.add(SystemTagVO_.tag, useOp(), useTagFormat()); return q.isExists(); }
public List<String> getTags(String resourceUuid, Class resourceClass) { SimpleQuery<SystemTagVO> q = dbf.createQuery(SystemTagVO.class); q.select(SystemTagVO_.tag); q.add(SystemTagVO_.resourceType, Op.EQ, resourceClass.getSimpleName()); q.add(SystemTagVO_.resourceUuid, Op.EQ, resourceUuid); q.add(SystemTagVO_.tag, useOp(), useTagFormat()); return q.listValue(); }
@Test public void test() throws ApiSenderException { VmInstanceInventory vm = deployer.vms.get("TestVm"); api.stopVmInstance(vm.getUuid()); String rootVolumeUuid = vm.getRootVolumeUuid(); VolumeVO vol = dbf.findByUuid(rootVolumeUuid, VolumeVO.class); BackupStorageInventory sftp = deployer.backupStorages.get("sftp"); ImageInventory image = api.createTemplateFromRootVolume("testImage", rootVolumeUuid, (List) null); Assert.assertEquals(sftp.getUuid(), image.getBackupStorageRefs().get(0).getBackupStorageUuid()); Assert.assertEquals(ImageStatus.Ready.toString(), image.getStatus()); Assert.assertEquals(vol.getSize(), image.getSize()); Assert.assertEquals(String.format("volume://%s", vol.getUuid()), image.getUrl()); ImageVO ivo = dbf.findByUuid(image.getUuid(), ImageVO.class); Assert.assertNotNull(ivo); }