Search in sources :

Example 16 with DataCenter

use of com.cloud.legacymodel.dc.DataCenter in project cosmic by MissionCriticalCloud.

the class AssociateIPAddrCmd method getNetworkId.

public Long getNetworkId() {
    if (vpcId != null) {
        return null;
    }
    if (networkId != null) {
        return networkId;
    }
    final Long zoneId = getZoneId();
    final DataCenter zone = _entityMgr.findById(DataCenter.class, zoneId);
    if (zone.getNetworkType() == NetworkType.Advanced) {
        final List<? extends Network> networks = _networkService.getIsolatedNetworksOwnedByAccountInZone(getZoneId(), _accountService.getAccount(getEntityOwnerId()));
        if (networks.size() == 0) {
            final String domain = _domainService.getDomain(getDomainId()).getName();
            throw new InvalidParameterValueException("Account name=" + getAccountName() + " domain=" + domain + " doesn't have virtual networks in zone=" + zone.getName());
        }
        if (networks.size() < 1) {
            throw new InvalidParameterValueException("Account doesn't have any isolated networks in the zone");
        } else if (networks.size() > 1) {
            throw new InvalidParameterValueException("Account has more than one isolated network in the zone");
        }
        return networks.get(0).getId();
    } else {
        final Network defaultGuestNetwork = _networkService.getExclusiveGuestNetwork(zoneId);
        if (defaultGuestNetwork == null) {
            throw new InvalidParameterValueException("Unable to find a default guest network for account " + getAccountName() + " in domain ID=" + getDomainId());
        } else {
            return defaultGuestNetwork.getId();
        }
    }
}
Also used : DataCenter(com.cloud.legacymodel.dc.DataCenter) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) Network(com.cloud.legacymodel.network.Network)

Example 17 with DataCenter

use of com.cloud.legacymodel.dc.DataCenter in project cosmic by MissionCriticalCloud.

the class CreateZoneCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Zone Name: " + getZoneName());
    final DataCenter result = _configService.createZone(this);
    if (result != null) {
        final ZoneResponse response = _responseGenerator.createZoneResponse(ResponseView.Full, result, false);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a zone");
    }
}
Also used : ZoneResponse(com.cloud.api.response.ZoneResponse) DataCenter(com.cloud.legacymodel.dc.DataCenter) ServerApiException(com.cloud.api.ServerApiException)

Example 18 with DataCenter

use of com.cloud.legacymodel.dc.DataCenter in project cosmic by MissionCriticalCloud.

the class UpdateZoneCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Zone Id: " + getId());
    final DataCenter result = _configService.editZone(this);
    if (result != null) {
        final ZoneResponse response = _responseGenerator.createZoneResponse(ResponseView.Full, result, false);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update zone; internal error.");
    }
}
Also used : ZoneResponse(com.cloud.api.response.ZoneResponse) DataCenter(com.cloud.legacymodel.dc.DataCenter) ServerApiException(com.cloud.api.ServerApiException)

Example 19 with DataCenter

use of com.cloud.legacymodel.dc.DataCenter 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 20 with DataCenter

use of com.cloud.legacymodel.dc.DataCenter in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createClusterResponse.

@Override
public ClusterResponse createClusterResponse(final Cluster cluster, final Boolean showCapacities) {
    final ClusterResponse clusterResponse = new ClusterResponse();
    clusterResponse.setId(cluster.getUuid());
    clusterResponse.setName(cluster.getName());
    final HostPodVO pod = ApiDBUtils.findPodById(cluster.getPodId());
    if (pod != null) {
        clusterResponse.setPodId(pod.getUuid());
        clusterResponse.setPodName(pod.getName());
    }
    final DataCenter dc = ApiDBUtils.findZoneById(cluster.getDataCenterId());
    if (dc != null) {
        clusterResponse.setZoneId(dc.getUuid());
        clusterResponse.setZoneName(dc.getName());
    }
    clusterResponse.setHypervisorType(cluster.getHypervisorType().toString());
    clusterResponse.setClusterType(cluster.getClusterType().toString());
    clusterResponse.setAllocationState(cluster.getAllocationState().toString());
    clusterResponse.setManagedState(cluster.getManagedState().toString());
    final String cpuOvercommitRatio = ApiDBUtils.findClusterDetails(cluster.getId(), "cpuOvercommitRatio");
    final String memoryOvercommitRatio = ApiDBUtils.findClusterDetails(cluster.getId(), "memoryOvercommitRatio");
    clusterResponse.setCpuOvercommitRatio(cpuOvercommitRatio);
    clusterResponse.setMemoryOvercommitRatio(memoryOvercommitRatio);
    if (showCapacities != null && showCapacities) {
        final List<SummedCapacity> capacities = ApiDBUtils.getCapacityByClusterPodZone(null, null, cluster.getId());
        final Set<CapacityResponse> capacityResponses = new HashSet<>();
        for (final SummedCapacity capacity : capacities) {
            final CapacityResponse capacityResponse = new CapacityResponse();
            capacityResponse.setCapacityType(capacity.getCapacityType());
            capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
            if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
                final List<SummedCapacity> c = ApiDBUtils.findNonSharedStorageForClusterPodZone(null, null, cluster.getId());
                capacityResponse.setCapacityTotal(capacity.getTotalCapacity() - c.get(0).getTotalCapacity());
                capacityResponse.setCapacityUsed(capacity.getUsedCapacity() - c.get(0).getUsedCapacity());
            } else {
                capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
            }
            if (capacityResponse.getCapacityTotal() != 0) {
                capacityResponse.setPercentageAllocated(s_percentFormat.format((float) capacityResponse.getCapacityUsed() / (float) capacityResponse.getCapacityTotal() * 100f));
            } else {
                capacityResponse.setPercentageAllocated(s_percentFormat.format(0L));
            }
            capacityResponses.add(capacityResponse);
        }
        // Do it for stats as well.
        capacityResponses.addAll(getStatsCapacityresponse(null, cluster.getId(), pod.getId(), pod.getDataCenterId()));
        clusterResponse.setCapacitites(new ArrayList<>(capacityResponses));
    }
    clusterResponse.setObjectName("cluster");
    return clusterResponse;
}
Also used : DataCenter(com.cloud.legacymodel.dc.DataCenter) CapacityResponse(com.cloud.api.response.CapacityResponse) ClusterResponse(com.cloud.api.response.ClusterResponse) SummedCapacity(com.cloud.capacity.dao.CapacityDaoImpl.SummedCapacity) HostPodVO(com.cloud.dc.HostPodVO) HashSet(java.util.HashSet)

Aggregations

DataCenter (com.cloud.legacymodel.dc.DataCenter)50 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)17 Network (com.cloud.legacymodel.network.Network)15 Account (com.cloud.legacymodel.user.Account)14 ArrayList (java.util.ArrayList)11 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)9 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)8 DB (com.cloud.utils.db.DB)8 ActionEvent (com.cloud.event.ActionEvent)7 PhysicalNetwork (com.cloud.network.PhysicalNetwork)7 NetworkVO (com.cloud.network.dao.NetworkVO)7 IPAddressVO (com.cloud.network.dao.IPAddressVO)6 TransactionStatus (com.cloud.utils.db.TransactionStatus)6 HashSet (java.util.HashSet)6 ResourceTagResponse (com.cloud.api.response.ResourceTagResponse)5 HostPodVO (com.cloud.dc.HostPodVO)5 InsufficientAddressCapacityException (com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException)5 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)5 Vpc (com.cloud.legacymodel.network.vpc.Vpc)5 NetworkOffering (com.cloud.offering.NetworkOffering)5