Search in sources :

Example 11 with NicSecondaryIpVO

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;
    }
}
Also used : Account(com.cloud.user.Account) PublicIp(com.cloud.network.addr.PublicIp) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) VMInstanceVO(com.cloud.vm.VMInstanceVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) DataCenter(com.cloud.dc.DataCenter) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) NicVO(com.cloud.vm.NicVO) VirtualMachine(com.cloud.vm.VirtualMachine) ActionEvent(com.cloud.event.ActionEvent)

Example 12 with NicSecondaryIpVO

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;
}
Also used : WhoHasThisAddressResponse(com.cloud.api.response.WhoHasThisAddressResponse) ServiceOfferingJoinVO(com.cloud.api.query.vo.ServiceOfferingJoinVO) ListDiskOfferingsCmd(com.cloud.api.command.user.offering.ListDiskOfferingsCmd) StringUtils(org.apache.commons.lang.StringUtils) HostDao(com.cloud.host.dao.HostDao) ListHostsCmd(com.cloud.api.command.admin.host.ListHostsCmd) HostTagVO(com.cloud.api.query.vo.HostTagVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) DomainManager(com.cloud.user.DomainManager) Map(java.util.Map) AllocationState(com.cloud.model.enumeration.AllocationState) UserResponse(com.cloud.api.response.UserResponse) Network(com.cloud.network.Network) ProjectAccountJoinVO(com.cloud.api.query.vo.ProjectAccountJoinVO) ListUsersCmd(com.cloud.api.command.admin.user.ListUsersCmd) ProjectInvitation(com.cloud.projects.ProjectInvitation) ProjectManager(com.cloud.projects.ProjectManager) ResourceTagVO(com.cloud.tags.ResourceTagVO) Project(com.cloud.projects.Project) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) ListAccountsCmd(com.cloud.api.command.user.account.ListAccountsCmd) ListVMsCmdByAdmin(com.cloud.api.command.admin.vm.ListVMsCmdByAdmin) ProjectInvitationJoinVO(com.cloud.api.query.vo.ProjectInvitationJoinVO) ManagerBase(com.cloud.utils.component.ManagerBase) TemplateState(com.cloud.engine.subsystem.api.storage.TemplateState) VMTemplateDao(com.cloud.storage.dao.VMTemplateDao) QueryService(com.cloud.query.QueryService) DataCenterJoinVO(com.cloud.api.query.vo.DataCenterJoinVO) ListVMGroupsCmd(com.cloud.api.command.user.vmgroup.ListVMGroupsCmd) ResourceMetaDataService(com.cloud.server.ResourceMetaDataService) AffinityGroupVMMapDao(com.cloud.affinity.dao.AffinityGroupVMMapDao) ImageStoreJoinVO(com.cloud.api.query.vo.ImageStoreJoinVO) SearchCriteria(com.cloud.utils.db.SearchCriteria) DomainRouterVO(com.cloud.vm.DomainRouterVO) DomainRouterJoinDao(com.cloud.api.query.dao.DomainRouterJoinDao) ProjectAccountResponse(com.cloud.api.response.ProjectAccountResponse) ListVMsCmd(com.cloud.api.command.user.vm.ListVMsCmd) ResourceTag(com.cloud.server.ResourceTag) ListTemplatesCmdByAdmin(com.cloud.api.command.admin.template.ListTemplatesCmdByAdmin) HostJoinVO(com.cloud.api.query.vo.HostJoinVO) HAWorkerResponse(com.cloud.api.response.HAWorkerResponse) HighAvailabilityManager(com.cloud.ha.HighAvailabilityManager) ListVolumesCmd(com.cloud.api.command.user.volume.ListVolumesCmd) CloudAuthenticationException(com.cloud.exception.CloudAuthenticationException) DomainJoinDao(com.cloud.api.query.dao.DomainJoinDao) UserVmDao(com.cloud.vm.dao.UserVmDao) NicDao(com.cloud.vm.dao.NicDao) ProjectResponse(com.cloud.api.response.ProjectResponse) Date(java.util.Date) StorageTagVO(com.cloud.api.query.vo.StorageTagVO) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO) StorageTagResponse(com.cloud.api.response.StorageTagResponse) ManagementServerHost(com.cloud.cluster.ManagementServerHost) ResourceObjectType(com.cloud.server.ResourceTag.ResourceObjectType) DiskOfferingResponse(com.cloud.api.response.DiskOfferingResponse) ListTagsCmd(com.cloud.api.command.user.tag.ListTagsCmd) InstanceGroupJoinVO(com.cloud.api.query.vo.InstanceGroupJoinVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) ListResourceDetailsCmd(com.cloud.api.command.user.volume.ListResourceDetailsCmd) Pair(com.cloud.utils.Pair) NicVO(com.cloud.vm.NicVO) Collectors(java.util.stream.Collectors) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) DomainResponse(com.cloud.api.response.DomainResponse) ListIsosCmdByAdmin(com.cloud.api.command.admin.iso.ListIsosCmdByAdmin) HaWorkVO(com.cloud.ha.HaWorkVO) DedicatedResourceVO(com.cloud.dc.DedicatedResourceVO) HostTagDao(com.cloud.api.query.dao.HostTagDao) VirtualMachine(com.cloud.vm.VirtualMachine) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) ProjectAccountJoinDao(com.cloud.api.query.dao.ProjectAccountJoinDao) AffinityGroupDomainMapDao(com.cloud.affinity.dao.AffinityGroupDomainMapDao) EventJoinDao(com.cloud.event.dao.EventJoinDao) NetworkDetailsDao(com.cloud.network.dao.NetworkDetailsDao) StoragePoolJoinDao(com.cloud.api.query.dao.StoragePoolJoinDao) Domain(com.cloud.domain.Domain) Inject(javax.inject.Inject) HashSet(java.util.HashSet) VolumeJoinVO(com.cloud.api.query.vo.VolumeJoinVO) ManagementServerHostDao(com.cloud.cluster.dao.ManagementServerHostDao) ListWhoHasThisMacCmd(com.cloud.api.command.admin.cloudops.ListWhoHasThisMacCmd) AsyncJobJoinVO(com.cloud.api.query.vo.AsyncJobJoinVO) AsyncJobJoinDao(com.cloud.api.query.dao.AsyncJobJoinDao) IPAddressVO(com.cloud.network.dao.IPAddressVO) ListDomainsCmdByAdmin(com.cloud.api.command.admin.domain.ListDomainsCmdByAdmin) ServiceOfferingResponse(com.cloud.api.response.ServiceOfferingResponse) IPAddressDao(com.cloud.network.dao.IPAddressDao) ListWhoHasThisIpCmd(com.cloud.api.command.admin.cloudops.ListWhoHasThisIpCmd) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) WhoHasThisAddressResponse(com.cloud.api.response.WhoHasThisAddressResponse) VMTemplateVO(com.cloud.storage.VMTemplateVO) StorageTagDao(com.cloud.api.query.dao.StorageTagDao) Logger(org.slf4j.Logger) ListIsosCmd(com.cloud.api.command.user.iso.ListIsosCmd) ListAsyncJobsCmd(com.cloud.api.command.user.job.ListAsyncJobsCmd) DataStoreRole(com.cloud.storage.DataStoreRole) ListHAWorkersCmd(com.cloud.api.command.admin.cloudops.ListHAWorkersCmd) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) ListStorageTagsCmd(com.cloud.api.command.admin.storage.ListStorageTagsCmd) ListSecondaryStagingStoresCmd(com.cloud.api.command.admin.storage.ListSecondaryStagingStoresCmd) ConfigKey(com.cloud.framework.config.ConfigKey) UserVmJoinDao(com.cloud.api.query.dao.UserVmJoinDao) ACLType(com.cloud.acl.ControlledEntity.ACLType) ProjectJoinVO(com.cloud.api.query.vo.ProjectJoinVO) AccountResponse(com.cloud.api.response.AccountResponse) BaseListProjectAndAccountResourcesCmd(com.cloud.api.BaseListProjectAndAccountResourcesCmd) TemplateType(com.cloud.storage.Storage.TemplateType) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ListServiceOfferingsCmd(com.cloud.api.command.user.offering.ListServiceOfferingsCmd) ServiceOfferingDao(com.cloud.service.dao.ServiceOfferingDao) ListTemplatesCmd(com.cloud.api.command.user.template.ListTemplatesCmd) Storage(com.cloud.storage.Storage) DataStoreDriver(com.cloud.engine.subsystem.api.storage.DataStoreDriver) AsyncJobResponse(com.cloud.api.response.AsyncJobResponse) EventJoinVO(com.cloud.api.query.vo.EventJoinVO) ResourceManager(com.cloud.resource.ResourceManager) ListAffinityGroupsCmd(com.cloud.api.command.user.affinitygroup.ListAffinityGroupsCmd) HostJoinDao(com.cloud.api.query.dao.HostJoinDao) ProjectJoinDao(com.cloud.api.query.dao.ProjectJoinDao) ScopeType(com.cloud.storage.ScopeType) Set(java.util.Set) StoragePoolJoinVO(com.cloud.api.query.vo.StoragePoolJoinVO) ResponseView(com.cloud.api.ResponseObject.ResponseView) DataStoreCapabilities(com.cloud.engine.subsystem.api.storage.DataStoreCapabilities) DomainJoinVO(com.cloud.api.query.vo.DomainJoinVO) ImageStoreJoinDao(com.cloud.api.query.dao.ImageStoreJoinDao) ServiceOfferingJoinDao(com.cloud.api.query.dao.ServiceOfferingJoinDao) AccountDao(com.cloud.user.dao.AccountDao) StoragePoolResponse(com.cloud.api.response.StoragePoolResponse) ZoneResponse(com.cloud.api.response.ZoneResponse) ResourceDetailResponse(com.cloud.api.response.ResourceDetailResponse) ResourceTagResponse(com.cloud.api.response.ResourceTagResponse) CallContext(com.cloud.context.CallContext) ListEventsCmd(com.cloud.api.command.user.event.ListEventsCmd) ListProjectsCmd(com.cloud.api.command.user.project.ListProjectsCmd) DiskOfferingJoinDao(com.cloud.api.query.dao.DiskOfferingJoinDao) ArrayList(java.util.ArrayList) AffinityGroupJoinDao(com.cloud.api.query.dao.AffinityGroupJoinDao) ConfigurationDao(com.cloud.framework.config.dao.ConfigurationDao) AccountJoinDao(com.cloud.api.query.dao.AccountJoinDao) VolumeJoinDao(com.cloud.api.query.dao.VolumeJoinDao) ResourceDetail(com.cloud.api.ResourceDetail) VMInstanceDao(com.cloud.vm.dao.VMInstanceDao) DomainRouterDao(com.cloud.vm.dao.DomainRouterDao) ImageStoreResponse(com.cloud.api.response.ImageStoreResponse) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) Op(com.cloud.utils.db.SearchCriteria.Op) AffinityGroupResponse(com.cloud.affinity.AffinityGroupResponse) ProjectDao(com.cloud.projects.dao.ProjectDao) InstanceGroupJoinDao(com.cloud.api.query.dao.InstanceGroupJoinDao) Vpc(com.cloud.network.vpc.Vpc) DiskOfferingJoinVO(com.cloud.api.query.vo.DiskOfferingJoinVO) ListZonesCmdByAdmin(com.cloud.api.command.admin.zone.ListZonesCmdByAdmin) ResourceTagDao(com.cloud.tags.dao.ResourceTagDao) ListResponse(com.cloud.api.response.ListResponse) AffinityGroupDomainMapVO(com.cloud.affinity.AffinityGroupDomainMapVO) AffinityGroupJoinVO(com.cloud.api.query.vo.AffinityGroupJoinVO) EventResponse(com.cloud.api.response.EventResponse) Host(com.cloud.host.Host) ProjectAccountDao(com.cloud.projects.dao.ProjectAccountDao) State(com.cloud.template.VirtualMachineTemplate.State) InstanceGroupResponse(com.cloud.api.response.InstanceGroupResponse) HostTagResponse(com.cloud.api.response.HostTagResponse) AccountManager(com.cloud.user.AccountManager) ImageFormat(com.cloud.storage.Storage.ImageFormat) DataCenterDetailsDao(com.cloud.dc.dao.DataCenterDetailsDao) NicSecondaryIpDao(com.cloud.vm.dao.NicSecondaryIpDao) ListRoutersCmd(com.cloud.api.command.admin.router.ListRoutersCmd) LoggerFactory(org.slf4j.LoggerFactory) ListAccountsCmdByAdmin(com.cloud.api.command.admin.account.ListAccountsCmdByAdmin) Func(com.cloud.utils.db.SearchCriteria.Func) Filter(com.cloud.utils.db.Filter) UserAccountJoinDao(com.cloud.api.query.dao.UserAccountJoinDao) JoinBuilder(com.cloud.utils.db.JoinBuilder) DomainDao(com.cloud.domain.dao.DomainDao) ResourceTagJoinDao(com.cloud.api.query.dao.ResourceTagJoinDao) ProjectInvitationResponse(com.cloud.api.response.ProjectInvitationResponse) TemplateResponse(com.cloud.api.response.TemplateResponse) ListStoragePoolsCmd(com.cloud.api.command.admin.storage.ListStoragePoolsCmd) Account(com.cloud.user.Account) DedicatedResourceDao(com.cloud.dc.dao.DedicatedResourceDao) DataStoreManager(com.cloud.engine.subsystem.api.storage.DataStoreManager) DataCenterJoinDao(com.cloud.api.query.dao.DataCenterJoinDao) DomainRouterResponse(com.cloud.api.response.DomainRouterResponse) List(java.util.List) TaggedResourceService(com.cloud.server.TaggedResourceService) ListProjectInvitationsCmd(com.cloud.api.command.user.project.ListProjectInvitationsCmd) VolumeResponse(com.cloud.api.response.VolumeResponse) ListDomainsCmd(com.cloud.api.command.admin.domain.ListDomainsCmd) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) SearchBuilder(com.cloud.utils.db.SearchBuilder) UserAccountJoinVO(com.cloud.api.query.vo.UserAccountJoinVO) Configurable(com.cloud.framework.config.Configurable) NetworkDao(com.cloud.network.dao.NetworkDao) HostResponse(com.cloud.api.response.HostResponse) TemplateJoinVO(com.cloud.api.query.vo.TemplateJoinVO) UserVmDetailsDao(com.cloud.vm.dao.UserVmDetailsDao) ListZonesCmd(com.cloud.api.command.user.zone.ListZonesCmd) AffinityGroupVMMapVO(com.cloud.affinity.AffinityGroupVMMapVO) ListProjectAccountsCmd(com.cloud.api.command.user.account.ListProjectAccountsCmd) ResourceTagJoinVO(com.cloud.api.query.vo.ResourceTagJoinVO) DomainRouterJoinVO(com.cloud.api.query.vo.DomainRouterJoinVO) ListImageStoresCmd(com.cloud.api.command.admin.storage.ListImageStoresCmd) Hypervisor(com.cloud.hypervisor.Hypervisor) UserVmResponse(com.cloud.api.response.UserVmResponse) DateUtil(com.cloud.utils.DateUtil) TemplateJoinDao(com.cloud.api.query.dao.TemplateJoinDao) UserVmVO(com.cloud.vm.UserVmVO) Component(org.springframework.stereotype.Component) ListHostTagsCmd(com.cloud.api.command.admin.host.ListHostTagsCmd) ListVolumesCmdByAdmin(com.cloud.api.command.admin.volume.ListVolumesCmdByAdmin) ProjectInvitationJoinDao(com.cloud.api.query.dao.ProjectInvitationJoinDao) Ternary(com.cloud.utils.Ternary) VpcDao(com.cloud.network.vpc.dao.VpcDao) Volume(com.cloud.storage.Volume) AccountJoinVO(com.cloud.api.query.vo.AccountJoinVO) DomainVO(com.cloud.domain.DomainVO) HighAvailabilityDao(com.cloud.ha.dao.HighAvailabilityDao) Account(com.cloud.user.Account) ListResponse(com.cloud.api.response.ListResponse) ArrayList(java.util.ArrayList) Vpc(com.cloud.network.vpc.Vpc) VMInstanceVO(com.cloud.vm.VMInstanceVO) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) Network(com.cloud.network.Network) IPAddressVO(com.cloud.network.dao.IPAddressVO) Domain(com.cloud.domain.Domain) NicVO(com.cloud.vm.NicVO)

Example 13 with NicSecondaryIpVO

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;
}
Also used : NicSecondaryIpResponse(com.cloud.api.response.NicSecondaryIpResponse) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) NicResponse(com.cloud.api.response.NicResponse)

Example 14 with NicSecondaryIpVO

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;
}
Also used : NicSecondaryIpResponse(com.cloud.api.response.NicSecondaryIpResponse) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) AffinityGroupResponse(com.cloud.affinity.AffinityGroupResponse) ArrayList(java.util.ArrayList) ResourceTagJoinVO(com.cloud.api.query.vo.ResourceTagJoinVO) NicResponse(com.cloud.api.response.NicResponse)

Example 15 with NicSecondaryIpVO

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;
}
Also used : User(com.cloud.user.User) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) UserVmResponse(com.cloud.api.response.UserVmResponse) NicSecondaryIpResponse(com.cloud.api.response.NicSecondaryIpResponse) UserVmDetailVO(com.cloud.vm.UserVmDetailVO) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) AffinityGroupResponse(com.cloud.affinity.AffinityGroupResponse) VmStats(com.cloud.vm.VmStats) ResourceTagJoinVO(com.cloud.api.query.vo.ResourceTagJoinVO) NicResponse(com.cloud.api.response.NicResponse)

Aggregations

NicSecondaryIpVO (com.cloud.vm.dao.NicSecondaryIpVO)18 Account (com.cloud.user.Account)11 VirtualMachine (com.cloud.vm.VirtualMachine)7 DataCenter (com.cloud.dc.DataCenter)6 IPAddressVO (com.cloud.network.dao.IPAddressVO)6 NicVO (com.cloud.vm.NicVO)6 ArrayList (java.util.ArrayList)6 ActionEvent (com.cloud.event.ActionEvent)5 User (com.cloud.user.User)5 TransactionStatus (com.cloud.utils.db.TransactionStatus)5 VMInstanceVO (com.cloud.vm.VMInstanceVO)5 ResourceTagJoinVO (com.cloud.api.query.vo.ResourceTagJoinVO)4 UserVmJoinVO (com.cloud.api.query.vo.UserVmJoinVO)4 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)4 AccountManager (com.cloud.user.AccountManager)4 SearchBuilder (com.cloud.utils.db.SearchBuilder)4 SearchCriteria (com.cloud.utils.db.SearchCriteria)4 AffinityGroupResponse (com.cloud.affinity.AffinityGroupResponse)3 NicResponse (com.cloud.api.response.NicResponse)3 NicSecondaryIpResponse (com.cloud.api.response.NicSecondaryIpResponse)3