Search in sources :

Example 1 with JsonJsContext

use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.

the class QuantityRules method getSuggestedQuantity.

@SuppressWarnings("checkstyle:indentation")
public SuggestedQuantity getSuggestedQuantity(Pool p, Consumer c, Date date) {
    JsonJsContext args = new JsonJsContext(mapper);
    Stream<EntitlementDTO> entStream = c.getEntitlements() == null ? Stream.empty() : c.getEntitlements().stream().filter(ent -> ent.isValidOnDate(date)).map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
    args.put("consumer", this.translator.translate(c, ConsumerDTO.class));
    args.put("pool", this.translator.translate(p, PoolDTO.class));
    args.put("validEntitlements", entStream.collect(Collectors.toSet()));
    args.put("log", log, false);
    args.put("guestIds", c.getGuestIds());
    String json = jsRules.runJsFunction(String.class, "get_suggested_quantity", args);
    SuggestedQuantity dto = mapper.toObject(json, SuggestedQuantity.class);
    return dto;
}
Also used : EntitlementDTO(org.candlepin.dto.rules.v1.EntitlementDTO) ConsumerDTO(org.candlepin.dto.rules.v1.ConsumerDTO) PoolDTO(org.candlepin.dto.rules.v1.PoolDTO) JsonJsContext(org.candlepin.policy.js.JsonJsContext)

Example 2 with JsonJsContext

use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.

the class QuantityRules method getSuggestedQuantities.

/**
 * Calculates the suggested quantities for many pools in one call. This allows for
 * performant list pools queries with large numbers of pools and a large amount of
 * entitlements to serialize.
 *
 * Map returned will map each pool ID to the suggested quantities for it. Every pool
 * provided should have it's ID present in the result.
 *
 * @param pools
 * @param c
 * @param date
 * @return suggested quantities for all pools requested
 */
@SuppressWarnings("checkstyle:indentation")
public Map<String, SuggestedQuantity> getSuggestedQuantities(List<Pool> pools, Consumer c, Date date) {
    JsonJsContext args = new JsonJsContext(mapper);
    Stream<PoolDTO> poolStream = pools == null ? Stream.empty() : pools.stream().map(this.translator.getStreamMapper(Pool.class, PoolDTO.class));
    Stream<EntitlementDTO> entStream = c.getEntitlements() == null ? Stream.empty() : c.getEntitlements().stream().filter(ent -> ent.isValidOnDate(date)).map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
    args.put("pools", poolStream.collect(Collectors.toSet()));
    args.put("consumer", this.translator.translate(c, ConsumerDTO.class));
    args.put("validEntitlements", entStream.collect(Collectors.toSet()));
    args.put("log", log, false);
    args.put("guestIds", c.getGuestIds());
    String json = jsRules.runJsFunction(String.class, "get_suggested_quantities", args);
    Map<String, SuggestedQuantity> resultMap;
    TypeReference<Map<String, SuggestedQuantity>> typeref = new TypeReference<Map<String, SuggestedQuantity>>() {
    };
    try {
        resultMap = mapper.toObject(json, typeref);
    } catch (Exception e) {
        throw new RuleExecutionException(e);
    }
    return resultMap;
}
Also used : PoolDTO(org.candlepin.dto.rules.v1.PoolDTO) 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) Map(java.util.Map)

Example 3 with JsonJsContext

use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.

the class EntitlementRules method filterPools.

@Override
@SuppressWarnings("checkstyle:indentation")
public List<Pool> filterPools(Consumer consumer, List<Pool> pools, boolean showAll) {
    JsonJsContext args = new JsonJsContext(objectMapper);
    Map<String, ValidationResult> resultMap = new HashMap<>();
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
    if (!ctype.isType(ConsumerTypeEnum.SHARE)) {
        Stream<PoolDTO> poolStream = pools == null ? Stream.empty() : pools.stream().map(this.translator.getStreamMapper(Pool.class, PoolDTO.class));
        Stream<EntitlementDTO> entStream = consumer.getEntitlements() == null ? Stream.empty() : consumer.getEntitlements().stream().map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
        args.put("consumer", this.translator.translate(consumer, ConsumerDTO.class));
        args.put("hostConsumer", this.translator.translate(getHost(consumer, pools), ConsumerDTO.class));
        args.put("consumerEntitlements", entStream.collect(Collectors.toSet()));
        args.put("standalone", config.getBoolean(ConfigProperties.STANDALONE));
        args.put("pools", poolStream.collect(Collectors.toSet()));
        args.put("caller", CallerType.LIST_POOLS.getLabel());
        args.put("log", log, false);
        String json = jsRules.runJsFunction(String.class, "validate_pools_list", args);
        TypeReference<Map<String, ValidationResult>> typeref = new TypeReference<Map<String, ValidationResult>>() {
        };
        try {
            resultMap = objectMapper.toObject(json, typeref);
        } catch (Exception e) {
            throw new RuleExecutionException(e);
        }
    }
    List<Pool> filteredPools = new LinkedList<>();
    for (Pool pool : pools) {
        ValidationResult result;
        if (ctype.isType(ConsumerTypeEnum.SHARE)) {
            result = new ValidationResult();
            resultMap.put(pool.getId(), result);
            validatePoolSharingEligibility(result, pool);
        } else {
            result = resultMap.get(pool.getId());
        }
        finishValidation(result, pool, 1);
        if (result.isSuccessful() && (!result.hasWarnings() || showAll)) {
            filteredPools.add(pool);
        } else if (log.isDebugEnabled()) {
            log.debug("Omitting pool due to failed rules: " + pool.getId());
            if (result.hasErrors()) {
                log.debug("\tErrors: " + result.getErrors());
            }
            if (result.hasWarnings()) {
                log.debug("\tWarnings: " + result.getWarnings());
            }
        }
    }
    return filteredPools;
}
Also used : HashMap(java.util.HashMap) PoolDTO(org.candlepin.dto.rules.v1.PoolDTO) ValidationResult(org.candlepin.policy.ValidationResult) RuleExecutionException(org.candlepin.policy.js.RuleExecutionException) LinkedList(java.util.LinkedList) EntitlementDTO(org.candlepin.dto.rules.v1.EntitlementDTO) ConsumerDTO(org.candlepin.dto.rules.v1.ConsumerDTO) JsonJsContext(org.candlepin.policy.js.JsonJsContext) Pool(org.candlepin.model.Pool) 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 4 with JsonJsContext

use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.

the class OverrideRules method canOverrideForConsumer.

public boolean canOverrideForConsumer(String name) {
    JsonJsContext args = new JsonJsContext(mapper);
    args.put("name", name);
    args.put("log", log, false);
    args.put("hosted", !config.getBoolean(ConfigProperties.STANDALONE));
    return !jsRules.runJsFunction(Boolean.class, "get_allow_override", args);
}
Also used : JsonJsContext(org.candlepin.policy.js.JsonJsContext)

Example 5 with JsonJsContext

use of org.candlepin.policy.js.JsonJsContext in project candlepin by candlepin.

the class ComplianceRules method isEntitlementCompliant.

public boolean isEntitlementCompliant(Consumer consumer, Entitlement ent, Date onDate) {
    List<Entitlement> ents = entCurator.listByConsumerAndDate(consumer, onDate).list();
    Stream<EntitlementDTO> entStream = ents == null ? Stream.empty() : ents.stream().map(this.translator.getStreamMapper(Entitlement.class, EntitlementDTO.class));
    JsonJsContext args = new JsonJsContext(mapper);
    args.put("consumer", this.translator.translate(consumer, ConsumerDTO.class));
    args.put("entitlement", this.translator.translate(ent, EntitlementDTO.class));
    args.put("entitlements", entStream.collect(Collectors.toSet()));
    args.put("log", log, false);
    args.put("guestIds", consumer.getGuestIds());
    return jsRules.runJsFunction(Boolean.class, "is_ent_compliant", args);
}
Also used : EntitlementDTO(org.candlepin.dto.rules.v1.EntitlementDTO) ConsumerDTO(org.candlepin.dto.rules.v1.ConsumerDTO) JsonJsContext(org.candlepin.policy.js.JsonJsContext) Entitlement(org.candlepin.model.Entitlement)

Aggregations

JsonJsContext (org.candlepin.policy.js.JsonJsContext)10 ConsumerDTO (org.candlepin.dto.rules.v1.ConsumerDTO)8 EntitlementDTO (org.candlepin.dto.rules.v1.EntitlementDTO)7 PoolDTO (org.candlepin.dto.rules.v1.PoolDTO)5 RuleExecutionException (org.candlepin.policy.js.RuleExecutionException)5 Map (java.util.Map)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ConsumerType (org.candlepin.model.ConsumerType)3 HashMap (java.util.HashMap)2 Entitlement (org.candlepin.model.Entitlement)2 Pool (org.candlepin.model.Pool)2 PoolQuantity (org.candlepin.model.PoolQuantity)2 ValidationResult (org.candlepin.policy.ValidationResult)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 OwnerDTO (org.candlepin.dto.rules.v1.OwnerDTO)1 Owner (org.candlepin.model.Owner)1 RhinoException (org.mozilla.javascript.RhinoException)1