use of org.candlepin.service.SubscriptionServiceAdapter 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.service.SubscriptionServiceAdapter in project candlepin by candlepin.
the class ConsumerResourceTest method testIdCertGetsRegenerated.
@Test
public void testIdCertGetsRegenerated() throws Exception {
// using lconsumer simply to avoid hiding consumer. This should
// get renamed once we refactor this test suite.
IdentityCertServiceAdapter mockIdSvc = Mockito.mock(IdentityCertServiceAdapter.class);
EventSink sink = Mockito.mock(EventSinkImpl.class);
SubscriptionServiceAdapter ssa = Mockito.mock(SubscriptionServiceAdapter.class);
ComplianceRules rules = Mockito.mock(ComplianceRules.class);
Consumer consumer = createConsumer(createOwner());
ComplianceStatus status = new ComplianceStatus();
when(rules.getStatus(any(Consumer.class), any(Date.class), anyBoolean())).thenReturn(status);
// cert expires today which will trigger regen
consumer.setIdCert(createIdCert());
BigInteger origserial = consumer.getIdCert().getSerial().getSerial();
when(mockIdSvc.regenerateIdentityCert(consumer)).thenReturn(createIdCert());
ConsumerResource cr = new ConsumerResource(mockConsumerCurator, mockConsumerTypeCurator, null, ssa, this.mockOwnerServiceAdapter, null, mockIdSvc, null, null, sink, eventFactory, null, null, null, null, null, mockOwnerCurator, null, null, rules, null, null, null, this.config, null, null, null, consumerBindUtil, null, null, this.factValidator, null, consumerEnricher, migrationProvider, translator);
ConsumerDTO c = cr.getConsumer(consumer.getUuid());
assertFalse(origserial.equals(c.getIdCert().getSerial().getSerial()));
}
use of org.candlepin.service.SubscriptionServiceAdapter in project candlepin by candlepin.
the class ConsumerResourceTest method futureHealing.
@Test
public void futureHealing() throws Exception {
Owner o = createOwner();
o.setId(TestUtil.randomString());
Consumer c = createConsumer(o);
SubscriptionServiceAdapter sa = mock(SubscriptionServiceAdapter.class);
Entitler e = mock(Entitler.class);
ConsumerCurator cc = mock(ConsumerCurator.class);
ConsumerInstalledProduct cip = mock(ConsumerInstalledProduct.class);
Set<ConsumerInstalledProduct> products = new HashSet<>();
products.add(cip);
when(mockOwnerCurator.findOwnerById(eq(o.getId()))).thenReturn(o);
when(cip.getProductId()).thenReturn("product-foo");
when(sa.hasUnacceptedSubscriptionTerms(eq(o))).thenReturn(false);
when(cc.verifyAndLookupConsumerWithEntitlements(eq(c.getUuid()))).thenReturn(c);
ConsumerResource cr = new ConsumerResource(cc, mockConsumerTypeCurator, null, sa, this.mockOwnerServiceAdapter, null, null, null, null, null, null, null, null, null, null, null, mockOwnerCurator, null, e, null, null, null, null, this.config, null, null, null, consumerBindUtil, null, null, this.factValidator, null, consumerEnricher, migrationProvider, translator);
String dtStr = "2011-09-26T18:10:50.184081+00:00";
Date dt = ResourceDateParser.parseDateString(dtStr);
cr.bind(c.getUuid(), null, null, null, null, null, false, dtStr, null);
AutobindData data = AutobindData.create(c, o).on(dt);
verify(e).bindByProducts(eq(data));
}
use of org.candlepin.service.SubscriptionServiceAdapter in project candlepin by candlepin.
the class ConsumerResourceTest method testIdCertDoesNotRegenerate.
@Test
public void testIdCertDoesNotRegenerate() throws Exception {
SubscriptionServiceAdapter ssa = Mockito.mock(SubscriptionServiceAdapter.class);
ComplianceRules rules = Mockito.mock(ComplianceRules.class);
Consumer consumer = createConsumer(createOwner());
ComplianceStatus status = new ComplianceStatus();
when(rules.getStatus(any(Consumer.class), any(Date.class), anyBoolean())).thenReturn(status);
consumer.setIdCert(createIdCert(TestUtil.createDate(2025, 6, 9)));
BigInteger origserial = consumer.getIdCert().getSerial().getSerial();
ConsumerResource cr = new ConsumerResource(mockConsumerCurator, mockConsumerTypeCurator, null, ssa, this.mockOwnerServiceAdapter, null, null, null, null, null, null, null, null, null, null, null, mockOwnerCurator, null, null, rules, null, null, null, this.config, null, null, null, consumerBindUtil, null, null, this.factValidator, null, consumerEnricher, migrationProvider, translator);
ConsumerDTO c = cr.getConsumer(consumer.getUuid());
assertEquals(origserial, c.getIdCert().getSerial().getSerial());
}
use of org.candlepin.service.SubscriptionServiceAdapter in project candlepin by candlepin.
the class ConsumerResourceTest method testProductNoPool.
@Test
public void testProductNoPool() throws Exception {
Owner o = createOwner();
o.setId(TestUtil.randomString());
Consumer c = createConsumer(o);
SubscriptionServiceAdapter sa = mock(SubscriptionServiceAdapter.class);
Entitler e = mock(Entitler.class);
ConsumerCurator cc = mock(ConsumerCurator.class);
String[] prodIds = { "notthere" };
when(sa.hasUnacceptedSubscriptionTerms(eq(o))).thenReturn(false);
when(cc.verifyAndLookupConsumerWithEntitlements(eq("fakeConsumer"))).thenReturn(c);
when(e.bindByProducts(any(AutobindData.class))).thenReturn(null);
when(mockOwnerCurator.findOwnerById(eq(o.getId()))).thenReturn(o);
ConsumerResource cr = new ConsumerResource(cc, mockConsumerTypeCurator, null, sa, this.mockOwnerServiceAdapter, null, null, null, i18n, null, null, null, null, null, null, null, mockOwnerCurator, null, e, null, null, null, null, this.config, null, null, null, consumerBindUtil, null, null, this.factValidator, null, consumerEnricher, migrationProvider, translator);
Response r = cr.bind("fakeConsumer", null, prodIds, null, null, null, false, null, null);
assertEquals(null, r.getEntity());
}
Aggregations