use of org.candlepin.dto.rules.v1.EntitlementDTO in project candlepin by candlepin.
the class EntitlementImporter method importObject.
public Subscription importObject(ObjectMapper mapper, Reader reader, Owner owner, Map<String, ProductDTO> productsById, String consumerUuid, Meta meta) throws IOException, SyncDataFormatException {
EntitlementDTO entitlementDTO = mapper.readValue(reader, EntitlementDTO.class);
Entitlement entitlement = new Entitlement();
populateEntity(entitlement, entitlementDTO);
Subscription subscription = new Subscription();
log.debug("Building subscription for owner: {}", owner);
log.debug("Using pool from entitlement: {}", entitlement.getPool());
// Now that we no longer store Subscriptions in the on-site database, we need to
// manually give the subscription a downstream ID. Note that this may later be
// overwritten by reconciliation code if it determines this Subscription
// should replace and existing one.
subscription.setId(Util.generateDbUUID());
subscription.setUpstreamPoolId(entitlement.getPool().getId());
subscription.setUpstreamEntitlementId(entitlement.getId());
subscription.setUpstreamConsumerId(consumerUuid);
subscription.setOwner(owner);
subscription.setStartDate(entitlement.getStartDate());
subscription.setEndDate(entitlement.getEndDate());
subscription.setAccountNumber(entitlement.getPool().getAccountNumber());
subscription.setContractNumber(entitlement.getPool().getContractNumber());
subscription.setOrderNumber(entitlement.getPool().getOrderNumber());
subscription.setQuantity(entitlement.getQuantity().longValue());
for (Branding b : entitlement.getPool().getBranding()) {
subscription.getBranding().add(new Branding(b.getProductId(), b.getType(), b.getName()));
}
String cdnLabel = meta.getCdnLabel();
if (!StringUtils.isBlank(cdnLabel)) {
Cdn cdn = cdnCurator.lookupByLabel(cdnLabel);
if (cdn != null) {
subscription.setCdn(cdn);
}
}
ProductDTO productDTO = this.findProduct(productsById, entitlement.getPool().getProductId());
subscription.setProduct(this.translator.translate(productDTO, ProductData.class));
// Add any sub product data to the subscription.
if (entitlement.getPool().getDerivedProductId() != null) {
productDTO = this.findProduct(productsById, entitlement.getPool().getDerivedProductId());
subscription.setDerivedProduct(this.translator.translate(productDTO, ProductData.class));
}
associateProvidedProducts(productsById, entitlement, subscription);
Set<EntitlementCertificate> certs = entitlement.getCertificates();
// subscriptions have one cert
int entcnt = 0;
for (EntitlementCertificate cert : certs) {
++entcnt;
CertificateSerial cs = new CertificateSerial();
cs.setCollected(cert.getSerial().isCollected());
cs.setExpiration(cert.getSerial().getExpiration());
cs.setUpdated(cert.getSerial().getUpdated());
cs.setCreated(cert.getSerial().getCreated());
SubscriptionsCertificate sc = new SubscriptionsCertificate();
sc.setKey(cert.getKey());
sc.setCertAsBytes(cert.getCertAsBytes());
sc.setSerial(cs);
subscription.setCertificate(sc);
}
if (entcnt > 1) {
log.error("More than one entitlement cert found for subscription");
}
return subscription;
}
use of org.candlepin.dto.rules.v1.EntitlementDTO in project candlepin by candlepin.
the class EntitlementImporter method populateEntity.
/**
* Populates the specified entity with data from the provided DTO.
*
* @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
*/
@SuppressWarnings("checkstyle:methodlength")
private void populateEntity(Entitlement entity, EntitlementDTO dto) {
if (entity == null) {
throw new IllegalArgumentException("the entitlement model entity is null");
}
if (dto == null) {
throw new IllegalArgumentException("the entitlement dto is null");
}
if (dto.getId() != null) {
entity.setId(dto.getId());
}
if (dto.getQuantity() != null) {
entity.setQuantity(dto.getQuantity());
}
if (dto.getUpdated() != null) {
entity.setUpdated(dto.getUpdated());
}
if (dto.getCreated() != null) {
entity.setCreated(dto.getCreated());
}
if (dto.getStartDate() != null) {
entity.setStartDate(dto.getStartDate());
}
if (dto.getEndDate() != null) {
entity.setEndDate(dto.getEndDate());
}
if (dto.getPool() != null) {
PoolDTO poolDTO = dto.getPool();
Pool poolEntity = new Pool();
if (poolDTO.getId() != null) {
poolEntity.setId(poolDTO.getId());
}
if (poolDTO.getQuantity() != null) {
poolEntity.setQuantity(poolDTO.getQuantity());
}
if (poolDTO.isActiveSubscription() != null) {
poolEntity.setActiveSubscription(poolDTO.isActiveSubscription());
}
if (poolDTO.isCreatedByShare() != null) {
poolEntity.setCreatedByShare(poolDTO.isCreatedByShare());
}
if (poolDTO.hasSharedAncestor() != null) {
poolEntity.setHasSharedAncestor(poolDTO.hasSharedAncestor());
}
if (poolDTO.getRestrictedToUsername() != null) {
poolEntity.setRestrictedToUsername(poolDTO.getRestrictedToUsername());
}
if (poolDTO.getConsumed() != null) {
poolEntity.setConsumed(poolDTO.getConsumed());
}
if (poolDTO.getExported() != null) {
poolEntity.setExported(poolDTO.getExported());
}
if (poolDTO.getShared() != null) {
poolEntity.setShared(poolDTO.getShared());
}
if (poolDTO.getStackId() != null && poolDTO.getSourceStackId() != null) {
SourceStack sourceStack = new SourceStack();
sourceStack.setId(poolDTO.getStackId());
sourceStack.setSourceStackId(poolDTO.getSourceStackId());
poolEntity.setSourceStack(sourceStack);
}
if (poolDTO.getProductId() != null) {
poolEntity.setProductId(poolDTO.getProductId());
}
if (poolDTO.getDerivedProductId() != null) {
poolEntity.setDerivedProductId(poolDTO.getDerivedProductId());
}
if (poolDTO.getStartDate() != null) {
poolEntity.setStartDate(poolDTO.getStartDate());
}
if (poolDTO.getEndDate() != null) {
poolEntity.setEndDate(poolDTO.getEndDate());
}
if (poolDTO.getCreated() != null) {
poolEntity.setCreated(poolDTO.getCreated());
}
if (poolDTO.getUpdated() != null) {
poolEntity.setUpdated(poolDTO.getUpdated());
}
if (poolDTO.getAccountNumber() != null) {
poolEntity.setAccountNumber(poolDTO.getAccountNumber());
}
if (poolDTO.getOrderNumber() != null) {
poolEntity.setOrderNumber(poolDTO.getOrderNumber());
}
if (poolDTO.getContractNumber() != null) {
poolEntity.setContractNumber(poolDTO.getContractNumber());
}
if (poolDTO.getOwner() != null) {
Owner ownerEntity = new Owner();
populateEntity(ownerEntity, poolDTO.getOwner());
poolEntity.setOwner(ownerEntity);
}
if (poolDTO.getUpstreamPoolId() != null) {
poolEntity.setUpstreamPoolId(poolDTO.getUpstreamPoolId());
}
if (poolDTO.getUpstreamConsumerId() != null) {
poolEntity.setUpstreamConsumerId(poolDTO.getUpstreamConsumerId());
}
if (poolDTO.getUpstreamEntitlementId() != null) {
poolEntity.setUpstreamEntitlementId(poolDTO.getUpstreamEntitlementId());
}
if (poolDTO.getSourceEntitlement() != null) {
EntitlementDTO sourceEntitlementDTO = poolDTO.getSourceEntitlement();
poolEntity.setSourceEntitlement(findEntitlement(sourceEntitlementDTO.getId()));
}
if (poolDTO.getSubscriptionSubKey() != null) {
poolEntity.setSubscriptionSubKey(poolDTO.getSubscriptionSubKey());
}
if (poolDTO.getSubscriptionId() != null) {
poolEntity.setSubscriptionId(poolDTO.getSubscriptionId());
}
if (poolDTO.getAttributes() != null) {
if (poolDTO.getAttributes().isEmpty()) {
poolEntity.setAttributes(Collections.emptyMap());
} else {
poolEntity.setAttributes(poolDTO.getAttributes());
}
}
if (poolDTO.getCalculatedAttributes() != null) {
if (poolDTO.getCalculatedAttributes().isEmpty()) {
poolEntity.setCalculatedAttributes(Collections.emptyMap());
} else {
poolEntity.setCalculatedAttributes(poolDTO.getCalculatedAttributes());
}
}
if (poolDTO.getProductAttributes() != null) {
if (poolDTO.getProductAttributes().isEmpty()) {
poolEntity.setProductAttributes(Collections.emptyMap());
} else {
poolEntity.setProductAttributes(poolDTO.getProductAttributes());
}
}
if (poolDTO.getDerivedProductAttributes() != null) {
if (poolDTO.getDerivedProductAttributes().isEmpty()) {
poolEntity.setDerivedProductAttributes(Collections.emptyMap());
} else {
poolEntity.setDerivedProductAttributes(poolDTO.getDerivedProductAttributes());
}
}
if (poolDTO.getBranding() != null) {
if (poolDTO.getBranding().isEmpty()) {
poolEntity.setBranding(Collections.emptySet());
} else {
Set<Branding> branding = new HashSet<>();
for (BrandingDTO brandingDTO : poolDTO.getBranding()) {
if (brandingDTO != null) {
Branding brandingEntity = new Branding(brandingDTO.getProductId(), brandingDTO.getType(), brandingDTO.getName());
brandingEntity.setId(brandingDTO.getId());
brandingEntity.setCreated(brandingDTO.getCreated());
brandingEntity.setUpdated(brandingDTO.getUpdated());
branding.add(brandingEntity);
}
}
poolEntity.setBranding(branding);
}
}
if (poolDTO.getProvidedProducts() != null) {
if (poolDTO.getProvidedProducts().isEmpty()) {
poolEntity.setProvidedProductDtos(Collections.emptySet());
} else {
Set<ProvidedProduct> providedProducts = new HashSet<>();
for (PoolDTO.ProvidedProductDTO ppDTO : poolDTO.getProvidedProducts()) {
if (ppDTO != null) {
ProvidedProduct providedProduct = new ProvidedProduct();
providedProduct.setProductId(ppDTO.getProductId());
providedProduct.setProductName(ppDTO.getProductName());
providedProducts.add(providedProduct);
}
}
poolEntity.setProvidedProductDtos(providedProducts);
}
}
if (poolDTO.getDerivedProvidedProducts() != null) {
if (poolDTO.getDerivedProvidedProducts().isEmpty()) {
poolEntity.setDerivedProvidedProductDtos(Collections.emptySet());
} else {
Set<ProvidedProduct> derivedProvidedProducts = new HashSet<>();
for (PoolDTO.ProvidedProductDTO dppDTO : poolDTO.getDerivedProvidedProducts()) {
if (dppDTO != null) {
ProvidedProduct derivedProvidedProduct = new ProvidedProduct();
derivedProvidedProduct.setProductId(dppDTO.getProductId());
derivedProvidedProduct.setProductName(dppDTO.getProductName());
derivedProvidedProducts.add(derivedProvidedProduct);
}
}
poolEntity.setDerivedProvidedProductDtos(derivedProvidedProducts);
}
}
entity.setPool(poolEntity);
}
if (dto.getCertificates() != null) {
if (dto.getCertificates().isEmpty()) {
entity.setCertificates(Collections.emptySet());
} else {
Set<EntitlementCertificate> entityCerts = new HashSet<>();
for (CertificateDTO dtoCert : dto.getCertificates()) {
if (dtoCert != null) {
EntitlementCertificate entityCert = new EntitlementCertificate();
entityCert.setId(dtoCert.getId());
entityCert.setKey(dtoCert.getKey());
entityCert.setCert(dtoCert.getCert());
entityCert.setCreated(dtoCert.getCreated());
entityCert.setUpdated(dtoCert.getUpdated());
if (dtoCert.getSerial() != null) {
CertificateSerialDTO dtoSerial = dtoCert.getSerial();
CertificateSerial entitySerial = new CertificateSerial();
entitySerial.setId(dtoSerial.getId());
entitySerial.setCollected(dtoSerial.isCollected());
entitySerial.setExpiration(dtoSerial.getExpiration());
entitySerial.setRevoked(dtoSerial.isRevoked());
entitySerial.setSerial(dtoSerial.getSerial() != null ? dtoSerial.getSerial().longValueExact() : null);
entitySerial.setCreated(dtoSerial.getCreated());
entitySerial.setUpdated(dtoSerial.getUpdated());
entityCert.setSerial(entitySerial);
}
entityCerts.add(entityCert);
}
}
entity.setCertificates(entityCerts);
}
}
}
use of org.candlepin.dto.rules.v1.EntitlementDTO in project candlepin by candlepin.
the class ConsumerResource method bind.
@ApiOperation(notes = "If a pool ID is specified, we know we're binding to that exact pool. " + "Specifying an entitle date in this case makes no sense and will throw an " + "error. If a list of product IDs are specified, we attempt to auto-bind to" + " subscriptions which will provide those products. An optional date can be" + " specified allowing the consumer to get compliant for some date in the " + "future. If no date is specified we assume the current date. If neither a " + "pool nor an ID is specified, this is a healing request. The path is similar " + "to the bind by products, but in this case we use the installed products on " + "the consumer, and their current compliant status, to determine which product" + " IDs should be requested. The entitle date is used the same as with bind by " + "products. The response will contain a list of Entitlement objects if async is" + " false, or a JobDetail object if async is true.", value = "Bind Entitlements")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 403, message = "Binds Entitlements"), @ApiResponse(code = 404, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{consumer_uuid}/entitlements")
@SuppressWarnings("checkstyle:indentation")
public Response bind(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @QueryParam("pool") @Verify(value = Pool.class, nullable = true, subResource = SubResource.ENTITLEMENTS) String poolIdString, @QueryParam("product") String[] productIds, @QueryParam("quantity") Integer quantity, @QueryParam("email") String email, @QueryParam("email_locale") String emailLocale, @QueryParam("async") @DefaultValue("false") boolean async, @QueryParam("entitle_date") String entitleDateStr, @QueryParam("from_pool") List<String> fromPools) {
/* NOTE: This method should NEVER be provided with a POST body.
While technically that change would be backwards compatible,
there are older clients which erroneously provide an empty string
as a post body and hence result in a serialization error.
ref: BZ: 1502807
*/
// TODO: really should do this in a before we get to this call
// so the method takes in a real Date object and not just a String.
Date entitleDate = ResourceDateParser.parseDateString(entitleDateStr);
// Verify consumer exists:
Consumer consumer = consumerCurator.verifyAndLookupConsumerWithEntitlements(consumerUuid);
ConsumerType ctype = this.consumerTypeCurator.getConsumerType(consumer);
log.debug("Consumer (post verify): {}", consumer);
// Check that only one query param was set, and some other validations
validateBindArguments(poolIdString, quantity, productIds, fromPools, entitleDate, consumer, async);
Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId());
try {
// I hate double negatives, but if they have accepted all
// terms, we want comeToTerms to be true.
long subTermsStart = System.currentTimeMillis();
if (!ctype.isType(ConsumerTypeEnum.SHARE) && subAdapter.hasUnacceptedSubscriptionTerms(owner)) {
return Response.serverError().build();
}
log.debug("Checked if consumer has unaccepted subscription terms in {}ms", (System.currentTimeMillis() - subTermsStart));
} catch (CandlepinException e) {
log.debug(e.getMessage());
throw e;
}
if (poolIdString != null && quantity == null) {
Pool pool = poolManager.find(poolIdString);
quantity = pool != null ? consumerBindUtil.getQuantityToBind(pool, consumer) : 1;
}
//
if (async) {
JobDetail detail = null;
if (poolIdString != null) {
detail = EntitlerJob.bindByPool(poolIdString, consumer, owner.getKey(), quantity);
} else {
detail = EntitleByProductsJob.bindByProducts(productIds, consumer, entitleDate, fromPools, owner.getKey());
}
// events will be triggered by the job
return Response.status(Response.Status.OK).type(MediaType.APPLICATION_JSON).entity(detail).build();
}
//
// otherwise we do what we do today.
//
List<Entitlement> entitlements = null;
if (poolIdString != null) {
entitlements = entitler.bindByPoolQuantity(consumer, poolIdString, quantity);
} else {
try {
AutobindData autobindData = AutobindData.create(consumer, owner).on(entitleDate).forProducts(productIds).withPools(fromPools);
entitlements = entitler.bindByProducts(autobindData);
} catch (AutobindDisabledForOwnerException e) {
throw new BadRequestException(i18n.tr("Ignoring request to auto-attach. " + "It is disabled for org \"{0}\".", owner.getKey()));
}
}
List<EntitlementDTO> entitlementDTOs = null;
if (entitlements != null) {
entitlementDTOs = new ArrayList<>();
for (Entitlement ent : entitlements) {
// we need to supply the compliance type for the pools
// the method in this class does not do quantity
addCalculatedAttributes(ent);
entitlementDTOs.add(this.translator.translate(ent, EntitlementDTO.class));
}
}
// Trigger events:
entitler.sendEvents(entitlements);
return Response.status(Response.Status.OK).type(MediaType.APPLICATION_JSON).entity(entitlementDTOs).build();
}
use of org.candlepin.dto.rules.v1.EntitlementDTO in project candlepin by candlepin.
the class ConsumerResource method listEntitlements.
@ApiOperation(notes = "Retrives a list of Entitlements", value = "listEntitlements")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 404, message = "") })
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/{consumer_uuid}/entitlements")
public List<EntitlementDTO> listEntitlements(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @QueryParam("product") String productId, @QueryParam("regen") @DefaultValue("true") Boolean regen, @QueryParam("matches") String matches, @QueryParam("attribute") @CandlepinParam(type = KeyValueParameter.class) List<KeyValueParameter> attrFilters, @Context PageRequest pageRequest) {
Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
if (regen) {
revokeOnGuestMigration(consumer);
}
EntitlementFilterBuilder filters = EntitlementFinderUtil.createFilter(matches, attrFilters);
Page<List<Entitlement>> entitlementsPage = entitlementCurator.listByConsumer(consumer, productId, filters, pageRequest);
// Store the page for the LinkHeaderPostInterceptor
ResteasyProviderFactory.pushContext(Page.class, entitlementsPage);
if (regen) {
poolManager.regenerateDirtyEntitlements(entitlementsPage.getPageData());
} else {
log.debug("Skipping certificate regeneration.");
}
// the method in this class does not do quantity
if (entitlementsPage.getPageData() != null) {
for (Entitlement ent : entitlementsPage.getPageData()) {
addCalculatedAttributes(ent);
}
}
List<EntitlementDTO> entitlementDTOs = new ArrayList<>();
for (Entitlement entitlement : entitlementsPage.getPageData()) {
entitlementDTOs.add(this.translator.translate(entitlement, EntitlementDTO.class));
}
return entitlementDTOs;
}
use of org.candlepin.dto.rules.v1.EntitlementDTO in project candlepin by candlepin.
the class PoolResource method getPoolEntitlements.
@ApiOperation(notes = "Retrieve a list of Entitlements for a Pool", value = "getPoolEntitlements")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@GET
@Path("{pool_id}/entitlements")
@Produces(MediaType.APPLICATION_JSON)
public List<EntitlementDTO> getPoolEntitlements(@PathParam("pool_id") @Verify(value = Pool.class, subResource = SubResource.ENTITLEMENTS) String id, @Context Principal principal) {
Pool pool = poolManager.find(id);
if (pool == null) {
throw new NotFoundException(i18n.tr("Subscription Pool with ID \"{0}\" could not be found.", id));
}
List<EntitlementDTO> entitlementDTOs = new ArrayList<>();
for (Entitlement entitlement : pool.getEntitlements()) {
entitlementDTOs.add(this.translator.translate(entitlement, EntitlementDTO.class));
}
return entitlementDTOs;
}
Aggregations