Search in sources :

Example 6 with EntitlementDTO

use of org.candlepin.dto.rules.v1.EntitlementDTO 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 7 with EntitlementDTO

use of org.candlepin.dto.rules.v1.EntitlementDTO 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 8 with EntitlementDTO

use of org.candlepin.dto.rules.v1.EntitlementDTO 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 9 with EntitlementDTO

use of org.candlepin.dto.rules.v1.EntitlementDTO 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)

Example 10 with EntitlementDTO

use of org.candlepin.dto.rules.v1.EntitlementDTO in project candlepin by candlepin.

the class OwnerResourceTest method getAllEntitlementsForOwner.

@Test
public void getAllEntitlementsForOwner() {
    PageRequest req = new PageRequest();
    req.setPage(1);
    req.setPerPage(10);
    Owner owner = this.createOwner();
    Consumer consumer = this.createConsumer(owner);
    Pool pool = this.createPool(owner, this.createProduct());
    Entitlement e = this.createEntitlement(owner, consumer, pool, null);
    List<Entitlement> entitlements = new ArrayList<>();
    entitlements.add(e);
    Page<List<Entitlement>> page = new Page<>();
    page.setPageData(entitlements);
    OwnerResource ownerres = new OwnerResource(this.ownerCurator, this.productCurator, null, null, i18n, null, null, null, null, null, null, null, null, null, null, null, null, this.entitlementCurator, null, null, null, null, null, null, null, null, null, null, null, this.modelTranslator);
    List<EntitlementDTO> result = ownerres.ownerEntitlements(owner.getKey(), null, null, null, req);
    assertEquals(1, result.size());
    assertEquals(e.getId(), result.get(0).getId());
}
Also used : Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) Page(org.candlepin.common.paging.Page) PageRequest(org.candlepin.common.paging.PageRequest) EntitlementDTO(org.candlepin.dto.api.v1.EntitlementDTO) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) Consumer(org.candlepin.model.Consumer) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Entitlement(org.candlepin.model.Entitlement) Test(org.junit.Test)

Aggregations

Entitlement (org.candlepin.model.Entitlement)15 EntitlementDTO (org.candlepin.dto.api.v1.EntitlementDTO)11 ArrayList (java.util.ArrayList)10 List (java.util.List)8 Pool (org.candlepin.model.Pool)8 ConsumerDTO (org.candlepin.dto.rules.v1.ConsumerDTO)7 EntitlementDTO (org.candlepin.dto.rules.v1.EntitlementDTO)7 JsonJsContext (org.candlepin.policy.js.JsonJsContext)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 Produces (javax.ws.rs.Produces)6 Consumer (org.candlepin.model.Consumer)6 Test (org.junit.Test)6 Path (javax.ws.rs.Path)5 ConsumerType (org.candlepin.model.ConsumerType)5 EntitlementFilterBuilder (org.candlepin.model.EntitlementFilterBuilder)5 Owner (org.candlepin.model.Owner)5 LinkedList (java.util.LinkedList)4 GET (javax.ws.rs.GET)4 RuleExecutionException (org.candlepin.policy.js.RuleExecutionException)4