Search in sources :

Example 1 with CapabilityResponse

use of com.cloud.api.response.CapabilityResponse 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());
    // 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);
        CollectionUtils.addIgnoreNull(tagResponses, 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.network.vpc.Vpc) NetworkACL(com.cloud.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.network.Network.Capability) NetworkOffering(com.cloud.offering.NetworkOffering) Service(com.cloud.network.Network.Service) VMInstanceVO(com.cloud.vm.VMInstanceVO) NetworkProfile(com.cloud.network.NetworkProfile) DataCenter(com.cloud.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) NetworkResponse(com.cloud.api.response.NetworkResponse) PhysicalNetworkResponse(com.cloud.api.response.PhysicalNetworkResponse) Domain(com.cloud.domain.Domain) Map(java.util.Map) HashMap(java.util.HashMap) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 2 with CapabilityResponse

use of com.cloud.api.response.CapabilityResponse in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createNetworkServiceResponse.

@Override
public ServiceResponse createNetworkServiceResponse(final Service service) {
    final ServiceResponse response = new ServiceResponse();
    response.setName(service.getName());
    // set list of capabilities required for the service
    final List<CapabilityResponse> capabilityResponses = new ArrayList<>();
    final Capability[] capabilities = service.getCapabilities();
    for (final Capability cap : capabilities) {
        final CapabilityResponse capabilityResponse = new CapabilityResponse();
        capabilityResponse.setName(cap.getName());
        capabilityResponse.setObjectName("capability");
        if (cap.getName().equals(Capability.SupportedLBIsolation.getName()) || cap.getName().equals(Capability.SupportedSourceNatTypes.getName()) || cap.getName().equals(Capability.RedundantRouter.getName())) {
            capabilityResponse.setCanChoose(true);
        } else {
            capabilityResponse.setCanChoose(false);
        }
        capabilityResponses.add(capabilityResponse);
    }
    response.setCapabilities(capabilityResponses);
    // set list of providers providing this service
    final List<? extends Network.Provider> serviceProviders = ApiDBUtils.getProvidersForService(service);
    final List<ProviderResponse> serviceProvidersResponses = new ArrayList<>();
    for (final Network.Provider serviceProvider : serviceProviders) {
        // return only Virtual Router/JuniperSRX/CiscoVnmc as a provider for the firewall
        if (service == Service.Firewall && serviceProvider != Provider.VirtualRouter) {
            continue;
        }
        final ProviderResponse serviceProviderResponse = createServiceProviderResponse(serviceProvider);
        serviceProvidersResponses.add(serviceProviderResponse);
    }
    response.setProviders(serviceProvidersResponses);
    response.setObjectName("networkservice");
    return response;
}
Also used : ServiceResponse(com.cloud.api.response.ServiceResponse) VirtualRouterProviderResponse(com.cloud.api.response.VirtualRouterProviderResponse) ProviderResponse(com.cloud.api.response.ProviderResponse) Capability(com.cloud.network.Network.Capability) Provider(com.cloud.network.Network.Provider) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ArrayList(java.util.ArrayList) CapabilityResponse(com.cloud.api.response.CapabilityResponse)

Example 3 with CapabilityResponse

use of com.cloud.api.response.CapabilityResponse in project cosmic by MissionCriticalCloud.

the class ApiResponseHelper method createNetworkOfferingResponse.

@Override
public NetworkOfferingResponse createNetworkOfferingResponse(final NetworkOffering offering) {
    final NetworkOfferingResponse response = new NetworkOfferingResponse();
    response.setId(offering.getUuid());
    response.setName(offering.getName());
    response.setDisplayText(offering.getDisplayText());
    response.setTags(offering.getTags());
    response.setTrafficType(offering.getTrafficType().toString());
    response.setIsDefault(offering.isDefault());
    response.setSpecifyVlan(offering.getSpecifyVlan());
    response.setConserveMode(offering.isConserveMode());
    response.setSpecifyIpRanges(offering.getSpecifyIpRanges());
    response.setAvailability(offering.getAvailability().toString());
    response.setIsPersistent(offering.getIsPersistent());
    response.setNetworkRate(ApiDBUtils.getNetworkRate(offering.getId()));
    response.setEgressDefaultPolicy(offering.getEgressDefaultPolicy());
    response.setConcurrentConnections(offering.getConcurrentConnections());
    response.setSupportsStrechedL2Subnet(offering.getSupportsStrechedL2());
    final Long so;
    if (offering.getServiceOfferingId() != null) {
        so = offering.getServiceOfferingId();
    } else {
        so = ApiDBUtils.findDefaultRouterServiceOffering();
    }
    if (so != null) {
        final ServiceOffering serviceOffering = ApiDBUtils.findServiceOfferingById(so);
        if (serviceOffering != null) {
            response.setServiceOfferingId(serviceOffering.getUuid());
            response.setServiceOfferingName(serviceOffering.getName());
        }
    }
    final ServiceOffering secondaryServiceOffering = _serviceOfferingDao.findById(offering.getSecondaryServiceOfferingId());
    if (secondaryServiceOffering != null) {
        response.setSecondaryServiceOfferingId(secondaryServiceOffering.getUuid());
        response.setSecondaryServiceOfferingName(secondaryServiceOffering.getName());
    }
    if (offering.getGuestType() != null) {
        response.setGuestIpType(offering.getGuestType().toString());
    }
    response.setState(offering.getState().name());
    final Map<Service, Set<Provider>> serviceProviderMap = ApiDBUtils.listNetworkOfferingServices(offering.getId());
    final List<ServiceResponse> serviceResponses = new ArrayList<>();
    for (final Map.Entry<Service, Set<Provider>> entry : serviceProviderMap.entrySet()) {
        final Service service = entry.getKey();
        final Set<Provider> srvc_providers = entry.getValue();
        final ServiceResponse svcRsp = new ServiceResponse();
        // skip gateway service
        if (service == Service.Gateway) {
            continue;
        }
        svcRsp.setName(service.getName());
        final List<ProviderResponse> providers = getProviderResponses(srvc_providers);
        svcRsp.setProviders(providers);
        if (Service.Lb == service) {
            final List<CapabilityResponse> lbCapResponse = new ArrayList<>();
            final CapabilityResponse lbIsoaltion = new CapabilityResponse();
            lbIsoaltion.setName(Capability.SupportedLBIsolation.getName());
            lbIsoaltion.setValue(offering.getDedicatedLB() ? "dedicated" : "shared");
            lbCapResponse.add(lbIsoaltion);
            final CapabilityResponse eLb = new CapabilityResponse();
            eLb.setName(Capability.ElasticLb.getName());
            eLb.setValue(offering.getElasticLb() ? "true" : "false");
            lbCapResponse.add(eLb);
            final CapabilityResponse inline = new CapabilityResponse();
            inline.setName(Capability.InlineMode.getName());
            inline.setValue(offering.isInline() ? "true" : "false");
            lbCapResponse.add(inline);
            svcRsp.setCapabilities(lbCapResponse);
        } else if (Service.SourceNat == service) {
            final List<CapabilityResponse> capabilities = new ArrayList<>();
            final CapabilityResponse sharedSourceNat = new CapabilityResponse();
            sharedSourceNat.setName(Capability.SupportedSourceNatTypes.getName());
            sharedSourceNat.setValue(offering.getSharedSourceNat() ? "perzone" : "peraccount");
            capabilities.add(sharedSourceNat);
            final CapabilityResponse redundantRouter = new CapabilityResponse();
            redundantRouter.setName(Capability.RedundantRouter.getName());
            redundantRouter.setValue(offering.getRedundantRouter() ? "true" : "false");
            capabilities.add(redundantRouter);
            svcRsp.setCapabilities(capabilities);
        } else if (service == Service.StaticNat) {
            final List<CapabilityResponse> staticNatCapResponse = new ArrayList<>();
            final CapabilityResponse eIp = new CapabilityResponse();
            eIp.setName(Capability.ElasticIp.getName());
            eIp.setValue(offering.getElasticIp() ? "true" : "false");
            staticNatCapResponse.add(eIp);
            final CapabilityResponse associatePublicIp = new CapabilityResponse();
            associatePublicIp.setName(Capability.AssociatePublicIP.getName());
            associatePublicIp.setValue(offering.getAssociatePublicIP() ? "true" : "false");
            staticNatCapResponse.add(associatePublicIp);
            svcRsp.setCapabilities(staticNatCapResponse);
        }
        serviceResponses.add(svcRsp);
    }
    response.setForVpc(_configMgr.isOfferingForVpc(offering));
    response.setServices(serviceResponses);
    // set network offering details
    final Map<Detail, String> details = _ntwkModel.getNtwkOffDetails(offering.getId());
    if (details != null && !details.isEmpty()) {
        response.setDetails(details);
    }
    response.setObjectName("networkoffering");
    return response;
}
Also used : EnumSet(java.util.EnumSet) HashSet(java.util.HashSet) Set(java.util.Set) VirtualRouterProviderResponse(com.cloud.api.response.VirtualRouterProviderResponse) ProviderResponse(com.cloud.api.response.ProviderResponse) NetworkOfferingResponse(com.cloud.api.response.NetworkOfferingResponse) ServiceOffering(com.cloud.offering.ServiceOffering) ArrayList(java.util.ArrayList) Service(com.cloud.network.Network.Service) CapabilityResponse(com.cloud.api.response.CapabilityResponse) VirtualRouterProvider(com.cloud.network.VirtualRouterProvider) PhysicalNetworkServiceProvider(com.cloud.network.PhysicalNetworkServiceProvider) Provider(com.cloud.network.Network.Provider) ServiceResponse(com.cloud.api.response.ServiceResponse) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) Detail(com.cloud.offering.NetworkOffering.Detail)

Aggregations

CapabilityResponse (com.cloud.api.response.CapabilityResponse)3 ServiceResponse (com.cloud.api.response.ServiceResponse)3 ArrayList (java.util.ArrayList)3 ProviderResponse (com.cloud.api.response.ProviderResponse)2 VirtualRouterProviderResponse (com.cloud.api.response.VirtualRouterProviderResponse)2 Capability (com.cloud.network.Network.Capability)2 Provider (com.cloud.network.Network.Provider)2 Service (com.cloud.network.Network.Service)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 NetworkOfferingResponse (com.cloud.api.response.NetworkOfferingResponse)1 NetworkResponse (com.cloud.api.response.NetworkResponse)1 PhysicalNetworkResponse (com.cloud.api.response.PhysicalNetworkResponse)1 ResourceTagResponse (com.cloud.api.response.ResourceTagResponse)1 DataCenter (com.cloud.dc.DataCenter)1 Domain (com.cloud.domain.Domain)1 Network (com.cloud.network.Network)1 NetworkProfile (com.cloud.network.NetworkProfile)1 PhysicalNetwork (com.cloud.network.PhysicalNetwork)1