Search in sources :

Example 1 with Content

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

the class DefaultContentAccessCertServiceAdapter method createContentAccessDataPayload.

private byte[] createContentAccessDataPayload(Owner owner, Environment environment) throws IOException {
    // fake a product dto as a container for the org content
    Set<Product> containerSet = new HashSet<>();
    CandlepinQuery<Content> ownerContent = ownerContentCurator.getContentByOwner(owner);
    Set<String> entitledProductIds = new HashSet<>();
    List<org.candlepin.model.dto.Product> productModels = new ArrayList<>();
    Map<String, EnvironmentContent> promotedContent = getPromotedContent(environment);
    String contentPrefix = getContentPrefix(owner, environment);
    Product container = new Product();
    Entitlement emptyEnt = new Entitlement();
    Pool emptyPool = new Pool();
    Product skuProduct = new Product();
    Consumer emptyConsumer = new Consumer();
    containerSet.add(container);
    container.setId("content_access");
    container.setName(" Content Access");
    for (Content c : ownerContent) {
        container.addContent(c, false);
    }
    emptyConsumer.setEnvironment(environment);
    emptyEnt.setPool(emptyPool);
    emptyEnt.setConsumer(emptyConsumer);
    emptyPool.setProduct(skuProduct);
    emptyPool.setStartDate(new Date());
    emptyPool.setEndDate(new Date());
    skuProduct.setName("Content Access");
    skuProduct.setId("content_access");
    entitledProductIds.add("content-access");
    org.candlepin.model.dto.Product productModel = v3extensionUtil.mapProduct(container, skuProduct, contentPrefix, promotedContent, emptyConsumer, emptyPool, entitledProductIds);
    productModels.add(productModel);
    return v3extensionUtil.createEntitlementDataPayload(productModels, emptyConsumer, emptyPool, null);
}
Also used : ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) EnvironmentContent(org.candlepin.model.EnvironmentContent) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet)

Example 2 with Content

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

the class CandlepinPoolManager method refreshPoolsWithRegeneration.

/*
     * We need to update/regen entitlements in the same transaction we update pools
     * so we don't miss anything
     */
@Transactional
@SuppressWarnings("checkstyle:methodlength")
@Traceable
void refreshPoolsWithRegeneration(SubscriptionServiceAdapter subAdapter, @TraceableParam("owner") Owner owner, boolean lazy) {
    Date now = new Date();
    owner = this.resolveOwner(owner);
    log.info("Refreshing pools for owner: {}", owner);
    Map<String, Subscription> subscriptionMap = new HashMap<>();
    Map<String, ProductData> productMap = new HashMap<>();
    Map<String, ContentData> contentMap = new HashMap<>();
    // Resolve all our subscriptions, products and content to ensure we don't have bad or
    // duplicate inbound data
    log.debug("Fetching subscriptions from adapter...");
    List<Subscription> subscriptions = subAdapter.getSubscriptions(owner);
    log.debug("Done. Processing subscriptions...");
    for (Subscription subscription : subscriptions) {
        if (subscription == null) {
            continue;
        }
        if (subscription.getId() == null) {
            log.error("subscription does not contain a mappable ID: {}", subscription);
            throw new IllegalStateException("subscription does not contain a mappable ID: " + subscription);
        }
        Subscription existingSub = subscriptionMap.get(subscription.getId());
        if (existingSub != null && !existingSub.equals(subscription)) {
            log.warn("Multiple versions of the same subscription received during refresh; " + "discarding duplicate: {} => {}, {}", subscription.getId(), existingSub, subscription);
            continue;
        }
        subscriptionMap.put(subscription.getId(), subscription);
        List<ProductData> products = new LinkedList<>();
        products.add(subscription.getProduct());
        products.add(subscription.getDerivedProduct());
        products.addAll(subscription.getProvidedProducts());
        products.addAll(subscription.getDerivedProvidedProducts());
        for (ProductData product : products) {
            if (product == null) {
                // forward.
                continue;
            }
            if (product.getId() == null) {
                log.error("product does not contain a mappable Red Hat ID: {}", product);
                throw new IllegalStateException("product does not contain a mappable Red Hat ID: " + product);
            }
            // Product is coming from an upstream source; lock it so only upstream can make
            // further changes to it.
            product.setLocked(true);
            ProductData existingProduct = productMap.get(product.getId());
            if (existingProduct != null && !existingProduct.equals(product)) {
                log.warn("Multiple versions of the same product received during refresh; " + "discarding duplicate: {} => {}, {}", product.getId(), existingProduct, product);
            } else {
                productMap.put(product.getId(), product);
                Collection<ProductContentData> pcdCollection = product.getProductContent();
                if (pcdCollection != null) {
                    for (ProductContentData pcd : pcdCollection) {
                        if (pcd == null) {
                            log.error("product contains a null product-content mapping: {}", product);
                            throw new IllegalStateException("product contains a null product-content mapping: " + product);
                        }
                        ContentData content = pcd.getContent();
                        // population validation for us.
                        if (content == null || content.getId() == null) {
                            log.error("product contains a null or incomplete product-content mapping: {}", product);
                            throw new IllegalStateException("product contains a null or incomplete " + "product-content mapping: " + product);
                        }
                        // We need to lock the incoming content here, but doing so will affect
                        // the equality comparison for products. We'll correct them later.
                        ContentData existingContent = contentMap.get(content.getId());
                        if (existingContent != null && !existingContent.equals(content)) {
                            log.warn("Multiple versions of the same content received during refresh; " + "discarding duplicate: {} => {}, {}", content.getId(), existingContent, content);
                        } else {
                            contentMap.put(content.getId(), content);
                        }
                    }
                }
            }
        }
    }
    // Persist content changes
    log.debug("Importing {} content...", contentMap.size());
    // TODO: Find a more efficient way of doing this, preferably within this method
    for (ContentData cdata : contentMap.values()) {
        cdata.setLocked(true);
    }
    Map<String, Content> importedContent = this.contentManager.importContent(owner, contentMap, productMap.keySet()).getImportedEntities();
    log.debug("Importing {} product(s)...", productMap.size());
    ImportResult<Product> importResult = this.productManager.importProducts(owner, productMap, importedContent);
    Map<String, Product> importedProducts = importResult.getImportedEntities();
    Map<String, Product> updatedProducts = importResult.getUpdatedEntities();
    log.debug("Refreshing {} pool(s)...", subscriptionMap.size());
    Iterator<Map.Entry<String, Subscription>> subsIterator = subscriptionMap.entrySet().iterator();
    while (subsIterator.hasNext()) {
        Map.Entry<String, Subscription> entry = subsIterator.next();
        Subscription sub = entry.getValue();
        if (now.after(sub.getEndDate())) {
            log.info("Skipping expired subscription: {}", sub);
            subsIterator.remove();
            continue;
        }
        log.debug("Processing subscription: {}", sub);
        Pool pool = this.convertToMasterPoolImpl(sub, owner, importedProducts);
        this.refreshPoolsForMasterPool(pool, false, lazy, updatedProducts);
    }
    // delete pools whose subscription disappeared:
    log.debug("Deleting pools for absent subscriptions...");
    List<Pool> poolsToDelete = new ArrayList<>();
    for (Pool pool : poolCurator.getPoolsFromBadSubs(owner, subscriptionMap.keySet())) {
        if (this.isManaged(pool)) {
            poolsToDelete.add(pool);
        }
    }
    deletePools(poolsToDelete);
    // TODO: break this call into smaller pieces. There may be lots of floating pools
    log.debug("Updating floating pools...");
    List<Pool> floatingPools = poolCurator.getOwnersFloatingPools(owner);
    updateFloatingPools(floatingPools, lazy, updatedProducts);
    log.info("Refresh pools for owner: {} completed in: {}ms", owner.getKey(), System.currentTimeMillis() - now.getTime());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) Entry(java.util.Map.Entry) ProductContentData(org.candlepin.model.dto.ProductContentData) ContentData(org.candlepin.model.dto.ContentData) Pool(org.candlepin.model.Pool) SourceSubscription(org.candlepin.model.SourceSubscription) Subscription(org.candlepin.model.dto.Subscription) ProductContentData(org.candlepin.model.dto.ProductContentData) ProductData(org.candlepin.model.dto.ProductData) Date(java.util.Date) LinkedList(java.util.LinkedList) Content(org.candlepin.model.Content) Map(java.util.Map) HashMap(java.util.HashMap) Traceable(org.candlepin.util.Traceable) Transactional(com.google.inject.persist.Transactional)

Example 3 with Content

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

the class ContentManager method createContent.

/**
 * Creates a new Content for the given owner, using the data in the provided DTO.
 *
 * @param dto
 *  A content DTO representing the content to create
 *
 * @param owner
 *  The owner for which to create the content
 *
 * @throws IllegalArgumentException
 *  if dto is null or incomplete, or owner is null
 *
 * @throws IllegalStateException
 *  if the dto represents content that already exists
 *
 * @return
 *  a new Content instance representing the specified content for the given owner
 */
public Content createContent(ContentDTO dto, Owner owner) {
    if (dto == null) {
        throw new IllegalArgumentException("dto is null");
    }
    if (dto.getId() == null || dto.getType() == null || dto.getLabel() == null || dto.getName() == null || dto.getVendor() == null) {
        throw new IllegalArgumentException("dto is incomplete");
    }
    if (this.ownerContentCurator.contentExists(owner, dto.getId())) {
        throw new IllegalStateException("content has already been created: " + dto.getId());
    }
    // TODO: more validation here...?
    Content entity = new Content(dto.getId());
    this.applyContentChanges(entity, dto);
    log.debug("Creating new content for org: {}, {}", entity, owner);
    // Check if we have an alternate version we can use instead.
    List<Content> alternateVersions = this.ownerContentCurator.getContentByVersions(owner, Collections.<String, Integer>singletonMap(entity.getId(), entity.getEntityVersion())).list();
    log.debug("Checking {} alternate content versions", alternateVersions.size());
    for (Content alt : alternateVersions) {
        if (alt.equals(entity)) {
            // If we're "creating" a content, we shouldn't have any other object references to
            // update for this content. Instead, we'll just add the new owner to the content.
            this.ownerContentCurator.mapContentToOwner(alt, owner);
            return alt;
        }
    }
    entity = this.contentCurator.create(entity);
    this.ownerContentCurator.mapContentToOwner(entity, owner);
    return entity;
}
Also used : ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) OwnerContent(org.candlepin.model.OwnerContent)

Example 4 with Content

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

the class ContentManager method updateContent.

/**
 * Updates the specified content instance, creating a new version of the content as necessary.
 * The content instance returned by this method is not guaranteed to be the same instance passed
 * in. As such, once this method has been called, callers should only use the instance output by
 * this method.
 *
 * @param owner
 *  The owner for which to update the content
 *
 * @param regenerateEntitlementCerts
 *  Whether or not changes made to the content should trigger the regeneration of entitlement
 *  certificates for affected consumers
 *
 * @throws IllegalStateException
 *  if the given content update references a content that does not exist for the specified owner
 *
 * @throws IllegalArgumentException
 *  if either the provided content entity or owner are null
 *
 * @return
 *  the updated content entity, or a new content entity
 */
@Transactional
public Content updateContent(ContentDTO update, Owner owner, boolean regenerateEntitlementCerts) {
    if (update == null) {
        throw new IllegalArgumentException("update is null");
    }
    if (update.getId() == null) {
        throw new IllegalArgumentException("update is incomplete");
    }
    if (owner == null) {
        throw new IllegalArgumentException("owner is null");
    }
    // Resolve the entity to ensure we're working with the merged entity, and to ensure it's
    // already been created.
    // TODO: FIXME:
    // There's a bug here where if changes are applied to an entity's collections, and then
    // this method is called, the check below will cause the changes to be persisted.
    // This needs to be re-written to use DTOs as the primary source of entity creation, rather
    // than a bolted-on utility method.
    // If we never edit the entity directly, however, this is safe.
    Content entity = this.ownerContentCurator.getContentById(owner, update.getId());
    if (entity == null) {
        // If we're doing an exclusive update, this should be an error condition
        throw new IllegalStateException("Content has not yet been created");
    }
    // TODO: Remove this shim and stop using DTOs in this class
    if (!this.isChangedBy(entity, update)) {
        return entity;
    }
    log.debug("Applying content update for org: {}, {}", entity, owner);
    Content updated = this.applyContentChanges((Content) entity.clone(), update);
    List<Content> alternateVersions = this.ownerContentCurator.getContentByVersions(owner, Collections.<String, Integer>singletonMap(updated.getId(), updated.getEntityVersion())).list();
    log.debug("Checking {} alternate content versions", alternateVersions.size());
    for (Content alt : alternateVersions) {
        if (alt.equals(updated)) {
            log.debug("Converging product with existing: {} => {}", updated, alt);
            // Make sure every product using the old version/entity are updated to use the new one
            List<Product> affectedProducts = this.productCurator.getProductsByContent(owner, Arrays.asList(updated.getId())).list();
            this.ownerContentCurator.updateOwnerContentReferences(owner, Collections.<String, String>singletonMap(entity.getUuid(), alt.getUuid()));
            log.debug("Updating {} affected products", affectedProducts.size());
            ContentDTO cdto = this.modelTranslator.translate(alt, ContentDTO.class);
            // TODO: Should we bulk this up like we do in importContent? Probably.
            for (Product product : affectedProducts) {
                log.debug("Updating affected product: {}", product);
                ProductDTO pdto = this.modelTranslator.translate(product, ProductDTO.class);
                ProductContentDTO pcdto = pdto.getProductContent(cdto.getId());
                if (pcdto != null) {
                    pdto.addContent(cdto, pcdto.isEnabled());
                    // Impl note: This should also take care of our entitlement cert regeneration
                    this.productManager.updateProduct(pdto, owner, regenerateEntitlementCerts);
                }
            }
            return alt;
        }
    }
    // Temporarily (?) disabled. If we ever move to clustered caching (rather than per-instance
    // caching, this branch should be re-enabled.
    /*
        // No alternate versions with which to converge. Check if we can do an in-place update instead
        if (this.ownerContentCurator.getOwnerCount(updated) < 2) {
            log.debug("Applying in-place update to content: {}", updated);

            updated = this.contentCurator.merge(this.applyContentChanges(entity, update, owner));

            if (regenerateEntitlementCerts) {
                // Every owner with a pool using any of the affected products needs an update.
                List<Product> affectedProducts = this.productCurator
                    .getProductsByContent(Arrays.asList(updated.getUuid()))
                    .list();

                this.entitlementCertGenerator.regenerateCertificatesOf(
                    Arrays.asList(owner), affectedProducts, true
                );
            }

            return updated;
        }
        */
    log.debug("Forking content and applying update: {}", updated);
    // Get products that currently use this content...
    List<Product> affectedProducts = this.productCurator.getProductsByContent(owner, Arrays.asList(updated.getId())).list();
    // Clear the UUID so Hibernate doesn't think our copy is a detached entity
    updated.setUuid(null);
    updated = this.contentCurator.create(updated);
    this.ownerContentCurator.updateOwnerContentReferences(owner, Collections.<String, String>singletonMap(entity.getUuid(), updated.getUuid()));
    // Impl note:
    // This block is a consequence of products and contents not being strongly related.
    log.debug("Updating {} affected products", affectedProducts.size());
    ContentDTO cdto = this.modelTranslator.translate(updated, ContentDTO.class);
    // TODO: Should we bulk this up like we do in importContent? Probably.
    for (Product product : affectedProducts) {
        log.debug("Updating affected product: {}", product);
        ProductDTO pdto = this.modelTranslator.translate(product, ProductDTO.class);
        ProductContentDTO pcdto = pdto.getProductContent(cdto.getId());
        if (pcdto != null) {
            pdto.addContent(cdto, pcdto.isEnabled());
            // Impl note: This should also take care of our entitlement cert regeneration
            this.productManager.updateProduct(pdto, owner, regenerateEntitlementCerts);
        }
    }
    return updated;
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ContentDTO(org.candlepin.dto.api.v1.ContentDTO) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) OwnerContent(org.candlepin.model.OwnerContent) Product(org.candlepin.model.Product) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Transactional(com.google.inject.persist.Transactional)

Example 5 with Content

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

the class ProductManager method applyProductChanges.

/**
 * Applies the changes from the given DTO to the specified entity
 *
 * @param entity
 *  The entity to modify
 *
 * @param update
 *  The DTO containing the modifications to apply
 *
 * @param content
 *  A mapping of Red Hat content ID to content entities to use for content resolution
 *
 * @throws IllegalArgumentException
 *  if entity, update or owner is null
 *
 * @return
 *  The updated product entity
 */
private Product applyProductChanges(Product entity, ProductDTO update, Map<String, Content> contentMap) {
    if (entity == null) {
        throw new IllegalArgumentException("entity is null");
    }
    if (update == null) {
        throw new IllegalArgumentException("update is null");
    }
    if (contentMap == null) {
        throw new IllegalArgumentException("contentMap is null");
    }
    if (update.getName() != null) {
        entity.setName(update.getName());
    }
    if (update.getMultiplier() != null) {
        entity.setMultiplier(update.getMultiplier());
    }
    if (update.getAttributes() != null) {
        entity.setAttributes(update.getAttributes());
    }
    if (update.getProductContent() != null) {
        Collection<ProductContent> productContent = new LinkedList<>();
        // Sort the existing ProductContent so we aren't iterating on it several times.
        // TODO: Remove this if/when product content is stored as a map on products.
        Map<String, ProductContent> existingLinks = new HashMap<>();
        for (ProductContent pc : entity.getProductContent()) {
            existingLinks.put(pc.getContent().getId(), pc);
        }
        // Actually process our list of content...
        for (ProductContentDTO pcd : update.getProductContent()) {
            if (pcd == null) {
                throw new IllegalStateException("Product data contains a null product-content mapping: " + update);
            }
            ContentDTO cdto = pcd.getContent();
            if (cdto == null || cdto.getId() == null) {
                // adding it to our link object. This is very bad.
                throw new IllegalStateException("Product data contains an incomplete product-content " + "mapping: " + update);
            }
            ProductContent existingLink = existingLinks.get(cdto.getId());
            Content content = contentMap.get(cdto.getId());
            if (content == null) {
                // Content doesn't exist yet -- it should have been created already
                throw new IllegalStateException("product references content which does not exist: " + cdto);
            }
            if (existingLink == null) {
                existingLink = new ProductContent(entity, content, pcd.isEnabled() != null ? pcd.isEnabled() : false);
            } else {
                existingLink.setContent(content);
                if (pcd.isEnabled() != null) {
                    existingLink.setEnabled(pcd.isEnabled());
                }
            }
            productContent.add(existingLink);
        }
        entity.setProductContent(productContent);
    }
    if (update.getDependentProductIds() != null) {
        entity.setDependentProductIds(update.getDependentProductIds());
    }
    if (update.isLocked() != null) {
        entity.setLocked(update.isLocked());
    }
    return entity;
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ContentDTO(org.candlepin.dto.api.v1.ContentDTO) HashMap(java.util.HashMap) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductContent(org.candlepin.model.ProductContent) LinkedList(java.util.LinkedList)

Aggregations

Content (org.candlepin.model.Content)97 Test (org.junit.Test)45 ProductContent (org.candlepin.model.ProductContent)41 Product (org.candlepin.model.Product)40 Owner (org.candlepin.model.Owner)39 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)25 HashMap (java.util.HashMap)18 EnvironmentContent (org.candlepin.model.EnvironmentContent)17 HashSet (java.util.HashSet)14 LinkedList (java.util.LinkedList)11 ProductDTO (org.candlepin.dto.api.v1.ProductDTO)10 ArrayList (java.util.ArrayList)9 Matchers.anyString (org.mockito.Matchers.anyString)9 Transactional (com.google.inject.persist.Transactional)8 Produces (javax.ws.rs.Produces)8 Parameters (junitparams.Parameters)8 ApiOperation (io.swagger.annotations.ApiOperation)7 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)7 Path (javax.ws.rs.Path)6 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)6