Search in sources :

Example 1 with ActivationKeyPool

use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.

the class ConsumerBindUtil method handleActivationKeyPools.

private boolean handleActivationKeyPools(Consumer consumer, ActivationKey key) {
    if (key.getPools().size() == 0) {
        return true;
    }
    boolean onePassed = false;
    List<ActivationKeyPool> toBind = new LinkedList<>();
    for (ActivationKeyPool akp : key.getPools()) {
        if (akp.getPool().getId() != null) {
            toBind.add(akp);
        }
    }
    // Sort pools before binding to avoid deadlocks
    Collections.sort(toBind);
    for (ActivationKeyPool akp : toBind) {
        int quantity = (akp.getQuantity() == null) ? getQuantityToBind(akp.getPool(), consumer) : akp.getQuantity().intValue();
        try {
            entitler.sendEvents(entitler.bindByPoolQuantity(consumer, akp.getPool().getId(), quantity));
            onePassed = true;
        } catch (ForbiddenException e) {
            log.warn(i18n.tr("Cannot bind to pool \"{0}\" in activation key \"{1}\": {2}", akp.getPool().getId(), akp.getKey().getName(), e.getMessage()));
        }
    }
    return onePassed;
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) LinkedList(java.util.LinkedList)

Example 2 with ActivationKeyPool

use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.

the class ActivationKeyTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public ActivationKeyDTO populate(ModelTranslator modelTranslator, ActivationKey source, ActivationKeyDTO dest) {
    dest = super.populate(modelTranslator, source, dest);
    dest.setId(source.getId()).setName(source.getName()).setDescription(source.getDescription()).setServiceLevel(source.getServiceLevel()).setAutoAttach(source.isAutoAttach());
    // Process nested objects if we have a model translator to use to the translation...
    if (modelTranslator != null) {
        Owner owner = source.getOwner();
        dest.setOwner(owner != null ? modelTranslator.translate(owner, OwnerDTO.class) : null);
        Set<ActivationKeyPool> pools = source.getPools();
        if (pools != null && !pools.isEmpty()) {
            for (ActivationKeyPool poolEntry : pools) {
                if (poolEntry != null) {
                    dest.addPool(new ActivationKeyDTO.ActivationKeyPoolDTO(poolEntry.getPool().getId(), poolEntry.getQuantity()));
                }
            }
        } else {
            dest.setPools(Collections.<ActivationKeyDTO.ActivationKeyPoolDTO>emptySet());
        }
        Set<Product> products = source.getProducts();
        if (products != null && !products.isEmpty()) {
            for (Product prod : products) {
                if (prod != null) {
                    dest.addProductId(prod.getId());
                }
            }
        } else {
            dest.setProductIds(Collections.<String>emptySet());
        }
        Set<ActivationKeyContentOverride> overrides = source.getContentOverrides();
        if (overrides != null && !overrides.isEmpty()) {
            for (ActivationKeyContentOverride override : overrides) {
                if (override != null) {
                    dest.addContentOverride(new ActivationKeyDTO.ActivationKeyContentOverrideDTO(override.getContentLabel(), override.getName(), override.getValue()));
                }
            }
        } else {
            dest.setContentOverrides(Collections.<ActivationKeyDTO.ActivationKeyContentOverrideDTO>emptySet());
        }
        Release release = source.getReleaseVer();
        if (release != null) {
            dest.setReleaseVersion(release.getReleaseVer());
        }
    }
    return dest;
}
Also used : Owner(org.candlepin.model.Owner) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Product(org.candlepin.model.Product) Release(org.candlepin.model.Release) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride)

Example 3 with ActivationKeyPool

use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.

the class ActivationKeyTranslatorTest method verifyOutput.

@Override
protected void verifyOutput(ActivationKey source, ActivationKeyDTO dest, boolean childrenGenerated) {
    if (source != null) {
        assertEquals(source.getId(), dest.getId());
        assertEquals(source.getName(), dest.getName());
        assertEquals(source.getDescription(), dest.getDescription());
        assertEquals(source.getServiceLevel(), dest.getServiceLevel());
        assertEquals(source.isAutoAttach(), dest.isAutoAttach());
        if (childrenGenerated) {
            this.ownerTranslatorTest.verifyOutput(source.getOwner(), dest.getOwner(), true);
            for (Product prod : source.getProducts()) {
                for (String prodDto : dest.getProductIds()) {
                    assertNotNull(prodDto);
                    if (prodDto.equals(prod.getId())) {
                    // Nothing else to assert on, since prodDto only holds the product id.
                    }
                }
            }
            for (ActivationKeyPool akPool : source.getPools()) {
                for (ActivationKeyDTO.ActivationKeyPoolDTO akPoolDto : dest.getPools()) {
                    assertNotNull(akPoolDto);
                    if (akPoolDto.getPoolId().equals(akPool.getId())) {
                        assertEquals(akPool.getQuantity(), akPoolDto.getQuantity());
                    }
                }
            }
            for (ActivationKeyContentOverride akOverride : source.getContentOverrides()) {
                for (ActivationKeyDTO.ActivationKeyContentOverrideDTO akOverrideDto : dest.getContentOverrides()) {
                    assertNotNull(akOverrideDto);
                    if (akOverrideDto.getName().equals(akOverride.getName())) {
                        assertEquals(akOverrideDto.getContentLabel(), akOverride.getContentLabel());
                        assertEquals(akOverrideDto.getValue(), akOverride.getValue());
                    }
                }
            }
            Release releaseSource = source.getReleaseVer();
            String releaseDestination = dest.getReleaseVersion();
            if (releaseSource != null) {
                assertEquals(releaseSource.getReleaseVer(), releaseDestination);
            } else {
                assertNull(releaseDestination);
            }
        } else {
            assertNull(dest.getOwner());
            assertNull(dest.getProductIds());
            assertNull(dest.getPools());
            assertNull(dest.getContentOverrides());
            assertNull(dest.getReleaseVersion());
        }
    } else {
        assertNull(dest);
    }
}
Also used : Product(org.candlepin.model.Product) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Release(org.candlepin.model.Release) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride)

Example 4 with ActivationKeyPool

use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.

the class ActivationKeyResourceTest method testActivationKeyWithSameHostReqPools.

@Test
public void testActivationKeyWithSameHostReqPools() {
    ActivationKey ak = genActivationKey();
    ActivationKeyCurator akc = mock(ActivationKeyCurator.class);
    PoolManager poolManager = mock(PoolManager.class);
    Pool p1 = genPool();
    p1.setAttribute(Pool.Attributes.REQUIRES_HOST, "host1");
    Pool p2 = genPool();
    p2.setAttribute(Pool.Attributes.REQUIRES_HOST, "host1");
    when(akc.verifyAndLookupKey(eq("testKey"))).thenReturn(ak);
    when(poolManager.find(eq("testPool1"))).thenReturn(p1);
    when(poolManager.find(eq("testPool2"))).thenReturn(p2);
    ActivationKeyResource akr = new ActivationKeyResource(akc, i18n, poolManager, serviceLevelValidator, activationKeyRules, null, new ProductCachedSerializationModule(productCurator), this.modelTranslator);
    akr.addPoolToKey("testKey", "testPool1", 1L);
    assertEquals(1, ak.getPools().size());
    Set<ActivationKeyPool> akPools = new HashSet<>();
    akPools.add(new ActivationKeyPool(ak, p1, 1L));
    ak.setPools(akPools);
    akr.addPoolToKey("testKey", "testPool2", 1L);
    assertEquals(2, ak.getPools().size());
}
Also used : ProductCachedSerializationModule(org.candlepin.jackson.ProductCachedSerializationModule) ActivationKeyCurator(org.candlepin.model.activationkeys.ActivationKeyCurator) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Pool(org.candlepin.model.Pool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) PoolManager(org.candlepin.controller.PoolManager) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with ActivationKeyPool

use of org.candlepin.model.activationkeys.ActivationKeyPool in project candlepin by candlepin.

the class TestUtil method createActivationKey.

public static ActivationKey createActivationKey(Owner owner, List<Pool> pools) {
    ActivationKey key = new ActivationKey();
    key.setOwner(owner);
    key.setName("A Test Key");
    key.setServiceLevel("TestLevel");
    key.setDescription("A test description for the test key.");
    if (pools != null) {
        Set<ActivationKeyPool> akPools = new HashSet<>();
        for (Pool p : pools) {
            akPools.add(new ActivationKeyPool(key, p, (long) 1));
        }
        key.setPools(akPools);
    }
    return key;
}
Also used : ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Pool(org.candlepin.model.Pool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) HashSet(java.util.HashSet)

Aggregations

ActivationKeyPool (org.candlepin.model.activationkeys.ActivationKeyPool)7 HashSet (java.util.HashSet)3 Product (org.candlepin.model.Product)3 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)3 ArrayList (java.util.ArrayList)2 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)2 Owner (org.candlepin.model.Owner)2 Pool (org.candlepin.model.Pool)2 Release (org.candlepin.model.Release)2 ActivationKeyContentOverride (org.candlepin.model.activationkeys.ActivationKeyContentOverride)2 LinkedList (java.util.LinkedList)1 PoolManager (org.candlepin.controller.PoolManager)1 ProductCachedSerializationModule (org.candlepin.jackson.ProductCachedSerializationModule)1 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)1 Entitlement (org.candlepin.model.Entitlement)1 ActivationKeyCurator (org.candlepin.model.activationkeys.ActivationKeyCurator)1 AutobindData (org.candlepin.resource.dto.AutobindData)1 CertVersionConflictException (org.candlepin.version.CertVersionConflictException)1 Test (org.junit.Test)1