Search in sources :

Example 56 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ConsumerResource method checkForInstalledProductsUpdate.

/**
 * Check if the consumers installed products have changed. If they do not appear to
 * have been specified in this PUT, skip updating installed products entirely.
 * <p></p>
 * It will return true if installed products were included in request and have changed.
 *
 * @param existing existing consumer
 * @param incoming incoming consumer
 * @return a boolean
 */
private boolean checkForInstalledProductsUpdate(Consumer existing, ConsumerDTO incoming) {
    if (incoming.getInstalledProducts() == null) {
        log.debug("Installed packages not included in this consumer update, skipping update.");
        return false;
    }
    Set<ConsumerInstalledProduct> incomingInstalledProducts = populateInstalledProducts(existing, incoming);
    if (existing.getInstalledProducts() == null || !existing.getInstalledProducts().equals(incomingInstalledProducts)) {
        log.info("Updating installed products.");
        // new collection here without breaking things.
        if (existing.getInstalledProducts() != null) {
            existing.getInstalledProducts().clear();
        }
        for (ConsumerInstalledProduct cip : incomingInstalledProducts) {
            existing.addInstalledProduct(cip);
        }
        return true;
    }
    log.debug("No change to installed products.");
    return false;
}
Also used : ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct)

Example 57 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ConsumerBindUtil method handleActivationKeyAutoBind.

private void handleActivationKeyAutoBind(Consumer consumer, ActivationKey key) throws AutobindDisabledForOwnerException {
    try {
        Set<String> productIds = new HashSet<>();
        List<String> poolIds = new ArrayList<>();
        for (Product akp : key.getProducts()) {
            productIds.add(akp.getId());
        }
        for (ConsumerInstalledProduct cip : consumer.getInstalledProducts()) {
            productIds.add(cip.getProductId());
        }
        for (ActivationKeyPool p : key.getPools()) {
            poolIds.add(p.getPool().getId());
        }
        Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
        AutobindData autobindData = AutobindData.create(consumer, owner).forProducts(productIds.toArray(new String[0])).withPools(poolIds);
        List<Entitlement> ents = entitler.bindByProducts(autobindData);
        entitler.sendEvents(ents);
    } catch (ForbiddenException fe) {
        throw fe;
    } catch (CertVersionConflictException cvce) {
        throw cvce;
    } catch (RuntimeException re) {
        log.warn("Unable to attach a subscription for a product that " + "has no pool: " + re.getMessage());
    }
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) CertVersionConflictException(org.candlepin.version.CertVersionConflictException) AutobindData(org.candlepin.resource.dto.AutobindData) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet)

Example 58 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ConsumerEnricher method enrich.

public void enrich(Consumer consumer) {
    if (consumer == null || CollectionUtils.isEmpty(consumer.getInstalledProducts())) {
        // No consumer or the consumer doesn't have any installed products -- nothing to do here.
        return;
    }
    ComplianceStatus status = this.complianceRules.getStatus(consumer, null, null, false, true, true, true);
    Map<String, DateRange> ranges = status.getProductComplianceDateRanges();
    // Compile the product IDs for the products we're going to be enriching
    Set<String> productIds = new HashSet<>();
    Map<String, Product> productMap = new HashMap<>();
    for (ConsumerInstalledProduct cip : consumer.getInstalledProducts()) {
        productIds.add(cip.getProductId());
    }
    for (Product product : this.ownerProductCurator.getProductsByIds(consumer.getOwnerId(), productIds)) {
        productMap.put(product.getId(), product);
    }
    // Perform enrichment of the consumer's installed products
    for (ConsumerInstalledProduct cip : consumer.getInstalledProducts()) {
        String pid = cip.getProductId();
        DateRange range = ranges != null ? ranges.get(pid) : null;
        // do those first.
        if (status.getCompliantProducts().containsKey(pid)) {
            cip.setStatus(GREEN_STATUS);
        } else if (status.getPartiallyCompliantProducts().containsKey(pid)) {
            cip.setStatus(YELLOW_STATUS);
        } else if (status.getNonCompliantProducts().contains(pid)) {
            cip.setStatus(RED_STATUS);
        }
        // Set the compliance date range if we have it
        if (range != null) {
            cip.setStartDate(range.getStartDate());
            cip.setEndDate(range.getEndDate());
        }
        // Fetch missing product information from the actual product
        Product product = productMap.get(pid);
        if (product != null) {
            if (cip.getVersion() == null) {
                cip.setVersion(product.getAttributeValue(Product.Attributes.VERSION));
            }
            if (cip.getArch() == null) {
                cip.setArch(product.getAttributeValue(Product.Attributes.ARCHITECTURE));
            }
        }
    }
}
Also used : DateRange(org.candlepin.policy.js.compliance.DateRange) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) HashMap(java.util.HashMap) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) HashSet(java.util.HashSet)

Example 59 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ConsumerTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public ConsumerDTO populate(ModelTranslator translator, Consumer source, ConsumerDTO dest) {
    dest = super.populate(translator, source, dest);
    dest.setId(source.getId()).setUuid(source.getUuid()).setName(source.getName()).setUsername(source.getUsername()).setEntitlementStatus(source.getEntitlementStatus()).setServiceLevel(source.getServiceLevel()).setEntitlementCount(source.getEntitlementCount()).setFacts(source.getFacts()).setLastCheckin(source.getLastCheckin()).setCanActivate(source.isCanActivate()).setContentTags(source.getContentTags()).setAutoheal(source.isAutoheal()).setRecipientOwnerKey(source.getRecipientOwnerKey()).setAnnotations(source.getAnnotations()).setContentAccessMode(source.getContentAccessMode());
    Release release = source.getReleaseVer();
    if (release != null) {
        dest.setReleaseVersion(release.getReleaseVer());
    }
    // Process nested objects if we have a ModelTranslator to use to the translation...
    if (translator != null) {
        if (StringUtils.isNotEmpty(source.getOwnerId())) {
            Owner owner = ownerCurator.findOwnerById(source.getOwnerId());
            dest.setOwner(translator.translate(owner, OwnerDTO.class));
        }
        Environment environment = this.environmentCurator.getConsumerEnvironment(source);
        dest.setEnvironment(translator.translate(environment, EnvironmentDTO.class));
        Set<ConsumerInstalledProduct> installedProducts = source.getInstalledProducts();
        if (installedProducts != null) {
            ObjectTranslator<ConsumerInstalledProduct, ConsumerInstalledProductDTO> cipTranslator = translator.findTranslatorByClass(ConsumerInstalledProduct.class, ConsumerInstalledProductDTO.class);
            Set<ConsumerInstalledProductDTO> ips = new HashSet<>();
            for (ConsumerInstalledProduct cip : installedProducts) {
                if (cip != null) {
                    ConsumerInstalledProductDTO dto = cipTranslator.translate(translator, cip);
                    if (dto != null) {
                        ips.add(dto);
                    }
                }
            }
            dest.setInstalledProducts(ips);
        }
        Set<ConsumerCapability> capabilities = source.getCapabilities();
        if (capabilities != null) {
            Set<CapabilityDTO> capabilitiesDTO = new HashSet<>();
            ObjectTranslator<ConsumerCapability, CapabilityDTO> capabilityTranslator = translator.findTranslatorByClass(ConsumerCapability.class, CapabilityDTO.class);
            for (ConsumerCapability capability : capabilities) {
                if (capability != null) {
                    CapabilityDTO dto = capabilityTranslator.translate(translator, capability);
                    if (dto != null) {
                        capabilitiesDTO.add(dto);
                    }
                }
            }
            dest.setCapabilities(capabilitiesDTO);
        }
        // Temporary measure to maintain API compatibility
        if (source.getTypeId() != null) {
            ConsumerType ctype = this.consumerTypeCurator.getConsumerType(source);
            dest.setType(translator.translate(ctype, ConsumerTypeDTO.class));
        } else {
            dest.setType(null);
        }
        // This will put in the property so that the virtWho instances won't error
        dest.setGuestIds(new ArrayList<>());
        dest.setHypervisorId(translator.translate(source.getHypervisorId(), HypervisorIdDTO.class));
        dest.setIdCert(translator.translate(source.getIdCert(), CertificateDTO.class));
    } else {
        dest.setReleaseVersion(null);
        dest.setOwner(null);
        dest.setEnvironment(null);
        dest.setInstalledProducts(null);
        dest.setCapabilities(null);
        dest.setHypervisorId(null);
        dest.setType(null);
        dest.setIdCert(null);
    }
    return dest;
}
Also used : Owner(org.candlepin.model.Owner) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ConsumerCapability(org.candlepin.model.ConsumerCapability) Environment(org.candlepin.model.Environment) ConsumerType(org.candlepin.model.ConsumerType) Release(org.candlepin.model.Release) HashSet(java.util.HashSet)

Example 60 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ConsumerTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public ConsumerDTO populate(ModelTranslator translator, Consumer source, ConsumerDTO dest) {
    dest = super.populate(translator, source, dest);
    dest.setUuid(source.getUuid()).setUsername(source.getUsername()).setServiceLevel(source.getServiceLevel()).setFacts(source.getFacts());
    // Process nested objects if we have a ModelTranslator to use to the translation...
    if (translator != null) {
        if (StringUtils.isNotEmpty(source.getOwnerId())) {
            Owner owner = ownerCurator.findOwnerById(source.getOwnerId());
            dest.setOwner(translator.translate(owner, OwnerDTO.class));
        }
        Set<ConsumerInstalledProduct> installedProducts = source.getInstalledProducts();
        if (installedProducts != null) {
            Set<String> ips = new HashSet<>();
            for (ConsumerInstalledProduct cip : installedProducts) {
                if (cip != null && cip.getProductId() != null) {
                    ips.add(cip.getProductId());
                }
            }
            dest.setInstalledProducts(ips);
        }
        Set<ConsumerCapability> capabilities = source.getCapabilities();
        if (capabilities != null) {
            Set<String> capabilitiesDTO = new HashSet<>();
            for (ConsumerCapability capability : capabilities) {
                if (capability != null && capability.getName() != null) {
                    capabilitiesDTO.add(capability.getName());
                }
            }
            dest.setCapabilities(capabilitiesDTO);
        }
        // Temporary measure to maintain API compatibility
        if (source.getTypeId() != null) {
            ConsumerType ctype = this.consumerTypeCurator.getConsumerType(source);
            dest.setType(translator.translate(ctype, ConsumerTypeDTO.class));
        } else {
            dest.setType(null);
        }
    } else {
        dest.setOwner(null);
        dest.setInstalledProducts(null);
        dest.setCapabilities(null);
        dest.setType(null);
    }
    return dest;
}
Also used : Owner(org.candlepin.model.Owner) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ConsumerCapability(org.candlepin.model.ConsumerCapability) ConsumerType(org.candlepin.model.ConsumerType) HashSet(java.util.HashSet)

Aggregations

ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)66 Consumer (org.candlepin.model.Consumer)54 Product (org.candlepin.model.Product)51 Test (org.junit.Test)49 Owner (org.candlepin.model.Owner)44 Date (java.util.Date)35 DateRange (org.candlepin.policy.js.compliance.DateRange)26 HashSet (java.util.HashSet)14 ArrayList (java.util.ArrayList)13 Entitlement (org.candlepin.model.Entitlement)11 Pool (org.candlepin.model.Pool)11 ConsumerType (org.candlepin.model.ConsumerType)10 AutobindData (org.candlepin.resource.dto.AutobindData)10 HashMap (java.util.HashMap)8 ConsumerCapability (org.candlepin.model.ConsumerCapability)7 List (java.util.List)6 ProductData (org.candlepin.model.dto.ProductData)6 ConsumerInstalledProductDTO (org.candlepin.dto.api.v1.ConsumerInstalledProductDTO)4 Environment (org.candlepin.model.Environment)4 GuestId (org.candlepin.model.GuestId)4