Search in sources :

Example 16 with SourceSubscription

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

the class AutobindRulesTest method createPool.

protected Pool createPool(Owner owner, Product product, int quantity, Date startDate, Date endDate) {
    Pool p = TestUtil.createPool(owner, product, quantity);
    p.setId("testpool" + TestUtil.randomInt());
    p.setSourceSubscription(new SourceSubscription("testsub" + TestUtil.randomInt(), "master"));
    p.setStartDate(startDate);
    p.setEndDate(endDate);
    return p;
}
Also used : SourceSubscription(org.candlepin.model.SourceSubscription) Pool(org.candlepin.model.Pool)

Example 17 with SourceSubscription

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

the class PoolHelper method createHostRestrictedPools.

/**
 * Create a pool only for virt guests of a particular host consumer.
 *
 * @param pools Pools these host restricted pools are being derived from.
 * @return pools the created pools
 */
public static List<Pool> createHostRestrictedPools(PoolManager poolManager, Consumer consumer, List<Pool> pools, Map<String, Entitlement> sourceEntitlements, Map<String, Map<String, String>> attributeMaps, ProductCurator productCurator) {
    List<Pool> poolsToCreate = new ArrayList<>();
    List<Pool> poolsToUpdateFromStack = new ArrayList<>();
    for (Pool pool : pools) {
        Product product = pool.getProduct();
        Pool consumerSpecificPool = null;
        Map<String, String> attributes = attributeMaps.get(pool.getId());
        String quantity = attributes.get("virt_limit");
        if (pool.getDerivedProduct() == null) {
            consumerSpecificPool = createPool(product, pool.getOwner(), quantity, pool.getStartDate(), pool.getEndDate(), pool.getContractNumber(), pool.getAccountNumber(), pool.getOrderNumber(), productCurator.getPoolProvidedProductsCached(pool), sourceEntitlements.get(pool.getId()), pool.hasSharedAncestor());
        } else {
            // If a derived product is on the pool, we want to define the
            // derived pool
            // with the derived product data that was defined on the parent
            // pool,
            // allowing the derived pool to have different attributes than
            // the parent.
            consumerSpecificPool = createPool(pool.getDerivedProduct(), pool.getOwner(), quantity, pool.getStartDate(), pool.getEndDate(), pool.getContractNumber(), pool.getAccountNumber(), pool.getOrderNumber(), productCurator.getPoolDerivedProvidedProductsCached(pool), sourceEntitlements.get(pool.getId()), pool.hasSharedAncestor());
        }
        consumerSpecificPool.setAttribute(Pool.Attributes.REQUIRES_HOST, consumer.getUuid());
        consumerSpecificPool.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
        consumerSpecificPool.setAttribute(Pool.Attributes.VIRT_ONLY, "true");
        consumerSpecificPool.setAttribute(Pool.Attributes.PHYSICAL_ONLY, "false");
        // parent pool.
        if (pool.isStacked()) {
            poolsToUpdateFromStack.add(consumerSpecificPool);
        } else {
            // attribute per 795431, useful for rolling up pool info in headpin
            consumerSpecificPool.setAttribute(Pool.Attributes.SOURCE_POOL_ID, pool.getId());
            String subscriptionId = pool.getSubscriptionId();
            if (subscriptionId != null && !subscriptionId.isEmpty()) {
                consumerSpecificPool.setSourceSubscription(new SourceSubscription(subscriptionId, sourceEntitlements.get(pool.getId()).getId()));
            }
        }
        poolsToCreate.add(consumerSpecificPool);
    }
    if (CollectionUtils.isNotEmpty(poolsToUpdateFromStack)) {
        poolManager.updatePoolsFromStack(consumer, poolsToUpdateFromStack);
    }
    return poolManager.createPools(poolsToCreate);
}
Also used : SourceSubscription(org.candlepin.model.SourceSubscription) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool)

Example 18 with SourceSubscription

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

the class CandlepinPoolManager method convertToMasterPoolImpl.

/**
 * Builds a pool instance from the given subscription, using the specified owner and products
 * for resolution.
 * <p></p>
 * The provided owner and products will be used to match and resolve the owner and product
 * DTOs present on the subscription. If the subscription uses DTOs which cannot be resolved,
 * this method will throw an exception.
 *
 * @param sub
 *  The subscription to convert to a pool
 *
 * @param owner
 *  The owner the pool will be assigned to
 */
private Pool convertToMasterPoolImpl(Subscription sub, Owner owner, Map<String, Product> productMap) {
    if (sub == null) {
        throw new IllegalArgumentException("subscription is null");
    }
    if (owner == null || (owner.getKey() == null && owner.getId() == null)) {
        throw new IllegalArgumentException("owner is null or incomplete");
    }
    if (productMap == null) {
        throw new IllegalArgumentException("productMap is null");
    }
    Pool pool = new Pool();
    // Validate and resolve owner...
    if (sub.getOwner() == null || (sub.getOwner().getId() != null ? !owner.getId().equals(sub.getOwner().getId()) : !owner.getKey().equals(sub.getOwner().getKey()))) {
        throw new IllegalStateException("Subscription references an invalid owner: " + sub.getOwner());
    }
    pool.setOwner(owner);
    pool.setQuantity(sub.getQuantity());
    pool.setStartDate(sub.getStartDate());
    pool.setEndDate(sub.getEndDate());
    pool.setContractNumber(sub.getContractNumber());
    pool.setAccountNumber(sub.getAccountNumber());
    pool.setOrderNumber(sub.getOrderNumber());
    // Copy over subscription details
    pool.setSourceSubscription(new SourceSubscription(sub.getId(), "master"));
    // Copy over upstream details
    pool.setUpstreamPoolId(sub.getUpstreamPoolId());
    pool.setUpstreamEntitlementId(sub.getUpstreamEntitlementId());
    pool.setUpstreamConsumerId(sub.getUpstreamConsumerId());
    pool.setCdn(sub.getCdn());
    pool.setCertificate(sub.getCertificate());
    // Add in branding
    if (sub.getBranding() != null) {
        Set<Branding> branding = new HashSet<>();
        for (Branding brand : sub.getBranding()) {
            // Impl note:
            // We create a new instance here since we don't have a separate branding DTO (yet),
            // and we need to be certain that we don't try to move or change a branding object
            // associated with another pool.
            branding.add(new Branding(brand.getProductId(), brand.getType(), brand.getName()));
        }
        pool.setBranding(branding);
    }
    if (sub.getProduct() == null || sub.getProduct().getId() == null) {
        throw new IllegalStateException("Subscription has no product, or its product is incomplete: " + sub.getProduct());
    }
    Product product = productMap.get(sub.getProduct().getId());
    if (product == null) {
        throw new IllegalStateException("Subscription references a product which cannot be resolved: " + sub.getProduct());
    }
    pool.setProduct(product);
    if (sub.getDerivedProduct() != null) {
        product = productMap.get(sub.getDerivedProduct().getId());
        if (product == null) {
            throw new IllegalStateException("Subscription's derived product references a product which " + "cannot be resolved: " + sub.getDerivedProduct());
        }
        pool.setDerivedProduct(product);
    }
    if (sub.getProvidedProducts() != null) {
        Set<Product> products = new HashSet<>();
        for (ProductData pdata : sub.getProvidedProducts()) {
            if (pdata != null) {
                product = productMap.get(pdata.getId());
                if (product == null) {
                    throw new IllegalStateException("Subscription's provided products references a " + "product which cannot be resolved: " + pdata);
                }
                products.add(product);
            }
        }
        pool.setProvidedProducts(products);
    }
    if (sub.getDerivedProvidedProducts() != null) {
        Set<Product> products = new HashSet<>();
        for (ProductData pdata : sub.getDerivedProvidedProducts()) {
            if (pdata != null) {
                product = productMap.get(pdata.getId());
                if (product == null) {
                    throw new IllegalStateException("Subscription's derived provided products " + "references a product which cannot be resolved: " + pdata);
                }
                products.add(product);
            }
        }
        pool.setDerivedProvidedProducts(products);
    }
    return pool;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) SourceSubscription(org.candlepin.model.SourceSubscription) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Branding(org.candlepin.model.Branding) HashSet(java.util.HashSet)

Example 19 with SourceSubscription

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

the class DatabaseTestFixture method createPool.

/**
 * Create an entitlement pool.
 *
 * @return an entitlement pool
 */
protected Pool createPool(Owner owner, Product product, Long quantity, Date startDate, Date endDate) {
    Pool pool = new Pool(owner, product, new HashSet<>(), quantity, startDate, endDate, DEFAULT_CONTRACT, DEFAULT_ACCOUNT, DEFAULT_ORDER);
    pool.setSourceSubscription(new SourceSubscription(Util.generateDbUUID(), "master"));
    return poolCurator.create(pool);
}
Also used : SourceSubscription(org.candlepin.model.SourceSubscription) Pool(org.candlepin.model.Pool)

Example 20 with SourceSubscription

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

the class PoolManagerTest method createPoolsForPoolBonusExist.

@Test(expected = IllegalStateException.class)
public void createPoolsForPoolBonusExist() {
    Owner owner = this.getOwner();
    PoolRules pRules = new PoolRules(manager, mockConfig, entitlementCurator, mockOwnerProductCurator, mockProductCurator);
    Product prod = TestUtil.createProduct();
    prod.setAttribute(Product.Attributes.VIRT_LIMIT, "4");
    List<Pool> existingPools = new LinkedList<>();
    Pool p = TestUtil.createPool(prod);
    p.setSourceSubscription(new SourceSubscription(TestUtil.randomString(), "derived"));
    existingPools.add(p);
    pRules.createAndEnrichPools(p, existingPools);
    List<Pool> newPools = pRules.createAndEnrichPools(p, existingPools);
    assertEquals(1, newPools.size());
    assertEquals("master", newPools.get(0).getSourceSubscription().getSubscriptionSubKey());
}
Also used : Owner(org.candlepin.model.Owner) SourceSubscription(org.candlepin.model.SourceSubscription) PoolRules(org.candlepin.policy.js.pool.PoolRules) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

Pool (org.candlepin.model.Pool)31 SourceSubscription (org.candlepin.model.SourceSubscription)31 Product (org.candlepin.model.Product)23 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)19 Owner (org.candlepin.model.Owner)19 Subscription (org.candlepin.model.dto.Subscription)18 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)15 LinkedList (java.util.LinkedList)14 PoolType (org.candlepin.model.Pool.PoolType)10 Entitlement (org.candlepin.model.Entitlement)9 HashSet (java.util.HashSet)8 List (java.util.List)7 Branding (org.candlepin.model.Branding)7 HashMap (java.util.HashMap)6 Consumer (org.candlepin.model.Consumer)6 Matchers.anyList (org.mockito.Matchers.anyList)6 Map (java.util.Map)5 Date (java.util.Date)4 CandlepinQuery (org.candlepin.model.CandlepinQuery)4