use of org.candlepin.model.Owner in project candlepin by candlepin.
the class ConsumerImporter method populateEntity.
/**
* Populates the specified entity with data from the provided DTO.
* This method does not set the upstreamConsumer field.
*
* @param entity
* The entity instance to populate
*
* @param dto
* The DTO containing the data with which to populate the entity
*
* @throws IllegalArgumentException
* if either entity or dto are null
*/
protected void populateEntity(Owner entity, OwnerDTO dto) {
if (entity == null) {
throw new IllegalArgumentException("the owner model entity is null");
}
if (dto == null) {
throw new IllegalArgumentException("the owner dto is null");
}
if (dto.getId() != null) {
entity.setId(dto.getId());
}
if (dto.getDisplayName() != null) {
entity.setDisplayName(dto.getDisplayName());
}
if (dto.getKey() != null) {
entity.setKey(dto.getKey());
}
if (dto.getLastRefreshed() != null) {
entity.setLastRefreshed(dto.getLastRefreshed());
}
if (dto.getContentAccessMode() != null) {
entity.setContentAccessMode(dto.getContentAccessMode());
}
if (dto.getContentAccessModeList() != null) {
entity.setContentAccessModeList(dto.getContentAccessModeList());
}
if (dto.getCreated() != null) {
entity.setCreated(dto.getCreated());
}
if (dto.getUpdated() != null) {
entity.setUpdated(dto.getUpdated());
}
if (dto.getParentOwner() != null) {
// Impl note:
// We do not allow modifying a parent owner through its children, so all we'll do here
// is set the parent owner and ignore everything else; including further nested owners.
OwnerDTO pdto = dto.getParentOwner();
Owner parent = null;
if (pdto.getId() != null) {
// look up by ID
parent = this.curator.find(pdto.getId());
} else if (pdto.getKey() != null) {
// look up by key
parent = this.curator.lookupByKey(pdto.getKey());
}
if (parent == null) {
throw new NotFoundException(i18n.tr("Unable to find parent owner: {0}", pdto));
}
entity.setParentOwner(parent);
}
if (dto.getContentPrefix() != null) {
entity.setContentPrefix(dto.getContentPrefix());
}
if (dto.getDefaultServiceLevel() != null) {
entity.setDefaultServiceLevel(dto.getDefaultServiceLevel());
}
if (dto.getLogLevel() != null) {
entity.setLogLevel(dto.getLogLevel());
}
if (dto.isAutobindDisabled() != null) {
entity.setAutobindDisabled(dto.isAutobindDisabled());
}
}
use of org.candlepin.model.Owner in project candlepin by candlepin.
the class ConsumerImporter method store.
public void store(Owner owner, ConsumerDTO consumer, ConflictOverrides forcedConflicts, IdentityCertificate idcert) throws SyncDataFormatException {
if (consumer.getUuid() == null) {
throw new SyncDataFormatException(i18n.tr("No ID for upstream subscription management application."));
}
// Make sure no other owner is already using this upstream UUID:
Owner alreadyUsing = curator.lookupWithUpstreamUuid(consumer.getUuid());
if (alreadyUsing != null && !alreadyUsing.getKey().equals(owner.getKey())) {
log.error("Cannot import manifest for org: {}", owner.getKey());
log.error("Upstream distributor {} already in used by org: {}", consumer.getUuid(), alreadyUsing.getKey());
// delete their manifest after which it could be used elsewhere.
throw new SyncDataFormatException(i18n.tr("This subscription management application has already been imported by another owner."));
}
if (owner.getUpstreamUuid() != null && !owner.getUpstreamUuid().equals(consumer.getUuid())) {
if (!forcedConflicts.isForced(Importer.Conflict.DISTRIBUTOR_CONFLICT)) {
throw new ImportConflictException(i18n.tr("Owner has already imported from another subscription management application."), Importer.Conflict.DISTRIBUTOR_CONFLICT);
} else {
log.warn("Forcing import from a new distributor for org: {}", owner.getKey());
log.warn("Old distributor UUID: {}", owner.getUpstreamUuid());
log.warn("New distributor UUID: {}", consumer.getUuid());
}
}
/*
* WARNING: Strange quirk here, we create a certificate serial object here which does not
* match the actual serial of the identity certificate. Presumably this is to prevent
* potential conflicts with a serial that came from somewhere else. This is consistent with
* importing entitlement certs (as subscription certs).
*/
if (idcert != null) {
CertificateSerial cs = new CertificateSerial();
cs.setCollected(idcert.getSerial().isCollected());
cs.setExpiration(idcert.getSerial().getExpiration());
cs.setUpdated(idcert.getSerial().getUpdated());
cs.setCreated(idcert.getSerial().getCreated());
serialCurator.create(cs);
idcert.setId(null);
idcert.setSerial(cs);
idCertCurator.create(idcert);
}
// create an UpstreamConsumer from the imported ConsumerDto
ConsumerType type = new ConsumerType();
populateEntity(type, consumer.getType());
Owner ownerToUse = new Owner();
if (consumer.getOwner() != null) {
populateEntity(ownerToUse, consumer.getOwner());
}
UpstreamConsumer uc = new UpstreamConsumer(consumer.getName(), ownerToUse, type, consumer.getUuid());
uc.setWebUrl(consumer.getUrlWeb());
uc.setApiUrl(consumer.getUrlApi());
uc.setIdCert(idcert);
uc.setContentAccessMode(consumer.getContentAccessMode());
owner.setUpstreamConsumer(uc);
curator.merge(owner);
}
use of org.candlepin.model.Owner in project candlepin by candlepin.
the class Exporter method exportProducts.
private void exportProducts(File baseDir, Consumer consumer) throws IOException {
File productDir = new File(baseDir.getCanonicalPath(), "products");
productDir.mkdir();
Map<String, Product> products = new HashMap<>();
for (Entitlement entitlement : consumer.getEntitlements()) {
Pool pool = entitlement.getPool();
for (Product providedProduct : productCurator.getPoolProvidedProductsCached(pool)) {
products.put(providedProduct.getId(), providedProduct);
}
// Don't forget the 'main' product!
Product product = pool.getProduct();
products.put(product.getId(), product);
// Also need to check for sub products
Product derivedProduct = pool.getDerivedProduct();
if (derivedProduct != null) {
products.put(derivedProduct.getId(), derivedProduct);
}
for (Product derivedProvidedProduct : productCurator.getPoolDerivedProvidedProductsCached(pool)) {
products.put(derivedProvidedProduct.getId(), derivedProvidedProduct);
}
}
for (Product product : products.values()) {
// Clear the owner and UUID so they can be re-generated/assigned on import
// product.setUuid(null);
// product.setOwner(null);
String path = productDir.getCanonicalPath();
String productId = product.getId();
File file = new File(path, productId + ".json");
FileWriter writer = null;
try {
writer = new FileWriter(file);
productExporter.export(mapper, writer, product);
} finally {
if (writer != null) {
writer.close();
}
}
// Real products have a numeric id.
if (StringUtils.isNumeric(product.getId())) {
Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
ProductCertificate cert = productAdapter.getProductCertificate(owner, product.getId());
// XXX: need to decide if the cert should always be in the export, or never.
if (cert != null) {
file = new File(productDir.getCanonicalPath(), product.getId() + ".pem");
writer = new FileWriter(file);
productCertExporter.export(writer, cert);
writer.close();
}
}
}
}
use of org.candlepin.model.Owner in project candlepin by candlepin.
the class ResolverUtil method resolveSubscription.
public Subscription resolveSubscription(Subscription subscription) {
// need to make sure it's not null.
if (subscription == null) {
throw new BadRequestException(i18n.tr("No subscription specified"));
}
// Ensure the owner is set and is valid
Owner owner = this.resolveOwner(subscription.getOwner());
subscription.setOwner(owner);
// Ensure the specified product(s) exists for the given owner
this.validateProductData(subscription.getProduct(), owner, false);
this.validateProductData(subscription.getDerivedProduct(), owner, true);
for (ProductData product : subscription.getProvidedProducts()) {
this.validateProductData(product, owner, true);
}
for (ProductData product : subscription.getDerivedProvidedProducts()) {
this.validateProductData(product, owner, true);
}
return subscription;
}
use of org.candlepin.model.Owner in project candlepin by candlepin.
the class ResolverUtil method resolveSubscriptionAndProduct.
/**
* used to resolve subscription but it resolves the product too.
* currently used in hostedtest resources
* @param subscription
* @return the resolved subscription
*/
public Subscription resolveSubscriptionAndProduct(Subscription subscription) {
// We just need to make sure it's not null.
if (subscription == null) {
throw new BadRequestException(i18n.tr("No subscription specified"));
}
// Ensure the owner is set and is valid
Owner owner = this.resolveOwner(subscription.getOwner());
subscription.setOwner(owner);
subscription.setProduct(new ProductData(this.resolveProduct(owner, subscription.getProduct().getId())));
if (subscription.getDerivedProduct() != null) {
ProductData p = new ProductData(this.resolveProduct(owner, subscription.getDerivedProduct().getId()));
subscription.setDerivedProduct(p);
}
HashSet<ProductData> providedProducts = new HashSet<>();
for (ProductData product : subscription.getProvidedProducts()) {
if (product != null) {
providedProducts.add(new ProductData(this.resolveProduct(owner, product.getId())));
}
}
subscription.setProvidedProducts(providedProducts);
HashSet<ProductData> derivedProvidedProducts = new HashSet<>();
for (ProductData product : subscription.getDerivedProvidedProducts()) {
if (product != null) {
derivedProvidedProducts.add(new ProductData(this.resolveProduct(owner, product.getId())));
}
}
subscription.setDerivedProvidedProducts(derivedProvidedProducts);
return subscription;
}
Aggregations