Search in sources :

Example 16 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO 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 17 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class Importer method importEntitlements.

protected List<Subscription> importEntitlements(Owner owner, Set<ProductDTO> products, File[] entitlements, String consumerUuid, Meta meta) throws IOException, SyncDataFormatException {
    log.debug("Importing entitlements for owner: {}", owner);
    EntitlementImporter importer = new EntitlementImporter(csCurator, cdnCurator, i18n, productCurator, entitlementCurator, translator);
    Map<String, ProductDTO> productsById = new HashMap<>();
    for (ProductDTO product : products) {
        log.debug("Adding product owned by {} to ID map", owner.getKey());
        // Note: This may actually be causing problems with subscriptions receiving the wrong
        // version of a product
        productsById.put(product.getId(), product);
    }
    List<Subscription> subscriptionsToImport = new ArrayList<>();
    for (File entitlement : entitlements) {
        Reader reader = null;
        try {
            log.debug("Import entitlement: {}", entitlement.getName());
            reader = new FileReader(entitlement);
            subscriptionsToImport.add(importer.importObject(mapper, reader, owner, productsById, consumerUuid, meta));
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
    // Reconcile the subscriptions so they line up with pools we're tracking
    this.subscriptionReconciler.reconcile(owner, subscriptionsToImport);
    return subscriptionsToImport;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) Subscription(org.candlepin.model.dto.Subscription) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO)

Example 18 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class ProductImporter method createObject.

public ProductDTO createObject(ObjectMapper mapper, Reader reader, Owner owner) throws IOException {
    ProductDTO importedProduct = mapper.readValue(reader, ProductDTO.class);
    // Make sure the (UU)ID's are null, otherwise Hibernate thinks these are
    // detached entities.
    importedProduct.setUuid(null);
    // Multiplication has already happened on the upstream candlepin. set this to 1
    // so we can use multipliers on local products if necessary.
    importedProduct.setMultiplier(1L);
    if (importedProduct.getProductContent() != null) {
        // Update attached content and ensure it isn't malformed
        for (ProductDTO.ProductContentDTO pc : importedProduct.getProductContent()) {
            ContentDTO content = pc.getContent();
            // Clear the UUID
            content.setUuid(null);
            // Fix the vendor string if it is/was cleared (BZ 990113)
            if (StringUtils.isBlank(content.getVendor())) {
                content.setVendor("unknown");
            }
            // On standalone servers we will set metadata expire to 1 second so
            // clients an immediately get changes to content when published on the
            // server. We would use 0, but the client plugin interprets this as unset
            // and ignores it completely resulting in the default yum values being
            // used.
            // 
            // We know this is a standalone server due to the fact that import is
            // being used, so there is no need to guard this behavior.
            content.setMetadataExpire(1L);
        }
    }
    return importedProduct;
}
Also used : ContentDTO(org.candlepin.dto.manifest.v1.ContentDTO) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO)

Example 19 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class OwnerProductResourceTest method testUpdateProductWithoutId.

@Test
public void testUpdateProductWithoutId() {
    Owner owner = this.createOwner("Update-Product-Owner");
    ProductDTO pdto = this.buildTestProductDTO();
    ProductDTO product = this.ownerProductResource.createProduct(owner.getKey(), pdto);
    ProductDTO update = new ProductDTO();
    update.setName(product.getName());
    update.setAttribute("attri", "bute");
    ProductDTO result = this.ownerProductResource.updateProduct(owner.getKey(), product.getId(), update);
    assertEquals("bute", result.getAttributeValue("attri"));
}
Also used : Owner(org.candlepin.model.Owner) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Example 20 with ProductDTO

use of org.candlepin.dto.manifest.v1.ProductDTO in project candlepin by candlepin.

the class OwnerProductResourceTest method testUpdateLockedProductFails.

@Test(expected = ForbiddenException.class)
public void testUpdateLockedProductFails() {
    Owner owner = this.createOwner("test_owner");
    Product product = this.createProduct("test_product", "test_product", owner);
    ProductDTO pdto = TestUtil.createProductDTO("test_product", "updated_name");
    product.setLocked(true);
    this.productCurator.merge(product);
    assertNotNull(this.ownerProductCurator.getProductById(owner, pdto.getId()));
    try {
        this.ownerProductResource.updateProduct(owner.getKey(), pdto.getId(), pdto);
    } catch (ForbiddenException e) {
        Product entity = this.ownerProductCurator.getProductById(owner, pdto.getId());
        ProductDTO expected = this.modelTranslator.translate(entity, ProductDTO.class);
        assertNotNull(entity);
        assertNotEquals(expected, pdto);
        throw e;
    }
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Aggregations

ProductDTO (org.candlepin.dto.api.v1.ProductDTO)29 Product (org.candlepin.model.Product)27 Test (org.junit.Test)27 Owner (org.candlepin.model.Owner)25 ProductDTO (org.candlepin.dto.manifest.v1.ProductDTO)12 Content (org.candlepin.model.Content)10 ProductContent (org.candlepin.model.ProductContent)9 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)7 Parameters (junitparams.Parameters)6 ContentDTO (org.candlepin.dto.manifest.v1.ContentDTO)6 Transactional (com.google.inject.persist.Transactional)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 Subscription (org.candlepin.model.dto.Subscription)4 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)3