Search in sources :

Example 16 with PoolUpdate

use of org.candlepin.policy.js.pool.PoolUpdate in project candlepin by candlepin.

the class PoolRulesTest method accountNumberChanged.

@Test
public void accountNumberChanged() {
    Pool p = TestUtil.createPool(owner, TestUtil.createProduct());
    p.setAccountNumber("123");
    // Setup a pool with a single (different) account number:
    Pool p1 = TestUtil.clone(p);
    p1.setQuantity(2000L);
    p1.setAccountNumber("ABC");
    List<Pool> existingPools = new LinkedList<>();
    existingPools.add(p1);
    List<PoolUpdate> updates = this.poolRules.updatePools(p, existingPools, p.getQuantity(), Collections.<String, Product>emptyMap());
    assertEquals(1, updates.size());
    PoolUpdate update = updates.get(0);
    assertTrue(update.getOrderChanged());
    assertEquals("123", update.getPool().getAccountNumber());
}
Also used : Pool(org.candlepin.model.Pool) PoolUpdate(org.candlepin.policy.js.pool.PoolUpdate) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 17 with PoolUpdate

use of org.candlepin.policy.js.pool.PoolUpdate in project candlepin by candlepin.

the class CandlepinPoolManager method processPoolUpdates.

protected Set<String> processPoolUpdates(Map<String, EventBuilder> poolEvents, List<PoolUpdate> updatedPools) {
    boolean flush = false;
    Set<String> existingPoolIds = new HashSet<>();
    Set<Pool> poolsToDelete = new HashSet<>();
    Set<Pool> poolsToRegenEnts = new HashSet<>();
    Set<String> entitlementsToRegen = new HashSet<>();
    // Get our list of pool IDs so we can check which of them still exist in the DB...
    for (PoolUpdate update : updatedPools) {
        if (update != null && update.getPool() != null && update.getPool().getId() != null) {
            existingPoolIds.add(update.getPool().getId());
        }
    }
    existingPoolIds = this.poolCurator.getExistingPoolIdsByIds(existingPoolIds);
    // Process pool updates...
    for (PoolUpdate updatedPool : updatedPools) {
        Pool existingPool = updatedPool.getPool();
        log.info("Pool changed: {}", updatedPool.toString());
        if (existingPool == null || !existingPoolIds.contains(existingPool.getId())) {
            log.info("Pool has already been deleted from the database.");
            continue;
        }
        // Delete pools the rules signal needed to be cleaned up:
        if (existingPool.isMarkedForDelete()) {
            log.warn("Deleting pool as requested by rules: {}", existingPool.getId());
            poolsToDelete.add(existingPool);
            continue;
        }
        // save changes for the pool. We'll flush these changes later.
        this.poolCurator.merge(existingPool);
        flush = true;
        // the quantity has not yet been expressed on the pool itself
        if (updatedPool.getQuantityChanged()) {
            RevocationOp revPlan = new RevocationOp(this.poolCurator, this.consumerTypeCurator, Collections.singletonList(existingPool));
            revPlan.execute(this);
        }
        // dates changed. regenerate all entitlement certificates
        if (updatedPool.getDatesChanged() || updatedPool.getProductsChanged() || updatedPool.getBrandingChanged()) {
            poolsToRegenEnts.add(existingPool);
        }
        // Build event for this update...
        EventBuilder builder = poolEvents.get(existingPool.getId());
        if (builder != null) {
            Event event = builder.setEventData(existingPool).buildEvent();
            sink.queueEvent(event);
        } else {
            log.warn("Pool updated without an event builder: {}", existingPool);
        }
    }
    // Flush our merged changes
    if (flush) {
        this.poolCurator.flush();
    }
    // Fetch entitlement IDs for updated pools...
    if (poolsToRegenEnts.size() > 0) {
        entitlementsToRegen.addAll(this.poolCurator.retrieveOrderedEntitlementIdsOf(poolsToRegenEnts));
    }
    // Delete pools marked for deletion
    if (poolsToDelete.size() > 0) {
        this.deletePools(poolsToDelete);
    }
    // Return entitlement IDs in need regeneration
    return entitlementsToRegen;
}
Also used : EventBuilder(org.candlepin.audit.EventBuilder) Event(org.candlepin.audit.Event) Pool(org.candlepin.model.Pool) PoolUpdate(org.candlepin.policy.js.pool.PoolUpdate) HashSet(java.util.HashSet)

Example 18 with PoolUpdate

use of org.candlepin.policy.js.pool.PoolUpdate in project candlepin by candlepin.

the class CandlepinPoolManager method updatePoolsForMasterPool.

/**
 * Update pool for master pool.
 *
 * @param existingPools the existing pools
 * @param pool the master pool
 * @param originalQuantity the pool's original quantity before multiplier was applied
 * @param updateStackDerived whether or not to attempt to update stack
 *        derived pools
 */
Set<String> updatePoolsForMasterPool(List<Pool> existingPools, Pool pool, Long originalQuantity, boolean updateStackDerived, Map<String, Product> changedProducts) {
    /*
         * Rules need to determine which pools have changed, but the Java must
         * send out the events. Create an event for each pool that could change,
         * even if we won't use them all.
         */
    if (CollectionUtils.isEmpty(existingPools)) {
        return new HashSet<>(0);
    }
    log.debug("Updating {} pools for existing master pool: {}", existingPools.size(), pool);
    Map<String, EventBuilder> poolEvents = new HashMap<>();
    for (Pool existing : existingPools) {
        EventBuilder eventBuilder = eventFactory.getEventBuilder(Target.POOL, Type.MODIFIED).setEventData(existing);
        poolEvents.put(existing.getId(), eventBuilder);
    }
    // Hand off to rules to determine which pools need updating:
    List<PoolUpdate> updatedPools = poolRules.updatePools(pool, existingPools, originalQuantity, changedProducts);
    String virtLimit = pool.getProduct().getAttributeValue(Product.Attributes.VIRT_LIMIT);
    boolean createsSubPools = !StringUtils.isBlank(virtLimit) && !"0".equals(virtLimit);
    // Update subpools if necessary
    if (updateStackDerived && !updatedPools.isEmpty() && createsSubPools && pool.isStacked()) {
        // Get all pools for the master pool owner derived from the pool's
        // stack id, because we cannot look it up by subscriptionId
        List<Pool> subPools = getOwnerSubPoolsForStackId(pool.getOwner(), pool.getStackId());
        for (Pool subPool : subPools) {
            PoolUpdate update = updatePoolFromStack(subPool, changedProducts);
            if (update.changed()) {
                updatedPools.add(update);
                EventBuilder eventBuilder = eventFactory.getEventBuilder(Target.POOL, Type.MODIFIED).setEventData(subPool);
                poolEvents.put(subPool.getId(), eventBuilder);
            }
        }
    }
    return processPoolUpdates(poolEvents, updatedPools);
}
Also used : EventBuilder(org.candlepin.audit.EventBuilder) HashMap(java.util.HashMap) Pool(org.candlepin.model.Pool) PoolUpdate(org.candlepin.policy.js.pool.PoolUpdate) HashSet(java.util.HashSet)

Example 19 with PoolUpdate

use of org.candlepin.policy.js.pool.PoolUpdate in project candlepin by candlepin.

the class PoolManagerTest method doesntMergeDeletedPools.

@Test
public void doesntMergeDeletedPools() {
    reset(mockPoolCurator);
    Map<String, EventBuilder> poolEvents = new HashMap<>();
    List<PoolUpdate> updatedPools = new ArrayList<>();
    Pool deletedPool = Mockito.mock(Pool.class);
    Pool normalPool = Mockito.mock(Pool.class);
    when(normalPool.getId()).thenReturn("normal-pool-id");
    Set<String> existingPoolIds = new HashSet<>();
    existingPoolIds.add("normal-pool-id");
    when(mockPoolCurator.getExistingPoolIdsByIds(any(Iterable.class))).thenReturn(existingPoolIds);
    when(mockPoolCurator.exists(deletedPool)).thenReturn(false);
    when(mockPoolCurator.exists(normalPool)).thenReturn(true);
    PoolUpdate deletedPu = Mockito.mock(PoolUpdate.class);
    PoolUpdate normalPu = Mockito.mock(PoolUpdate.class);
    when(deletedPu.getPool()).thenReturn(deletedPool);
    when(normalPu.getPool()).thenReturn(normalPool);
    updatedPools.add(deletedPu);
    updatedPools.add(normalPu);
    manager.processPoolUpdates(poolEvents, updatedPools);
    verify(mockPoolCurator, never()).merge(deletedPool);
    verify(mockPoolCurator, times(1)).merge(normalPool);
}
Also used : EventBuilder(org.candlepin.audit.EventBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) PoolUpdate(org.candlepin.policy.js.pool.PoolUpdate) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with PoolUpdate

use of org.candlepin.policy.js.pool.PoolUpdate in project candlepin by candlepin.

the class PoolRulesInstanceTest method standaloneInstanceBasedUpdatePool.

@Test
public void standaloneInstanceBasedUpdatePool() {
    Subscription s = createInstanceBasedSub("INSTANCEPROD", 100, 2, true);
    Pool masterPool = TestUtil.copyFromSub(s);
    List<Pool> pools = poolRules.createAndEnrichPools(masterPool, new LinkedList<>());
    assertEquals(1, pools.size());
    Pool pool = pools.get(0);
    masterPool = TestUtil.copyFromSub(s);
    // Change the value of instance multiplier:
    masterPool.getProduct().setAttribute(Product.Attributes.INSTANCE_MULTIPLIER, "4");
    // Change the quantity as well:
    masterPool.setQuantity(new Long(200));
    List<Pool> existingPools = new LinkedList<>();
    existingPools.add(pool);
    List<PoolUpdate> updates = poolRules.updatePools(masterPool, existingPools, masterPool.getQuantity(), TestUtil.stubChangedProducts(masterPool.getProduct()));
    assertEquals(1, updates.size());
    PoolUpdate update = updates.get(0);
    assertTrue(update.getQuantityChanged());
    // Change in instance multiplier would have no impact on a standalone update, we
    // only need to worry about an actual change on the subscription quantity.
    assertEquals(new Long(200), update.getPool().getQuantity());
}
Also used : Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) PoolUpdate(org.candlepin.policy.js.pool.PoolUpdate) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

PoolUpdate (org.candlepin.policy.js.pool.PoolUpdate)38 Test (org.junit.Test)35 Pool (org.candlepin.model.Pool)28 LinkedList (java.util.LinkedList)10 Product (org.candlepin.model.Product)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 EventBuilder (org.candlepin.audit.EventBuilder)4 Entitlement (org.candlepin.model.Entitlement)4 Subscription (org.candlepin.model.dto.Subscription)4 ArrayList (java.util.ArrayList)3 Date (java.util.Date)2 Branding (org.candlepin.model.Branding)2 Transactional (com.google.inject.persist.Transactional)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Event (org.candlepin.audit.Event)1 CandlepinQuery (org.candlepin.model.CandlepinQuery)1 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)1