Search in sources :

Example 1 with ProductContent

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

the class ProductManager method applyProductChanges.

/**
 * Applies the changes from the given DTO to the specified entity
 *
 * @param entity
 *  The entity to modify
 *
 * @param update
 *  The DTO containing the modifications to apply
 *
 * @param content
 *  A mapping of Red Hat content ID to content entities to use for content resolution
 *
 * @throws IllegalArgumentException
 *  if entity, update or owner is null
 *
 * @return
 *  The updated product entity
 */
private Product applyProductChanges(Product entity, ProductDTO update, Map<String, Content> contentMap) {
    if (entity == null) {
        throw new IllegalArgumentException("entity is null");
    }
    if (update == null) {
        throw new IllegalArgumentException("update is null");
    }
    if (contentMap == null) {
        throw new IllegalArgumentException("contentMap is null");
    }
    if (update.getName() != null) {
        entity.setName(update.getName());
    }
    if (update.getMultiplier() != null) {
        entity.setMultiplier(update.getMultiplier());
    }
    if (update.getAttributes() != null) {
        entity.setAttributes(update.getAttributes());
    }
    if (update.getProductContent() != null) {
        Collection<ProductContent> productContent = new LinkedList<>();
        // Sort the existing ProductContent so we aren't iterating on it several times.
        // TODO: Remove this if/when product content is stored as a map on products.
        Map<String, ProductContent> existingLinks = new HashMap<>();
        for (ProductContent pc : entity.getProductContent()) {
            existingLinks.put(pc.getContent().getId(), pc);
        }
        // Actually process our list of content...
        for (ProductContentDTO pcd : update.getProductContent()) {
            if (pcd == null) {
                throw new IllegalStateException("Product data contains a null product-content mapping: " + update);
            }
            ContentDTO cdto = pcd.getContent();
            if (cdto == null || cdto.getId() == null) {
                // adding it to our link object. This is very bad.
                throw new IllegalStateException("Product data contains an incomplete product-content " + "mapping: " + update);
            }
            ProductContent existingLink = existingLinks.get(cdto.getId());
            Content content = contentMap.get(cdto.getId());
            if (content == null) {
                // Content doesn't exist yet -- it should have been created already
                throw new IllegalStateException("product references content which does not exist: " + cdto);
            }
            if (existingLink == null) {
                existingLink = new ProductContent(entity, content, pcd.isEnabled() != null ? pcd.isEnabled() : false);
            } else {
                existingLink.setContent(content);
                if (pcd.isEnabled() != null) {
                    existingLink.setEnabled(pcd.isEnabled());
                }
            }
            productContent.add(existingLink);
        }
        entity.setProductContent(productContent);
    }
    if (update.getDependentProductIds() != null) {
        entity.setDependentProductIds(update.getDependentProductIds());
    }
    if (update.isLocked() != null) {
        entity.setLocked(update.isLocked());
    }
    return entity;
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ContentDTO(org.candlepin.dto.api.v1.ContentDTO) HashMap(java.util.HashMap) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductContent(org.candlepin.model.ProductContent) LinkedList(java.util.LinkedList)

Example 2 with ProductContent

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

the class DefaultEntitlementCertServiceAdapterTest method testPrepareV3EntitlementData.

@Test
public void testPrepareV3EntitlementData() throws IOException, GeneralSecurityException {
    Set<Product> products = new HashSet<>();
    products.add(product);
    consumer.setFact("system.certificate_version", "3.3");
    consumer.setFact("uname.machine", "x86_64");
    Product product = pool.getProduct();
    product.setAttribute(Product.Attributes.WARNING_PERIOD, "20");
    product.setAttribute(Product.Attributes.SOCKETS, "4");
    product.setAttribute(Product.Attributes.RAM, "8");
    product.setAttribute(Product.Attributes.CORES, "4");
    product.setAttribute(Product.Attributes.MANAGEMENT_ENABLED, "true");
    product.setAttribute(Product.Attributes.STACKING_ID, "45678");
    pool.setAttribute(Product.Attributes.VIRT_ONLY, "true");
    product.setAttribute(Product.Attributes.SUPPORT_LEVEL, "slevel");
    product.setAttribute(Product.Attributes.SUPPORT_TYPE, "stype");
    pool.setAccountNumber("account1");
    pool.setContractNumber("contract1");
    pool.setOrderNumber("order1");
    for (ProductContent pc : product.getProductContent()) {
        pc.setEnabled(false);
    }
    Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions();
    Map<String, X509ExtensionWrapper> map = new HashMap<>();
    for (X509ExtensionWrapper ext : extensions) {
        map.put(ext.getOid(), ext);
    }
    assertTrue(map.containsKey("1.3.6.1.4.1.2312.9.6"));
    assertEquals(map.get("1.3.6.1.4.1.2312.9.6").getValue(), ("3.3"));
    byte[] payload = v3extensionUtil.createEntitlementDataPayload(getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity());
    String stringValue = "";
    try {
        stringValue = processPayload(payload);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue, Map.class);
    assertEquals(data.get("consumer"), "test-consumer");
    assertEquals(data.get("quantity"), 10);
    Map<String, Object> subs = (Map<String, Object>) data.get("subscription");
    assertEquals(subs.get("sku"), subscription.getProduct().getId());
    assertEquals(subs.get("name"), subscription.getProduct().getName());
    assertEquals(subs.get("warning"), 20);
    assertEquals(subs.get("sockets"), 4);
    assertEquals(subs.get("ram"), 8);
    assertEquals(subs.get("cores"), 4);
    assertTrue((Boolean) subs.get("management"));
    assertEquals(subs.get("stacking_id"), "45678");
    assertTrue((Boolean) subs.get("virt_only"));
    Map<String, Object> service = (Map<String, Object>) subs.get("service");
    assertEquals(service.get("level"), "slevel");
    assertEquals(service.get("type"), "stype");
    Map<String, Object> order = (Map<String, Object>) data.get("order");
    assertEquals(order.get("number"), pool.getOrderNumber());
    assertTrue(((Integer) order.get("quantity")).intValue() == subscription.getQuantity());
    assertNotNull(order.get("start"));
    assertNotNull(order.get("end"));
    // assertEquals(order.get("contract"), subscription.getContractNumber());
    // assertEquals(order.get("account"), subscription.getAccountNumber());
    List<Map<String, Object>> prods = (List<Map<String, Object>>) data.get("products");
    List<Map<String, Object>> contents = null;
    for (Map<String, Object> prod : prods) {
        assertEquals(prod.get("id"), product.getId());
        assertEquals(prod.get("name"), product.getName());
        assertEquals(prod.get("version"), product.getAttributeValue(Product.Attributes.VERSION));
        String arch = product.hasAttribute(Product.Attributes.ARCHITECTURE) ? product.getAttributeValue(Product.Attributes.ARCHITECTURE) : "";
        StringTokenizer st = new StringTokenizer(arch, ",");
        while (st.hasMoreElements()) {
            assertTrue(((List) prod.get("architectures")).contains(st.nextElement()));
        }
        contents = (List<Map<String, Object>>) prod.get("content");
        for (Map<String, Object> cont : contents) {
            assertEquals(cont.get("id"), CONTENT_ID);
            assertEquals(cont.get("name"), CONTENT_NAME);
            assertEquals(cont.get("type"), CONTENT_TYPE);
            assertEquals(cont.get("label"), CONTENT_LABEL);
            assertEquals(cont.get("vendor"), CONTENT_VENDOR);
            assertEquals(cont.get("gpg_url"), CONTENT_GPG_URL);
            assertEquals(cont.get("path"), "prefix" + CONTENT_URL);
            assertFalse((Boolean) cont.get("enabled"));
            assertEquals(cont.get("metadata_expire"), 3200);
            List<String> arches = new ArrayList<>();
            arches.add(ARCH_LABEL);
            assertEquals(cont.get("arches"), arches);
            String rTags = content.getRequiredTags();
            st = new StringTokenizer(rTags, ",");
            while (st.hasMoreElements()) {
                assertTrue(((List) cont.get("required_tags")).contains(st.nextElement()));
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) Matchers.anyString(org.mockito.Matchers.anyString) ProductContent(org.candlepin.model.ProductContent) CertificateExpiredException(java.security.cert.CertificateExpiredException) GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExpectedException(org.junit.rules.ExpectedException) CertificateSizeException(org.candlepin.util.CertificateSizeException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) StringTokenizer(java.util.StringTokenizer) X509ExtensionWrapper(org.candlepin.pki.X509ExtensionWrapper) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ProductContent

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

the class DefaultEntitlementCertServiceAdapterTest method tooManyContentSets.

@Test(expected = CertificateSizeException.class)
public void tooManyContentSets() throws Exception {
    Set<Content> productContent = generateContent(X509ExtensionUtil.V1_CONTENT_LIMIT + 1, "TestContent");
    product.setProductContent(null);
    for (Content content : productContent) {
        product.addContent(content, false);
    }
    certServiceAdapter.createX509Certificate(consumer, owner, pool, entitlement, product, new HashSet<>(), getProductModels(product, new HashSet<>(), "prefix", entitlement), new BigInteger("1234"), keyPair, true);
}
Also used : ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent) BigInteger(java.math.BigInteger) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with ProductContent

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

the class DefaultEntitlementCertServiceAdapterTest method testPrepareV3EntitlementDataForDefaults.

@Test
public void testPrepareV3EntitlementDataForDefaults() throws IOException {
    Set<Product> products = new HashSet<>();
    products.add(product);
    consumer.setFact("system.certificate_version", "3.3");
    consumer.setFact("uname.machine", "x86_64");
    subscription.getProduct().setAttribute(Product.Attributes.WARNING_PERIOD, "0");
    subscription.getProduct().setAttribute(Product.Attributes.MANAGEMENT_ENABLED, "false");
    entitlement.getPool().setAttribute(Product.Attributes.VIRT_ONLY, "false");
    for (ProductContent pc : product.getProductContent()) {
        pc.setEnabled(true);
    }
    Set<X509ExtensionWrapper> extensions = certServiceAdapter.prepareV3Extensions();
    Map<String, X509ExtensionWrapper> map = new HashMap<>();
    for (X509ExtensionWrapper ext : extensions) {
        map.put(ext.getOid(), ext);
    }
    assertTrue(map.containsKey("1.3.6.1.4.1.2312.9.6"));
    assertEquals(map.get("1.3.6.1.4.1.2312.9.6").getValue(), ("3.3"));
    byte[] payload = v3extensionUtil.createEntitlementDataPayload(getProductModels(product, products, "prefix", entitlement), consumer, pool, entitlement.getQuantity());
    String stringValue = "";
    try {
        stringValue = processPayload(payload);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Map<String, Object> data = (Map<String, Object>) Util.fromJson(stringValue, Map.class);
    assertEquals(data.get("consumer"), "test-consumer");
    // each has been set to the default and should not be populated in the cert
    Map<String, Object> subs = (Map<String, Object>) data.get("subscription");
    assertNull(subs.get("warning"));
    assertNull(subs.get("management"));
    assertNull(subs.get("virt_only"));
    List<Map<String, Object>> prods = (List<Map<String, Object>>) data.get("products");
    for (Map<String, Object> prod : prods) {
        List<Map<String, Object>> contents = (List<Map<String, Object>>) prod.get("content");
        for (Map<String, Object> cont : contents) {
            assertNull(cont.get("enabled"));
        }
    }
}
Also used : HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Matchers.anyString(org.mockito.Matchers.anyString) ProductContent(org.candlepin.model.ProductContent) CertificateExpiredException(java.security.cert.CertificateExpiredException) GeneralSecurityException(java.security.GeneralSecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ExpectedException(org.junit.rules.ExpectedException) CertificateSizeException(org.candlepin.util.CertificateSizeException) IOException(java.io.IOException) X509ExtensionWrapper(org.candlepin.pki.X509ExtensionWrapper) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with ProductContent

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

the class HostedTestSubscriptionResource method addBatchContent.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/owners/{owner_key}/products/{product_id}/batch_content")
@Transactional
public ProductDTO addBatchContent(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId, Map<String, Boolean> contentMap) {
    Owner owner = this.getOwnerByKey(ownerKey);
    Product product = this.fetchProduct(owner, productId);
    Collection<ProductContent> productContent = new LinkedList<>();
    ProductDTO pdto = this.translator.translate(product, ProductDTO.class);
    // Impl note:
    // This is a wholely inefficient way of doing this. When we return to using ID-based linking
    // and we're not linking the universe with our model, we can just attach the IDs directly
    // without needing all this DTO conversion back and forth.
    // Alternatively, we can shut off Hibernate's auto-commit junk and get in the habit of
    // calling commit methods as necessary so we don't have to work with DTOs internally.
    boolean changed = false;
    for (Entry<String, Boolean> entry : contentMap.entrySet()) {
        Content content = this.fetchContent(owner, entry.getKey());
        boolean enabled = entry.getValue() != null ? entry.getValue() : ProductContent.DEFAULT_ENABLED_STATE;
        ContentDTO cdto = this.translator.translate(content, ContentDTO.class);
        changed |= pdto.addContent(cdto, enabled);
        addContentToUpstreamSubscriptions(product, content, enabled);
    }
    if (changed) {
        product = this.productManager.updateProduct(pdto, owner, true);
    }
    return this.translator.translate(product, ProductDTO.class);
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductContent(org.candlepin.model.ProductContent) LinkedList(java.util.LinkedList) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Transactional(com.google.inject.persist.Transactional)

Aggregations

ProductContent (org.candlepin.model.ProductContent)30 Content (org.candlepin.model.Content)20 Test (org.junit.Test)11 Product (org.candlepin.model.Product)9 HashSet (java.util.HashSet)7 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)4 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)4 Owner (org.candlepin.model.Owner)4 X509ExtensionWrapper (org.candlepin.pki.X509ExtensionWrapper)4 Transactional (com.google.inject.persist.Transactional)3 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 ProductDTO (org.candlepin.dto.api.v1.ProductDTO)3 ContentData (org.candlepin.model.dto.ContentData)3 ProductContentData (org.candlepin.model.dto.ProductContentData)3 CertificateSizeException (org.candlepin.util.CertificateSizeException)3 IOException (java.io.IOException)2