diff options
author | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-07-29 16:40:55 -0700 |
---|---|---|
committer | Mike McCune <mmccune@gibson.pdx.redhat.com> | 2009-07-29 16:40:55 -0700 |
commit | e53b58df385618405f055a11cc5019253a587490 (patch) | |
tree | df056607781043481aeab09a8e70d7e72c5b2ce6 | |
parent | be6a8faf6b68a242d09d2043625858ba308930d3 (diff) | |
download | candlepin-e53b58df385618405f055a11cc5019253a587490.tar.gz candlepin-e53b58df385618405f055a11cc5019253a587490.tar.xz candlepin-e53b58df385618405f055a11cc5019253a587490.zip |
adding first steps towards entitlement 'matching'
8 files changed, 200 insertions, 32 deletions
diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/BaseApi.java b/proxy/code/src/org/fedoraproject/candlepin/api/BaseApi.java index 7b5411f..4edebcb 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/BaseApi.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/BaseApi.java @@ -21,9 +21,7 @@ import org.fedoraproject.candlepin.model.BaseModel; import org.fedoraproject.candlepin.model.ObjectFactory; import org.fedoraproject.candlepin.util.MethodUtil; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementApi.java b/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementApi.java index c8cef2b..d00712e 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementApi.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementApi.java @@ -56,26 +56,24 @@ public class EntitlementApi extends BaseApi { return Entitlement.class; } + private Object validateObjectInput(Form form, String fieldName, Class clazz) { + String uuid = form.getFirst(fieldName); + log.debug("UUID: " + uuid); + Object o = ObjectFactory.get().lookupByUUID(clazz, uuid); + if (o == null) { + throw new RuntimeException(clazz.getName() + " with UUID: [" + + uuid + "] not found"); + } + return o; + } @POST @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED}) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Path("/entitle") public Object entitle(Form form) { - String consumerUuid = form.getFirst("consumer_uuid"); - String productUuid = form.getFirst("product_uuid"); - log.debug("UUID: " + consumerUuid); - Consumer c = (Consumer) ObjectFactory.get().lookupByUUID(Consumer.class, - consumerUuid); - if (c == null) { - throw new RuntimeException("Consumer with UUID: [" + - consumerUuid + "] not found"); - } - Product p = (Product) ObjectFactory.get().lookupByUUID(Product.class, productUuid); - if (p == null) { - throw new RuntimeException("Product with UUID: [" + - productUuid + "] not found"); - } + Consumer c = (Consumer) validateObjectInput(form, "consumer_uuid", Consumer.class); + Product p = (Product) validateObjectInput(form, "product_uuid", Product.class); // Possibly refactor this down into some 'business layer' // Check for a matching EntitlementPool @@ -112,6 +110,55 @@ public class EntitlementApi extends BaseApi { return null; } + /** + * Check to see if a given Consumer is entitled to given Product + * @param form with consumer_uuid and product_uuid to check if entitled or not + * @return boolean if entitled or not + */ + @GET + @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) + public boolean hasEntitlement(Form form) { + Consumer c = (Consumer) validateObjectInput(form, "consumer_uuid", Consumer.class); + Product p = (Product) validateObjectInput(form, "product_uuid", Product.class); + for (Entitlement e : c.getEntitlements()) { + if (e.getProduct().equals(p)) { + return true; + } + } + return false; + } + + /** + * Match/List the available entitlements for a given Consumer. Right now this returns + * ALL entitlements because we haven't built any filtering logic. + * @param form containing consumer_uuid + * @return List<Entitlement> of applicable + */ + public List<EntitlementPool> listAvailableEntitlements(Form form) { + Consumer c = (Consumer) validateObjectInput(form, "consumer_uuid", Consumer.class); + List<EntitlementPool> entitlementPools = new EntitlementPoolApi().list(); + List<EntitlementPool> retval = new ArrayList<EntitlementPool>(); + EntitlementMatcher matcher = new EntitlementMatcher(); + for (EntitlementPool ep : entitlementPools) { + boolean add = false; + if (ep.getMaxMembers() > ep.getCurrentMembers()) { + add = true; + } + if (matcher.isCompatible(c, ep.getProduct())) { + add = true; + } + if (add) { + retval.add(ep); + } + } + return retval; + } + + + // TODO: + // EntitlementLib.UnentitleProduct(Consumer, Entitlement) + + @GET @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) public List<Entitlement> list() { diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementMatcher.java b/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementMatcher.java new file mode 100644 index 0000000..60609dd --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/api/EntitlementMatcher.java @@ -0,0 +1,39 @@ +/** + * 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.api; + +import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.Product; + + +/** + * EntitlementMatcher - initial class for matching products compatability with + * an entitlement. + * @version $Rev$ + */ +public class EntitlementMatcher { + + + /** + * Check if a given consumer is compat with given product. + * @param consumer to check + * @param product to check + * @return boolean if compat or not + */ + public boolean isCompatible(Consumer c, Product p) { + return true; + } + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/cert/CertGenerator.java b/proxy/code/src/org/fedoraproject/candlepin/api/cert/CertGenerator.java index 9c202f5..118f588 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/cert/CertGenerator.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/cert/CertGenerator.java @@ -109,8 +109,8 @@ public class CertGenerator { // the CA for the attribute certificate should be different to that of // the client certificate // - X509Certificate clientCert = createClientCert(pubKey, - caPrivKey, caPubKey); + X509Certificate clientCert = createClientCert("Tito Walker", + "dev-null@fedoraproject.org", pubKey, caPrivKey, caPubKey); retval = clientCert; } catch (Exception e) { @@ -123,8 +123,8 @@ public class CertGenerator { /** * we generate a certificate signed by our CA's intermediate certficate */ - public static X509Certificate createClientCert(PublicKey pubKey, - PrivateKey caPrivKey, PublicKey caPubKey) throws Exception { + public static X509Certificate createClientCert(String name, String email, + PublicKey pubKey, PrivateKey caPrivKey, PublicKey caPubKey) throws Exception { // // issuer // @@ -139,9 +139,9 @@ public class CertGenerator { attrs.put(X509Principal.C, "US"); attrs.put(X509Principal.O, "The Players of Candlepin"); attrs.put(X509Principal.L, "Raleigh"); - attrs.put(X509Principal.CN, "Tito Walker"); + attrs.put(X509Principal.CN, name); attrs.put(X509Principal.EmailAddress, - "dev-null@fedoraproject.org"); + email); order.addElement(X509Principal.C); order.addElement(X509Principal.O); diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/cert/test/CertTest.java b/proxy/code/src/org/fedoraproject/candlepin/api/cert/test/CertTest.java index 62beae1..da9731e 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/cert/test/CertTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/cert/test/CertTest.java @@ -50,7 +50,7 @@ public class CertTest extends TestCase { System.out.println("Cert: " + cert); } - public void zzzzCertExample() throws Exception { + public void testCertExample() throws Exception { Security.addProvider(new BouncyCastleProvider()); diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementApiTest.java b/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementApiTest.java index e91d8ca..f04dae4 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementApiTest.java +++ b/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementApiTest.java @@ -24,6 +24,7 @@ import org.fedoraproject.candlepin.model.Product; import org.fedoraproject.candlepin.model.test.TestUtil; import java.sql.Date; +import java.util.List; import junit.framework.TestCase; @@ -33,23 +34,34 @@ import junit.framework.TestCase; * @version $Rev$ */ public class EntitlementApiTest extends TestCase { - - public void testEntitle() throws Exception { - - Consumer c = TestUtil.createConsumer(); - Product p = TestUtil.createProduct(); - EntitlementPool ep = new EntitlementPool(); + + private Consumer c; + private Product p; + private EntitlementPool ep; + + /** + * {@inheritDoc} + */ + @Override + protected void setUp() throws Exception { + // TODO Auto-generated method stub + super.setUp(); + c = TestUtil.createConsumer(); + p = TestUtil.createProduct(); + ep = new EntitlementPool(); ep.setProduct(p); ep.setOwner(c.getOwner()); ep.setMaxMembers(10); ep.setCurrentMembers(0); Date futuredate = new Date(System.currentTimeMillis() + 1000000000); - Date pastdate = new Date(System.currentTimeMillis() - 1000000000); - System.out.println("future: " + futuredate); - System.out.println("past: " + pastdate); ep.setEndDate(futuredate); ObjectFactory.get().store(ep); + + } + + public void testEntitle() throws Exception { + EntitlementApi eapi = new EntitlementApi(); Form f = new Form(); @@ -77,6 +89,7 @@ public class EntitlementApiTest extends TestCase { assertTrue("we didnt hit max members", failed); // Test expiration + Date pastdate = new Date(System.currentTimeMillis() - 1000000000); ep.setEndDate(pastdate); failed = false; try { @@ -90,4 +103,28 @@ public class EntitlementApiTest extends TestCase { } + + public void testHasEntitlement() { + System.out.println("Foo"); + + EntitlementApi eapi = new EntitlementApi(); + Form f = new Form(); + f.add("consumer_uuid", c.getUuid()); + f.add("product_uuid", p.getUuid()); + eapi.entitle(f); + + assertTrue(eapi.hasEntitlement(f)); + } + + public void testListAvailableEntitlements() { + EntitlementApi eapi = new EntitlementApi(); + Form f = new Form(); + f.add("consumer_uuid", c.getUuid()); + f.add("product_uuid", p.getUuid()); + + List avail = eapi.listAvailableEntitlements(f); + assertNotNull(avail); + assertTrue(avail.size() > 0); + + } } diff --git a/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementMatcherTest.java b/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementMatcherTest.java new file mode 100644 index 0000000..f332e8a --- /dev/null +++ b/proxy/code/src/org/fedoraproject/candlepin/api/test/EntitlementMatcherTest.java @@ -0,0 +1,38 @@ +/** + * 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.api.test; + +import org.fedoraproject.candlepin.api.EntitlementMatcher; +import org.fedoraproject.candlepin.model.Consumer; +import org.fedoraproject.candlepin.model.Product; +import org.fedoraproject.candlepin.model.test.TestUtil; + +import junit.framework.TestCase; + +/** + * EntitlementMatcherTest + * @version $Rev$ + */ +public class EntitlementMatcherTest extends TestCase { + + public void testIsCompatable() throws Exception { + Consumer c = TestUtil.createConsumer(); + Product p = TestUtil.createProduct(); + + EntitlementMatcher m = new EntitlementMatcher(); + assertTrue(m.isCompatible(c, p)); + } + +} diff --git a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java index be39196..bf8e444 100644 --- a/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java +++ b/proxy/code/src/org/fedoraproject/candlepin/model/ConsumerInfo.java @@ -26,6 +26,15 @@ public class ConsumerInfo { private String 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"; + + /** * @return Returns the parent. */ |