java/code/src/com/redhat/rhn/common/db/datasource/xml/Channel_queries.xml |
5
java/code/src/com/redhat/rhn/common/db/datasource/xml/Procedure_queries.xml |
36 ++
java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.hbm.xml |
22 +
java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.java |
3
java/code/src/com/redhat/rhn/domain/channel/ChannelFamilyFactory.java |
12
java/code/src/com/redhat/rhn/domain/channel/PrivateChannelFamily.hbm.xml |
17 +
java/code/src/com/redhat/rhn/domain/org/OrgImpl.java |
26 -
java/code/src/com/redhat/rhn/domain/server/ServerFactory.java |
37 ++
java/code/src/com/redhat/rhn/domain/server/ServerGroup.hbm.xml |
16
java/code/src/com/redhat/rhn/domain/server/ServerGroupFactory.java |
13
java/code/src/com/redhat/rhn/domain/server/ServerGroupType.java |
10
java/code/src/com/redhat/rhn/domain/server/test/HostBuilder.java |
13
java/code/src/com/redhat/rhn/domain/server/test/ServerFactoryTest.java |
43 +-
java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java |
18 +
java/code/src/com/redhat/rhn/manager/channel/ChannelProcedure.java |
12
java/code/src/com/redhat/rhn/manager/entitlement/EntitlementProcedure.java |
120 +++++++
java/code/src/com/redhat/rhn/manager/entitlement/test/EntitlementProcedureTest.java |
169 ++++++++++
java/code/src/com/redhat/rhn/testing/UserTestUtils.java |
14
18 files changed, 547 insertions(+), 39 deletions(-)
New commits:
commit e98f75f74750f61d0a3cd91f2b52c23123f4df7d
Author: Justin Sherrill <jsherril(a)redhat.com>
Date: Wed Jun 2 18:20:27 2010 -0400
porting repollGuestVirtEntitlements to java
diff --git a/java/code/src/com/redhat/rhn/common/db/datasource/xml/Channel_queries.xml
b/java/code/src/com/redhat/rhn/common/db/datasource/xml/Channel_queries.xml
index c482685..7dc328b 100644
--- a/java/code/src/com/redhat/rhn/common/db/datasource/xml/Channel_queries.xml
+++ b/java/code/src/com/redhat/rhn/common/db/datasource/xml/Channel_queries.xml
@@ -339,6 +339,11 @@ ORDER BY UPPER(C.name)
</callable-mode>
+<callable-mode name="update_family_counts">
+ <query params="fid, org_id">
+ {:result = call rhn_channel.update_family_counts(:fid, :org_id)}
+ </query>
+</callable-mode>
<mode name="channel_entitlements"
class="com.redhat.rhn.frontend.dto.ChannelOverview">
<query params="org_id">
diff --git a/java/code/src/com/redhat/rhn/common/db/datasource/xml/Procedure_queries.xml
b/java/code/src/com/redhat/rhn/common/db/datasource/xml/Procedure_queries.xml
index d4444ab..881efcc 100644
--- a/java/code/src/com/redhat/rhn/common/db/datasource/xml/Procedure_queries.xml
+++ b/java/code/src/com/redhat/rhn/common/db/datasource/xml/Procedure_queries.xml
@@ -118,6 +118,42 @@
</write-mode>
+<write-mode name="update_group_current_members">
+ <query params="oid">
+ update rhnServerGroup set current_members = :current_members
+ where id = :sgid
+ </query>
+</write-mode>
+
+
+<mode name="compute_physical_server_group_count">
+ <query params="sgid">
+ select count(sep.server_id) as count
+ from rhnServerEntitlementPhysical sep
+ where sep.server_group_id = :sgid
+ </query>
+</mode>
+
+
+<callable-mode name="remove_server_entitlement">
+ <query params="sid, group_type">
+ {call rhn_entitlements.remove_server_entitlement(:sid, :group_type)}
+ </query>
+</callable-mode>
+
+<!-- <mode name="remove_server_entitlement">
+ <query params="sid, group_type">
+ call rhn_entitlements.remove_server_entitlement(:sid, :group_type)
+ </query>
+</mode> -->
+
+
+<callable-mode name="unsubscribe_server_from_family">
+ <query params="sid, fam_id">
+ {call rhn_channel.unsubscribe_server_from_family(:sid, :fam_id)}
+ </query>
+</callable-mode>
+
diff --git a/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.hbm.xml
b/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.hbm.xml
index 8982b45..647825a 100644
--- a/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.hbm.xml
+++ b/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.hbm.xml
@@ -62,4 +62,26 @@ PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
CHANNEL_FAMILY_ID = :id
]]>
</sql-query>
+
+
+ <sql-query name="ChannelFamily.listGuestChannelFamilies">
+ <![CDATA[
+ select distinct {pcf.*}
+ from
+ rhnChannelFamilyServers cfs,
+ rhnVirtualInstance vi,
+ rhnPrivateChannelFamily pcf
+ where
+ vi.host_system_id = :host_id
+ and vi.virtual_system_id = cfs.server_id
+ and pcf.channel_family_id = cfs.channel_family_id
+ and pcf.org_id = cfs.customer_id
+ ]]>
+ <return alias="pcf"
class="com.redhat.rhn.domain.channel.PrivateChannelFamily" />
+ </sql-query>
+
+
+
+
+
</hibernate-mapping>
diff --git a/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.java
b/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.java
index d564af9..5f76c04 100644
--- a/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.java
+++ b/java/code/src/com/redhat/rhn/domain/channel/ChannelFamily.java
@@ -144,8 +144,7 @@ public class ChannelFamily extends BaseDomainHelper {
}
ChannelFamily castOther = (ChannelFamily) other;
- return new EqualsBuilder().append(getId(), castOther.getId())
- .append(getLabel(), castOther.getLabel())
+ return new EqualsBuilder().append(getLabel(), castOther.getLabel())
.append(getName(), castOther.getName())
.append(getOrg(), castOther.getOrg())
.append(getProductUrl(), castOther.getProductUrl())
diff --git a/java/code/src/com/redhat/rhn/domain/channel/ChannelFamilyFactory.java
b/java/code/src/com/redhat/rhn/domain/channel/ChannelFamilyFactory.java
index 3eac0ca..b13fbec 100644
--- a/java/code/src/com/redhat/rhn/domain/channel/ChannelFamilyFactory.java
+++ b/java/code/src/com/redhat/rhn/domain/channel/ChannelFamilyFactory.java
@@ -230,4 +230,16 @@ public class ChannelFamilyFactory extends HibernateFactory {
"ChannelFamily.findLicensePath", params);
}
+ /**
+ * Get a list of channel families that guests of a host are using
+ * @param hostId the host id
+ * @return the list of private channel families
+ */
+ public static List<PrivateChannelFamily> listGuestChannelFamiles(Long hostId)
{
+ Map params = new HashMap();
+ params.put("host_id", hostId);
+ return singleton.listObjectsByNamedQuery(
+ "ChannelFamily.listGuestChannelFamilies", params);
+ }
+
}
diff --git a/java/code/src/com/redhat/rhn/domain/channel/PrivateChannelFamily.hbm.xml
b/java/code/src/com/redhat/rhn/domain/channel/PrivateChannelFamily.hbm.xml
index 62a6f7b..7860d3b 100644
--- a/java/code/src/com/redhat/rhn/domain/channel/PrivateChannelFamily.hbm.xml
+++ b/java/code/src/com/redhat/rhn/domain/channel/PrivateChannelFamily.hbm.xml
@@ -25,4 +25,21 @@ PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<![CDATA[from com.redhat.rhn.domain.channel.PrivateChannelFamily as cfam where
cfam.org.id = :orgId]]>
</query>
+
+ <sql-query name="PrivateChannelFamily.listGuestsChannelFamiles">
+ <![CDATA[
+ select distinct {pcf.*}
+ from
+ rhnChannelFamilyServers cfs,
+ rhnVirtualInstance vi,
+ rhnPrivateChannelFamily pcf
+ where
+ vi.host_system_id = :sid
+ and vi.virtual_system_id = cfs.server_id
+ and cfs.channel_family_id = pcf.channel_family_id
+ and pcf.org_id = cfs.customer_id
+ ]]>
+ <return alias="pcf"
class="com.redhat.rhn.domain.channel.PrivateChannelFamily"/>
+ </sql-query>
+
</hibernate-mapping>
diff --git a/java/code/src/com/redhat/rhn/domain/org/OrgImpl.java
b/java/code/src/com/redhat/rhn/domain/org/OrgImpl.java
index 70d5700..1cf393e 100644
--- a/java/code/src/com/redhat/rhn/domain/org/OrgImpl.java
+++ b/java/code/src/com/redhat/rhn/domain/org/OrgImpl.java
@@ -39,6 +39,8 @@ import com.redhat.rhn.domain.user.UserFactory;
import com.redhat.rhn.manager.channel.ChannelManager;
import com.redhat.rhn.manager.entitlement.EntitlementManager;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.hibernate.Session;
@@ -649,22 +651,13 @@ public class OrgImpl extends BaseDomainHelper implements Org {
if (this == obj) {
return true;
}
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
+ if (!(obj instanceof OrgImpl)) {
return false;
}
final OrgImpl other = (OrgImpl) obj;
- if (getId() == null) {
- if (other.getId() != null) {
- return false;
- }
- }
- else if (!getId().equals(other.getId())) {
- return false;
- }
- return true;
+ EqualsBuilder build = new EqualsBuilder();
+ build.append(this.getName(), other.getName());
+ return build.isEquals();
}
/**
@@ -672,9 +665,8 @@ public class OrgImpl extends BaseDomainHelper implements Org {
*/
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
- return result;
+ HashCodeBuilder build = new HashCodeBuilder();
+ build.append(this.getName());
+ return build.toHashCode();
}
}
diff --git a/java/code/src/com/redhat/rhn/domain/server/ServerFactory.java
b/java/code/src/com/redhat/rhn/domain/server/ServerFactory.java
index a5ba37c..95059b2 100644
--- a/java/code/src/com/redhat/rhn/domain/server/ServerFactory.java
+++ b/java/code/src/com/redhat/rhn/domain/server/ServerFactory.java
@@ -21,6 +21,7 @@ import com.redhat.rhn.common.db.datasource.ModeFactory;
import com.redhat.rhn.common.db.datasource.SelectMode;
import com.redhat.rhn.common.hibernate.HibernateFactory;
import com.redhat.rhn.common.validator.ValidatorError;
+import com.redhat.rhn.domain.channel.ChannelFamily;
import com.redhat.rhn.domain.org.CustomDataKey;
import
com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.rhnpackage.PackageEvrFactory;
@@ -794,4 +795,40 @@ public class ServerFactory extends HibernateFactory {
new HashMap(), systemIds, "sids");
}
+ /**
+ * List quantity number of guests that are using the provided channel family
+ * in order to remove them
+ * @param fam the channel family
+ * @param quantity the quantity of them you want
+ * @param hostId the host's id
+ * @return The list of server ids (guests)
+ */
+ public static List<Long> listGuestIdsForRemoval(ChannelFamily fam,
+ Long quantity, Long hostId) {
+ Map params = new HashMap();
+ params.put("host_id", hostId);
+ params.put("quantity_in", quantity);
+ params.put("family_id_in", fam.getId());
+ return singleton.listObjectsByNamedQuery(
+ "VirtualInstance.guestsForRemovalByFamily", params);
+ }
+
+ /**
+ * List quantity number of guests that are using the provided server group
+ * in order to remove them
+ * @param group the server Group
+ * @param quantity the quantity of them you want
+ * @param hostId the host's id
+ * @return The list of server ids (guests)
+ */
+ public static List<Long> listGuestIdsForRemoval(EntitlementServerGroup group,
+ Long quantity, Long hostId)
{
+ Map params = new HashMap();
+ params.put("host_id", hostId);
+ params.put("quantity_in", quantity);
+ params.put("group_type", group.getGroupType().getId());
+ return singleton.listObjectsByNamedQuery(
+ "VirtualInstance.guestsForRemovalByFamily", params);
+ }
+
}
diff --git a/java/code/src/com/redhat/rhn/domain/server/ServerGroup.hbm.xml
b/java/code/src/com/redhat/rhn/domain/server/ServerGroup.hbm.xml
index 046ca4c..35add7b 100644
--- a/java/code/src/com/redhat/rhn/domain/server/ServerGroup.hbm.xml
+++ b/java/code/src/com/redhat/rhn/domain/server/ServerGroup.hbm.xml
@@ -125,6 +125,22 @@ PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<return alias="sg"
class="com.redhat.rhn.domain.server.ServerGroup"/>
</sql-query>
+
+ <sql-query name="ServerGroup.lookupGuestGroups">
+ <![CDATA[ select distinct {sg.*}
+ from
+ rhnServerGroupType sgt,
+ rhnServerGroup sg,
+ rhnServerGroupMembers sgm,
+ rhnVirtualInstance vi
+ where
+ vi.host_system_id = :sid
+ and vi.virtual_system_id = sgm.server_id
+ and sgm.server_group_id = sg.id
+ and sg.group_type = sgt.id ]]>
+ <return alias="sg"
class="com.redhat.rhn.domain.server.EntitlementServerGroup"/>
+ </sql-query>
+
<sql-query name="ServerGroup.lookupServerIds">
<![CDATA[ select s.id
from rhnServer s
diff --git a/java/code/src/com/redhat/rhn/domain/server/ServerGroupFactory.java
b/java/code/src/com/redhat/rhn/domain/server/ServerGroupFactory.java
index 0a4b371..149801b 100644
--- a/java/code/src/com/redhat/rhn/domain/server/ServerGroupFactory.java
+++ b/java/code/src/com/redhat/rhn/domain/server/ServerGroupFactory.java
@@ -252,6 +252,19 @@ public class ServerGroupFactory extends HibernateFactory {
}
/**
+ * List the Entitlement server groups that a host's guests are using
+ * @param hostId the host id
+ * @return the list of entitlement server groups
+ */
+ public static List<EntitlementServerGroup> listGuestsServerGroups(Long hostId)
{
+ Map params = new HashMap();
+ params.put("sid", hostId);
+ return SINGLETON.listObjectsByNamedQuery(
+ "ServerGroup.lookupGuestGroups", params);
+ }
+
+
+ /**
* Returns the list of Entitlement ServerGroups associated to a server.
* @param s the server to find the server groups of
* @return list of EntitlementServerGroup objects that are associated to
diff --git a/java/code/src/com/redhat/rhn/domain/server/ServerGroupType.java
b/java/code/src/com/redhat/rhn/domain/server/ServerGroupType.java
index ede23eb..7a458f9 100644
--- a/java/code/src/com/redhat/rhn/domain/server/ServerGroupType.java
+++ b/java/code/src/com/redhat/rhn/domain/server/ServerGroupType.java
@@ -15,6 +15,8 @@
package com.redhat.rhn.domain.server;
import com.redhat.rhn.domain.AbstractLabelNameHelper;
+import com.redhat.rhn.domain.entitlement.Entitlement;
+import com.redhat.rhn.manager.entitlement.EntitlementManager;
import java.util.HashSet;
import java.util.Set;
@@ -74,4 +76,12 @@ public class ServerGroupType extends AbstractLabelNameHelper {
public void setFeatures(Set<Feature> featuresIn) {
features = featuresIn;
}
+
+ /**
+ * Get the associated Entitlement
+ * @return the Entitlement
+ */
+ public Entitlement getAssociatedEntitlement() {
+ return EntitlementManager.getByName(this.getLabel());
+ }
}
diff --git a/java/code/src/com/redhat/rhn/domain/server/test/HostBuilder.java
b/java/code/src/com/redhat/rhn/domain/server/test/HostBuilder.java
index 299067f..faf56ce 100644
--- a/java/code/src/com/redhat/rhn/domain/server/test/HostBuilder.java
+++ b/java/code/src/com/redhat/rhn/domain/server/test/HostBuilder.java
@@ -20,10 +20,13 @@ import com.redhat.rhn.domain.server.ServerConstants;
import com.redhat.rhn.domain.server.ServerGroupType;
import com.redhat.rhn.domain.server.VirtualInstance;
import com.redhat.rhn.domain.user.User;
+import com.redhat.rhn.manager.entitlement.EntitlementManager;
+import com.redhat.rhn.manager.system.SystemManager;
import com.redhat.rhn.testing.TestUtils;
import org.hibernate.Session;
+import java.util.Date;
import java.util.Iterator;
@@ -80,6 +83,9 @@ public class HostBuilder {
private HostBuilder createHost(ServerGroupType type) throws Exception {
host = ServerFactoryTest.createTestServer(owner, true, type);
+ if (host.getBaseEntitlement() == null) {
+ SystemManager.entitleServer(host, EntitlementManager.MANAGEMENT);
+ }
return this;
}
@@ -162,11 +168,16 @@ public class HostBuilder {
virtualInstance.setUuid(TestUtils.randomString());
if (register) {
- guest = ServerFactoryTest.createTestServer(owner);
+ guest = ServerFactoryTest.createUnentitledTestServer(owner, true,
+ ServerFactoryTest.TYPE_SERVER_NORMAL , new Date());
virtualInstance.setGuestSystem(guest);
}
host.addGuest(virtualInstance);
+ TestUtils.saveAndFlush(host);
+ TestUtils.saveAndFlush(guest);
+ //ServerFactory.save(guest);
+
}
}
diff --git a/java/code/src/com/redhat/rhn/domain/server/test/ServerFactoryTest.java
b/java/code/src/com/redhat/rhn/domain/server/test/ServerFactoryTest.java
index 68f3016..ffd5ef4 100644
--- a/java/code/src/com/redhat/rhn/domain/server/test/ServerFactoryTest.java
+++ b/java/code/src/com/redhat/rhn/domain/server/test/ServerFactoryTest.java
@@ -535,17 +535,31 @@ public class ServerFactoryTest extends RhnBaseTestCase {
new Date());
}
- public static Server createTestServer(User owner, boolean ensureOwnerAccess,
- ServerGroupType type, Date dateCreated) throws Exception {
- return createTestServer(owner, ensureOwnerAccess, type, TYPE_SERVER_NORMAL,
- dateCreated);
- }
public static Server createTestServer(User owner, boolean ensureOwnerAccess,
ServerGroupType type, int stype) throws Exception {
return createTestServer(owner, ensureOwnerAccess, type, stype, new Date());
}
+
+ private static Server createTestServer(User owner, boolean ensureOwnerAccess,
+ ServerGroupType type, int stype, Date dateCreated)
+ throws Exception {
+
+ Server newS = createUnentitledTestServer(owner, ensureOwnerAccess, stype,
+ dateCreated);
+
+ if (!type.getAssociatedEntitlement().isBase()) {
+ SystemManager.entitleServer(newS, EntitlementManager.MANAGEMENT);
+ }
+
+
+ EntitlementServerGroup sg = ServerGroupTestUtils.createEntitled(owner.getOrg(),
+ type);
+ SystemManager.entitleServer(newS, sg.getGroupType().getAssociatedEntitlement());
+ return (Server) TestUtils.saveAndReload(newS);
+ }
+
/**
* Create a test Server and commit it to the DB.
* @param owner the owner of this Server
@@ -553,18 +567,14 @@ public class ServerFactoryTest extends RhnBaseTestCase {
* access to the new server.
* @return Server that was created
*/
- private static Server createTestServer(User owner, boolean ensureOwnerAccess,
- ServerGroupType type, int stype, Date dateCreated)
+ public static Server createUnentitledTestServer(User owner, boolean
ensureOwnerAccess,
+ int stype, Date dateCreated)
throws Exception {
- //Create a server and a server group
- EntitlementServerGroup sg = ServerGroupTestUtils.createEntitled(owner.getOrg(),
- type);
- if (sg.getMaxMembers() != null) {
- sg.setMaxMembers(new Long(sg.getMaxMembers().longValue() + 10L));
- }
+
Server newS = createServer(stype);
+
// We have to commit this change manually since
// ServerGroups aren't actually mapped from within
// the Server class.
@@ -587,9 +597,11 @@ public class ServerFactoryTest extends RhnBaseTestCase {
newS.addNetworkInterface(netint);
+
ServerFactory.save(newS);
- ServerFactory.addServerToGroup(newS, sg);
+ TestUtils.saveAndReload(newS);
+
/* Since we added a server to the Org we need
* to update the User's permissions as associated with
@@ -705,7 +717,7 @@ public class ServerFactoryTest extends RhnBaseTestCase {
throws Exception {
return createTestServer(owner, ensureOwnerAccess,
- ServerConstants.getServerGroupTypeUpdateEntitled());
+ ServerConstants.getServerGroupTypeEnterpriseEntitled());
}
private static Server createServer(int type) {
@@ -988,4 +1000,3 @@ public class ServerFactoryTest extends RhnBaseTestCase {
}
}
-
diff --git a/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
b/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
index f53e716..ffd8f1d 100644
--- a/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
+++ b/java/code/src/com/redhat/rhn/manager/channel/ChannelManager.java
@@ -38,6 +38,7 @@ import com.redhat.rhn.domain.channel.ClonedChannel;
import com.redhat.rhn.domain.channel.ContentSource;
import com.redhat.rhn.domain.channel.DistChannelMap;
import com.redhat.rhn.domain.channel.InvalidChannelRoleException;
+import com.redhat.rhn.domain.channel.PrivateChannelFamily;
import com.redhat.rhn.domain.channel.ProductName;
import com.redhat.rhn.domain.channel.ReleaseChannelMap;
import com.redhat.rhn.domain.common.CommonConstants;
@@ -2906,5 +2907,22 @@ public class ChannelManager extends BaseManager {
return toRet;
}
+ /**
+ * Recompute channel family counts
+ * Just calls the stored proc
+ *
+ * @param family the channel family
+ */
+ public static void updateChannelFamilyCounts(PrivateChannelFamily family) {
+
+ Map params = new HashMap();
+ params.put("fid", family.getChannelFamily().getId());
+ params.put("org_id", family.getOrg().getId());
+
+ CallableMode mode = ModeFactory.getCallableMode(
+ "Channel_queries", "update_family_counts");
+ mode.execute(params, new HashMap());
+ }
+
}
diff --git a/java/code/src/com/redhat/rhn/manager/channel/ChannelProcedure.java
b/java/code/src/com/redhat/rhn/manager/channel/ChannelProcedure.java
index 7b4aed4..9b1348f 100644
--- a/java/code/src/com/redhat/rhn/manager/channel/ChannelProcedure.java
+++ b/java/code/src/com/redhat/rhn/manager/channel/ChannelProcedure.java
@@ -192,7 +192,15 @@ public class ChannelProcedure {
channelFamilyIn.setCurrentMembers(orgIn, currentMembers);
}
- private long computeCurrentMemberCounts(ChannelFamily channelFamilyIn,
+ /**
+ * COmpute current member counts. That is the current number of
+ * systems using a channel family (basically a real time computation
+ * of rhnPrivateChannelFamily.current_members
+ * @param channelFamilyIn the channel family
+ * @param orgIn the Org
+ * @return the Number used
+ */
+ public Long computeCurrentMemberCounts(ChannelFamily channelFamilyIn,
Org orgIn) {
SelectMode m = ModeFactory.getMode("Channel_queries",
"compute_channel_family_curent_members");
@@ -203,4 +211,6 @@ public class ChannelProcedure {
return (Long) dr.get(0).get("count");
}
+
+
}
diff --git a/java/code/src/com/redhat/rhn/manager/entitlement/EntitlementProcedure.java
b/java/code/src/com/redhat/rhn/manager/entitlement/EntitlementProcedure.java
index 8f23e7e..3252ddb 100644
--- a/java/code/src/com/redhat/rhn/manager/entitlement/EntitlementProcedure.java
+++ b/java/code/src/com/redhat/rhn/manager/entitlement/EntitlementProcedure.java
@@ -14,16 +14,26 @@
*/
package com.redhat.rhn.manager.entitlement;
+import com.redhat.rhn.common.db.datasource.CallableMode;
+import com.redhat.rhn.common.db.datasource.DataResult;
+import com.redhat.rhn.common.db.datasource.ModeFactory;
+import com.redhat.rhn.common.db.datasource.SelectMode;
+import com.redhat.rhn.common.db.datasource.WriteMode;
import com.redhat.rhn.domain.channel.ChannelFactory;
import com.redhat.rhn.domain.channel.ChannelFamilyFactory;
import com.redhat.rhn.domain.channel.PrivateChannelFamily;
import
com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.org.OrgFactory;
import com.redhat.rhn.domain.server.EntitlementServerGroup;
+import com.redhat.rhn.domain.server.ServerFactory;
import com.redhat.rhn.domain.server.ServerGroupFactory;
import com.redhat.rhn.frontend.xmlrpc.SatelliteOrgException;
+import com.redhat.rhn.manager.channel.ChannelManager;
+import com.redhat.rhn.manager.channel.ChannelProcedure;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
*
@@ -94,4 +104,114 @@ public class EntitlementProcedure {
}
+ /**
+ * -- *******************************************************************
+ * PROCEDURE: repoll_virt_guest_entitlements
+ *
+ * Whenever we add/remove a virtualization_host* entitlement from
+ * a host, we can call this procedure to update what type of slots
+ * the guests are consuming.
+ *
+ * If you're removing the entitlement, it's
+ * possible the guests will become unentitled if you don't have enough
+ * physical slots to cover them.
+ -- All channel families associated with the guests of server_id_in
+ *
+ * If you're adding the entitlement, you end up freeing up physical
+ * slots for other systems.
+ * @param sid the server Id
+ */
+ public void repollGuestVirtEntitlements(Long sid) {
+
+ List<PrivateChannelFamily> chanFamilies =
+ ChannelFamilyFactory.listGuestChannelFamiles(sid);
+ List<EntitlementServerGroup> sysEntitlements =
+ ServerGroupFactory.listGuestsServerGroups(sid);
+
+ for (PrivateChannelFamily family : chanFamilies) {
+ Long physicalMembers =
+ ChannelProcedure.getInstance().computeCurrentMemberCounts(
+ family.getChannelFamily(), family.getOrg());
+
+ if (physicalMembers > family.getMaxMembers()) {
+ List<Long> idsToRemove = ServerFactory.listGuestIdsForRemoval(
+ family.getChannelFamily(), physicalMembers.longValue() -
+ family.getMaxMembers().longValue(), sid);
+ for (Long toRemove : idsToRemove) {
+ unsubscribeServerFromFamily(toRemove,
+ family.getChannelFamily().getId());
+ }
+
+ /*
+ * update current_members for the family. This will set the value
+ * to reflect adding/removing the entitlement.
+ */
+ ChannelManager.updateChannelFamilyCounts(family);
+
+ }
+ }
+
+ for (EntitlementServerGroup group : sysEntitlements) {
+ ServerGroupFactory.lockEntitledServerGroup(group);
+ Long physicalCount = getCurrentServerGroupCount(group);
+ if (physicalCount > group.getMaxMembers()) {
+ List<Long> idsToRemove =
ServerFactory.listGuestIdsForRemoval(group,
+ physicalCount.longValue() - group.getMaxMembers().longValue(),
+ sid);
+ for (Long guestId : idsToRemove) {
+ removeServerEntitlement(guestId, group.getGroupType().getLabel());
+ physicalCount--;
+ }
+ updateServerGroupCurrentMembers(physicalCount, group);
+ }
+
+ }
+
+
+ }
+
+ private void unsubscribeServerFromFamily(Long sid, Long familyId) {
+ CallableMode m = ModeFactory.getCallableMode("Procedure_queries",
+ "unsubscribe_server_from_family");
+ Map params = new HashMap();
+ params.put("sid", sid);
+ params.put("fam_id", familyId);
+ m.execute(params, new HashMap());
+
+ }
+
+
+ private void removeServerEntitlement(Long sid, String groupType) {
+ CallableMode m = ModeFactory.getCallableMode("Procedure_queries",
+ "remove_server_entitlement");
+ Map params = new HashMap();
+ params.put("sid", sid);
+ params.put("group_type", groupType);
+ m.execute(params, new HashMap());
+ }
+
+
+ private void updateServerGroupCurrentMembers(
+ Long currentMembers, EntitlementServerGroup group) {
+ WriteMode m = ModeFactory.getWriteMode("Procedure_queries",
+ "update_group_current_members");
+ Map params = new HashMap();
+ params.put("sgid", group.getId());
+ params.put("current_members", currentMembers);
+ m.executeUpdate(params);
+ }
+
+ /**
+ * Calculates the actual current_members value
+ * (the number of systems using that entitlement)
+ */
+ private Long getCurrentServerGroupCount(EntitlementServerGroup group) {
+ SelectMode m = ModeFactory.getMode("Procedure_queries",
+ "compute_physical_server_group_count");
+ Map params = new HashMap();
+ params.put("sgid", group.getId());
+ DataResult<Map<String, Object>> dr = m.execute(params);
+ return (Long) dr.get(0).get("count");
+ }
+
}
diff --git
a/java/code/src/com/redhat/rhn/manager/entitlement/test/EntitlementProcedureTest.java
b/java/code/src/com/redhat/rhn/manager/entitlement/test/EntitlementProcedureTest.java
new file mode 100644
index 0000000..9def21f
--- /dev/null
+++ b/java/code/src/com/redhat/rhn/manager/entitlement/test/EntitlementProcedureTest.java
@@ -0,0 +1,169 @@
+/**
+ * Copyright (c) 2009--2010 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 com.redhat.rhn.manager.entitlement.test;
+
+import com.redhat.rhn.common.db.datasource.CallableMode;
+import com.redhat.rhn.common.db.datasource.ModeFactory;
+import com.redhat.rhn.domain.channel.Channel;
+import com.redhat.rhn.domain.channel.ChannelFactory;
+import com.redhat.rhn.domain.channel.ChannelFamily;
+import com.redhat.rhn.domain.channel.ChannelFamilyFactory;
+import
com.redhat.rhn.domain.org.Org;
+import com.redhat.rhn.domain.org.OrgFactory;
+import com.redhat.rhn.domain.org.test.OrgFactoryTest;
+import com.redhat.rhn.domain.role.RoleFactory;
+import com.redhat.rhn.domain.server.EntitlementServerGroup;
+import com.redhat.rhn.domain.server.Server;
+import com.redhat.rhn.domain.server.ServerFactory;
+import com.redhat.rhn.domain.server.VirtualInstance;
+import com.redhat.rhn.domain.server.test.HostBuilder;
+import com.redhat.rhn.domain.user.User;
+import com.redhat.rhn.domain.user.UserFactory;
+import com.redhat.rhn.manager.entitlement.EntitlementManager;
+import com.redhat.rhn.manager.entitlement.EntitlementProcedure;
+import com.redhat.rhn.manager.org.UpdateOrgSoftwareEntitlementsCommand;
+import com.redhat.rhn.manager.org.UpdateOrgSystemEntitlementsCommand;
+import com.redhat.rhn.manager.system.ServerGroupManager;
+import com.redhat.rhn.manager.system.SystemManager;
+import com.redhat.rhn.testing.RhnBaseTestCase;
+import com.redhat.rhn.testing.UserTestUtils;
+
+import org.apache.commons.lang.RandomStringUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+public class EntitlementProcedureTest extends RhnBaseTestCase {
+
+
+ public void testRepollGuestVirtEntitlements() throws Exception {
+ OrgFactoryTest test = new OrgFactoryTest();
+ Org org =
UserTestUtils.createNewOrgFull(RandomStringUtils.randomAlphabetic(10));
+ User user = UserTestUtils.createUser(RandomStringUtils.randomAlphabetic(10),
+ org.getId());
+ user.addRole(RoleFactory.ORG_ADMIN);
+ UserFactory.save(user);
+ Long ents = 3L;
+ Long guestsToCreate = 6L;
+
+ //Give it some system entitlements
+ UpdateOrgSystemEntitlementsCommand cmd1 = new
UpdateOrgSystemEntitlementsCommand(
+ EntitlementManager.MANAGEMENT, org, ents);
+ cmd1.store();
+
+ UpdateOrgSoftwareEntitlementsCommand cmd2
+ = new UpdateOrgSoftwareEntitlementsCommand("rhel-server", org,
ents);
+ cmd2.store();
+ OrgFactory.getSession().clear();
+
+
+ HostBuilder builder = new HostBuilder(org.getActiveOrgAdmins().get(0));
+ builder.createVirtHost().withGuests(guestsToCreate.intValue());
+ Server host = builder.build();
+
+
+ ServerFactory.save(host);
+ Channel rhelServer =
ChannelFactory.lookupByLabel("rhel-i386-server-5");
+ SystemManager.subscribeServerToChannel(user, host, rhelServer);
+ //SystemManager.entitleServer(host, EntitlementManager.MANAGEMENT);
+ ServerFactory.save(host);
+ for (VirtualInstance inst : host.getGuests()) {
+ SystemManager.subscribeServerToChannel(user,
+ inst.getGuestSystem(), rhelServer);
+ SystemManager.entitleServer(inst.getGuestSystem(),
+ EntitlementManager.MANAGEMENT);
+ ServerFactory.save(inst.getGuestSystem());
+ }
+
+ OrgFactory.getSession().clear();
+
+ //Verify everything is as it should be
+ EntitlementServerGroup mgmnt =
+ ServerGroupManager.getInstance().lookupEntitled(
+ EntitlementManager.MANAGEMENT, user);
+ assertEquals(new Long(1L), mgmnt.getCurrentMembers());
+ ChannelFamily family =
ChannelFamilyFactory.lookupByLabel("rhel-server", org);
+ assertEquals(new Long(1L), family.getCurrentMembers(org));
+
+
+ OrgFactory.getSession().clear();
+
+ EntitlementProcedure.getInstance().repollGuestVirtEntitlements(host.getId());
+
+
+ //verify that after repolling with no changes, eerything is teh same
+ mgmnt = ServerGroupManager.getInstance().lookupEntitled(
+ EntitlementManager.MANAGEMENT, user);
+ assertEquals(new Long(1L), mgmnt.getCurrentMembers());
+ family = ChannelFamilyFactory.lookupByLabel("rhel-server", org);
+ assertEquals(new Long(1L), family.getCurrentMembers(org));
+
+
+ //Now clear the virtualization entitlements without going through stored proc
+ // (To test repoll)
+ removeServerEntitlement(host.getId(),
+ ServerGroupManager.getInstance().lookupEntitled(
+ EntitlementManager.VIRTUALIZATION, user).getGroupType().getLabel());
+
+ EntitlementProcedure.getInstance().repollGuestVirtEntitlements(host.getId());
+
+ OrgFactory.getSession().clear();
+
+ mgmnt = ServerGroupManager.getInstance().lookupEntitled(
+ EntitlementManager.MANAGEMENT, user);
+ assertEquals(ents, mgmnt.getCurrentMembers());
+ family = ChannelFamilyFactory.lookupByLabel("rhel-server", org);
+ assertEquals(ents, family.getCurrentMembers(org));
+
+ host = ServerFactory.lookupById(host.getId());
+ int groupUnent = 0;
+ int familyUnent = 0;
+ for (VirtualInstance virt : host.getGuests()) {
+ if (virt.getGuestSystem().getBaseEntitlement() == null) {
+ groupUnent++;
+ }
+ if (virt.getGuestSystem().getBaseChannel() == null) {
+ familyUnent++;
+ }
+
+ }
+
+
+ //The number of unentitled guests should be the number of guests
+ // - the number of open slots (leaving one out for the host)
+ assertEquals(guestsToCreate.intValue() - (ents.intValue() - 1), groupUnent);
+ assertEquals(guestsToCreate.intValue() - (ents.intValue() - 1), familyUnent);
+
+
+ }
+
+
+ private static void removeServerEntitlement(Long sid, String groupType) {
+ CallableMode m = ModeFactory.getCallableMode("Procedure_queries",
+ "remove_server_entitlement");
+
+ // SelectMode m = ModeFactory.getMode("Procedure_queries",
+ // "remove_server_entitlement");
+
+ Map params = new HashMap();
+ params.put("sid", sid);
+ params.put("group_type", groupType);
+
+
+ m.execute(params, new HashMap());
+ // m.execute(params);
+ }
+}
diff --git a/java/code/src/com/redhat/rhn/testing/UserTestUtils.java
b/java/code/src/com/redhat/rhn/testing/UserTestUtils.java
index 308e2d1..04f61d9 100644
--- a/java/code/src/com/redhat/rhn/testing/UserTestUtils.java
+++ b/java/code/src/com/redhat/rhn/testing/UserTestUtils.java
@@ -53,15 +53,25 @@ public class UserTestUtils extends Assert {
* @return long The Org id.
*/
public static Long createOrg(String orgName) {
+ return createNewOrgFull(orgName).getId();
+ }
+
+ /**
+ * Creates a new Org with the given orgName.
+ * The current time is appended to the given orgName.
+ * @param orgName Name of org.
+ * @return long The Org
+ */
+ public static Org createNewOrgFull(String orgName) {
Org org1 = OrgFactory.createOrg();
org1.setName(orgName + TestUtils.randomString());
org1.setCustomerType("B");
org1 = OrgFactory.save(org1);
assertTrue(org1.getId().longValue() > 0);
-
- return org1.getId();
+ return org1;
}
+
/**
* Creates a new User and Org with the given userName and orgName.
* The current time is appended to the given username and orgName.