diff options
author | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-08-10 17:19:34 -0700 |
---|---|---|
committer | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-08-10 17:19:34 -0700 |
commit | a0ab6f9fdfc2be3184ef8eaa10e2b07782d7def6 (patch) | |
tree | a0193167716911f9143f33e62ffc86d726d01a4e | |
parent | 6c56db5112ac5d844096bf0f232398f76cda4695 (diff) | |
download | candlepin-a0ab6f9fdfc2be3184ef8eaa10e2b07782d7def6.tar.gz candlepin-a0ab6f9fdfc2be3184ef8eaa10e2b07782d7def6.tar.xz candlepin-a0ab6f9fdfc2be3184ef8eaa10e2b07782d7def6.zip |
adding ConsumerType object and more better 'matching' algorithm
10 files changed, 326 insertions, 31 deletions
diff --git a/proxy/code/src/log4j.properties b/proxy/code/src/log4j.properties index 590cc89..fa3d128 100644 --- a/proxy/code/src/log4j.properties +++ b/proxy/code/src/log4j.properties @@ -4,8 +4,10 @@ log4j.appender.RootAppender.layout=org.apache.log4j.PatternLayout log4j.appender.RootAppender.layout.ConversionPattern=%m%n log4j.rootLogger=DEBUG,RootAppender +log4j.logger.org.fedoraproject.candlepin=INFO +log4j.logger.org.fedoraproject.candlepin.util.filters.DumpFilter=DEBUG + # General debug for everything. Very noisy #log4j.logger.org.fedoraproject.candlepin=DEBUG -log4j.logger.org.fedoraproject.candlepin=INFO -log4j.logger.org.fedoraproject.candlepin.util.filters.DumpFilter=DEBUG + diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementMatcher.java b/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementMatcher.java index 60609dd..ed949c6 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementMatcher.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementMatcher.java @@ -14,9 +14,15 @@ */ package org.fedoraproject.candlepin.api; +import org.apache.log4j.Logger; + import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.ObjectFactory; import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.model.ProductFactory; +import java.util.List; /** * EntitlementMatcher - initial class for matching products compatability with @@ -24,6 +30,12 @@ import org.fedoraproject.candlepin.model.Product; * @version $Rev$ */ public class EntitlementMatcher { + + /** + * Logger for this class + */ + private static final Logger log = Logger + .getLogger(EntitlementMatcher.class); /** @@ -33,7 +45,14 @@ public class EntitlementMatcher { * @return boolean if compat or not */ public boolean isCompatible(Consumer c, Product p) { - return true; + log.debug("isCompatible() : Consuner: " + c + " product: " + p); + if (c.getType() == null) { + throw new NullPointerException("Consumer has no type."); + } + List f = ObjectFactory.get().listObjectsByClass(ConsumerType.class); + List<ConsumerType> types = ProductFactory.get().getCompatibleConsumerTypes(p); + + return (types != null && types.contains(c.getType())); } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementMatcherTest.java b/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementMatcherTest.java index f332e8a..9f25757 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementMatcherTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementMatcherTest.java @@ -16,9 +16,14 @@ package org.fedoraproject.candlepin.api.test; import org.fedoraproject.candlepin.api.EntitlementMatcher; import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.ConsumerType; +import org.fedoraproject.candlepin.model.ObjectFactory; import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.model.ProductFactory; import org.fedoraproject.candlepin.model.test.TestUtil; +import java.util.List; + import junit.framework.TestCase; /** @@ -28,11 +33,25 @@ import junit.framework.TestCase; public class EntitlementMatcherTest extends TestCase { public void testIsCompatable() throws Exception { - Consumer c = TestUtil.createConsumer(); - Product p = TestUtil.createProduct(); + Consumer consumer = TestUtil.createConsumer(); + ConsumerType typeSystem = ProductFactory.get().lookupConsumerTypeByLabel("system"); + consumer.setType(typeSystem); + List f = ObjectFactory.get().listObjectsByClass(Product.class); + Product rhel = (Product) ObjectFactory.get().lookupByFieldName( + Product.class, "label", "rhel"); + Product rhelvirt = (Product) ObjectFactory.get().lookupByFieldName( + Product.class, "label", "rhel-virt"); + EntitlementMatcher m = new EntitlementMatcher(); - assertTrue(m.isCompatible(c, p)); + + assertTrue(m.isCompatible(consumer, rhel)); + + ConsumerType vmwarehost = + ProductFactory.get().lookupConsumerTypeByLabel("vmwarehost"); + consumer.setType(vmwarehost); + + // Check that you can't use rhelvirt on a vmware host + assertFalse(m.isCompatible(consumer, rhelvirt)); } - } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/BaseModel.java b/proxy/code/src/org/fedoraproject/candlepin/model/BaseModel.java index c474958..761796a 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/BaseModel.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/BaseModel.java @@ -69,4 +69,31 @@ public class BaseModel { public static String generateUUID() { return UUID.randomUUID().toString(); } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((uuid == null) ? 0 : uuid.hashCode()); + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (getClass() != obj.getClass()) return false; + BaseModel other = (BaseModel) obj; + if (this.getUuid() == null) { + if (other.getUuid() != null) return false; + } + else if (!this.getUuid().equals(other.getUuid())) return false; + return true; + } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java index c690f85..1da0e91 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Consumer.java @@ -55,7 +55,7 @@ public class Consumer extends BaseModel { /** * @return the type */ - public String getType() { + public ConsumerType getType() { if (this.info == null) { return null; } @@ -68,7 +68,7 @@ public class Consumer extends BaseModel { * Set the type of this Consumer. * @param typeIn to set */ - public void setType(String typeIn) { + public void setType(ConsumerType typeIn) { if (this.info == null) { this.info = new ConsumerInfo(); } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java index bf8e444..7b2ea37 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java @@ -23,16 +23,9 @@ import javax.xml.bind.annotation.XmlTransient; public class ConsumerInfo { private Consumer parent; - private String type; + private ConsumerType type; private Map<String, String> metadata; - public static final String TYPE_XENVM = "xenvm"; - public static final String TYPE_QEMUVM = "qemuvm"; - public static final String TYPE_VMWAREVM = "vmwarevm"; - public static final String TYPE_XENHOST = "xenhost"; - public static final String TYPE_VMWAREHOST = "vmwarehost"; - public static final String TYPE_SYSTEM = "system"; - public static final String TYPE_BLADE_SYSTEM = "bladesystem"; /** @@ -53,14 +46,14 @@ public class ConsumerInfo { /** * @return Returns the type. */ - public String getType() { + public ConsumerType getType() { return type; } /** * @param typeIn The type to set. */ - public void setType(String typeIn) { + public void setType(ConsumerType typeIn) { type = typeIn; } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerType.java b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerType.java new file mode 100644 index 0000000..ff6fc6b --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerType.java @@ -0,0 +1,59 @@ +/** + * Copyright (c) 2009 Red Hat, Inc. + * + * This software is licensed to you under the GNU General Public License, + * version 2 (GPLv2). There is NO WARRANTY for this software, express or + * implied, including the implied warranties of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 + * along with this software; if not, see + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. + * + * Red Hat trademarks are not licensed under GPLv2. No permission is + * granted to use or replicate Red Hat trademarks that are incorporated + * in this software or its documentation. + */ +package org.fedoraproject.candlepin.model; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.PROPERTY) +public class ConsumerType extends BaseModel { + + private String label; + + /** + * ConsumerType constructor with label + * @param labelIn to set + */ + public ConsumerType(String labelIn) { + super(BaseModel.generateUUID()); + this.label = labelIn; + } + + /** + * @return Returns the label. + */ + public String getLabel() { + return label; + } + + /** + * @param labelIn The label to set. + */ + public void setLabel(String labelIn) { + label = labelIn; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return "ConsumerType [label=" + label + "]"; + } + + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java index 5cfdd6d..0bf6569 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ObjectFactory.java @@ -34,13 +34,13 @@ public class ObjectFactory { /** the singleton instance of the ObjectFactory */ private static ObjectFactory instance = new ObjectFactory(); - private Map objects; + private static Map objects; /** * default constructor */ - private ObjectFactory() { + protected ObjectFactory() { objects = new HashMap(); initMockObjects(); } @@ -51,9 +51,6 @@ public class ObjectFactory { private void initMockObjects() { Owner org = new Owner(BaseModel.generateUUID()); org.setName("test-org"); - // Product - Product rhel = new Product(BaseModel.generateUUID()); - rhel.setName("Red Hat Enterprise Linux"); // User User u = new User(); @@ -66,17 +63,10 @@ public class ObjectFactory { c.setName("fake-consumer-i386"); c.setOwner(org); org.addConsumer(c); - c.addConsumedProduct(rhel); - // EntitlementPool - EntitlementPool pool = new EntitlementPool(BaseModel.generateUUID()); - org.addEntitlementPool(pool); - pool.setProduct(rhel); - this.store(org); this.store(u); this.store(c); - this.store(pool); } /** diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/Product.java b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java index 5186592..caad6ee 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/Product.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/Product.java @@ -25,6 +25,7 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlAccessorType(XmlAccessType.PROPERTY) public class Product extends BaseModel { + private String label; private List<Product> childProducts; /** @@ -65,4 +66,37 @@ public class Product extends BaseModel { } this.childProducts.add(p); } + + /** + * Get the list of + * @return + */ + public List<String> getCompatibleConsumerTypes() { + + return null; + } + + + /** + * @return Returns the label. + */ + public String getLabel() { + return label; + } + + + /** + * @param labelIn The label to set. + */ + public void setLabel(String labelIn) { + label = labelIn; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + return "Product [label=" + label + "]"; + } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ProductFactory.java b/proxy/code/src/org/fedoraproject/candlepin/model/ProductFactory.java new file mode 100644 index 0000000..1363039 --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ProductFactory.java @@ -0,0 +1,152 @@ +/** + * Copyright (c) 2008 Red Hat, Inc. + * + * This software is licensed to you under the GNU General Public License, + * version 2 (GPLv2). There is NO WARRANTY for this software, express or + * implied, including the implied warranties of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 + * along with this software; if not, see + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. + * + * Red Hat trademarks are not licensed under GPLv2. No permission is + * granted to use or replicate Red Hat trademarks that are incorporated + * in this software or its documentation. + */ +package org.fedoraproject.candlepin.model; + +import org.apache.log4j.Logger; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + + +/** + * ProductFactory + * @version $Rev$ + */ +public class ProductFactory extends ObjectFactory { + + /** + * Logger for this class + */ + private static final Logger log = Logger.getLogger(ProductFactory.class); + + private static ProductFactory instance = new ProductFactory(); + + private Map<Product, List<ConsumerType>> prodConsumerMap; + + + protected ProductFactory() { + super(); + prodConsumerMap = new HashMap(); + + // TODO: Move this all into the DB for definition + // Create some Products + Product rhel = new Product(BaseModel.generateUUID()); + rhel.setName("Red Hat Enterprise Linux"); + rhel.setLabel("rhel"); + this.store(rhel); + + Product jboss = new Product(BaseModel.generateUUID()); + jboss.setName("JBoss Application Server"); + jboss.setLabel("jboss-as"); + this.store(jboss); + + Product virt = new Product(BaseModel.generateUUID()); + virt.setName("RHEL Virtualization"); + virt.setLabel("rhel-virt"); + rhel.addChildProduct(virt); + this.store(virt); + + Product cluster = new Product(BaseModel.generateUUID()); + cluster.setName("RHEL Cluster-Storage"); + cluster.setLabel("rhel-cluster"); + rhel.addChildProduct(cluster); + this.store(cluster); + + Product jbossdev = new Product(BaseModel.generateUUID()); + jbossdev.setName("JBoss Developer Studio (v1) for Linux"); + this.store(jbossdev); + + List f = ObjectFactory.get().listObjectsByClass(Product.class); + + ConsumerType xenvm = new ConsumerType("xenvm"); + ConsumerType qemuvm = new ConsumerType("qemuvm"); + ConsumerType vmwarevm = new ConsumerType("vmwarevm"); + ConsumerType xenhost = new ConsumerType("xenhost"); + ConsumerType vmwarehost = new ConsumerType("vmwarehost"); + ConsumerType system = new ConsumerType("system"); + ConsumerType bladesystem = new ConsumerType("bladesystem"); + // ConsumerType javavm = new ConsumerType("javavm"); + this.store(xenvm); + this.store(qemuvm); + this.store(vmwarevm); + this.store(xenhost); + this.store(vmwarehost); + this.store(system); + this.store(bladesystem); + + List alltypes = new LinkedList(); + alltypes.add(xenvm); + alltypes.add(qemuvm); + alltypes.add(vmwarevm); + alltypes.add(xenhost); + alltypes.add(vmwarehost); + alltypes.add(system); + alltypes.add(bladesystem); + // alltypes.add(javavm); + + List virttypes = new LinkedList(); + virttypes.add(xenhost); + virttypes.add(system); + virttypes.add(bladesystem); + + // List javatypes = new LinkedList(); + //javatypes.add(javavm); + + + prodConsumerMap.put(rhel, alltypes); + prodConsumerMap.put(jboss, alltypes); + prodConsumerMap.put(virt, virttypes); + + // EntitlementPool + Owner owner = (Owner) listObjectsByClass(Owner.class).get(0); + EntitlementPool pool = new EntitlementPool(BaseModel.generateUUID()); + owner.addEntitlementPool(pool); + pool.setProduct(rhel); + store(pool); + + log.debug("ProductFactory constructor done."); + + } + + /** + * Returns the instance of the ObjectFactory. + * @return the instance of the ObjectFactory. + */ + public static ProductFactory get() { + return instance; + } + + /** + * Lookup a ConsumerTYpe by name + * @param labelIn to lookup by + * @return ConsumerType found + */ + public ConsumerType lookupConsumerTypeByLabel(String labelIn) { + return (ConsumerType) lookupByFieldName(ConsumerType.class, "label", labelIn); + } + + /** + * Get the list of ConsumerTypes that are compatible with a given Product. + * + * @param productIn to check + * @return List of ConsumerType objects that are compatible + */ + public List<ConsumerType> getCompatibleConsumerTypes(Product productIn) { + log.debug("getCompatibleConsumerTypes: " + productIn); + return prodConsumerMap.get(productIn); + } +} |