Search in sources :

Example 6 with ValidationError

use of org.candlepin.policy.ValidationError in project candlepin by candlepin.

the class EntitlerJob method toExecute.

@Override
public void toExecute(JobExecutionContext ctx) throws JobExecutionException {
    try {
        JobDataMap map = ctx.getMergedJobDataMap();
        String uuid = (String) map.get(JobStatus.TARGET_ID);
        PoolIdAndQuantity[] poolQuantities = (PoolIdAndQuantity[]) map.get("pool_and_quantities");
        Map<String, Integer> poolMap = new HashMap<>();
        for (PoolIdAndQuantity poolIdAndQuantity : poolQuantities) {
            poolMap.put(poolIdAndQuantity.getPoolId(), poolIdAndQuantity.getQuantity());
        }
        List<Entitlement> ents = entitler.bindByPoolQuantities(uuid, poolMap);
        entitler.sendEvents(ents);
        PoolIdAndQuantity[] consumed = new PoolIdAndQuantity[ents.size()];
        for (int i = 0; i < ents.size(); i++) {
            consumed[i] = new PoolIdAndQuantity(ents.get(i).getPool().getId(), ents.get(i).getQuantity());
        }
        ctx.setResult(Arrays.asList(consumed));
        poolCurator.clear();
    } catch (EntitlementRefusedException e) {
        log.error("EntitlerJob encountered a problem, translating errors", e);
        Map<String, ValidationResult> validationResults = e.getResults();
        EntitlementRulesTranslator translator = new EntitlementRulesTranslator(i18n);
        List<PoolIdAndErrors> poolErrors = new ArrayList<>();
        for (Pool pool : poolCurator.listAllByIds(validationResults.keySet())) {
            List<String> errorMessages = new ArrayList<>();
            for (ValidationError error : validationResults.get(pool.getId()).getErrors()) {
                errorMessages.add(translator.poolErrorToMessage(pool, error));
            }
            poolErrors.add(new PoolIdAndErrors(pool.getId(), errorMessages));
        }
        ctx.setResult(poolErrors);
    }// so that the job will be properly cleaned up on failure.
     catch (Exception e) {
        log.error("EntitlerJob encountered a problem.", e);
        throw new JobExecutionException(e.getMessage(), e, false);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) PoolIdAndQuantity(org.candlepin.model.dto.PoolIdAndQuantity) PoolIdAndErrors(org.candlepin.model.dto.PoolIdAndErrors) HashMap(java.util.HashMap) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) SchedulerException(org.quartz.SchedulerException) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException) ArrayList(java.util.ArrayList) List(java.util.List) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) Entitlement(org.candlepin.model.Entitlement) EntitlementRulesTranslator(org.candlepin.policy.js.entitlement.EntitlementRulesTranslator) HashMap(java.util.HashMap) Map(java.util.Map) JobDataMap(org.quartz.JobDataMap)

Example 7 with ValidationError

use of org.candlepin.policy.ValidationError in project candlepin by candlepin.

the class Entitler method getDryRun.

/**
 * Entitles the given Consumer to the given Product. Will seek out pools
 * which provide access to this product, either directly or as a child, and
 * select the best one based on a call to the rules engine.
 *
 * @param consumer The consumer being entitled.
 * @return List of Entitlements
 */
public List<PoolQuantity> getDryRun(Consumer consumer, Owner owner, String serviceLevelOverride) {
    List<PoolQuantity> result = new ArrayList<>();
    try {
        if (consumer.isDev()) {
            if (config.getBoolean(ConfigProperties.STANDALONE) || !poolCurator.hasActiveEntitlementPools(consumer.getOwnerId(), null)) {
                throw new ForbiddenException(i18n.tr("Development units may only be used on hosted servers" + " and with orgs that have active subscriptions."));
            }
            // Look up the dev pool for this consumer, and if not found
            // create one. If a dev pool already exists, remove it and
            // create a new one.
            String sku = consumer.getFact("dev_sku");
            Pool devPool = poolCurator.findDevPool(consumer);
            if (devPool != null) {
                poolManager.deletePool(devPool);
            }
            devPool = poolManager.createPool(assembleDevPool(consumer, owner, sku));
            result.add(new PoolQuantity(devPool, 1));
        } else {
            result = poolManager.getBestPools(consumer, null, null, owner.getId(), serviceLevelOverride, null);
        }
        log.debug("Created Pool Quantity list: {}", result);
    } catch (EntitlementRefusedException e) {
        // to the process
        if (log.isDebugEnabled()) {
            log.debug("consumer {} dry-run errors:", consumer.getUuid());
            for (Entry<String, ValidationResult> entry : e.getResults().entrySet()) {
                log.debug("errors for pool id: {}", entry.getKey());
                for (ValidationError error : entry.getValue().getErrors()) {
                    log.debug(error.getResourceKey());
                }
            }
        }
    }
    return result;
}
Also used : PoolQuantity(org.candlepin.model.PoolQuantity) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Entry(java.util.Map.Entry) EntitlementRefusedException(org.candlepin.policy.EntitlementRefusedException) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError)

Example 8 with ValidationError

use of org.candlepin.policy.ValidationError in project candlepin by candlepin.

the class ManifestEntitlementRulesTest method preEntitlementShouldNotAllowListOfDerivedPools.

@Test
public void preEntitlementShouldNotAllowListOfDerivedPools() {
    Consumer c = this.createMockConsumer(true);
    Product prod = TestUtil.createProduct();
    Pool p = TestUtil.createPool(prod);
    p.setAttribute(Product.Attributes.VIRT_ONLY, "true");
    p.setAttribute(Pool.Attributes.DERIVED_POOL, "true");
    p.setId("poolId");
    ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.LIST_POOLS);
    assertNotNull(results);
    assertEquals(1, results.getErrors().size());
    ValidationError error = results.getErrors().get(0);
    assertEquals("pool.not.available.to.manifest.consumers", error.getResourceKey());
}
Also used : Consumer(org.candlepin.model.Consumer) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Example 9 with ValidationError

use of org.candlepin.policy.ValidationError in project candlepin by candlepin.

the class ManifestEntitlementRulesTest method preEntitlementNoInstanceCapableBindError.

@Test
public void preEntitlementNoInstanceCapableBindError() {
    // Test with sockets to make sure that they are skipped.
    Consumer c = this.createMockConsumer(true);
    Set<ConsumerCapability> caps = new HashSet<>();
    c.setCapabilities(caps);
    Product prod = TestUtil.createProduct();
    prod.setAttribute(Product.Attributes.INSTANCE_MULTIPLIER, "2");
    Pool p = TestUtil.createPool(prod);
    p.setId("poolId");
    ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.BIND);
    assertNotNull(results);
    assertEquals(0, results.getWarnings().size());
    ValidationError error = results.getErrors().get(0);
    assertEquals("rulefailed.instance.unsupported.by.consumer", error.getResourceKey());
}
Also used : Consumer(org.candlepin.model.Consumer) Product(org.candlepin.model.Product) ConsumerCapability(org.candlepin.model.ConsumerCapability) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) ValidationResult(org.candlepin.policy.ValidationResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with ValidationError

use of org.candlepin.policy.ValidationError in project candlepin by candlepin.

the class ManifestEntitlementRulesTest method preEntitlementNoDerivedProductCapabilityProducesErrorOnBind.

@Test
public void preEntitlementNoDerivedProductCapabilityProducesErrorOnBind() {
    Consumer c = this.createMockConsumer(true);
    c.setCapabilities(new HashSet<>());
    Product prod = TestUtil.createProduct();
    Product derived = TestUtil.createProduct("sub-prod-id");
    Pool p = TestUtil.createPool(prod);
    p.setDerivedProduct(derived);
    p.setId("poolId");
    ValidationResult results = enforcer.preEntitlement(c, p, 1, CallerType.BIND);
    assertNotNull(results);
    assertEquals(1, results.getErrors().size());
    assertTrue(results.getWarnings().isEmpty());
    ValidationError error = results.getErrors().get(0);
    assertEquals("rulefailed.derivedproduct.unsupported.by.consumer", error.getResourceKey());
}
Also used : Consumer(org.candlepin.model.Consumer) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) ValidationError(org.candlepin.policy.ValidationError) ValidationResult(org.candlepin.policy.ValidationResult) Test(org.junit.Test)

Aggregations

ValidationError (org.candlepin.policy.ValidationError)15 Pool (org.candlepin.model.Pool)14 ValidationResult (org.candlepin.policy.ValidationResult)13 Consumer (org.candlepin.model.Consumer)11 Product (org.candlepin.model.Product)11 Test (org.junit.Test)11 HashSet (java.util.HashSet)4 ArrayList (java.util.ArrayList)3 ConsumerCapability (org.candlepin.model.ConsumerCapability)3 EntitlementRefusedException (org.candlepin.policy.EntitlementRefusedException)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Entitlement (org.candlepin.model.Entitlement)2 PoolQuantity (org.candlepin.model.PoolQuantity)2 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)1