use of org.candlepin.dto.api.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;
}
use of org.candlepin.dto.api.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;
}
use of org.candlepin.dto.api.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;
}
use of org.candlepin.dto.api.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"));
}
use of org.candlepin.dto.api.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;
}
}
Aggregations