Search in sources :

Example 6 with ImportSubscriptionServiceAdapter

use of org.candlepin.service.impl.ImportSubscriptionServiceAdapter in project candlepin by candlepin.

the class PoolManagerFunctionalTest method testEntitleByProductsWithModifierAndModifiee.

@Test
public void testEntitleByProductsWithModifierAndModifiee() throws EntitlementRefusedException {
    Product modifier = TestUtil.createProduct("modifier", "modifier");
    Set<String> modified = new HashSet<>();
    modified.add(PRODUCT_VIRT_HOST);
    Content content = TestUtil.createContent("modifier-content", "modifier-content");
    content.setModifiedProductIds(modified);
    modifier.addContent(content, true);
    contentCurator.create(content);
    productCurator.create(modifier);
    this.ownerContentCurator.mapContentToOwner(content, this.o);
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    Subscription sub = TestUtil.createSubscription(o, modifier, new HashSet<>());
    sub.setQuantity(5L);
    sub.setStartDate(new Date());
    sub.setEndDate(TestUtil.createDate(3020, 12, 12));
    sub.setModified(new Date());
    sub.setId(Util.generateDbUUID());
    subscriptions.add(sub);
    poolManager.getRefresher(subAdapter, ownerAdapter).add(o).run();
    // This test simulates https://bugzilla.redhat.com/show_bug.cgi?id=676870
    // where entitling first to the modifier then to the modifiee causes the modifier's
    // entitlement cert to get regenerated, but since it's all in the same http call,
    // this ends up causing a hibernate failure (the old cert is asked to be deleted,
    // but it hasn't been saved yet). Since getting the pool ordering right is tricky
    // inside an entitleByProducts call, we do it in two singular calls here.
    AutobindData data = AutobindData.create(parentSystem, o).on(new Date()).forProducts(new String[] { "modifier" });
    poolManager.entitleByProducts(data);
    try {
        data = AutobindData.create(parentSystem, o).on(new Date()).forProducts(new String[] { PRODUCT_VIRT_HOST });
        poolManager.entitleByProducts(data);
    } catch (EntityNotFoundException e) {
        throw e;
    // fail("Hibernate failed to properly save entitlement certs!");
    }
// If we get here, no exception was raised, so we're happy!
}
Also used : Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) EntityNotFoundException(javax.persistence.EntityNotFoundException) LinkedList(java.util.LinkedList) Date(java.util.Date) ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) Content(org.candlepin.model.Content) AutobindData(org.candlepin.resource.dto.AutobindData) Subscription(org.candlepin.model.dto.Subscription) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with ImportSubscriptionServiceAdapter

use of org.candlepin.service.impl.ImportSubscriptionServiceAdapter in project candlepin by candlepin.

the class Importer method importObjects.

@SuppressWarnings("checkstyle:methodlength")
@Transactional(rollbackOn = { IOException.class, ImporterException.class, RuntimeException.class, ImportConflictException.class })
public // WARNING: Keep this method public, otherwise @Transactional is ignored:
List<Subscription> importObjects(Owner owner, Map<String, File> importFiles, ConflictOverrides overrides) throws IOException, ImporterException {
    ownerCurator.lock(owner);
    log.debug("Importing objects for owner: {}", owner);
    File metadata = importFiles.get(ImportFile.META.fileName());
    if (metadata == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required meta.json file"));
    }
    File consumerTypes = importFiles.get(ImportFile.CONSUMER_TYPE.fileName());
    if (consumerTypes == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required consumer_types directory"));
    }
    File consumerFile = importFiles.get(ImportFile.CONSUMER.fileName());
    if (consumerFile == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required consumer.json file"));
    }
    File products = importFiles.get(ImportFile.PRODUCTS.fileName());
    File entitlements = importFiles.get(ImportFile.ENTITLEMENTS.fileName());
    if (products != null && entitlements == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required entitlements directory"));
    }
    // system level elements
    /*
         * Checking a system wide last import date breaks multi-tenant deployments whenever
         * one org imports a manifest slightly older than another org who has already
         * imported. Disabled for now. See bz #769644.
         */
    // validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, metadata, force);
    // If any calls find conflicts we'll assemble them into one exception detailing all
    // the conflicts which occurred, so the caller can override them all at once
    // if desired:
    List<ImportConflictException> conflictExceptions = new LinkedList<>();
    File rules = importFiles.get(ImportFile.RULES_FILE.fileName());
    importRules(rules, metadata);
    importConsumerTypes(consumerTypes.listFiles());
    File distributorVersions = importFiles.get(ImportFile.DISTRIBUTOR_VERSIONS.fileName());
    if (distributorVersions != null) {
        importDistributorVersions(distributorVersions.listFiles());
    }
    File cdns = importFiles.get(ImportFile.CONTENT_DELIVERY_NETWORKS.fileName());
    if (cdns != null) {
        importContentDeliveryNetworks(cdns.listFiles());
    }
    // per user elements
    try {
        validateMetadata(ExporterMetadata.TYPE_PER_USER, owner, metadata, overrides);
    } catch (ImportConflictException e) {
        conflictExceptions.add(e);
    }
    ConsumerDTO consumer = null;
    try {
        Meta m = mapper.readValue(metadata, Meta.class);
        File upstreamFile = importFiles.get(ImportFile.UPSTREAM_CONSUMER.fileName());
        File[] dafiles = new File[0];
        if (upstreamFile != null) {
            dafiles = upstreamFile.listFiles();
        }
        consumer = importConsumer(owner, consumerFile, dafiles, overrides, m);
    } catch (ImportConflictException e) {
        conflictExceptions.add(e);
    }
    // At this point we're done checking for any potential conflicts:
    if (!conflictExceptions.isEmpty()) {
        log.error("Conflicts occurred during import that were not overridden:");
        for (ImportConflictException e : conflictExceptions) {
            log.error("{}", e.message().getConflicts());
        }
        throw new ImportConflictException(conflictExceptions);
    }
    if (consumer == null) {
        throw new IllegalStateException("No consumer found during import");
    }
    // If the consumer has no entitlements, this products directory will end up empty.
    // This also implies there will be no entitlements to import.
    Meta meta = mapper.readValue(metadata, Meta.class);
    List<Subscription> importSubs;
    if (importFiles.get(ImportFile.PRODUCTS.fileName()) != null) {
        ProductImporter importer = new ProductImporter();
        Set<ProductDTO> productsToImport = importProducts(importFiles.get(ImportFile.PRODUCTS.fileName()).listFiles(), importer, owner);
        importSubs = importEntitlements(owner, productsToImport, entitlements.listFiles(), consumer.getUuid(), meta);
    } else {
        log.warn("No products found to import, skipping product import.");
        log.warn("No entitlements in manifest, removing all subscriptions for owner.");
        importSubs = importEntitlements(owner, new HashSet<>(), new File[] {}, consumer.getUuid(), meta);
    }
    // Setup our import subscription adapter with the subscriptions imported:
    final String contentAccessMode = StringUtils.isEmpty(consumer.getContentAccessMode()) ? ContentAccessCertServiceAdapter.DEFAULT_CONTENT_ACCESS_MODE : consumer.getContentAccessMode();
    SubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(importSubs);
    OwnerServiceAdapter ownerAdapter = new OwnerServiceAdapter() {

        @Override
        public boolean isOwnerKeyValidForCreation(String ownerKey) {
            return true;
        }

        @Override
        public String getContentAccessMode(String ownerKey) {
            return contentAccessMode;
        }

        @Override
        public String getContentAccessModeList(String ownerKey) {
            return contentAccessMode;
        }
    };
    Refresher refresher = poolManager.getRefresher(subAdapter, ownerAdapter);
    refresher.add(owner);
    refresher.run();
    return importSubs;
}
Also used : Refresher(org.candlepin.controller.Refresher) LinkedList(java.util.LinkedList) ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) Subscription(org.candlepin.model.dto.Subscription) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO) ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) SubscriptionServiceAdapter(org.candlepin.service.SubscriptionServiceAdapter) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Example 8 with ImportSubscriptionServiceAdapter

use of org.candlepin.service.impl.ImportSubscriptionServiceAdapter in project candlepin by candlepin.

the class ConsumerResourceVirtEntitlementTest method setUp.

@Before
public void setUp() {
    List<Subscription> subscriptions = new ArrayList<>();
    subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    manifestType = consumerTypeCurator.create(new ConsumerType(ConsumerType.ConsumerTypeEnum.CANDLEPIN));
    systemType = consumerTypeCurator.create(new ConsumerType(ConsumerType.ConsumerTypeEnum.SYSTEM));
    owner = ownerCurator.create(new Owner("test-owner"));
    ownerCurator.create(owner);
    manifestConsumer = TestUtil.createConsumer(manifestType, owner);
    consumerCurator.create(manifestConsumer);
    systemConsumer = TestUtil.createConsumer(systemType, owner);
    consumerCurator.create(systemConsumer);
    // create a physical pool with numeric virt_limit
    productLimit = TestUtil.createProduct();
    productLimit.setAttribute(Product.Attributes.VIRT_LIMIT, "10");
    productLimit.setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes");
    productLimit = this.createProduct(productLimit, owner);
    Subscription limitSub = TestUtil.createSubscription(owner, productLimit, new HashSet<>());
    limitSub.setId(Util.generateDbUUID());
    limitSub.setQuantity(10L);
    limitSub.setStartDate(TestUtil.createDate(2010, 1, 1));
    limitSub.setEndDate(TestUtil.createDate(2020, 1, 1));
    limitSub.setModified(TestUtil.createDate(2000, 1, 1));
    subscriptions.add(limitSub);
    limitPools = poolManager.createAndEnrichPools(limitSub);
    // create a physical pool with unlimited virt_limit
    productUnlimit = TestUtil.createProduct();
    productUnlimit.setAttribute(Product.Attributes.VIRT_LIMIT, "unlimited");
    productUnlimit.setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes");
    productUnlimit = this.createProduct(productUnlimit, owner);
    Subscription unlimitSub = TestUtil.createSubscription(owner, productUnlimit, new HashSet<>());
    unlimitSub.setId(Util.generateDbUUID());
    unlimitSub.setQuantity(10L);
    unlimitSub.setStartDate(TestUtil.createDate(2010, 1, 1));
    unlimitSub.setEndDate(TestUtil.createDate(2020, 1, 1));
    unlimitSub.setModified(TestUtil.createDate(2000, 1, 1));
    subscriptions.add(unlimitSub);
    unlimitPools = poolManager.createAndEnrichPools(unlimitSub);
}
Also used : Owner(org.candlepin.model.Owner) ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) ArrayList(java.util.ArrayList) Subscription(org.candlepin.model.dto.Subscription) ConsumerType(org.candlepin.model.ConsumerType) Before(org.junit.Before)

Example 9 with ImportSubscriptionServiceAdapter

use of org.candlepin.service.impl.ImportSubscriptionServiceAdapter in project candlepin by candlepin.

the class OwnerResourceTest method testRefreshPoolsWithNewSubscriptions.

@Test
public void testRefreshPoolsWithNewSubscriptions() {
    Product prod = this.createProduct(owner);
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    OwnerServiceAdapter ownerAdapter = new DefaultOwnerServiceAdapter(this.ownerCurator, this.i18n);
    Subscription sub = TestUtil.createSubscription(owner, prod, new HashSet<>());
    sub.setId(Util.generateDbUUID());
    sub.setQuantity(2000L);
    sub.setStartDate(TestUtil.createDate(2010, 2, 9));
    sub.setEndDate(TestUtil.createDate(3000, 2, 9));
    sub.setModified(TestUtil.createDate(2010, 2, 12));
    subscriptions.add(sub);
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    List<Pool> pools = poolCurator.listByOwnerAndProduct(owner, prod.getId());
    assertEquals(1, pools.size());
    Pool newPool = pools.get(0);
    assertEquals(sub.getId(), newPool.getSubscriptionId());
    assertEquals(sub.getQuantity(), newPool.getQuantity());
    assertEquals(sub.getStartDate(), newPool.getStartDate());
    assertEquals(sub.getEndDate(), newPool.getEndDate());
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) LinkedList(java.util.LinkedList) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Test(org.junit.Test)

Example 10 with ImportSubscriptionServiceAdapter

use of org.candlepin.service.impl.ImportSubscriptionServiceAdapter in project candlepin by candlepin.

the class OwnerResourceTest method testRefreshMultiplePools.

@Test
public void testRefreshMultiplePools() {
    Product prod = this.createProduct(owner);
    Product prod2 = this.createProduct(owner);
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    OwnerServiceAdapter ownerAdapter = new DefaultOwnerServiceAdapter(this.ownerCurator, this.i18n);
    Subscription sub = TestUtil.createSubscription(owner, prod, new HashSet<>());
    sub.setId(Util.generateDbUUID());
    sub.setQuantity(2000L);
    sub.setStartDate(TestUtil.createDate(2010, 2, 9));
    sub.setEndDate(TestUtil.createDate(3000, 2, 9));
    sub.setModified(TestUtil.createDate(2010, 2, 12));
    subscriptions.add(sub);
    Subscription sub2 = TestUtil.createSubscription(owner, prod2, new HashSet<>());
    sub2.setId(Util.generateDbUUID());
    sub2.setQuantity(800L);
    sub2.setStartDate(TestUtil.createDate(2010, 2, 9));
    sub2.setEndDate(TestUtil.createDate(3000, 2, 9));
    sub2.setModified(TestUtil.createDate(2010, 2, 12));
    subscriptions.add(sub2);
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    List<Pool> pools = poolCurator.listByOwner(owner).list();
    assertEquals(2, pools.size());
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) LinkedList(java.util.LinkedList) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Test(org.junit.Test)

Aggregations

Subscription (org.candlepin.model.dto.Subscription)13 ImportSubscriptionServiceAdapter (org.candlepin.service.impl.ImportSubscriptionServiceAdapter)13 LinkedList (java.util.LinkedList)12 Product (org.candlepin.model.Product)10 OwnerServiceAdapter (org.candlepin.service.OwnerServiceAdapter)9 DefaultOwnerServiceAdapter (org.candlepin.service.impl.DefaultOwnerServiceAdapter)9 Test (org.junit.Test)9 Pool (org.candlepin.model.Pool)8 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Owner (org.candlepin.model.Owner)3 Matchers.anyString (org.mockito.Matchers.anyString)3 Consumer (org.candlepin.model.Consumer)2 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)2 ConsumerType (org.candlepin.model.ConsumerType)2 Before (org.junit.Before)2 Transactional (com.google.inject.persist.Transactional)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1