Search in sources :

Example 21 with Domain

use of com.cloud.legacymodel.domain.Domain in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createNetworkResponse.

@Override
public NetworkResponse createNetworkResponse(final ResponseView view, final Network network) {
    // need to get network profile in order to retrieve dns information from
    // there
    final NetworkProfile profile = ApiDBUtils.getNetworkProfile(network.getId());
    final NetworkResponse response = new NetworkResponse();
    response.setId(network.getUuid());
    response.setName(network.getName());
    response.setDisplaytext(network.getDisplayText());
    if (network.getBroadcastDomainType() != null) {
        response.setBroadcastDomainType(network.getBroadcastDomainType().toString());
    }
    if (network.getTrafficType() != null) {
        response.setTrafficType(network.getTrafficType().name());
    }
    if (network.getGuestType() != null) {
        response.setType(network.getGuestType().toString());
    }
    response.setGateway(network.getGateway());
    // FIXME - either set netmask or cidr
    response.setCidr(network.getCidr());
    response.setNetworkCidr(network.getNetworkCidr());
    // network cidr
    if (network.getNetworkCidr() != null) {
        response.setNetmask(NetUtils.cidr2Netmask(network.getNetworkCidr()));
    }
    if (network.getCidr() != null && network.getNetworkCidr() == null) {
        response.setNetmask(NetUtils.cidr2Netmask(network.getCidr()));
    }
    response.setIpExclusionList(((NetworkVO) network).getIpExclusionList());
    response.setIp6Gateway(network.getIp6Gateway());
    response.setIp6Cidr(network.getIp6Cidr());
    // create response for reserved IP ranges that can be used for
    // non-cloudstack purposes
    String reservation = null;
    if (network.getCidr() != null && NetUtils.isNetworkAWithinNetworkB(network.getCidr(), network.getNetworkCidr())) {
        final String[] guestVmCidrPair = network.getCidr().split("\\/");
        final String[] guestCidrPair = network.getNetworkCidr().split("\\/");
        final Long guestVmCidrSize = Long.valueOf(guestVmCidrPair[1]);
        final Long guestCidrSize = Long.valueOf(guestCidrPair[1]);
        final String[] guestVmIpRange = NetUtils.getIpRangeFromCidr(guestVmCidrPair[0], guestVmCidrSize);
        final String[] guestIpRange = NetUtils.getIpRangeFromCidr(guestCidrPair[0], guestCidrSize);
        final long startGuestIp = NetUtils.ip2Long(guestIpRange[0]);
        final long endGuestIp = NetUtils.ip2Long(guestIpRange[1]);
        final long startVmIp = NetUtils.ip2Long(guestVmIpRange[0]);
        final long endVmIp = NetUtils.ip2Long(guestVmIpRange[1]);
        if (startVmIp == startGuestIp && endVmIp < endGuestIp - 1) {
            reservation = NetUtils.long2Ip(endVmIp + 1) + "-" + NetUtils.long2Ip(endGuestIp);
        }
        if (endVmIp == endGuestIp && startVmIp > startGuestIp + 1) {
            reservation = NetUtils.long2Ip(startGuestIp) + "-" + NetUtils.long2Ip(startVmIp - 1);
        }
        if (startVmIp > startGuestIp + 1 && endVmIp < endGuestIp - 1) {
            reservation = NetUtils.long2Ip(startGuestIp) + "-" + NetUtils.long2Ip(startVmIp - 1) + " ,  " + NetUtils.long2Ip(endVmIp + 1) + "-" + NetUtils.long2Ip(endGuestIp);
        }
    }
    response.setReservedIpRange(reservation);
    if (network.getBroadcastUri() != null) {
        final String broadcastUri = network.getBroadcastUri().toString();
        response.setBroadcastUri(broadcastUri);
        String vlan = "N/A";
        switch(BroadcastDomainType.getSchemeValue(network.getBroadcastUri())) {
            case Vlan:
            case Vxlan:
                vlan = BroadcastDomainType.getValue(network.getBroadcastUri());
                break;
        }
        response.setVlan(vlan);
    }
    final DataCenter zone = ApiDBUtils.findZoneById(network.getDataCenterId());
    if (zone != null) {
        response.setZoneId(zone.getUuid());
        response.setZoneName(zone.getName());
    }
    if (network.getPhysicalNetworkId() != null) {
        final PhysicalNetworkVO pnet = ApiDBUtils.findPhysicalNetworkById(network.getPhysicalNetworkId());
        response.setPhysicalNetworkId(pnet.getUuid());
    }
    // populate network offering information
    final NetworkOffering networkOffering = ApiDBUtils.findNetworkOfferingById(network.getNetworkOfferingId());
    if (networkOffering != null) {
        response.setNetworkOfferingId(networkOffering.getUuid());
        response.setNetworkOfferingName(networkOffering.getName());
        response.setNetworkOfferingDisplayText(networkOffering.getDisplayText());
        response.setNetworkOfferingConserveMode(networkOffering.isConserveMode());
        response.setIsSystem(networkOffering.isSystemOnly());
        response.setNetworkOfferingAvailability(networkOffering.getAvailability().toString());
        response.setIsPersistent(networkOffering.getIsPersistent());
    }
    if (network.getAclType() != null) {
        response.setAclType(network.getAclType().toString());
    }
    response.setDisplayNetwork(network.getDisplayNetwork());
    response.setState(network.getState().toString());
    response.setRestartRequired(network.isRestartRequired());
    final NetworkVO nw = ApiDBUtils.findNetworkById(network.getRelated());
    if (nw != null) {
        response.setRelated(nw.getUuid());
    }
    response.setNetworkDomain(network.getNetworkDomain());
    response.setDns1(profile.getDns1());
    response.setDns2(profile.getDns2());
    response.setDhcpTftpServer(profile.getDhcpTftpServer());
    response.setDhcpBootfileName(profile.getDhcpBootfileName());
    // populate capability
    final Map<Service, Map<Capability, String>> serviceCapabilitiesMap = ApiDBUtils.getNetworkCapabilities(network.getId(), network.getDataCenterId());
    final List<ServiceResponse> serviceResponses = new ArrayList<>();
    if (serviceCapabilitiesMap != null) {
        for (final Map.Entry<Service, Map<Capability, String>> entry : serviceCapabilitiesMap.entrySet()) {
            final Service service = entry.getKey();
            final ServiceResponse serviceResponse = new ServiceResponse();
            // skip gateway service
            if (service == Service.Gateway) {
                continue;
            }
            serviceResponse.setName(service.getName());
            // set list of capabilities for the service
            final List<CapabilityResponse> capabilityResponses = new ArrayList<>();
            final Map<Capability, String> serviceCapabilities = entry.getValue();
            if (serviceCapabilities != null) {
                for (final Map.Entry<Capability, String> ser_cap_entries : serviceCapabilities.entrySet()) {
                    final Capability capability = ser_cap_entries.getKey();
                    final CapabilityResponse capabilityResponse = new CapabilityResponse();
                    final String capabilityValue = ser_cap_entries.getValue();
                    capabilityResponse.setName(capability.getName());
                    capabilityResponse.setValue(capabilityValue);
                    capabilityResponse.setObjectName("capability");
                    capabilityResponses.add(capabilityResponse);
                }
                serviceResponse.setCapabilities(capabilityResponses);
            }
            serviceResponse.setObjectName("service");
            serviceResponses.add(serviceResponse);
        }
    }
    response.setServices(serviceResponses);
    if (network.getAclType() == null || network.getAclType() == ACLType.Account) {
        populateOwner(response, network);
    } else {
        // get domain from network_domain table
        final Pair<Long, Boolean> domainNetworkDetails = ApiDBUtils.getDomainNetworkDetails(network.getId());
        if (domainNetworkDetails.first() != null) {
            final Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first());
            if (domain != null) {
                response.setDomainId(domain.getUuid());
            }
        }
        response.setSubdomainAccess(domainNetworkDetails.second());
    }
    final Long dedicatedDomainId = ApiDBUtils.getDedicatedNetworkDomain(network.getId());
    if (dedicatedDomainId != null) {
        final Domain domain = ApiDBUtils.findDomainById(dedicatedDomainId);
        if (domain != null) {
            response.setDomainId(domain.getUuid());
            response.setDomainName(domain.getName());
        }
    }
    response.setSpecifyIpRanges(network.getSpecifyIpRanges());
    if (network.getVpcId() != null) {
        final Vpc vpc = ApiDBUtils.findVpcById(network.getVpcId());
        if (vpc != null) {
            response.setVpcId(vpc.getUuid());
            response.setVpcName(vpc.getName());
        }
    }
    response.setCanUseForDeploy(ApiDBUtils.canUseForDeploy(network));
    // set tag information
    final List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Network, network.getId());
    final List<ResourceTagResponse> tagResponses = new ArrayList<>();
    for (final ResourceTag tag : tags) {
        final ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        if (tagResponse != null) {
            tagResponses.add(tagResponse);
        }
    }
    response.setTags(tagResponses);
    if (network.getNetworkACLId() != null) {
        final NetworkACL acl = ApiDBUtils.findByNetworkACLId(network.getNetworkACLId());
        if (acl != null) {
            response.setAclId(acl.getUuid());
            response.setAclName(acl.getName());
        }
    }
    response.setStrechedL2Subnet(network.isStrechedL2Network());
    if (network.isStrechedL2Network()) {
        final Set<String> networkSpannedZones = new HashSet<>();
        final List<VMInstanceVO> vmInstances = new ArrayList<>();
        vmInstances.addAll(ApiDBUtils.listUserVMsByNetworkId(network.getId()));
        vmInstances.addAll(ApiDBUtils.listDomainRoutersByNetworkId(network.getId()));
        for (final VirtualMachine vm : vmInstances) {
            final DataCenter vmZone = ApiDBUtils.findZoneById(vm.getDataCenterId());
            networkSpannedZones.add(vmZone.getUuid());
        }
        response.setNetworkSpannedZones(networkSpannedZones);
    }
    response.setObjectName("network");
    return response;
}
Also used : ArrayList(java.util.ArrayList) Vpc(com.cloud.legacymodel.network.vpc.Vpc) NetworkACL(com.cloud.legacymodel.network.vpc.NetworkACL) CapabilityResponse(com.cloud.api.response.CapabilityResponse) ServiceResponse(com.cloud.api.response.ServiceResponse) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) ResourceTagResponse(com.cloud.api.response.ResourceTagResponse) HashSet(java.util.HashSet) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) Capability(com.cloud.legacymodel.network.Network.Capability) NetworkOffering(com.cloud.offering.NetworkOffering) Service(com.cloud.legacymodel.network.Network.Service) VMInstanceVO(com.cloud.vm.VMInstanceVO) NetworkProfile(com.cloud.network.NetworkProfile) DataCenter(com.cloud.legacymodel.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) NetworkResponse(com.cloud.api.response.NetworkResponse) PhysicalNetworkResponse(com.cloud.api.response.PhysicalNetworkResponse) Domain(com.cloud.legacymodel.domain.Domain) Map(java.util.Map) HashMap(java.util.HashMap) VirtualMachine(com.cloud.legacymodel.vm.VirtualMachine)

Example 22 with Domain

use of com.cloud.legacymodel.domain.Domain in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createLBStickinessPolicyResponse.

@Override
public LBStickinessResponse createLBStickinessPolicyResponse(final List<? extends StickinessPolicy> stickinessPolicies, final LoadBalancer lb) {
    final LBStickinessResponse spResponse = new LBStickinessResponse();
    if (lb == null) {
        return spResponse;
    }
    spResponse.setlbRuleId(lb.getUuid());
    final Account account = ApiDBUtils.findAccountById(lb.getAccountId());
    if (account != null) {
        spResponse.setAccountName(account.getAccountName());
        final Domain domain = ApiDBUtils.findDomainById(account.getDomainId());
        if (domain != null) {
            spResponse.setDomainId(domain.getUuid());
            spResponse.setDomainName(domain.getName());
        }
    }
    final List<LBStickinessPolicyResponse> responses = new ArrayList<>();
    for (final StickinessPolicy stickinessPolicy : stickinessPolicies) {
        final LBStickinessPolicyResponse ruleResponse = new LBStickinessPolicyResponse(stickinessPolicy);
        responses.add(ruleResponse);
    }
    spResponse.setRules(responses);
    spResponse.setObjectName("stickinesspolicies");
    return spResponse;
}
Also used : UserAccount(com.cloud.legacymodel.user.UserAccount) Account(com.cloud.legacymodel.user.Account) ArrayList(java.util.ArrayList) Domain(com.cloud.legacymodel.domain.Domain) LBStickinessPolicyResponse(com.cloud.api.response.LBStickinessPolicyResponse) LBStickinessResponse(com.cloud.api.response.LBStickinessResponse) StickinessPolicy(com.cloud.network.rules.StickinessPolicy)

Example 23 with Domain

use of com.cloud.legacymodel.domain.Domain in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createDomainResponse.

@Override
public DomainResponse createDomainResponse(final Domain domain) {
    final DomainResponse domainResponse = new DomainResponse();
    domainResponse.setDomainName(domain.getName());
    domainResponse.setId(domain.getUuid());
    domainResponse.setLevel(domain.getLevel());
    domainResponse.setNetworkDomain(domain.getNetworkDomain());
    domainResponse.setEmail(domain.getEmail());
    domainResponse.setSlackChannelName(domain.getSlackChannelName());
    final Domain parentDomain = ApiDBUtils.findDomainById(domain.getParent());
    if (parentDomain != null) {
        domainResponse.setParentDomainId(parentDomain.getUuid());
    }
    final StringBuilder domainPath = new StringBuilder("ROOT");
    domainPath.append(domain.getPath()).deleteCharAt(domainPath.length() - 1);
    domainResponse.setPath(domainPath.toString());
    if (domain.getParent() != null) {
        domainResponse.setParentDomainName(ApiDBUtils.findDomainById(domain.getParent()).getName());
    }
    if (domain.getChildCount() > 0) {
        domainResponse.setHasChild(true);
    }
    domainResponse.setObjectName("domain");
    return domainResponse;
}
Also used : DomainResponse(com.cloud.api.response.DomainResponse) Domain(com.cloud.legacymodel.domain.Domain)

Example 24 with Domain

use of com.cloud.legacymodel.domain.Domain in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForAccountsInternal.

private Pair<List<AccountJoinVO>, Integer> searchForAccountsInternal(final ListAccountsCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    Long domainId = cmd.getDomainId();
    Long accountId = cmd.getId();
    final String accountName = cmd.getSearchName();
    boolean isRecursive = cmd.isRecursive();
    final boolean listAll = cmd.listAll();
    Boolean listForDomain = false;
    if (accountId != null) {
        final Account account = _accountDao.findById(accountId);
        if (account == null || account.getId() == Account.ACCOUNT_ID_SYSTEM) {
            throw new InvalidParameterValueException("Unable to find account by id " + accountId);
        }
        _accountMgr.checkAccess(caller, null, true, account);
    }
    if (domainId != null) {
        final Domain domain = _domainDao.findById(domainId);
        if (domain == null) {
            throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist");
        }
        _accountMgr.checkAccess(caller, domain);
        if (accountName != null) {
            final Account account = _accountDao.findActiveAccount(accountName, domainId);
            if (account == null || account.getId() == Account.ACCOUNT_ID_SYSTEM) {
                throw new InvalidParameterValueException("Unable to find account by name " + accountName + " in domain " + domainId);
            }
            _accountMgr.checkAccess(caller, null, true, account);
        }
    }
    if (accountId == null) {
        if (_accountMgr.isAdmin(caller.getId()) && listAll && domainId == null) {
            listForDomain = true;
            isRecursive = true;
            if (domainId == null) {
                domainId = caller.getDomainId();
            }
        } else if (_accountMgr.isAdmin(caller.getId()) && domainId != null) {
            listForDomain = true;
        } else {
            accountId = caller.getAccountId();
        }
    }
    final Filter searchFilter = new Filter(AccountJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final Object type = cmd.getAccountType();
    final Object state = cmd.getState();
    final Object isCleanupRequired = cmd.isCleanupRequired();
    final Object keyword = cmd.getKeyword();
    final SearchBuilder<AccountJoinVO> sb = _accountJoinDao.createSearchBuilder();
    sb.and("accountName", sb.entity().getAccountName(), SearchCriteria.Op.EQ);
    sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("needsCleanup", sb.entity().isNeedsCleanup(), SearchCriteria.Op.EQ);
    sb.and("typeNEQ", sb.entity().getType(), SearchCriteria.Op.NEQ);
    sb.and("idNEQ", sb.entity().getId(), SearchCriteria.Op.NEQ);
    if (listForDomain && isRecursive) {
        sb.and("path", sb.entity().getDomainPath(), SearchCriteria.Op.LIKE);
    }
    final SearchCriteria<AccountJoinVO> sc = sb.create();
    sc.setParameters("idNEQ", Account.ACCOUNT_ID_SYSTEM);
    if (keyword != null) {
        final SearchCriteria<AccountJoinVO> ssc = _accountJoinDao.createSearchCriteria();
        ssc.addOr("accountName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("accountName", SearchCriteria.Op.SC, ssc);
    }
    if (type != null) {
        sc.setParameters("type", type);
    }
    if (state != null) {
        sc.setParameters("state", state);
    }
    if (isCleanupRequired != null) {
        sc.setParameters("needsCleanup", isCleanupRequired);
    }
    if (accountName != null) {
        sc.setParameters("accountName", accountName);
    }
    // don't return account of type project to the end user
    sc.setParameters("typeNEQ", 5);
    if (accountId != null) {
        sc.setParameters("id", accountId);
    }
    if (listForDomain) {
        if (isRecursive) {
            final Domain domain = _domainDao.findById(domainId);
            sc.setParameters("path", domain.getPath() + "%");
        } else {
            sc.setParameters("domainId", domainId);
        }
    }
    return _accountJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) Domain(com.cloud.legacymodel.domain.Domain) ProjectAccountJoinVO(com.cloud.api.query.vo.ProjectAccountJoinVO) UserAccountJoinVO(com.cloud.api.query.vo.UserAccountJoinVO) AccountJoinVO(com.cloud.api.query.vo.AccountJoinVO)

Example 25 with Domain

use of com.cloud.legacymodel.domain.Domain in project cosmic by MissionCriticalCloud.

the class QueryManagerImpl method searchForDomainsInternal.

private Pair<List<DomainJoinVO>, Integer> searchForDomainsInternal(final ListDomainsCmd cmd) {
    final Account caller = CallContext.current().getCallingAccount();
    Long domainId = cmd.getId();
    final boolean listAll = cmd.listAll();
    boolean isRecursive = false;
    if (domainId != null) {
        final Domain domain = _domainDao.findById(domainId);
        if (domain == null) {
            throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist");
        }
        _accountMgr.checkAccess(caller, domain);
    } else {
        if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
            domainId = caller.getDomainId();
        }
        if (listAll) {
            isRecursive = true;
        }
    }
    final Filter searchFilter = new Filter(DomainJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    final String domainName = cmd.getDomainName();
    final Integer level = cmd.getLevel();
    final Object keyword = cmd.getKeyword();
    final SearchBuilder<DomainJoinVO> sb = _domainJoinDao.createSearchBuilder();
    sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
    sb.and("level", sb.entity().getLevel(), SearchCriteria.Op.EQ);
    sb.and("path", sb.entity().getPath(), SearchCriteria.Op.LIKE);
    sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
    final SearchCriteria<DomainJoinVO> sc = sb.create();
    if (keyword != null) {
        final SearchCriteria<DomainJoinVO> ssc = _domainJoinDao.createSearchCriteria();
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        sc.addAnd("name", SearchCriteria.Op.SC, ssc);
    }
    if (domainName != null) {
        sc.setParameters("name", domainName);
    }
    if (level != null) {
        sc.setParameters("level", level);
    }
    if (domainId != null) {
        if (isRecursive) {
            sc.setParameters("path", _domainDao.findById(domainId).getPath() + "%");
        } else {
            sc.setParameters("id", domainId);
        }
    }
    // return only Active domains to the API
    sc.setParameters("state", Domain.State.Active);
    return _domainJoinDao.searchAndCount(sc, searchFilter);
}
Also used : Account(com.cloud.legacymodel.user.Account) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Filter(com.cloud.utils.db.Filter) TemplateFilter(com.cloud.legacymodel.storage.VirtualMachineTemplate.TemplateFilter) Domain(com.cloud.legacymodel.domain.Domain) DomainJoinVO(com.cloud.api.query.vo.DomainJoinVO)

Aggregations

Domain (com.cloud.legacymodel.domain.Domain)55 Account (com.cloud.legacymodel.user.Account)37 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)20 UserAccount (com.cloud.legacymodel.user.UserAccount)19 ArrayList (java.util.ArrayList)16 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)11 Project (com.cloud.projects.Project)11 DomainVO (com.cloud.domain.DomainVO)10 Network (com.cloud.legacymodel.network.Network)10 DomainResponse (com.cloud.api.response.DomainResponse)8 Pair (com.cloud.legacymodel.utils.Pair)7 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)7 HostVO (com.cloud.host.HostVO)6 List (java.util.List)6 Filter (com.cloud.utils.db.Filter)5 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 AffinityGroupResponse (com.cloud.affinity.AffinityGroupResponse)4 CloudAuthenticationException (com.cloud.legacymodel.exceptions.CloudAuthenticationException)4 NetworkVO (com.cloud.network.dao.NetworkVO)4