Search in sources :

Example 51 with PoolQuantity

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

the class EntitlementRules method preEntitlement.

public ValidationResult preEntitlement(Consumer consumer, Consumer host, Pool entitlementPool, Integer quantity, CallerType caller) {
    List<PoolQuantity> poolQuantities = new ArrayList<>();
    poolQuantities.add(new PoolQuantity(entitlementPool, quantity));
    return preEntitlement(consumer, host, poolQuantities, caller).get(entitlementPool.getId());
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) ArrayList(java.util.ArrayList)

Example 52 with PoolQuantity

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

the class EntitlementRules method preEntitlement.

@Override
@SuppressWarnings("checkstyle:indentation")
public Map<String, ValidationResult> preEntitlement(Consumer consumer, Consumer host, Collection<PoolQuantity> entitlementPoolQuantities, CallerType caller) {
    Map<String, ValidationResult> resultMap = new HashMap<>();
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    /* This document describes the java script portion of the pre entitlement rules check:
         * http://www.candlepinproject.org/docs/candlepin/pre_entitlement_rules_check.html
         * As described in the document, none of the checks are related to share binds, so we
         * skip that step for share consumers.
         */
    if (!ctype.isType(ConsumerTypeEnum.SHARE)) {
        Stream<EntitlementDTO> entStream = consumer.getEntitlements() == null ? Stream.empty() : consumer.getEntitlements().stream().map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
        JsonJsContext args = new JsonJsContext(objectMapper);
        args.put("consumer", this.translator.translate(consumer, ConsumerDTO.class));
        args.put("hostConsumer", this.translator.translate(host, ConsumerDTO.class));
        args.put("consumerEntitlements", entStream.collect(Collectors.toSet()));
        args.put("standalone", config.getBoolean(ConfigProperties.STANDALONE));
        args.put("poolQuantities", entitlementPoolQuantities);
        args.put("caller", caller.getLabel());
        args.put("log", log, false);
        String json = jsRules.runJsFunction(String.class, "validate_pools_batch", args);
        TypeReference<Map<String, ValidationResult>> typeref = new TypeReference<Map<String, ValidationResult>>() {
        };
        try {
            resultMap = objectMapper.toObject(json, typeref);
            for (PoolQuantity poolQuantity : entitlementPoolQuantities) {
                if (!resultMap.containsKey(poolQuantity.getPool().getId())) {
                    resultMap.put(poolQuantity.getPool().getId(), new ValidationResult());
                    log.info("no result returned for pool: {}", poolQuantity.getPool());
                }
            }
        } catch (Exception e) {
            throw new RuleExecutionException(e);
        }
    }
    for (PoolQuantity poolQuantity : entitlementPoolQuantities) {
        if (ctype.isType(ConsumerTypeEnum.SHARE)) {
            ValidationResult result = new ValidationResult();
            resultMap.put(poolQuantity.getPool().getId(), result);
            validatePoolSharingEligibility(result, poolQuantity.getPool());
        }
        finishValidation(resultMap.get(poolQuantity.getPool().getId()), poolQuantity.getPool(), poolQuantity.getQuantity());
    }
    return resultMap;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) HashMap(java.util.HashMap) ValidationResult(org.candlepin.policy.ValidationResult) RuleExecutionException(org.candlepin.policy.js.RuleExecutionException) EntitlementDTO(org.candlepin.dto.rules.v1.EntitlementDTO) ConsumerDTO(org.candlepin.dto.rules.v1.ConsumerDTO) JsonJsContext(org.candlepin.policy.js.JsonJsContext) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RuleExecutionException(org.candlepin.policy.js.RuleExecutionException) ConsumerType(org.candlepin.model.ConsumerType) Map(java.util.Map) HashMap(java.util.HashMap)

Example 53 with PoolQuantity

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

the class AutobindRules method selectBestPools.

public List<PoolQuantity> selectBestPools(Consumer consumer, String[] productIds, List<Pool> pools, ComplianceStatus compliance, String serviceLevelOverride, Set<String> exemptLevels, boolean considerDerived) {
    List<PoolQuantity> bestPools = new ArrayList<>();
    int poolsBeforeContentFilter = pools.size();
    pools = filterPoolsForV1Certificates(consumer, pools);
    log.debug("pools.size() before V1 certificate filter: {}, after: {}", poolsBeforeContentFilter, pools.size());
    if (pools.size() == 0) {
        if (compliance.getReasons().size() == 0) {
            log.info("Consumer is compliant and does not require more entitlements.");
        } else {
            logProducts("No pools available to complete compliance for the set of products: {}" + " and consumer installed products: {}", productIds, consumer, false);
        }
        return bestPools;
    }
    if (log.isDebugEnabled()) {
        logProducts("Selecting best entitlement pool for products: {}" + "and consumer installed products: {}", productIds, consumer, true);
        if (poolsBeforeContentFilter != pools.size()) {
            log.debug("{} pools filtered due to too much content", (poolsBeforeContentFilter - pools.size()));
        }
    }
    List<PoolDTO> poolDTOs = new ArrayList<>();
    for (Pool pool : pools) {
        poolDTOs.add(this.translator.translate(pool, PoolDTO.class));
    }
    // Provide objects for the script:
    JsonJsContext args = new JsonJsContext(mapper);
    args.put("consumer", this.translator.translate(consumer, ConsumerDTO.class));
    Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
    args.put("owner", this.translator.translate(owner, OwnerDTO.class));
    args.put("serviceLevelOverride", serviceLevelOverride);
    args.put("pools", poolDTOs.toArray());
    args.put("products", productIds);
    args.put("log", log, false);
    args.put("compliance", compliance);
    args.put("exemptList", exemptLevels);
    args.put("considerDerived", considerDerived);
    args.put("guestIds", consumer.getGuestIds());
    // Convert the JSON returned into a Map object:
    Map<String, Integer> result = null;
    try {
        String json = jsRules.invokeMethod(SELECT_POOL_FUNCTION, args);
        result = mapper.toObject(json, Map.class);
        if (log.isDebugEnabled()) {
            log.debug("Executed javascript rule: {}", SELECT_POOL_FUNCTION);
        }
    } catch (NoSuchMethodException e) {
        log.warn("No method found: {}", SELECT_POOL_FUNCTION);
        log.warn("Resorting to default pool selection behavior.");
        return selectBestPoolDefault(pools);
    } catch (RhinoException e) {
        throw new RuleExecutionException(e);
    }
    if (pools.size() > 0 && (result == null || result.isEmpty())) {
        logProducts("Rules did not select a pool for products: {} and consumer installed products: {}", productIds, consumer, false);
        return bestPools;
    }
    for (Pool p : pools) {
        for (Entry<String, Integer> entry : result.entrySet()) {
            if (p.getId().equals(entry.getKey())) {
                log.debug("Best pool: {}", p);
                int quantity = entry.getValue();
                bestPools.add(new PoolQuantity(p, quantity));
            }
        }
    }
    return bestPools;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) PoolDTO(org.candlepin.dto.rules.v1.PoolDTO) ConsumerDTO(org.candlepin.dto.rules.v1.ConsumerDTO) OwnerDTO(org.candlepin.dto.rules.v1.OwnerDTO) Pool(org.candlepin.model.Pool) JsonJsContext(org.candlepin.policy.js.JsonJsContext) RhinoException(org.mozilla.javascript.RhinoException) RuleExecutionException(org.candlepin.policy.js.RuleExecutionException) Map(java.util.Map)

Example 54 with PoolQuantity

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

the class AutobindRulesTest method testSelectBestPoolsFiltersTooMuchContent.

@Test
public void testSelectBestPoolsFiltersTooMuchContent() {
    Pool pool = createV3OnlyPool();
    List<Pool> pools = new LinkedList<>();
    pools.add(pool);
    List<PoolQuantity> poolQs = autobindRules.selectBestPools(consumer, new String[] { productId }, pools, compliance, null, new HashSet<>(), false);
    assertEquals(0, poolQs.size());
    // Try again with explicitly setting the consumer to cert v1:
    consumer.setFact("system.certificate_version", "1.0");
    poolQs = autobindRules.selectBestPools(consumer, new String[] { productId }, pools, compliance, null, new HashSet<>(), false);
    assertEquals(0, poolQs.size());
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Pool(org.candlepin.model.Pool) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 55 with PoolQuantity

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

the class AutobindRulesTest method autobindForPhysicalSocketPicksBestFitOvercoverageStack.

@Test
public void autobindForPhysicalSocketPicksBestFitOvercoverageStack() {
    List<Pool> pools = createSocketPool(2, 100, "1");
    pools.addAll(createSocketPool(32, 100, "1"));
    setupConsumer("8", false);
    List<PoolQuantity> bestPools = autobindRules.selectBestPools(consumer, new String[] { productId }, pools, compliance, null, new HashSet<>(), false);
    // Should always pick the 2 socket subscriptions because there is no over-coverage
    assertEquals(1, bestPools.size());
    PoolQuantity q = bestPools.get(0);
    assertEquals(new Integer(4), q.getQuantity());
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) Pool(org.candlepin.model.Pool) Test(org.junit.Test)

Aggregations

PoolQuantity (org.candlepin.model.PoolQuantity)75 Pool (org.candlepin.model.Pool)65 Test (org.junit.Test)52 Entitlement (org.candlepin.model.Entitlement)34 LinkedList (java.util.LinkedList)30 Product (org.candlepin.model.Product)28 HashMap (java.util.HashMap)27 ArrayList (java.util.ArrayList)21 Consumer (org.candlepin.model.Consumer)12 List (java.util.List)11 ConsumerType (org.candlepin.model.ConsumerType)11 Matchers.anyString (org.mockito.Matchers.anyString)11 Date (java.util.Date)8 Set (java.util.Set)8 HashSet (java.util.HashSet)7 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)7 ValidationResult (org.candlepin.policy.ValidationResult)7 Subscription (org.candlepin.model.dto.Subscription)6 Matchers.anyLong (org.mockito.Matchers.anyLong)6 PoolFilterBuilder (org.candlepin.model.PoolFilterBuilder)5