Search in sources :

Example 1 with PoolFilterBuilder

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

the class CandlepinPoolManager method getBestPoolsForHost.

/**
 * Here we pick uncovered products from the guest where no virt-only
 * subscriptions exist, and have the host bind non-zero virt_limit
 * subscriptions in order to generate pools for the guest to bind later.
 *
 * @param guest whose products we want to provide
 * @param host to bind entitlements to
 * @param entitleDate
 * @param owner
 * @param serviceLevelOverride
 * @return PoolQuantity list to attempt to attach
 * @throws EntitlementRefusedException if unable to bind
 */
@Override
@SuppressWarnings("checkstyle:methodlength")
public List<PoolQuantity> getBestPoolsForHost(Consumer guest, Consumer host, Date entitleDate, String ownerId, String serviceLevelOverride, Collection<String> fromPools) throws EntitlementRefusedException {
    Map<String, ValidationResult> failedResults = new HashMap<>();
    log.debug("Looking up best pools for host: {}", host);
    boolean tempLevel = false;
    if (StringUtils.isEmpty(host.getServiceLevel())) {
        host.setServiceLevel(guest.getServiceLevel());
        tempLevel = true;
    }
    Date activePoolDate = entitleDate;
    if (entitleDate == null) {
        activePoolDate = new Date();
    }
    PoolFilterBuilder poolFilter = new PoolFilterBuilder();
    poolFilter.addIdFilters(fromPools);
    List<Pool> allOwnerPools = this.listAvailableEntitlementPools(host, null, ownerId, null, null, activePoolDate, false, poolFilter, null, false, false, null).getPageData();
    log.debug("Found {} total pools in org.", allOwnerPools.size());
    logPools(allOwnerPools);
    List<Pool> allOwnerPoolsForGuest = this.listAvailableEntitlementPools(guest, null, ownerId, null, null, activePoolDate, false, poolFilter, null, false, false, null).getPageData();
    log.debug("Found {} total pools already available for guest", allOwnerPoolsForGuest.size());
    logPools(allOwnerPoolsForGuest);
    for (Entitlement ent : host.getEntitlements()) {
        // filter out pools that are attached, there is no need to
        // complete partial stacks, as they are already granting
        // virtual pools
        log.debug("Removing pool host is already entitled to: {}", ent.getPool());
        allOwnerPools.remove(ent.getPool());
    }
    List<Pool> filteredPools = new LinkedList<>();
    ComplianceStatus guestCompliance = complianceRules.getStatus(guest, entitleDate, false);
    Set<String> tmpSet = new HashSet<>();
    // we only want to heal red products, not yellow
    tmpSet.addAll(guestCompliance.getNonCompliantProducts());
    log.debug("Guest's non-compliant products: {}", Util.collectionToString(tmpSet));
    /*Do not attempt to create subscriptions for products that
          already have virt_only pools available to the guest */
    Set<String> productsToRemove = getProductsToRemove(allOwnerPoolsForGuest, tmpSet);
    log.debug("Guest already will have virt-only pools to cover: {}", Util.collectionToString(productsToRemove));
    tmpSet.removeAll(productsToRemove);
    String[] productIds = tmpSet.toArray(new String[] {});
    if (log.isDebugEnabled()) {
        log.debug("Attempting host autobind for guest products: {}", Util.collectionToString(tmpSet));
    }
    // Bulk fetch our provided and derived provided product IDs so we're not hitting the DB
    // several times for this lookup.
    Map<String, Set<String>> providedProductIds = this.poolCurator.getProvidedProductIds(allOwnerPools);
    Map<String, Set<String>> derivedProvidedProductIds = this.poolCurator.getDerivedProvidedProductIds(allOwnerPools);
    for (Pool pool : allOwnerPools) {
        boolean providesProduct = false;
        // and we only need to check that it's non-zero
        if (pool.getProduct().hasAttribute(Product.Attributes.VIRT_LIMIT) && !pool.getProduct().getAttributeValue(Product.Attributes.VIRT_LIMIT).equals("0")) {
            Map<String, Set<String>> providedProductMap;
            String baseProductId;
            // Determine which set of provided products we should use...
            if (pool.getDerivedProduct() != null) {
                providedProductMap = derivedProvidedProductIds;
                baseProductId = pool.getDerivedProduct().getId();
            } else {
                providedProductMap = providedProductIds;
                baseProductId = pool.getProduct().getId();
            }
            // Add the base product to the list of derived provided products...
            Set<String> poolProvidedProductIds = providedProductMap.get(pool.getId());
            if (baseProductId != null) {
                if (poolProvidedProductIds != null) {
                    poolProvidedProductIds.add(baseProductId);
                } else {
                    poolProvidedProductIds = Collections.<String>singleton(baseProductId);
                }
            }
            // Check if the pool provides any of the specified products
            if (poolProvidedProductIds != null) {
                for (String productId : productIds) {
                    // provides anything for the guest, otherwise we use the parent.
                    if (poolProvidedProductIds.contains(productId)) {
                        log.debug("Found virt_limit pool providing product {}: {}", productId, pool);
                        providesProduct = true;
                        break;
                    }
                }
            }
        }
        if (providesProduct) {
            ValidationResult result = enforcer.preEntitlement(host, pool, 1, CallerType.BEST_POOLS);
            if (result.hasErrors() || result.hasWarnings()) {
                // Just keep the last one around, if we need it
                failedResults.put(pool.getId(), result);
                if (log.isDebugEnabled()) {
                    log.debug("Pool filtered from candidates due to failed rule(s): {}", pool);
                    log.debug("  warnings: {}", Util.collectionToString(result.getWarnings()));
                    log.debug("  errors: {}", Util.collectionToString(result.getErrors()));
                }
            } else {
                filteredPools.add(pool);
            }
        }
    }
    // Only throw refused exception if we actually hit the rules:
    if (filteredPools.size() == 0 && !failedResults.isEmpty()) {
        throw new EntitlementRefusedException(failedResults);
    }
    ComplianceStatus hostCompliance = complianceRules.getStatus(host, entitleDate, false);
    log.debug("Host pools being sent to rules: {}", filteredPools.size());
    logPools(filteredPools);
    List<PoolQuantity> enforced = autobindRules.selectBestPools(host, productIds, filteredPools, hostCompliance, serviceLevelOverride, poolCurator.retrieveServiceLevelsForOwner(ownerId, true), true);
    if (log.isDebugEnabled()) {
        log.debug("Host selectBestPools returned {} pools: ", enforced.size());
        for (PoolQuantity poolQuantity : enforced) {
            log.debug("  " + poolQuantity.getPool());
        }
    }
    if (tempLevel) {
        host.setServiceLevel("");
        // complianceRules.getStatus may have persisted the host with the temp service level,
        // so we need to be certain we undo that.
        consumerCurator.update(host);
    }
    return enforced;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ComplianceStatus(org.candlepin.policy.js.compliance.ComplianceStatus) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) ValidationResult(org.candlepin.policy.ValidationResult) Date(java.util.Date) LinkedList(java.util.LinkedList) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet)

Example 2 with PoolFilterBuilder

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

the class PoolManagerFunctionalTest method testListConditionDevPools.

@Test
public void testListConditionDevPools() {
    Owner owner = createOwner();
    Product p = TestUtil.createProduct("test-product", "Test Product");
    productCurator.create(p);
    Pool pool1 = createPool(owner, p, 10L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
    pool1.setAttribute(Pool.Attributes.DEVELOPMENT_POOL, "true");
    poolCurator.create(pool1);
    Pool pool2 = createPool(owner, p, 10L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
    poolCurator.create(pool2);
    Consumer devSystem = new Consumer("dev", "user", owner, systemType);
    devSystem.setFact("dev_sku", p.getId());
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(p));
    Consumer nonDevSystem = new Consumer("system", "user", owner, systemType);
    nonDevSystem.addInstalledProduct(new ConsumerInstalledProduct(p));
    Page<List<Pool>> results = poolManager.listAvailableEntitlementPools(devSystem, null, owner.getId(), null, null, null, true, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    assertEquals(2, results.getPageData().size());
    results = poolManager.listAvailableEntitlementPools(nonDevSystem, null, owner.getId(), null, null, null, true, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    assertEquals(1, results.getPageData().size());
    Pool found2 = results.getPageData().get(0);
    assertEquals(pool2, found2);
}
Also used : Owner(org.candlepin.model.Owner) PageRequest(org.candlepin.common.paging.PageRequest) Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with PoolFilterBuilder

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

the class PoolManagerFunctionalTest method testListAllForConsumerExcludesErrors.

@Test
public void testListAllForConsumerExcludesErrors() {
    Product p = TestUtil.createProduct("test-product", "Test Product");
    productCurator.create(p);
    Page<List<Pool>> results = poolManager.listAvailableEntitlementPools(parentSystem, null, parentSystem.getOwnerId(), null, null, null, true, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    assertEquals(4, results.getPageData().size());
    // Creating a pool with no entitlements available, which will trigger
    // a rules error:
    Pool pool = createPool(o, p, 0L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
    poolCurator.create(pool);
    results = poolManager.listAvailableEntitlementPools(parentSystem, null, parentSystem.getOwnerId(), null, null, null, true, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    // Pool in error should not be included. Should have the same number of
    // initial pools.
    assertEquals(4, results.getPageData().size());
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Pool(org.candlepin.model.Pool) Test(org.junit.Test)

Example 4 with PoolFilterBuilder

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

the class PoolManagerFunctionalTest method testListAllForActKeyExcludesErrors.

@Test
public void testListAllForActKeyExcludesErrors() {
    Product p = TestUtil.createProduct("test-product", "Test Product");
    productCurator.create(p);
    ActivationKey ak = new ActivationKey();
    Pool akpool = new Pool();
    akpool.setAttribute(Pool.Attributes.PHYSICAL_ONLY, "true");
    ak.addPool(akpool, 1L);
    Page<List<Pool>> results = poolManager.listAvailableEntitlementPools(null, ak, parentSystem.getOwnerId(), null, null, null, true, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    assertEquals(4, results.getPageData().size());
    // Creating a pool with no entitlements available, which does not trigger
    // a rules error:
    Pool pool = createPool(o, p, 0L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
    poolCurator.create(pool);
    results = poolManager.listAvailableEntitlementPools(null, ak, parentSystem.getOwnerId(), null, null, null, true, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    // Pool in error should not be included. Should have the same number of
    // initial pools.
    assertEquals(5, results.getPageData().size());
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) Pool(org.candlepin.model.Pool) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Test(org.junit.Test)

Example 5 with PoolFilterBuilder

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

the class PoolManagerFunctionalTest method testListForConsumerExcludesWarnings.

@Test
public void testListForConsumerExcludesWarnings() {
    Page<List<Pool>> results = poolManager.listAvailableEntitlementPools(parentSystem, null, parentSystem.getOwnerId(), (String) null, null, null, true, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    assertEquals(4, results.getPageData().size());
    Pool pool = createPool(o, socketLimitedProduct, 100L, TestUtil.createDate(2000, 3, 2), TestUtil.createDate(2050, 3, 2));
    poolCurator.create(pool);
    parentSystem.setFact("cpu.cpu_socket(s)", "4");
    results = poolManager.listAvailableEntitlementPools(parentSystem, null, parentSystem.getOwnerId(), (String) null, null, null, false, new PoolFilterBuilder(), new PageRequest(), false, false, null);
    // Pool in error should not be included. Should have the same number of
    // initial pools.
    assertEquals(4, results.getPageData().size());
}
Also used : PageRequest(org.candlepin.common.paging.PageRequest) PoolFilterBuilder(org.candlepin.model.PoolFilterBuilder) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Pool(org.candlepin.model.Pool) Test(org.junit.Test)

Aggregations

PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)11 LinkedList (java.util.LinkedList)10 Pool (org.candlepin.model.Pool)10 ArrayList (java.util.ArrayList)8 List (java.util.List)8 PageRequest (org.candlepin.common.paging.PageRequest)7 Test (org.junit.Test)6 Product (org.candlepin.model.Product)5 Date (java.util.Date)4 Consumer (org.candlepin.model.Consumer)4 HashSet (java.util.HashSet)3 Set (java.util.Set)3 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)3 Owner (org.candlepin.model.Owner)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 HashMap (java.util.HashMap)2 GET (javax.ws.rs.GET)2 Entitlement (org.candlepin.model.Entitlement)2 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)2