Java Code Examples for org.apache.commons.collections.CollectionUtils#addIgnoreNull()

The following examples show how to use org.apache.commons.collections.CollectionUtils#addIgnoreNull() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
@Override
public SnapshotPolicyResponse createSnapshotPolicyResponse(SnapshotPolicy policy) {
    SnapshotPolicyResponse policyResponse = new SnapshotPolicyResponse();
    policyResponse.setId(policy.getUuid());
    Volume vol = ApiDBUtils.findVolumeById(policy.getVolumeId());
    if (vol != null) {
        policyResponse.setVolumeId(vol.getUuid());
    }
    policyResponse.setSchedule(policy.getSchedule());
    policyResponse.setIntervalType(policy.getInterval());
    policyResponse.setMaxSnaps(policy.getMaxSnaps());
    policyResponse.setTimezone(policy.getTimezone());
    policyResponse.setForDisplay(policy.isDisplay());
    policyResponse.setObjectName("snapshotpolicy");

    List<? extends ResourceTag> tags = _resourceTagDao.listBy(policy.getId(), ResourceObjectType.SnapshotPolicy);
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, false);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    policyResponse.setTags(new HashSet<>(tagResponses));

    return policyResponse;
}
 
Example 2
Source File: SolrUtils.java    From vind with Apache License 2.0 5 votes vote down vote up
public static Set<String> getFieldnames(FieldDescriptor descriptor, String context) {
    Set<String> fieldsnames = new HashSet<>();
    for(UseCase useCase : UseCase.values()) {
        CollectionUtils.addIgnoreNull(fieldsnames, getFieldname(descriptor, useCase, context));
    }
    return fieldsnames;
}
 
Example 3
Source File: RecommendationServiceImpl.java    From the-app with Apache License 2.0 5 votes vote down vote up
@Override
public List<ProductInfo> getTopsellerRecommendations(int limit) {
    // specification is topseller of each category here
    List<ProductInfo> result = new ArrayList<>(ProductType.values().length);
    for (ProductType type : ProductType.values()) {
        if (result.size() >= limit) {
           return result;
        }
        List<ProductInfo> randomlyChosenProducts = getRandomlyChosenProducts(1, type);
        if(!randomlyChosenProducts.isEmpty()){
            CollectionUtils.addIgnoreNull(result, randomlyChosenProducts.get(0));
        }
    }
    return result;
}
 
Example 4
Source File: RecommendationServiceImpl.java    From AppStash with Apache License 2.0 5 votes vote down vote up
@Override
public List<ProductInfo> getTopsellerRecommendations(int limit) {
    // specification is topseller of each category here
    List<ProductInfo> result = new ArrayList<>(ProductType.values().length);
    for (ProductType type : ProductType.values()) {
        if (result.size() >= limit) {
           return result;
        }
        List<ProductInfo> randomlyChosenProducts = getRandomlyChosenProducts(1, type);
        if(!randomlyChosenProducts.isEmpty()){
            CollectionUtils.addIgnoreNull(result, randomlyChosenProducts.get(0));
        }
    }
    return result;
}
 
Example 5
Source File: DomainRouterDaoImpl.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public List<DomainRouterVO> listIsolatedByHostId(final Long hostId) {
    final SearchCriteria<DomainRouterVO> sc = HostUpSearch.create();
    if (hostId != null) {
        sc.setParameters("host", hostId);
    }
    sc.setJoinParameters("networkRouter", "type", Network.GuestType.Isolated);
    final List<DomainRouterVO> routerIds = listBy(sc);
    final List<DomainRouterVO> routers = new ArrayList<DomainRouterVO>();
    for (final DomainRouterVO router : routerIds) {
        CollectionUtils.addIgnoreNull(routers, findById(router.getId()));
    }
    return routers;
}
 
Example 6
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public StaticRouteResponse createStaticRouteResponse(StaticRoute result) {
    StaticRouteResponse response = new StaticRouteResponse();
    response.setId(result.getUuid());
    if (result.getVpcId() != null) {
        Vpc vpc = ApiDBUtils.findVpcById(result.getVpcId());
        if (vpc != null) {
            response.setVpcId(vpc.getUuid());
        }
    }
    response.setCidr(result.getCidr());

    StaticRoute.State state = result.getState();
    if (state.equals(StaticRoute.State.Revoke)) {
        state = StaticRoute.State.Deleting;
    }
    response.setState(state.toString());
    populateAccount(response, result.getAccountId());
    populateDomain(response, result.getDomainId());

    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.StaticRoute, result.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses,tagResponse);
    }
    response.setTags(tagResponses);
    response.setObjectName("staticroute");

    return response;
}
 
Example 7
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public SnapshotResponse createSnapshotResponse(Snapshot snapshot) {
    SnapshotResponse snapshotResponse = new SnapshotResponse();
    snapshotResponse.setId(snapshot.getUuid());

    populateOwner(snapshotResponse, snapshot);

    VolumeVO volume = findVolumeById(snapshot.getVolumeId());
    String snapshotTypeStr = snapshot.getRecurringType().name();
    snapshotResponse.setSnapshotType(snapshotTypeStr);
    if (volume != null) {
        snapshotResponse.setVolumeId(volume.getUuid());
        snapshotResponse.setVolumeName(volume.getName());
        snapshotResponse.setVolumeType(volume.getVolumeType().name());
        snapshotResponse.setVirtualSize(volume.getSize());
        DataCenter zone = ApiDBUtils.findZoneById(volume.getDataCenterId());
        if (zone != null) {
            snapshotResponse.setZoneId(zone.getUuid());
        }

        if (volume.getVolumeType() == Volume.Type.ROOT && volume.getInstanceId() != null) {
            //TODO combine lines and 489 into a join in the volume dao
            VMInstanceVO instance = ApiDBUtils.findVMInstanceById(volume.getInstanceId());
            if (instance != null) {
                GuestOS guestOs = ApiDBUtils.findGuestOSById(instance.getGuestOSId());
                if (guestOs != null) {
                    snapshotResponse.setOsTypeId(guestOs.getUuid());
                    snapshotResponse.setOsDisplayName(guestOs.getDisplayName());
                }
            }
        }
    }
    snapshotResponse.setCreated(snapshot.getCreated());
    snapshotResponse.setName(snapshot.getName());
    snapshotResponse.setIntervalType(ApiDBUtils.getSnapshotIntervalTypes(snapshot.getId()));
    snapshotResponse.setState(snapshot.getState());
    snapshotResponse.setLocationType(ApiDBUtils.getSnapshotLocationType(snapshot.getId()));

    SnapshotInfo snapshotInfo = null;

    if (snapshot instanceof SnapshotInfo) {
        snapshotInfo = (SnapshotInfo)snapshot;
    } else {
        DataStoreRole dataStoreRole = getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);

        snapshotInfo = snapshotfactory.getSnapshot(snapshot.getId(), dataStoreRole);
    }

    if (snapshotInfo == null) {
        s_logger.debug("Unable to find info for image store snapshot with uuid " + snapshot.getUuid());
        snapshotResponse.setRevertable(false);
    } else {
    snapshotResponse.setRevertable(snapshotInfo.isRevertable());
    snapshotResponse.setPhysicaSize(snapshotInfo.getPhysicalSize());
    }

    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Snapshot, snapshot.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    snapshotResponse.setTags(new HashSet<>(tagResponses));

    snapshotResponse.setObjectName("snapshot");
    return snapshotResponse;
}
 
Example 8
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public VMSnapshotResponse createVMSnapshotResponse(VMSnapshot vmSnapshot) {
    VMSnapshotResponse vmSnapshotResponse = new VMSnapshotResponse();
    vmSnapshotResponse.setId(vmSnapshot.getUuid());
    vmSnapshotResponse.setName(vmSnapshot.getName());
    vmSnapshotResponse.setState(vmSnapshot.getState());
    vmSnapshotResponse.setCreated(vmSnapshot.getCreated());
    vmSnapshotResponse.setDescription(vmSnapshot.getDescription());
    vmSnapshotResponse.setDisplayName(vmSnapshot.getDisplayName());
    UserVm vm = ApiDBUtils.findUserVmById(vmSnapshot.getVmId());
    if (vm != null) {
        vmSnapshotResponse.setVirtualMachineid(vm.getUuid());
        DataCenterVO datacenter = ApiDBUtils.findZoneById(vm.getDataCenterId());
        if (datacenter != null) {
            vmSnapshotResponse.setZoneId(datacenter.getUuid());
        }
    }
    if (vmSnapshot.getParent() != null) {
        VMSnapshot vmSnapshotParent = ApiDBUtils.getVMSnapshotById(vmSnapshot.getParent());
        if (vmSnapshotParent != null) {
            vmSnapshotResponse.setParent(vmSnapshotParent.getUuid());
            vmSnapshotResponse.setParentName(vmSnapshotParent.getDisplayName());
        }
    }
    populateOwner(vmSnapshotResponse, vmSnapshot);
    Project project = ApiDBUtils.findProjectByProjectAccountId(vmSnapshot.getAccountId());
    if (project != null) {
        vmSnapshotResponse.setProjectId(project.getUuid());
        vmSnapshotResponse.setProjectName(project.getName());
    }
    Account account = ApiDBUtils.findAccountById(vmSnapshot.getAccountId());
    if (account != null) {
        vmSnapshotResponse.setAccountName(account.getAccountName());
    }
    DomainVO domain = ApiDBUtils.findDomainById(vmSnapshot.getDomainId());
    if (domain != null) {
        vmSnapshotResponse.setDomainId(domain.getUuid());
        vmSnapshotResponse.setDomainName(domain.getName());
    }

    List<? extends ResourceTag> tags = _resourceTagDao.listBy(vmSnapshot.getId(), ResourceObjectType.VMSnapshot);
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, false);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    vmSnapshotResponse.setTags(new HashSet<>(tagResponses));

    vmSnapshotResponse.setCurrent(vmSnapshot.getCurrent());
    vmSnapshotResponse.setType(vmSnapshot.getType().toString());
    vmSnapshotResponse.setObjectName("vmsnapshot");
    return vmSnapshotResponse;
}
 
Example 9
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public LoadBalancerResponse createLoadBalancerResponse(LoadBalancer loadBalancer) {
    LoadBalancerResponse lbResponse = new LoadBalancerResponse();
    lbResponse.setId(loadBalancer.getUuid());
    lbResponse.setName(loadBalancer.getName());
    lbResponse.setDescription(loadBalancer.getDescription());
    List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(loadBalancer.getId());
    lbResponse.setCidrList(StringUtils.join(cidrs, ","));

    IPAddressVO publicIp = ApiDBUtils.findIpAddressById(loadBalancer.getSourceIpAddressId());
    lbResponse.setPublicIpId(publicIp.getUuid());
    lbResponse.setPublicIp(publicIp.getAddress().addr());
    lbResponse.setPublicPort(Integer.toString(loadBalancer.getSourcePortStart()));
    lbResponse.setPrivatePort(Integer.toString(loadBalancer.getDefaultPortStart()));
    lbResponse.setAlgorithm(loadBalancer.getAlgorithm());
    lbResponse.setLbProtocol(loadBalancer.getLbProtocol());
    lbResponse.setForDisplay(loadBalancer.isDisplay());
    FirewallRule.State state = loadBalancer.getState();
    String stateToSet = state.toString();
    if (state.equals(FirewallRule.State.Revoke)) {
        stateToSet = "Deleting";
    }
    lbResponse.setState(stateToSet);
    populateOwner(lbResponse, loadBalancer);
    DataCenter zone = ApiDBUtils.findZoneById(publicIp.getDataCenterId());
    if (zone != null) {
        lbResponse.setZoneId(zone.getUuid());
        lbResponse.setZoneName(zone.getName());
    }

    //set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.LoadBalancer, loadBalancer.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    lbResponse.setTags(tagResponses);

    Network ntwk = ApiDBUtils.findNetworkById(loadBalancer.getNetworkId());
    lbResponse.setNetworkId(ntwk.getUuid());

    lbResponse.setObjectName("loadbalancer");
    return lbResponse;
}
 
Example 10
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public FirewallRuleResponse createPortForwardingRuleResponse(PortForwardingRule fwRule) {
    FirewallRuleResponse response = new FirewallRuleResponse();
    response.setId(fwRule.getUuid());
    response.setPrivateStartPort(Integer.toString(fwRule.getDestinationPortStart()));
    response.setPrivateEndPort(Integer.toString(fwRule.getDestinationPortEnd()));
    response.setProtocol(fwRule.getProtocol());
    response.setPublicStartPort(Integer.toString(fwRule.getSourcePortStart()));
    response.setPublicEndPort(Integer.toString(fwRule.getSourcePortEnd()));
    List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(fwRule.getId());
    response.setCidrList(StringUtils.join(cidrs, ","));

    Network guestNtwk = ApiDBUtils.findNetworkById(fwRule.getNetworkId());
    response.setNetworkId(guestNtwk.getUuid());


    IpAddress ip = ApiDBUtils.findIpAddressById(fwRule.getSourceIpAddressId());

    if (ip != null)
    {
        response.setPublicIpAddressId(ip.getUuid());
        response.setPublicIpAddress(ip.getAddress().addr());
        if (fwRule.getDestinationIpAddress() != null)
        {
            response.setDestNatVmIp(fwRule.getDestinationIpAddress().toString());
            UserVm vm = ApiDBUtils.findUserVmById(fwRule.getVirtualMachineId());
            if (vm != null) {
                response.setVirtualMachineId(vm.getUuid());
                response.setVirtualMachineName(vm.getHostName());

                if (vm.getDisplayName() != null) {
                    response.setVirtualMachineDisplayName(vm.getDisplayName());
                } else {
                    response.setVirtualMachineDisplayName(vm.getHostName());
                }
            }
        }
    }
    FirewallRule.State state = fwRule.getState();
    String stateToSet = state.toString();
    if (state.equals(FirewallRule.State.Revoke)) {
        stateToSet = "Deleting";
    }

    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.PortForwardingRule, fwRule.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    response.setTags(tagResponses);

    response.setState(stateToSet);
    response.setForDisplay(fwRule.isDisplay());
    response.setObjectName("portforwardingrule");
    return response;
}
 
Example 11
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public FirewallResponse createFirewallResponse(FirewallRule fwRule) {
    FirewallResponse response = new FirewallResponse();

    response.setId(fwRule.getUuid());
    response.setProtocol(fwRule.getProtocol());
    if (fwRule.getSourcePortStart() != null) {
        response.setStartPort(fwRule.getSourcePortStart());
    }

    if (fwRule.getSourcePortEnd() != null) {
        response.setEndPort(fwRule.getSourcePortEnd());
    }

    List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(fwRule.getId());
    response.setCidrList(StringUtils.join(cidrs, ","));

    if(fwRule.getTrafficType() == FirewallRule.TrafficType.Egress){
        List<String> destCidrs = ApiDBUtils.findFirewallDestCidrs(fwRule.getId());
        response.setDestCidr(StringUtils.join(destCidrs,","));
    }

    if (fwRule.getTrafficType() == FirewallRule.TrafficType.Ingress) {
        IpAddress ip = ApiDBUtils.findIpAddressById(fwRule.getSourceIpAddressId());
        response.setPublicIpAddressId(ip.getUuid());
        response.setPublicIpAddress(ip.getAddress().addr());
    }

        Network network = ApiDBUtils.findNetworkById(fwRule.getNetworkId());
        response.setNetworkId(network.getUuid());

    FirewallRule.State state = fwRule.getState();
    String stateToSet = state.toString();
    if (state.equals(FirewallRule.State.Revoke)) {
        stateToSet = "Deleting";
    }

    response.setIcmpCode(fwRule.getIcmpCode());
    response.setIcmpType(fwRule.getIcmpType());
    response.setForDisplay(fwRule.isDisplay());

    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.FirewallRule, fwRule.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    response.setTags(tagResponses);

    response.setState(stateToSet);
    response.setObjectName("firewallrule");
    return response;
}
 
Example 12
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public NetworkACLItemResponse createNetworkACLItemResponse(NetworkACLItem aclItem) {
    NetworkACLItemResponse response = new NetworkACLItemResponse();

    response.setId(aclItem.getUuid());
    response.setProtocol(aclItem.getProtocol());
    if (aclItem.getSourcePortStart() != null) {
        response.setStartPort(Integer.toString(aclItem.getSourcePortStart()));
    }

    if (aclItem.getSourcePortEnd() != null) {
        response.setEndPort(Integer.toString(aclItem.getSourcePortEnd()));
    }

    response.setCidrList(StringUtils.join(aclItem.getSourceCidrList(), ","));

    response.setTrafficType(aclItem.getTrafficType().toString());

    NetworkACLItem.State state = aclItem.getState();
    String stateToSet = state.toString();
    if (state.equals(NetworkACLItem.State.Revoke)) {
        stateToSet = "Deleting";
    }

    response.setIcmpCode(aclItem.getIcmpCode());
    response.setIcmpType(aclItem.getIcmpType());

    response.setState(stateToSet);
    response.setNumber(aclItem.getNumber());
    response.setAction(aclItem.getAction().toString());
    response.setForDisplay(aclItem.isDisplay());

    NetworkACL acl = ApiDBUtils.findByNetworkACLId(aclItem.getAclId());
    if (acl != null) {
        response.setAclId(acl.getUuid());
    }

    //set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.NetworkACL, aclItem.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    response.setTags(tagResponses);
    response.setReason(aclItem.getReason());
    response.setObjectName("networkacl");
    return response;
}
 
Example 13
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public VpcResponse createVpcResponse(ResponseView view, Vpc vpc) {
    VpcResponse response = new VpcResponse();
    response.setId(vpc.getUuid());
    response.setName(vpc.getName());
    response.setDisplayText(vpc.getDisplayText());
    response.setState(vpc.getState().name());
    VpcOffering voff = ApiDBUtils.findVpcOfferingById(vpc.getVpcOfferingId());
    if (voff != null) {
        response.setVpcOfferingId(voff.getUuid());
        response.setVpcOfferingName(voff.getName());
    }
    response.setCidr(vpc.getCidr());
    response.setRestartRequired(vpc.isRestartRequired());
    response.setNetworkDomain(vpc.getNetworkDomain());
    response.setForDisplay(vpc.isDisplay());
    response.setUsesDistributedRouter(vpc.usesDistributedRouter());
    response.setRedundantRouter(vpc.isRedundant());
    response.setRegionLevelVpc(vpc.isRegionLevelVpc());

    Map<Service, Set<Provider>> serviceProviderMap = ApiDBUtils.listVpcOffServices(vpc.getVpcOfferingId());
    List<ServiceResponse> serviceResponses = new ArrayList<ServiceResponse>();
    for (Map.Entry<Service,Set<Provider>>entry : serviceProviderMap.entrySet()) {
        Service service = entry.getKey();
        Set<Provider> serviceProviders = entry.getValue();
        ServiceResponse svcRsp = new ServiceResponse();
        // skip gateway service
        if (service == Service.Gateway) {
            continue;
        }
        svcRsp.setName(service.getName());
        List<ProviderResponse> providers = new ArrayList<ProviderResponse>();
        for (Provider provider : serviceProviders) {
            if (provider != null) {
                ProviderResponse providerRsp = new ProviderResponse();
                providerRsp.setName(provider.getName());
                providers.add(providerRsp);
            }
        }
        svcRsp.setProviders(providers);

        serviceResponses.add(svcRsp);
    }

    List<NetworkResponse> networkResponses = new ArrayList<NetworkResponse>();
    List<? extends Network> networks = ApiDBUtils.listVpcNetworks(vpc.getId());
    for (Network network : networks) {
        NetworkResponse ntwkRsp = createNetworkResponse(view, network);
        networkResponses.add(ntwkRsp);
    }

    DataCenter zone = ApiDBUtils.findZoneById(vpc.getZoneId());
    if (zone != null) {
        response.setZoneId(zone.getUuid());
        response.setZoneName(zone.getName());
    }

    response.setNetworks(networkResponses);
    response.setServices(serviceResponses);
    populateOwner(response, vpc);

    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Vpc, vpc.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    response.setTags(tagResponses);
    response.setObjectName("vpc");
    return response;
}
 
Example 14
Source File: ApiResponseHelper.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public ApplicationLoadBalancerResponse createLoadBalancerContainerReponse(ApplicationLoadBalancerRule lb, Map<Ip, UserVm> lbInstances) {

    ApplicationLoadBalancerResponse lbResponse = new ApplicationLoadBalancerResponse();
    lbResponse.setId(lb.getUuid());
    lbResponse.setName(lb.getName());
    lbResponse.setDescription(lb.getDescription());
    lbResponse.setAlgorithm(lb.getAlgorithm());
    lbResponse.setForDisplay(lb.isDisplay());
    Network nw = ApiDBUtils.findNetworkById(lb.getNetworkId());
    lbResponse.setNetworkId(nw.getUuid());
    populateOwner(lbResponse, lb);

    if (lb.getScheme() == Scheme.Internal) {
        lbResponse.setSourceIp(lb.getSourceIp().addr());
        //TODO - create the view for the load balancer rule to reflect the network uuid
        Network network = ApiDBUtils.findNetworkById(lb.getNetworkId());
        lbResponse.setSourceIpNetworkId(network.getUuid());
    } else {
        //for public, populate the ip information from the ip address
        IpAddress publicIp = ApiDBUtils.findIpAddressById(lb.getSourceIpAddressId());
        lbResponse.setSourceIp(publicIp.getAddress().addr());
        Network ntwk = ApiDBUtils.findNetworkById(publicIp.getNetworkId());
        lbResponse.setSourceIpNetworkId(ntwk.getUuid());
    }

    //set load balancer rules information (only one rule per load balancer in this release)
    List<ApplicationLoadBalancerRuleResponse> ruleResponses = new ArrayList<ApplicationLoadBalancerRuleResponse>();
    ApplicationLoadBalancerRuleResponse ruleResponse = new ApplicationLoadBalancerRuleResponse();
    ruleResponse.setInstancePort(lb.getDefaultPortStart());
    ruleResponse.setSourcePort(lb.getSourcePortStart());
    FirewallRule.State stateToSet = lb.getState();
    if (stateToSet.equals(FirewallRule.State.Revoke)) {
        stateToSet = FirewallRule.State.Deleting;
    }
    ruleResponse.setState(stateToSet.toString());
    ruleResponse.setObjectName("loadbalancerrule");
    ruleResponses.add(ruleResponse);
    lbResponse.setLbRules(ruleResponses);

    //set Lb instances information
    List<ApplicationLoadBalancerInstanceResponse> instanceResponses = new ArrayList<ApplicationLoadBalancerInstanceResponse>();
    for (Map.Entry<Ip,UserVm> entry : lbInstances.entrySet()) {
        Ip ip = entry.getKey();
        UserVm vm = entry.getValue();
        ApplicationLoadBalancerInstanceResponse instanceResponse = new ApplicationLoadBalancerInstanceResponse();
        instanceResponse.setIpAddress(ip.addr());
        instanceResponse.setId(vm.getUuid());
        instanceResponse.setName(vm.getInstanceName());
        instanceResponse.setObjectName("loadbalancerinstance");
        instanceResponses.add(instanceResponse);
    }

    lbResponse.setLbInstances(instanceResponses);

    //set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.LoadBalancer, lb.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses,tagResponse);
    }
    lbResponse.setTags(tagResponses);

    lbResponse.setObjectName("loadbalancer");
    return lbResponse;
}