public static List<VmNicInventory> valueOf(Collection<VmNicVO> vos) { List<VmNicInventory> invs = new ArrayList<VmNicInventory>(vos.size()); for (VmNicVO vo : vos) { invs.add(VmNicInventory.valueOf(vo)); } return invs; }
@Override public VmNicInventory call(VmNicVO arg) { if (arg.getL3NetworkUuid().equals(l3uuid)) { return VmNicInventory.valueOf(arg); } return null; }
@Transactional private EipStruct eipVOtoEipStruct(EipVO vo) { String sql = "select nic from VmNicVO nic where nic.uuid = :uuid"; TypedQuery<VmNicVO> q = dbf.getEntityManager().createQuery(sql, VmNicVO.class); q.setParameter("uuid", vo.getVmNicUuid()); VmNicVO nicvo = q.getSingleResult(); sql = "select vip from VipVO vip where vip.uuid = :uuid"; TypedQuery<VipVO> vq = dbf.getEntityManager().createQuery(sql, VipVO.class); vq.setParameter("uuid", vo.getVipUuid()); VipVO vipvo = vq.getSingleResult(); UsedIpInventory guestIp = eipMgr.getEipGuestIp(vo.getUuid()); EipStruct struct = eipMgr.generateEipStruct(VmNicInventory.valueOf(nicvo), VipInventory.valueOf(vipvo), EipInventory.valueOf(vo), guestIp); struct.setSnatInboundTraffic(EipGlobalConfig.SNAT_INBOUND_TRAFFIC.value(Boolean.class)); return struct; }
private void handle(APIListVmNicMsg msg) { List<VmNicVO> vos = dbf.listByApiMessage(msg, VmNicVO.class); List<VmNicInventory> invs = VmNicInventory.valueOf(vos); APIListVmNicReply reply = new APIListVmNicReply(); reply.setInventories(invs); bus.reply(msg, reply); }
@Override protected void scripts() { List<String> guestNetworks = sql("select l3.uuid" + " from L3NetworkVO l3, NetworkServiceL3NetworkRefVO ref" + " where l3.uuid = ref.l3NetworkUuid" + " and ref.networkServiceType = :type") .param("type", LoadBalancerConstants.LB_NETWORK_SERVICE_TYPE_STRING) .list(); List<VmNicVO> nics = new ArrayList<>(); if (guestNetworks != null && !guestNetworks.isEmpty()) { nics = sql("select nic" + " from VmNicVO nic, VmInstanceVO vm" + " where nic.l3NetworkUuid in (:guestNetworks)" + " and nic.vmInstanceUuid = vm.uuid" + " and vm.type = :vmType" + " and vm.state in (:vmStates)") .param("guestNetworks",guestNetworks) .param("vmType", VmInstanceConstant.USER_VM_TYPE) .param("vmStates", asList(VmInstanceState.Running, VmInstanceState.Stopped)) .list(); } reply.setInventories(callGetCandidateVmNicsForLoadBalancerExtensionPoint(msg, VmNicInventory.valueOf(nics))); } }.execute();
private LoadBalancerStruct makeStruct(LoadBalancerVO vo) { LoadBalancerStruct struct = new LoadBalancerStruct(); struct.setLb(LoadBalancerInventory.valueOf(vo)); List<String> activeNicUuids = new ArrayList<String>(); for (LoadBalancerListenerVO l : vo.getListeners()) { activeNicUuids.addAll(CollectionUtils.transformToList(l.getVmNicRefs(), new Function<String, LoadBalancerListenerVmNicRefVO>() { @Override public String call(LoadBalancerListenerVmNicRefVO arg) { return arg.getStatus() == LoadBalancerVmNicStatus.Active || arg.getStatus() == LoadBalancerVmNicStatus.Pending ? arg.getVmNicUuid() : null; } })); } if (activeNicUuids.isEmpty()) { struct.setVmNics(new HashMap<String, VmNicInventory>()); } else { SimpleQuery<VmNicVO> nq = dbf.createQuery(VmNicVO.class); nq.add(VmNicVO_.uuid, Op.IN, activeNicUuids); List<VmNicVO> nicvos = nq.list(); Map<String, VmNicInventory> m = new HashMap<String, VmNicInventory>(); for (VmNicVO n : nicvos) { m.put(n.getUuid(), VmNicInventory.valueOf(n)); } struct.setVmNics(m); } struct.setListeners(LoadBalancerListenerInventory.valueOf(vo.getListeners())); return struct; }
private LoadBalancerStruct makeStruct() { LoadBalancerStruct struct = new LoadBalancerStruct(); struct.setLb(reloadAndGetInventory()); List<String> activeNicUuids = new ArrayList<String>(); for (LoadBalancerListenerVO l : self.getListeners()) { activeNicUuids.addAll(CollectionUtils.transformToList(l.getVmNicRefs(), new Function<String, LoadBalancerListenerVmNicRefVO>() { @Override public String call(LoadBalancerListenerVmNicRefVO arg) { return arg.getStatus() == LoadBalancerVmNicStatus.Active || arg.getStatus() == LoadBalancerVmNicStatus.Pending ? arg.getVmNicUuid() : null; } })); } if (activeNicUuids.isEmpty()) { struct.setVmNics(new HashMap<String, VmNicInventory>()); } else { SimpleQuery<VmNicVO> nq = dbf.createQuery(VmNicVO.class); nq.add(VmNicVO_.uuid, Op.IN, activeNicUuids); List<VmNicVO> nicvos = nq.list(); Map<String, VmNicInventory> m = new HashMap<String, VmNicInventory>(); for (VmNicVO n : nicvos) { m.put(n.getUuid(), VmNicInventory.valueOf(n)); } struct.setVmNics(m); } struct.setListeners(LoadBalancerListenerInventory.valueOf(self.getListeners())); return struct; }
private void handle(APIGetCandidateVmNicForSecurityGroupMsg msg) { APIGetCandidateVmNicForSecurityGroupReply reply = new APIGetCandidateVmNicForSecurityGroupReply(); reply.setInventories(VmNicInventory.valueOf(getCandidateVmNic(msg.getSecurityGroupUuid(), msg.getSession().getAccountUuid()))); bus.reply(msg, reply); }
private void handle(final AttachL3NetworkToVmNicMsg msg) { final AttachL3NetworkToVmNicReply reply = new AttachL3NetworkToVmNicReply(); VmNicVO vmNicVO = Q.New(VmNicVO.class).eq(VmNicVO_.uuid, msg.getVmNicUuid()).find(); doAttachL3ToNic(VmNicInventory.valueOf(vmNicVO), msg.getL3NetworkUuid(), false, new Completion(msg) { @Override public void success() { bus.reply(msg, reply); } @Override public void fail(ErrorCode errorCode) { reply.setError(errorCode); bus.reply(msg, reply); } }); }
private void removeNics(List<String> listenerUuids, final List<String> vmNicUuids, final Completion completion) { SimpleQuery<VmNicVO> q = dbf.createQuery(VmNicVO.class); q.add(VmNicVO_.uuid, Op.IN, vmNicUuids); List<VmNicVO> vos = q.list(); List<VmNicInventory> nics = VmNicInventory.valueOf(vos); LoadBalancerBackend bkd = getBackend(); bkd.removeVmNics(removeNicStruct(listenerUuids, vmNicUuids), nics, new Completion(completion) { @Override public void success() { UpdateQuery.New(LoadBalancerListenerVmNicRefVO.class) .condAnd(LoadBalancerListenerVmNicRefVO_.vmNicUuid, Op.IN, vmNicUuids) .condAnd(LoadBalancerListenerVmNicRefVO_.listenerUuid, Op.IN, listenerUuids) .delete(); completion.success(); } @Override public void fail(ErrorCode errorCode) { completion.fail(errorCode); } }); }
private void handle(final APIDeleteVmNicMsg msg) { APIDeleteVmNicEvent evt = new APIDeleteVmNicEvent(msg.getId()); VmNicVO nicVO = Q.New(VmNicVO.class).eq(VmNicVO_.uuid, msg.getUuid()).find(); doDeleteVmNic(VmNicInventory.valueOf(nicVO), new Completion(msg) { @Override public void success() { bus.publish(evt); } @Override public void fail(ErrorCode errorCode) { evt.setError(errorCode); bus.publish(evt); } }); }
private void updateNic(VmUpdateNicOnHypervisorMsg msg, NoErrorCompletion completion) { checkStateAndStatus(); final VmUpdateNicOnHypervisorReply reply = new VmUpdateNicOnHypervisorReply(); List<VmNicVO> nics = Q.New(VmNicVO.class).eq(VmNicVO_.vmInstanceUuid, msg.getVmInstanceUuid()).list(); UpdateNicCmd cmd = new UpdateNicCmd(); cmd.setVmInstanceUuid(msg.getVmInstanceUuid()); cmd.setNics(VmNicInventory.valueOf(nics).stream().map(this::completeNicInfo).collect(Collectors.toList())); KVMHostInventory inv = (KVMHostInventory) getSelfInventory(); for (KVMPreUpdateNicExtensionPoint ext : pluginRgty.getExtensionList(KVMPreUpdateNicExtensionPoint.class)) { ext.preUpdateNic(inv, cmd); } new Http<>(updateNicPath, cmd, AttachNicResponse.class).call(new ReturnValueCompletion<AttachNicResponse>(msg, completion) { @Override public void success(AttachNicResponse ret) { if (!ret.isSuccess()) { reply.setError(operr("failed to update nic[vm:%s] on kvm host[uuid:%s, ip:%s]," + "because %s", msg.getVmInstanceUuid(), self.getUuid(), self.getManagementIp(), ret.getError())); } bus.reply(msg, reply); completion.done(); } @Override public void fail(ErrorCode errorCode) { reply.setError(errorCode); bus.reply(msg, reply); completion.done(); } }); }
@Override public void prepareDbInitialValue() { List<VmNicVO> nics = Q.New(VmNicVO.class).notNull(VmNicVO_.vmInstanceUuid).list(); for (VmNicVO nic : nics) { if (nic.getUsedIps().size() <= 1) { continue; } /* systemTags for dualStack nic existed */ List<String> secondaryL3Uuids = new DualStackNicSecondaryNetworksOperator().getSecondaryNetworksByVmUuidNic(nic.getVmInstanceUuid(), nic.getL3NetworkUuid()); if (secondaryL3Uuids != null) { continue; } for (UsedIpVO ip : nic.getUsedIps()) { if (ip.getL3NetworkUuid().equals(nic.getL3NetworkUuid())) { continue; } new DualStackNicSecondaryNetworksOperator().createSecondaryNetworksByVmNic(VmNicInventory.valueOf(nic), ip.getL3NetworkUuid()); } } } }
@Override public void run(MessageReply reply) { for (VmNicExtensionPoint ext : pluginRgty.getExtensionList(VmNicExtensionPoint.class)) { ext.afterDelIpAddress(nic.getUuid(), ipInventory.getUuid()); } VmNicInventory nicInv = VmNicInventory.valueOf(dbf.findByUuid(nic.getUuid(), VmNicVO.class)); new DualStackNicSecondaryNetworksOperator().deleteSecondaryNetworksByVmNic(nicInv, l3.getUuid()); chain.rollback(); } });
@Override public void run(final FlowTrigger trigger, final Map data) { final VmNicInventory nic = (VmNicInventory) data.get(VmInstanceConstant.Params.VmNicInventory.toString()); if (nic.getVmInstanceUuid() == null) { trigger.next(); return; } VmInstanceVO vmVo = dbf.findByUuid(nic.getVmInstanceUuid(), VmInstanceVO.class); VmNicInventory vmNic = VmNicInventory.valueOf(dbf.findByUuid(nic.getUuid(), VmNicVO.class)); /* if this is default nic and vm default l3 is different from nic l3 */ if (vmVo.getDefaultL3NetworkUuid() == null || VmNicHelper.isDefaultNic(nic, VmInstanceInventory.valueOf(vmVo))) { String primaryL3 = VmNicHelper.getPrimaryL3Uuid(vmNic); if (vmVo.getDefaultL3NetworkUuid() == null || !vmVo.getDefaultL3NetworkUuid().equals(primaryL3)) { vmVo.setDefaultL3NetworkUuid(primaryL3); dbf.updateAndRefresh(vmVo); } } trigger.next(); } }
@Override public void rollback(final FlowRollback chain, Map data) { VmInstanceSpec spec = (VmInstanceSpec) data.get(VmInstanceConstant.Params.VmInstanceSpec.toString()); List<DetachIpAddressFromVmNicMsg> msgs = new ArrayList<>(); /* reload vmnic */ List<VmNicVO> vmNicVOS = Q.New(VmNicVO.class).eq(VmNicVO_.vmInstanceUuid, spec.getVmInventory().getUuid()).list(); for (VmNicVO nic : vmNicVOS) { List<String> ipUuids = VmNicHelper.getCanDetachL3List(VmNicInventory.valueOf(nic), spec.getL3Networks()); for (String uuid : ipUuids) { DetachIpAddressFromVmNicMsg msg = new DetachIpAddressFromVmNicMsg(); msg.setVmNicUuid(nic.getUuid()); msg.setUsedIpUuid(uuid); bus.makeTargetServiceIdByResourceUuid(msg, VmInstanceConstant.SERVICE_ID, nic.getUuid()); msgs.add(msg); } } new While<>(msgs).each((msg, comp) -> bus.send(msg, new CloudBusCallBack(comp) { @Override public void run(MessageReply reply) { comp.done(); } })).run(new NoErrorCompletion(chain) { @Override public void done() { chain.rollback(); } }); } }
@Override public void run(MessageReply reply) { if (reply.isSuccess()) { AllocateIpReply areply = reply.castReply(); UsedIpInventory ipInventory = areply.getIpInventory(); for (VmNicExtensionPoint ext : pluginRgty.getExtensionList(VmNicExtensionPoint.class)) { ext.afterAddIpAddress(vmNicVO.getUuid(), ipInventory.getUuid()); } data.put(VmInstanceConstant.Params.UsedIPInventory.toString(), ipInventory); VmNicInventory nicInv = VmNicInventory.valueOf(dbf.findByUuid(nic.getUuid(), VmNicVO.class)); new DualStackNicSecondaryNetworksOperator().createSecondaryNetworksByVmNic(nicInv, l3.getUuid()); trigger.next(); } else { trigger.fail(reply.getError()); } } });
private void handle(final APIAttachL3NetworkToVmNicMsg msg) { final APIAttachL3NetworkToVmNicEvent evt = new APIAttachL3NetworkToVmNicEvent(msg.getId()); VmNicVO vmNicVO = Q.New(VmNicVO.class).eq(VmNicVO_.uuid, msg.getVmNicUuid()).find(); if (msg.getStaticIp() != null) { new StaticIpOperator().setStaticIp(vmNicVO.getVmInstanceUuid(), msg.getL3NetworkUuid(), msg.getStaticIp()); } doAttachL3ToNic(VmNicInventory.valueOf(vmNicVO), msg.getL3NetworkUuid(), true, new Completion(msg) { @Override public void success() { VmNicVO vmNicVO = Q.New(VmNicVO.class).eq(VmNicVO_.uuid, msg.getVmNicUuid()).find(); evt.setInventory(VmNicInventory.valueOf(vmNicVO)); bus.publish(evt); } @Override public void fail(ErrorCode errorCode) { new StaticIpOperator().deleteStaticIpByVmUuidAndL3Uuid(vmNicVO.getVmInstanceUuid(), msg.getL3NetworkUuid()); evt.setError(errorCode); bus.publish(evt); } }); }
@Override public void run(FlowTrigger chain, Map data) { DesignatedAllocateHostMsg msg = new DesignatedAllocateHostMsg(); msg.setCpuCapacity(cpuNum - oldCpuNum); msg.setMemoryCapacity(struct.alignedMemory - oldMemorySize); msg.setAllocatorStrategy(HostAllocatorConstant.DESIGNATED_HOST_ALLOCATOR_STRATEGY_TYPE); msg.setVmInstance(VmInstanceInventory.valueOf(self)); if (self.getImageUuid() != null && dbf.findByUuid(self.getImageUuid(), ImageVO.class) != null) { msg.setImage(ImageInventory.valueOf(dbf.findByUuid(self.getImageUuid(), ImageVO.class))); } msg.setHostUuid(self.getHostUuid()); msg.setFullAllocate(false); msg.setL3NetworkUuids(VmNicHelper.getL3Uuids(VmNicInventory.valueOf(self.getVmNics()))); msg.setServiceId(bus.makeLocalServiceId(HostAllocatorConstant.SERVICE_ID)); bus.send(msg, new CloudBusCallBack(chain) { @Override public void run(MessageReply reply) { if (!reply.isSuccess()) { ErrorCode err = operr("host[uuid:%s] capacity is not enough to offer cpu[%s], memory[%s bytes]", self.getHostUuid(), cpuNum - oldCpuNum, struct.alignedMemory - oldMemorySize); err.setCause(reply.getError()); chain.fail(err); } else { result = true; logger.debug(String.format("reserve memory %s bytes and cpu %s on host[uuid:%s]", memorySize - self.getMemorySize(), cpuNum - self.getCpuNum(), self.getHostUuid())); chain.next(); } } }); }
protected VmInstanceInventory(VmInstanceVO vo) { this.setUuid(vo.getUuid()); this.setName(vo.getName()); this.setDescription(vo.getDescription()); this.setZoneUuid(vo.getZoneUuid()); this.setClusterUuid(vo.getClusterUuid()); this.setImageUuid(vo.getImageUuid()); this.setHostUuid(vo.getHostUuid()); this.setLastHostUuid(vo.getLastHostUuid()); this.setInstanceOfferingUuid(vo.getInstanceOfferingUuid()); this.setType(vo.getType()); this.setHypervisorType(vo.getHypervisorType()); this.setCreateDate(vo.getCreateDate()); this.setLastOpDate(vo.getLastOpDate()); this.setState(vo.getState().toString()); this.setRootVolumeUuid(vo.getRootVolumeUuid()); this.setAllVolumes(VolumeInventory.valueOf(vo.getAllVolumes())); this.setVmNics(VmNicInventory.valueOf(vo.getVmNics())); this.setVmCdRoms(VmCdRomInventory.valueOf(vo.getVmCdRoms())); this.setInternalId(vo.getInternalId()); this.setDefaultL3NetworkUuid(vo.getDefaultL3NetworkUuid()); this.setCpuNum(vo.getCpuNum()); this.setCpuSpeed(vo.getCpuSpeed()); this.setMemorySize(vo.getMemorySize()); this.setAllocatorStrategy(vo.getAllocatorStrategy()); this.setPlatform(vo.getPlatform()); }