public static VipInventory valueOf(VipVO vo) { VipInventory inv = new VipInventory(); inv.setName(vo.getName()); inv.setDescription(vo.getDescription()); inv.setCreateDate(vo.getCreateDate()); inv.setGateway(vo.getGateway()); inv.setIp(vo.getIp()); inv.setIpRangeUuid(vo.getIpRangeUuid()); inv.setServiceProvider(vo.getServiceProvider()); inv.setL3NetworkUuid(vo.getL3NetworkUuid()); inv.setLastOpDate(vo.getLastOpDate()); inv.setNetmask(vo.getNetmask()); inv.setPrefixLen(vo.getPrefixLen()); inv.setUseFor(vo.getUseFor()); inv.setUuid(vo.getUuid()); inv.setState(vo.getState().toString()); inv.setUsedIpUuid(vo.getUsedIpUuid()); if (vo.getPeerL3NetworkRefs() != null && !vo.getPeerL3NetworkRefs().isEmpty()) { inv.setPeerL3NetworkUuids(vo.getPeerL3NetworkRefs().stream() .map(ref -> ref.getL3NetworkUuid()) .collect(Collectors.toList())); } return inv; }
@Override public void run(final FlowTrigger trigger, final Map data) { DebugUtils.Assert(self.getPeerL3NetworkUuids() != null, "peerL3NetworkUuid cannot be null"); s.setL3Network(L3NetworkInventory.valueOf(dbf.findByUuid(self.getPeerL3NetworkUuids().iterator().next(), L3NetworkVO.class))); if (self.getServiceProvider().equals(VirtualRouterConstant.VIRTUAL_ROUTER_PROVIDER_TYPE)) { appVmType = VirtualRouterConstant.VIRTUAL_ROUTER_VM_TYPE; } else if (self.getServiceProvider().equals(VyosConstants.VYOS_ROUTER_PROVIDER_TYPE)) { appVmType = VyosConstants.VYOS_VM_TYPE; } else { throw new CloudRuntimeException(String.format("unknown network service provider type[%s]", self.getServiceProvider())); s.setProviderType(self.getServiceProvider()); s.setOfferingValidator(offering -> { if (!offering.getPublicNetworkUuid().equals(self.getL3NetworkUuid())) { throw new OperationFailureException(operr("found a virtual router offering[uuid:%s] for L3Network[uuid:%s] in zone[uuid:%s]; however, the network's public network[uuid:%s] is not the same to VIP[uuid:%s]'s; you may need to use system tag" + " guestL3Network::l3NetworkUuid to specify a particular virtual router offering for the L3Network", offering.getUuid(), s.getL3Network().getUuid(), s.getL3Network().getZoneUuid(), self.getL3NetworkUuid(), self.getUuid()));
@Override public void fail(ErrorCode errorCode) { logger.warn(String.format("failed to release vip[uuid:%s, name:%s, ip:%s] on service[%s], its garbage collector should" + " handle this", self.getUuid(), self.getName(), self.getIp(), self.getServiceProvider())); completion.fail(errorCode); } });
@Override public void success() { logger.debug(String.format("successfully released vip[uuid:%s, name:%s, ip:%s] on service[%s]", self.getUuid(), self.getName(), self.getIp(), self.getServiceProvider())); VipUseForList useForList = new VipUseForList(self.getUseFor()); self.setUseFor(null); clearPeerL3Network(); self.setServiceProvider(null); dbf.update(self); completion.success(); }
@Transactional(readOnly = true) private void checkIfVmAlreadyHasVipNetwork(String vmUuid, VipVO vip) { String sql = "select count(*) from VmNicVO nic, VmInstanceVO vm where nic.vmInstanceUuid = vm.uuid" + " and vm.uuid = :vmUuid and nic.l3NetworkUuid = :vipL3Uuid"; TypedQuery<Long> q = dbf.getEntityManager().createQuery(sql, Long.class); q.setParameter("vmUuid", vmUuid); q.setParameter("vipL3Uuid", vip.getL3NetworkUuid()); Long c = q.getSingleResult(); if (c > 0) { throw new ApiMessageInterceptionException(argerr("the vm[uuid:%s] that the EIP is about to attach is already on the public network[uuid:%s] from which" + " the vip[uuid:%s, name:%s, ip:%s] comes", vmUuid, vip.getL3NetworkUuid(), vip.getUuid(), vip.getName(), vip.getIp())); } }
List<VirtualRouterVipVO> refs = new ArrayList<>(); for (VipVO vip : vips) { if (vipTOS.stream().anyMatch(v -> v.getIp().equals(vip.getIp()))) { logger.warn(String.format( "found duplicate vip ip[uuid; %s, uuids: %s] for vr[uuid: %s]", vip.getIp(), vips.stream(). filter(v -> v.getIp().equals(vip.getIp())) .map(v -> v.getUuid()) .collect(Collectors.toSet()), nic.getVmInstanceUuid())); to.setIp(vip.getIp()); to.setGateway(vip.getGateway()); to.setNetmask(vip.getNetmask()); to.setOwnerEthernetMac(vr.getVmNics().stream() .filter(n -> n.getL3NetworkUuid().equals(vip.getL3NetworkUuid())) .findFirst().get().getMac()); to.setVipUuid(vip.getUuid()); vipTOS.add(to); .eq(VirtualRouterVipVO_.uuid, vip.getUuid()) .isExists()) { VirtualRouterVipVO vo = new VirtualRouterVipVO(); vo.setUuid(vip.getUuid()); vo.setVirtualRouterVmUuid(nic.getVmInstanceUuid()); refs.add(vo);
VipVO vipvo = new VipVO(); vipvo.setUuid(Platform.getUuid()); vipvo.setName(String.format("vip-for-%s", vr.getName())); vipvo.setDescription("Vip backend created for virtual router"); vipvo.setState(VipState.Enabled); vipvo.setGateway(nic.getGateway()); vipvo.setIp(nic.getIp()); vipvo.setIpRangeUuid(ips.getUuid()); vipvo.setL3NetworkUuid(nic.getL3NetworkUuid()); vipvo.setNetmask(nic.getNetmask()); vipvo.setUsedIpUuid(nic.getUsedIpUuid()); vipvo.setUseFor(VirtualRouterConstant.SNAT_NETWORK_SERVICE_TYPE); vipvo.setAccountUuid(accountUuid); if(!vr.getGuestL3Networks().isEmpty()){ String peerL3network = vr.getGuestL3Networks().get(0); NetworkServiceProviderType providerType = nwServiceMgr.getTypeOfNetworkServiceProviderForService(peerL3network, NetworkServiceType.SNAT); vipvo.setServiceProvider(providerType.toString()); } catch (OperationFailureException e){ vipvo.setServiceProvider(null); vrvip.setUuid(vipvo.getUuid()); vrvip.setVirtualRouterVmUuid(vr.getUuid()); new SQLBatch(){ vo.setVipUuid(vipvo.getUuid()); vo.setL3NetworkUuid(vr.getGuestL3Networks().get(0)); dbf.persistAndRefresh(vo);
@Override public void success() { logger.debug(String.format("successfully acquired vip[uuid:%s, name:%s, ip:%s] on service[%s]", self.getUuid(), self.getName(), self.getIp(), s.getServiceProvider())); VipUseForList useForList = new VipUseForList(self.getUseFor()); useForList.add(s.getUseFor()); VipVO vo = Q.New(VipVO.class).eq(VipVO_.uuid, self.getUuid()).find(); vo.setUseFor(useForList.toString()); dbf.updateAndRefresh(vo); addPeerL3NetworkUuid(s.getPeerL3NetworkUuid()); completion.success(); }
@Override public void run(MessageReply reply) { if(!reply.isSuccess()){ logger.debug(String.format("VirtualRouter remove the vip[uuid %s] on the public interface failed.", vo.getUuid())); } completion.success(); } });
VipUseForList useForList = new VipUseForList(self.getUseFor()); if(useForList.isIncluded(VipUseForList.SNAT_NETWORK_SERVICE_TYPE)){ evt.setError(operr("Vip [uuid %s, ip %s] of router public interface can not be deleted", self.getUuid(), self.getIp())); bus.publish(evt); return; final List<VipInventory> ctx = Arrays.asList(VipInventory.valueOf(self)); FlowChain chain = FlowChainBuilder.newShareFlowChain(); chain.setName(String.format("delete-vip-%s", self.getUuid())); chain.then(new ShareFlow() { @Override
protected boolean releaseCheckModifyVipAttributeStruct( ModifyVipAttributesStruct s) { VipUseForList useForList = new VipUseForList(self.getUseFor()); if (s.isUserFor() && s.getUseFor().equals(NetworkServiceType.SNAT.toString())) { useForList.del(s.getUseFor()); if (useForList.getUseForList().isEmpty()){ /* snat is bound to router public interface, it is created automatically, * so it should be deleted automatically, but don't need to remove from backend */ dbf.remove(self); } else { self.setUseFor(useForList.toString()); dbf.update(self); } return false; } for (VipGetServiceReferencePoint ext : pluginRgty.getExtensionList(VipGetServiceReferencePoint.class)) { VipGetServiceReferencePoint.ServiceReference service = ext.getServiceReference(self.getUuid()); if (service.useFor.equals(s.getUseFor()) && service.count <= 1){ useForList.del(s.getUseFor()); self.setUseFor(useForList.toString()); dbf.update(self); } } if (useForList.getUseForList().isEmpty()){ return true; } else { return false; } }
private void validate(APICreateEipMsg msg) { VipVO vip = dbf.findByUuid(msg.getVipUuid(), VipVO.class); if (vip.getUseFor() != null) { VipUseForList useForList = new VipUseForList(vip.getUseFor()); if(!useForList.validateNewAdded(EipConstant.EIP_NETWORK_SERVICE_TYPE)) { throw new ApiMessageInterceptionException(operr("vip[uuid:%s] has been occupied other network service entity[%s]", msg.getVipUuid(), vip.getUseFor())); } } if (vip.getState() != VipState.Enabled) { throw new ApiMessageInterceptionException(operr("vip[uuid:%s] is not in state[%s], current state is %s", msg.getVipUuid(), VipState.Enabled, vip.getState())); } if (msg.getVmNicUuid() != null) { SimpleQuery<VmNicVO> nicq = dbf.createQuery(VmNicVO.class); nicq.add(VmNicVO_.uuid, Op.EQ, msg.getVmNicUuid()); VmNicVO nic = nicq.find(); if (VmNicHelper.getL3Uuids(nic).contains(vip.getL3NetworkUuid())) { throw new ApiMessageInterceptionException(argerr("guest l3Network of vm nic[uuid:%s] and vip l3Network of vip[uuid: %s] are the same network", msg.getVmNicUuid(), msg.getVipUuid())); } if (msg.getUsedIpUuid() == null) { msg.setUsedIpUuid(nic.getUsedIpUuid()); } else { validateEipGuestIpUuid(msg.getVmNicUuid(), msg.getUsedIpUuid()); } // check if the vm already has a network where the vip comes checkIfVmAlreadyHasVipNetwork(nic.getVmInstanceUuid(), vip); } if (msg.getUsedIpUuid() != null) { isVipInVmNicSubnet(msg.getVipUuid(), msg.getUsedIpUuid()); } }
vq.add(VmNicVO_.uuid, Op.EQ, msg.getVmNicUuid()); String guestL3Uuid = vq.findValue(); if (guestL3Uuid.equals(vip.getL3NetworkUuid())) { throw new ApiMessageInterceptionException(argerr("guest l3Network of vm nic[uuid:%s] and vip l3Network of port forwarding rule[uuid:%s] are the same network", msg.getVmNicUuid(), msg.getRuleUuid())); checkIfAnotherVip(vip.getUuid(), msg.getVmNicUuid()); if (vip.getPeerL3NetworkUuids() != null && vip.getPeerL3NetworkUuids().contains(guestL3Uuid)) { return;
.param("lbUuid", msg.getLoadBalancerUuid()).find(); if (lbVipVO.getUseFor() != null && lbVipVO.getUseFor().contains(SNAT_NETWORK_SERVICE_TYPE)) { vrUuid = Q.New(VirtualRouterVipVO.class).select(VirtualRouterVipVO_.virtualRouterVmUuid) .eq(VirtualRouterVipVO_.uuid, lbVipVO.getUuid()).findValue(); return getCandidateVmNicsIfLoadBalancerBound(msg, candidates, vrUuid);
VipVO vipvo = dbf.findByUuid(pfvo.getVipUuid(), VipVO.class); L3NetworkVO l3vo = dbf.findByUuid(vipvo.getL3NetworkUuid(), L3NetworkVO.class);
protected void returnVip(Completion completion) { ReturnIpMsg msg = new ReturnIpMsg(); msg.setL3NetworkUuid(self.getL3NetworkUuid()); msg.setUsedIpUuid(self.getUsedIpUuid()); bus.makeTargetServiceIdByResourceUuid(msg, L3NetworkConstant.SERVICE_ID, self.getL3NetworkUuid()); bus.send(msg, new CloudBusCallBack(completion){ @Override public void run(MessageReply reply) { completion.success(); } }); }