use of com.cloud.vm.dao.NicSecondaryIpVO in project cosmic by MissionCriticalCloud.
the class NetworkServiceImpl method allocateSecondaryGuestIP.
@Override
@ActionEvent(eventType = EventTypes.EVENT_NIC_SECONDARY_IP_ASSIGN, eventDescription = "assigning secondary ip to nic", create = true)
public NicSecondaryIp allocateSecondaryGuestIP(final long nicId, final String requestedIp) throws InsufficientAddressCapacityException {
final Account caller = CallContext.current().getCallingAccount();
// check whether the nic belongs to user vm.
final NicVO nicVO = _nicDao.findById(nicId);
if (nicVO == null) {
throw new InvalidParameterValueException("There is no nic for the " + nicId);
}
if (nicVO.getVmType() != VirtualMachine.Type.User) {
throw new InvalidParameterValueException("The nic is not belongs to user vm");
}
final VirtualMachine vm = _userVmDao.findById(nicVO.getInstanceId());
if (vm == null) {
throw new InvalidParameterValueException("There is no vm with the nic");
}
final long networkId = nicVO.getNetworkId();
final Account ipOwner = _accountMgr.getAccount(vm.getAccountId());
// verify permissions
_accountMgr.checkAccess(caller, null, true, vm);
final Network network = _networksDao.findById(networkId);
if (network == null) {
throw new InvalidParameterValueException("Invalid network id is given");
}
final int maxAllowedIpsPerNic = NumbersUtil.parseInt(_configDao.getValue(Config.MaxNumberOfSecondaryIPsPerNIC.key()), 10);
final Long nicWiseIpCount = _nicSecondaryIpDao.countByNicId(nicId);
if (nicWiseIpCount.intValue() >= maxAllowedIpsPerNic) {
s_logger.error("Maximum Number of Ips \"vm.network.nic.max.secondary.ipaddresses = \"" + maxAllowedIpsPerNic + " per Nic has been crossed for the nic " + nicId + ".");
throw new InsufficientAddressCapacityException("Maximum Number of Ips per Nic has been crossed.", Nic.class, nicId);
}
s_logger.debug("Calling the ip allocation ...");
String ipaddr = null;
// Isolated network can exist in Basic zone only, so no need to verify the zone type
if (network.getGuestType() == Network.GuestType.Isolated) {
try {
ipaddr = _ipAddrMgr.allocateGuestIP(network, requestedIp);
} catch (final InsufficientAddressCapacityException e) {
throw new InvalidParameterValueException("Allocating guest ip for nic failed");
}
} else if (network.getGuestType() == Network.GuestType.Shared) {
// for basic zone, need to provide the podId to ensure proper ip alloation
Long podId = null;
final DataCenter dc = _dcDao.findById(network.getDataCenterId());
if (dc.getNetworkType() == NetworkType.Basic) {
final VMInstanceVO vmi = (VMInstanceVO) vm;
podId = vmi.getPodIdToDeployIn();
if (podId == null) {
throw new InvalidParameterValueException("vm pod id is null in Basic zone; can't decide the range for ip allocation");
}
}
try {
ipaddr = _ipAddrMgr.allocatePublicIpForGuestNic(network, podId, ipOwner, requestedIp);
if (ipaddr == null) {
throw new InvalidParameterValueException("Allocating ip to guest nic " + nicId + " failed");
}
} catch (final InsufficientAddressCapacityException e) {
s_logger.error("Allocating ip to guest nic " + nicId + " failed");
return null;
}
} else if (network.getTrafficType() == TrafficType.Public) {
try {
final PublicIp ip = _ipAddrMgr.assignPublicIpAddress(vm.getDataCenterId(), null, ipOwner, VlanType.VirtualNetwork, null, requestedIp, false);
ipaddr = ip.getAddress().toString();
} catch (final InsufficientAddressCapacityException e) {
throw new InvalidParameterValueException("Allocating public ip for nic failed");
}
} else {
s_logger.error("AddIpToVMNic is not supported in this network...");
return null;
}
if (ipaddr != null) {
// we got the ip addr so up the nics table and secondary ip
final String addrFinal = ipaddr;
final long id = Transaction.execute(new TransactionCallback<Long>() {
@Override
public Long doInTransaction(final TransactionStatus status) {
final boolean nicSecondaryIpSet = nicVO.getSecondaryIp();
if (!nicSecondaryIpSet) {
nicVO.setSecondaryIp(true);
// commit when previously set ??
s_logger.debug("Setting nics table ...");
_nicDao.update(nicId, nicVO);
}
s_logger.debug("Setting nic_secondary_ip table ...");
final Long vmId = nicVO.getInstanceId();
final NicSecondaryIpVO secondaryIpVO = new NicSecondaryIpVO(nicId, addrFinal, vmId, ipOwner.getId(), ipOwner.getDomainId(), networkId);
_nicSecondaryIpDao.persist(secondaryIpVO);
return secondaryIpVO.getId();
}
});
return getNicSecondaryIp(id);
} else {
return null;
}
}
use of com.cloud.vm.dao.NicSecondaryIpVO in project cosmic by MissionCriticalCloud.
the class QueryManagerImpl method listWhoHasThisIp.
@Override
public ListResponse<WhoHasThisAddressResponse> listWhoHasThisIp(final ListWhoHasThisIpCmd cmd) {
final ListResponse<WhoHasThisAddressResponse> whoHasThisIpList = new ListResponse<>();
final List<WhoHasThisAddressResponse> responsesList = new ArrayList<>();
final String cleanedIpAddress = StringUtils.deleteWhitespace(cmd.getIpAddress());
final List<IPAddressVO> ipAddresses = _ipAddressDao.listByIpAddress(cleanedIpAddress);
ipAddresses.forEach(ipAddress -> {
final WhoHasThisAddressResponse response = new WhoHasThisAddressResponse();
response.setObjectName("whohasthisip");
response.setIpAddress(ipAddress.getAddress().toString());
response.setUuid(ipAddress.getUuid());
response.setState(ipAddress.getState().toString());
final Domain domain = _domainDao.findById(ipAddress.getDomainId());
if (domain != null) {
response.setDomainName(domain.getName());
response.setDomainUuid(domain.getUuid());
}
final Network network = _networkDao.findById(ipAddress.getNetworkId());
if (network != null) {
response.setNetworkUuid(network.getUuid());
response.setCreated(ipAddress.getAllocatedTime());
response.setMode(network.getMode());
}
if (ipAddress.getVpcId() != null) {
final Vpc vpc = _vpcDao.findById(ipAddress.getVpcId());
if (vpc != null) {
response.setNetworkName(vpc.getName());
response.setVpcName(vpc.getName());
response.setVpcUuid(vpc.getUuid());
}
} else if (network != null && !StringUtils.isEmpty(network.getName())) {
response.setNetworkName(network.getName());
}
final Network associatedNetwork = _networkDao.findById(ipAddress.getAssociatedWithNetworkId());
if (associatedNetwork != null) {
response.setAssociatedNetworkName(associatedNetwork.getName());
response.setAssociatedNetworkUuid(associatedNetwork.getUuid());
}
responsesList.add(response);
});
final List<NicVO> nics = _nicDao.listByIpAddress(cleanedIpAddress);
nics.forEach(nic -> {
final WhoHasThisAddressResponse response = new WhoHasThisAddressResponse();
response.setObjectName("whohasthisip");
queryNicsTableResponse(responsesList, nic, response);
});
final List<NicSecondaryIpVO> nicSecondaryIps = _nicSecondaryIpDao.listByIpAddress(cleanedIpAddress);
nicSecondaryIps.forEach(nicSecondaryIp -> {
final WhoHasThisAddressResponse response = new WhoHasThisAddressResponse();
response.setObjectName("whohasthisip");
response.setIpAddress(nicSecondaryIp.getIp4Address());
response.setUuid(nicSecondaryIp.getUuid());
response.setCreated(nicSecondaryIp.getCreated());
final NicVO nicVO = _nicDao.findById(nicSecondaryIp.getNicId());
if (nicVO != null) {
response.setMode(nicVO.getMode());
response.setBroadcastUri(nicVO.getBroadcastUri());
response.setNetmask(nicVO.getIPv4Netmask());
response.setMacAddress(nicVO.getMacAddress());
response.setState(nicVO.getState().toString());
}
final Network network = _networkDao.findById(nicSecondaryIp.getNetworkId());
if (network != null) {
response.setNetworkUuid(network.getUuid());
if (!StringUtils.isEmpty(network.getName())) {
response.setNetworkName(network.getName());
}
}
final VMInstanceVO vm = _vmInstanceDao.findById(nicSecondaryIp.getVmId());
getVMInfo(response, nicVO, vm);
responsesList.add(response);
});
final Account account = CallContext.current().getCallingAccount();
final Domain domain = _domainDao.findById(account.getDomainId());
final List<WhoHasThisAddressResponse> filteredResponsesList = responsesList.stream().filter(response -> ((account.getDomainId() == Domain.ROOT_DOMAIN || domain.getUuid().equals(response.getDomainUuid())) && (StringUtils.isEmpty(cmd.getUuid()) || (!StringUtils.isEmpty(cmd.getUuid()) && response.getUuid().equals(cmd.getUuid()))))).skip(cmd.getStartIndex()).limit(cmd.getPageSizeVal()).collect(Collectors.toList());
whoHasThisIpList.setResponses(filteredResponsesList);
return whoHasThisIpList;
}
use of com.cloud.vm.dao.NicSecondaryIpVO in project cosmic by MissionCriticalCloud.
the class ApiResponseHelper method createNicResponse.
@Override
public NicResponse createNicResponse(final Nic result) {
final NicResponse response = new NicResponse();
final NetworkVO network = _entityMgr.findById(NetworkVO.class, result.getNetworkId());
final VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, result.getInstanceId());
response.setId(result.getUuid());
response.setNetworkid(network.getUuid());
if (vm != null) {
response.setVmId(vm.getUuid());
}
response.setIpaddress(result.getIPv4Address());
if (result.getSecondaryIp()) {
final List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(result.getId());
if (secondaryIps != null) {
final List<NicSecondaryIpResponse> ipList = new ArrayList<>();
for (final NicSecondaryIpVO ip : secondaryIps) {
final NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
ipRes.setId(ip.getUuid());
ipRes.setIpAddr(ip.getIp4Address());
ipList.add(ipRes);
}
response.setSecondaryIps(ipList);
}
}
response.setGateway(result.getIPv4Gateway());
response.setNetmask(result.getIPv4Netmask());
response.setMacAddress(result.getMacAddress());
if (result.getIPv6Address() != null) {
response.setIp6Address(result.getIPv6Address());
}
response.setIsDefault(result.isDefaultNic());
return response;
}
use of com.cloud.vm.dao.NicSecondaryIpVO in project cosmic by MissionCriticalCloud.
the class UserVmJoinDaoImpl method setUserVmResponse.
@Override
public UserVmResponse setUserVmResponse(final ResponseView view, final UserVmResponse userVmData, final UserVmJoinVO uvo) {
final long nic_id = uvo.getNicId();
if (nic_id > 0) {
final NicResponse nicResponse = new NicResponse();
nicResponse.setId(uvo.getNicUuid());
nicResponse.setIpaddress(uvo.getIpAddress());
nicResponse.setGateway(uvo.getGateway());
nicResponse.setNetmask(uvo.getNetmask());
nicResponse.setNetworkid(uvo.getNetworkUuid());
nicResponse.setNetworkName(uvo.getNetworkName());
nicResponse.setMacAddress(uvo.getMacAddress());
nicResponse.setIp6Address(uvo.getIp6Address());
nicResponse.setIp6Gateway(uvo.getIp6Gateway());
nicResponse.setIp6Cidr(uvo.getIp6Cidr());
if (uvo.getBroadcastUri() != null) {
nicResponse.setBroadcastUri(uvo.getBroadcastUri().toString());
}
if (uvo.getIsolationUri() != null) {
nicResponse.setIsolationUri(uvo.getIsolationUri().toString());
}
if (uvo.getTrafficType() != null) {
nicResponse.setTrafficType(uvo.getTrafficType().toString());
}
if (uvo.getGuestType() != null) {
nicResponse.setType(uvo.getGuestType().toString());
}
nicResponse.setIsDefault(uvo.isDefaultNic());
final List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(uvo.getNicId());
if (secondaryIps != null) {
final List<NicSecondaryIpResponse> ipList = new ArrayList<>();
for (final NicSecondaryIpVO ip : secondaryIps) {
final NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
ipRes.setId(ip.getUuid());
ipRes.setIpAddr(ip.getIp4Address());
ipList.add(ipRes);
}
nicResponse.setSecondaryIps(ipList);
}
nicResponse.setObjectName("nic");
userVmData.addNic(nicResponse);
}
final long tag_id = uvo.getTagId();
if (tag_id > 0 && !userVmData.containTag(tag_id)) {
final ResourceTagJoinVO vtag = ApiDBUtils.findResourceTagViewById(tag_id);
if (vtag != null) {
userVmData.addTag(ApiDBUtils.newResourceTagResponse(vtag, false));
}
}
final Long affinityGroupId = uvo.getAffinityGroupId();
if (affinityGroupId != null && affinityGroupId.longValue() != 0) {
final AffinityGroupResponse resp = new AffinityGroupResponse();
resp.setId(uvo.getAffinityGroupUuid());
resp.setName(uvo.getAffinityGroupName());
resp.setDescription(uvo.getAffinityGroupDescription());
resp.setObjectName("affinitygroup");
resp.setAccountName(uvo.getAccountName());
userVmData.addAffinityGroup(resp);
}
return userVmData;
}
use of com.cloud.vm.dao.NicSecondaryIpVO in project cosmic by MissionCriticalCloud.
the class UserVmJoinDaoImpl method newUserVmResponse.
@Override
public UserVmResponse newUserVmResponse(final ResponseView view, final String objectName, final UserVmJoinVO userVm, final EnumSet<VMDetails> details, final Account caller) {
final UserVmResponse userVmResponse = new UserVmResponse();
if (userVm.getHypervisorType() != null) {
userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
}
userVmResponse.setId(userVm.getUuid());
userVmResponse.setName(userVm.getName());
if (userVm.getDisplayName() != null) {
userVmResponse.setDisplayName(userVm.getDisplayName());
} else {
userVmResponse.setDisplayName(userVm.getName());
}
if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
userVmResponse.setProjectId(userVm.getProjectUuid());
userVmResponse.setProjectName(userVm.getProjectName());
} else {
userVmResponse.setAccountName(userVm.getAccountName());
}
final User user = _userDao.getUser(userVm.getUserId());
if (user != null) {
userVmResponse.setUserId(user.getUuid());
userVmResponse.setUserName(user.getUsername());
}
userVmResponse.setDomainId(userVm.getDomainUuid());
userVmResponse.setDomainName(userVm.getDomainName());
userVmResponse.setCreated(userVm.getCreated());
userVmResponse.setDisplayVm(userVm.isDisplayVm());
if (userVm.getState() != null) {
userVmResponse.setState(userVm.getState().toString());
}
userVmResponse.setHaEnable(userVm.isHaEnabled());
if (details.contains(VMDetails.all) || details.contains(VMDetails.group)) {
userVmResponse.setGroupId(userVm.getInstanceGroupUuid());
userVmResponse.setGroup(userVm.getInstanceGroupName());
}
userVmResponse.setZoneId(userVm.getDataCenterUuid());
userVmResponse.setZoneName(userVm.getDataCenterName());
if (view == ResponseView.Full) {
userVmResponse.setHostId(userVm.getHostUuid());
}
userVmResponse.setInstanceName(userVm.getInstanceName());
userVmResponse.setHostName(userVm.getHostName());
if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
userVmResponse.setTemplateId(userVm.getTemplateUuid());
userVmResponse.setTemplateName(userVm.getTemplateName());
userVmResponse.setTemplateDisplayText(userVm.getTemplateDisplayText());
userVmResponse.setPasswordEnabled(userVm.isPasswordEnabled());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.iso)) {
userVmResponse.setIsoId(userVm.getIsoUuid());
userVmResponse.setIsoName(userVm.getIsoName());
userVmResponse.setIsoDisplayText(userVm.getIsoDisplayText());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff)) {
userVmResponse.setServiceOfferingId(userVm.getServiceOfferingUuid());
userVmResponse.setServiceOfferingName(userVm.getServiceOfferingName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.diskoff)) {
userVmResponse.setDiskOfferingId(userVm.getDiskOfferingUuid());
userVmResponse.setDiskOfferingName(userVm.getDiskOfferingName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff) || details.contains(VMDetails.stats)) {
userVmResponse.setCpuNumber(userVm.getCpu());
userVmResponse.setMemory(userVm.getRamSize());
final ServiceOfferingDetailsVO serviceOfferingDetail = ApiDBUtils.findServiceOfferingDetail(userVm.getServiceOfferingId(), GPU.Keys.vgpuType.toString());
if (serviceOfferingDetail != null) {
userVmResponse.setVgpu(serviceOfferingDetail.getValue());
}
}
userVmResponse.setGuestOsId(userVm.getGuestOsUuid());
if (details.contains(VMDetails.all) || details.contains(VMDetails.volume)) {
userVmResponse.setRootDeviceId(userVm.getVolumeDeviceId());
if (userVm.getVolumeType() != null) {
userVmResponse.setRootDeviceType(userVm.getVolumeType().toString());
}
}
userVmResponse.setPassword(userVm.getPassword());
if (userVm.getJobId() != null) {
userVmResponse.setJobId(userVm.getJobUuid());
userVmResponse.setJobStatus(userVm.getJobStatus());
}
// userVmResponse.setForVirtualNetwork(userVm.getForVirtualNetwork());
userVmResponse.setPublicIpId(userVm.getPublicIpUuid());
userVmResponse.setPublicIp(userVm.getPublicIpAddress());
userVmResponse.setKeyPairName(userVm.getKeypairName());
userVmResponse.setOsTypeId(userVm.getGuestOsId());
if (details.contains(VMDetails.all) || details.contains(VMDetails.stats)) {
// stats calculation
final VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
if (vmStats != null) {
userVmResponse.setCpuUsed(new DecimalFormat("#.##").format(vmStats.getCPUUtilization()) + "%");
userVmResponse.setNetworkKbsRead((long) vmStats.getNetworkReadKBs());
userVmResponse.setNetworkKbsWrite((long) vmStats.getNetworkWriteKBs());
if ((userVm.getHypervisorType() != null) && (userVm.getHypervisorType().equals(HypervisorType.KVM) || userVm.getHypervisorType().equals(HypervisorType.XenServer))) {
// support KVM and XenServer only util 2013.06.25
userVmResponse.setDiskKbsRead((long) vmStats.getDiskReadKBs());
userVmResponse.setDiskKbsWrite((long) vmStats.getDiskWriteKBs());
userVmResponse.setDiskIORead((long) vmStats.getDiskReadIOs());
userVmResponse.setDiskIOWrite((long) vmStats.getDiskWriteIOs());
}
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.nics)) {
final long nic_id = userVm.getNicId();
if (nic_id > 0) {
final NicResponse nicResponse = new NicResponse();
nicResponse.setId(userVm.getNicUuid());
nicResponse.setIpaddress(userVm.getIpAddress());
nicResponse.setGateway(userVm.getGateway());
nicResponse.setNetmask(userVm.getNetmask());
nicResponse.setNetworkid(userVm.getNetworkUuid());
nicResponse.setNetworkName(userVm.getNetworkName());
nicResponse.setMacAddress(userVm.getMacAddress());
nicResponse.setIp6Address(userVm.getIp6Address());
nicResponse.setIp6Gateway(userVm.getIp6Gateway());
nicResponse.setIp6Cidr(userVm.getIp6Cidr());
if (userVm.getBroadcastUri() != null) {
nicResponse.setBroadcastUri(userVm.getBroadcastUri().toString());
}
if (userVm.getIsolationUri() != null) {
nicResponse.setIsolationUri(userVm.getIsolationUri().toString());
}
if (userVm.getTrafficType() != null) {
nicResponse.setTrafficType(userVm.getTrafficType().toString());
}
if (userVm.getGuestType() != null) {
nicResponse.setType(userVm.getGuestType().toString());
}
nicResponse.setIsDefault(userVm.isDefaultNic());
final List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(userVm.getNicId());
if (secondaryIps != null) {
final List<NicSecondaryIpResponse> ipList = new ArrayList<>();
for (final NicSecondaryIpVO ip : secondaryIps) {
final NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
ipRes.setId(ip.getUuid());
ipRes.setIpAddr(ip.getIp4Address());
ipList.add(ipRes);
}
nicResponse.setSecondaryIps(ipList);
}
nicResponse.setObjectName("nic");
userVmResponse.addNic(nicResponse);
}
}
// update tag information
final long tag_id = userVm.getTagId();
if (tag_id > 0 && !userVmResponse.containTag(tag_id)) {
final ResourceTagJoinVO vtag = ApiDBUtils.findResourceTagViewById(tag_id);
if (vtag != null) {
userVmResponse.addTag(ApiDBUtils.newResourceTagResponse(vtag, false));
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.affgrp)) {
final Long affinityGroupId = userVm.getAffinityGroupId();
if (affinityGroupId != null && affinityGroupId.longValue() != 0) {
final AffinityGroupResponse resp = new AffinityGroupResponse();
resp.setId(userVm.getAffinityGroupUuid());
resp.setName(userVm.getAffinityGroupName());
resp.setDescription(userVm.getAffinityGroupDescription());
resp.setObjectName("affinitygroup");
resp.setAccountName(userVm.getAccountName());
userVmResponse.addAffinityGroup(resp);
}
}
// set resource details map
// only hypervisortoolsversion can be returned to the end user
final UserVmDetailVO hypervisorToolsVersion = _userVmDetailsDao.findDetail(userVm.getId(), VmDetailConstants.HYPERVISOR_TOOLS_VERSION);
if (hypervisorToolsVersion != null) {
final Map<String, String> resourceDetails = new HashMap<>();
resourceDetails.put(hypervisorToolsVersion.getName(), hypervisorToolsVersion.getValue());
userVmResponse.setDetails(resourceDetails);
}
userVmResponse.setObjectName(objectName);
if (userVm.isDynamicallyScalable() == null) {
userVmResponse.setDynamicallyScalable(false);
} else {
userVmResponse.setDynamicallyScalable(userVm.isDynamicallyScalable());
}
return userVmResponse;
}
Aggregations