Search in sources :

Example 1 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class ResolverUtil method resolveSubscription.

public Subscription resolveSubscription(Subscription subscription) {
    // need to make sure it's not null.
    if (subscription == null) {
        throw new BadRequestException(i18n.tr("No subscription specified"));
    }
    // Ensure the owner is set and is valid
    Owner owner = this.resolveOwner(subscription.getOwner());
    subscription.setOwner(owner);
    // Ensure the specified product(s) exists for the given owner
    this.validateProductData(subscription.getProduct(), owner, false);
    this.validateProductData(subscription.getDerivedProduct(), owner, true);
    for (ProductData product : subscription.getProvidedProducts()) {
        this.validateProductData(product, owner, true);
    }
    for (ProductData product : subscription.getDerivedProvidedProducts()) {
        this.validateProductData(product, owner, true);
    }
    return subscription;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException)

Example 2 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class ResolverUtil method resolveSubscriptionAndProduct.

/**
 * used to resolve subscription but it resolves the product too.
 * currently used in hostedtest resources
 * @param subscription
 * @return the resolved subscription
 */
public Subscription resolveSubscriptionAndProduct(Subscription subscription) {
    // We just need to make sure it's not null.
    if (subscription == null) {
        throw new BadRequestException(i18n.tr("No subscription specified"));
    }
    // Ensure the owner is set and is valid
    Owner owner = this.resolveOwner(subscription.getOwner());
    subscription.setOwner(owner);
    subscription.setProduct(new ProductData(this.resolveProduct(owner, subscription.getProduct().getId())));
    if (subscription.getDerivedProduct() != null) {
        ProductData p = new ProductData(this.resolveProduct(owner, subscription.getDerivedProduct().getId()));
        subscription.setDerivedProduct(p);
    }
    HashSet<ProductData> providedProducts = new HashSet<>();
    for (ProductData product : subscription.getProvidedProducts()) {
        if (product != null) {
            providedProducts.add(new ProductData(this.resolveProduct(owner, product.getId())));
        }
    }
    subscription.setProvidedProducts(providedProducts);
    HashSet<ProductData> derivedProvidedProducts = new HashSet<>();
    for (ProductData product : subscription.getDerivedProvidedProducts()) {
        if (product != null) {
            derivedProvidedProducts.add(new ProductData(this.resolveProduct(owner, product.getId())));
        }
    }
    subscription.setDerivedProvidedProducts(derivedProvidedProducts);
    return subscription;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) HashSet(java.util.HashSet)

Example 3 with ProductData

use of org.candlepin.model.dto.ProductData 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 4 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class CandlepinPoolManager method convertToMasterPool.

/*
     * if you are using this method, you might want to override the quantity
     * with PoolRules.calculateQuantity
     */
@Override
public Pool convertToMasterPool(Subscription sub) {
    if (sub == null) {
        throw new IllegalArgumentException("subscription is null");
    }
    // Resolve the subscription's owner...
    if (sub.getOwner() == null || (sub.getOwner().getId() == null && sub.getOwner().getKey() == null)) {
        throw new IllegalStateException("Subscription references an invalid owner: " + sub.getOwner());
    }
    Owner owner = sub.getOwner().getId() != null ? this.ownerCurator.find(sub.getOwner().getId()) : this.ownerCurator.lookupByKey(sub.getOwner().getKey());
    if (owner == null) {
        throw new IllegalStateException("Subscription references an owner which cannot be resolved: " + sub.getOwner());
    }
    // Gather the product IDs referenced by this subscription...
    Set<ProductData> productData = new HashSet<>();
    Set<String> productIds = new HashSet<>();
    Map<String, Product> productMap = new HashMap<>();
    productData.add(sub.getProduct());
    productData.add(sub.getDerivedProduct());
    if (sub.getProvidedProducts() != null) {
        productData.addAll(sub.getProvidedProducts());
    }
    if (sub.getDerivedProvidedProducts() != null) {
        productData.addAll(sub.getDerivedProvidedProducts());
    }
    for (ProductData pdata : productData) {
        if (pdata != null) {
            if (pdata.getId() == null) {
                throw new IllegalStateException("Subscription references an incomplete product: " + pdata);
            }
            productIds.add(pdata.getId());
        }
    }
    // Build the product map from the product IDs we pulled off the subscription...
    for (Product product : this.ownerProductCurator.getProductsByIds(owner, productIds)) {
        productMap.put(product.getId(), product);
    }
    return this.convertToMasterPoolImpl(sub, owner, productMap);
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) Product(org.candlepin.model.Product) HashSet(java.util.HashSet)

Example 5 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class SubscriptionReconcilerTest method createSubscription.

private Subscription createSubscription(Owner daOwner, String productId, String poolId, String entId, String conId, long quantity) {
    ProductData pdata = new ProductData();
    pdata.setId(productId);
    pdata.setName(productId);
    Subscription sub = new Subscription();
    sub.setProduct(pdata);
    sub.setUpstreamPoolId(poolId);
    sub.setUpstreamEntitlementId(entId);
    sub.setUpstreamConsumerId(conId);
    sub.setQuantity(quantity);
    sub.setOwner(daOwner);
    sub.setId("" + index++);
    return sub;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Subscription(org.candlepin.model.dto.Subscription)

Aggregations

ProductData (org.candlepin.model.dto.ProductData)30 Product (org.candlepin.model.Product)22 Pool (org.candlepin.model.Pool)13 Owner (org.candlepin.model.Owner)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 List (java.util.List)10 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)10 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)8 SourceSubscription (org.candlepin.model.SourceSubscription)8 Subscription (org.candlepin.model.dto.Subscription)8 Date (java.util.Date)7 Consumer (org.candlepin.model.Consumer)7 LinkedList (java.util.LinkedList)5 Content (org.candlepin.model.Content)5 ProductContentData (org.candlepin.model.dto.ProductContentData)5 AutobindData (org.candlepin.resource.dto.AutobindData)5 Map (java.util.Map)4 ContentData (org.candlepin.model.dto.ContentData)4