Search in sources :

Example 26 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ComplianceStatusHasherTest method createConsumer.

private Consumer createConsumer(Owner owner) {
    ConsumerType ctype = new ConsumerType(ConsumerType.ConsumerTypeEnum.SYSTEM);
    ctype.setId("test-ctype");
    Consumer consumer = new Consumer("test-consumer", "test-consumer", owner, ctype);
    consumer.setId("1");
    consumer.setUuid("12345");
    consumer.setFact("ram", "4");
    consumer.setFact("cores", "2");
    Product product1 = TestUtil.createProduct("installed-1");
    Product product2 = TestUtil.createProduct("installed-2");
    Set<ConsumerInstalledProduct> installedProducts = new HashSet<>();
    installedProducts.add(new ConsumerInstalledProduct(product1));
    installedProducts.add(new ConsumerInstalledProduct(product2));
    consumer.setInstalledProducts(installedProducts);
    return consumer;
}
Also used : Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ConsumerType(org.candlepin.model.ConsumerType) HashSet(java.util.HashSet)

Example 27 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ComplianceRulesTest method ensureConsumerComplianceStatusHashIsUpdatedWhenComplianceChanges.

@Test
public void ensureConsumerComplianceStatusHashIsUpdatedWhenComplianceChanges() {
    Consumer c = mockFullyEntitledConsumer();
    ComplianceStatus originalStatus = compliance.getStatus(c);
    assertEquals("valid", originalStatus.getStatus());
    String initialHash = c.getComplianceStatusHash();
    assertNotNull(initialHash);
    assertFalse(initialHash.isEmpty());
    verify(consumerCurator).update(eq(c), eq(false));
    String pid = "testinstalledprod";
    c.addInstalledProduct(new ConsumerInstalledProduct(pid, pid));
    ComplianceStatus updated = compliance.getStatus(c);
    assertNotEquals(originalStatus.getStatus(), updated.getStatus());
    String updatedHash = c.getComplianceStatusHash();
    assertNotNull(updatedHash);
    assertFalse(updatedHash.isEmpty());
    assertNotEquals(initialHash, updatedHash);
}
Also used : Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Test(org.junit.Test)

Example 28 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ComplianceRulesTest method ensureConsumerEntitlementStatusIsUpdatedWhenChanged.

@Test
public void ensureConsumerEntitlementStatusIsUpdatedWhenChanged() {
    Consumer c = mockFullyEntitledConsumer();
    ComplianceStatus originalStatus = compliance.getStatus(c);
    assertEquals("valid", originalStatus.getStatus());
    verify(consumerCurator).update(eq(c), eq(false));
    String pid = "testinstalledprod";
    c.addInstalledProduct(new ConsumerInstalledProduct(pid, pid));
    ComplianceStatus updated = compliance.getStatus(c);
    assertNotEquals(originalStatus.getStatus(), updated.getStatus());
    assertEquals(c.getEntitlementStatus(), updated.getStatus());
}
Also used : Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Test(org.junit.Test)

Example 29 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class ConsumerResource method populateEntity.

/**
 * Populates the specified entity with data from the provided DTO, during consumer creation (not update).
 * This method will not set the ID, entitlementStatus, complianceStatusHash, idCert, entitlements,
 * keyPair and canActivate, because clients are not allowed to create or update those properties.
 *
 * while autoheal is populated, it is overridden in create method.
 *
 * owner is not populated because create populates it differently.
 *
 * @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(Consumer entity, ConsumerDTO dto) {
    if (entity == null) {
        throw new IllegalArgumentException("the consumer model entity is null");
    }
    if (dto == null) {
        throw new IllegalArgumentException("the consumer dto is null");
    }
    if (dto.getCreated() != null) {
        entity.setCreated(dto.getCreated());
    }
    if (dto.getName() != null) {
        entity.setName(dto.getName());
    }
    if (dto.getUuid() != null) {
        entity.setUuid(dto.getUuid());
    }
    if (dto.getFacts() != null) {
        entity.setFacts(dto.getFacts());
    }
    if (dto.getUsername() != null) {
        entity.setUsername(dto.getUsername());
    }
    if (dto.getServiceLevel() != null) {
        entity.setServiceLevel(dto.getServiceLevel());
    }
    if (dto.getReleaseVersion() != null) {
        entity.setReleaseVer(new Release(dto.getReleaseVersion()));
    }
    if (dto.getEnvironment() != null) {
        Environment env = environmentCurator.find(dto.getEnvironment().getId());
        if (env == null) {
            throw new NotFoundException(i18n.tr("Environment \"{0}\" could not be found.", dto.getEnvironment().getId()));
        }
        entity.setEnvironment(env);
    }
    if (dto.getLastCheckin() != null) {
        entity.setLastCheckin(dto.getLastCheckin());
    }
    if (dto.getCapabilities() != null) {
        Set<ConsumerCapability> capabilities = populateCapabilities(entity, dto);
        entity.setCapabilities(capabilities);
    }
    if (dto.getGuestIds() != null) {
        List<GuestId> guestIds = new ArrayList<>();
        for (GuestIdDTO guestIdDTO : dto.getGuestIds()) {
            if (guestIdDTO != null) {
                guestIds.add(new GuestId(guestIdDTO.getGuestId(), entity, guestIdDTO.getAttributes()));
            }
        }
        entity.setGuestIds(guestIds);
    }
    if (dto.getHypervisorId() != null && entity.getOwnerId() != null) {
        HypervisorId hypervisorId = new HypervisorId(entity, ownerCurator.findOwnerById(entity.getOwnerId()), dto.getHypervisorId().getHypervisorId(), dto.getHypervisorId().getReporterId());
        entity.setHypervisorId(hypervisorId);
    }
    if (dto.getHypervisorId() == null && dto.getFact("system_uuid") != null && !"true".equals(dto.getFact("virt.is_guest")) && entity.getOwnerId() != null) {
        HypervisorId hypervisorId = new HypervisorId(entity, ownerCurator.findOwnerById(entity.getOwnerId()), dto.getFact("system_uuid"));
        entity.setHypervisorId(hypervisorId);
    }
    if (dto.getContentTags() != null) {
        entity.setContentTags(dto.getContentTags());
    }
    if (dto.getAutoheal() != null) {
        entity.setAutoheal(dto.getAutoheal());
    }
    if (dto.getContentAccessMode() != null) {
        entity.setContentAccessMode(dto.getContentAccessMode());
    }
    if (dto.getRecipientOwnerKey() != null) {
        entity.setRecipientOwnerKey(dto.getRecipientOwnerKey());
    }
    if (dto.getAnnotations() != null) {
        entity.setAnnotations(dto.getAnnotations());
    }
    if (dto.getInstalledProducts() != null) {
        Set<ConsumerInstalledProduct> installedProducts = populateInstalledProducts(entity, dto);
        entity.setInstalledProducts(installedProducts);
    }
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ArrayList(java.util.ArrayList) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ConsumerCapability(org.candlepin.model.ConsumerCapability) GuestId(org.candlepin.model.GuestId) Environment(org.candlepin.model.Environment) HypervisorId(org.candlepin.model.HypervisorId) Release(org.candlepin.model.Release)

Example 30 with ConsumerInstalledProduct

use of org.candlepin.model.ConsumerInstalledProduct in project candlepin by candlepin.

the class Entitler method getDevProductMap.

/**
 * Looks up all Products matching the specified SKU and the consumer's
 * installed products.
 *
 * @param consumer the consumer to pull the installed product id list from.
 * @param sku the product id of the SKU.
 * @return a {@link DeveloperProducts} object that contains the Product objects
 *         from the adapter.
 */
private DeveloperProducts getDevProductMap(Consumer consumer, Owner owner, String sku) {
    List<String> devProductIds = new ArrayList<>();
    devProductIds.add(sku);
    for (ConsumerInstalledProduct ip : consumer.getInstalledProducts()) {
        devProductIds.add(ip.getProductId());
    }
    Map<String, ProductData> productMap = new HashMap<>();
    Map<String, ContentData> contentMap = new HashMap<>();
    log.debug("Importing products for dev pool resolution...");
    for (ProductData product : this.productAdapter.getProductsByIds(owner, devProductIds)) {
        if (product == null) {
            continue;
        }
        if (sku.equals(product.getId()) && StringUtils.isEmpty(product.getAttributeValue(Product.Attributes.SUPPORT_LEVEL))) {
            // if there is no SLA, apply the default
            product.setAttribute(Product.Attributes.SUPPORT_LEVEL, this.DEFAULT_DEV_SLA);
        }
        // Product is coming from an upstream source; lock it so only upstream can make
        // further changes to it.
        product.setLocked(true);
        ProductData existingProduct = productMap.get(product.getId());
        if (existingProduct != null && !existingProduct.equals(product)) {
            log.warn("Multiple versions of the same product received during dev pool resolution; " + "discarding duplicate: {} => {}, {}", product.getId(), existingProduct, product);
        } else {
            productMap.put(product.getId(), product);
            Collection<ProductContentData> pcdCollection = product.getProductContent();
            if (pcdCollection != null) {
                for (ProductContentData pcd : pcdCollection) {
                    if (pcd == null) {
                        log.error("product contains a null product-content mapping: {}", product);
                        throw new IllegalStateException("product contains a null product-content mapping: " + product);
                    }
                    ContentData content = pcd.getContent();
                    // population validation for us.
                    if (content == null || content.getId() == null) {
                        log.error("product contains a null or incomplete product-content mapping: {}", product);
                        throw new IllegalStateException("product contains a null or incomplete " + "product-content mapping: " + product);
                    }
                    // We need to lock the incoming content here, but doing so will affect
                    // the equality comparison for products. We'll correct them later.
                    ContentData existingContent = contentMap.get(content.getId());
                    if (existingContent != null && !existingContent.equals(content)) {
                        log.warn("Multiple versions of the same content received during dev pool " + "resolution; discarding duplicate: {} => {}, {}", content.getId(), existingContent, content);
                    } else {
                        contentMap.put(content.getId(), content);
                    }
                }
            }
        }
    }
    log.debug("Importing {} content...", contentMap.size());
    for (ContentData cdata : contentMap.values()) {
        cdata.setLocked(true);
    }
    Map<String, Content> importedContent = this.contentManager.importContent(owner, contentMap, productMap.keySet()).getImportedEntities();
    log.debug("Importing {} product(s)...", productMap.size());
    Map<String, Product> importedProducts = this.productManager.importProducts(owner, productMap, importedContent).getImportedEntities();
    log.debug("Resolved {} dev product(s) for sku: {}", productMap.size(), sku);
    return new DeveloperProducts(sku, importedProducts);
}
Also used : ProductData(org.candlepin.model.dto.ProductData) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ContentData(org.candlepin.model.dto.ContentData) ProductContentData(org.candlepin.model.dto.ProductContentData) Content(org.candlepin.model.Content) ProductContentData(org.candlepin.model.dto.ProductContentData)

Aggregations

ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)66 Consumer (org.candlepin.model.Consumer)54 Product (org.candlepin.model.Product)51 Test (org.junit.Test)49 Owner (org.candlepin.model.Owner)44 Date (java.util.Date)35 DateRange (org.candlepin.policy.js.compliance.DateRange)26 HashSet (java.util.HashSet)14 ArrayList (java.util.ArrayList)13 Entitlement (org.candlepin.model.Entitlement)11 Pool (org.candlepin.model.Pool)11 ConsumerType (org.candlepin.model.ConsumerType)10 AutobindData (org.candlepin.resource.dto.AutobindData)10 HashMap (java.util.HashMap)8 ConsumerCapability (org.candlepin.model.ConsumerCapability)7 List (java.util.List)6 ProductData (org.candlepin.model.dto.ProductData)6 ConsumerInstalledProductDTO (org.candlepin.dto.api.v1.ConsumerInstalledProductDTO)4 Environment (org.candlepin.model.Environment)4 GuestId (org.candlepin.model.GuestId)4