Skip to content

Commit

Permalink
refactor MinionServerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Jan 10, 2025
1 parent fea0109 commit 578e5d5
Showing 1 changed file with 86 additions and 56 deletions.
142 changes: 86 additions & 56 deletions java/code/src/com/redhat/rhn/domain/server/MinionServerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,18 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.hibernate.type.LongType;
import org.hibernate.type.StringType;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.NoResultException;

/**
* MinionFactory - the singleton class used to fetch and store
Expand All @@ -61,11 +54,12 @@ public class MinionServerFactory extends HibernateFactory {
* @return the Server found
*/
public static List<MinionServer> lookupByOrg(Long orgId) {
return HibernateFactory.getSession()
.createCriteria(MinionServer.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.add(Restrictions.eq("org.id", orgId))
.list();
return getSession().createNativeQuery("""
SELECT DISTINCT * from rhnServer
WHERE org_id = :org
""", MinionServer.class)
.setParameter("org", orgId, StringType.INSTANCE)
.getResultList();
}

/**
Expand All @@ -91,10 +85,19 @@ protected Logger getLogger() {
* @return server corresponding to the given machine_id
*/
public static Optional<MinionServer> findByMachineId(String machineId) {
Session session = getSession();
Criteria criteria = session.createCriteria(MinionServer.class);
criteria.add(Restrictions.eq("machineId", machineId));
return Optional.ofNullable((MinionServer) criteria.uniqueResult());
Optional<MinionServer> minion;
try {
minion = Optional.ofNullable(getSession().createNativeQuery("""
SELECT * from rhnServer
WHERE machine_id = :machineId
""", MinionServer.class)
.setParameter("machineId", machineId, StringType.INSTANCE)
.uniqueResult());
}
catch (NoResultException e) {
minion = Optional.empty();
}
return minion;
}

/**
Expand All @@ -104,10 +107,19 @@ public static Optional<MinionServer> findByMachineId(String machineId) {
* @return server corresponding to the given machine_id
*/
public static Optional<MinionServer> findByMinionId(String minionId) {
Session session = getSession();
Criteria criteria = session.createCriteria(MinionServer.class);
criteria.add(Restrictions.eq("minionId", minionId));
return Optional.ofNullable((MinionServer) criteria.uniqueResult());
Optional<MinionServer> minion;
try {
minion = Optional.ofNullable(getSession().createNativeQuery("""
SELECT * from suseMinionInfo
WHERE minion_id = :minion
""", MinionServer.class)
.setParameter("minion", minionId, StringType.INSTANCE)
.uniqueResult());
}
catch (NoResultException e) {
minion = Optional.empty();
}
return minion;
}

/**
Expand All @@ -117,9 +129,10 @@ public static Optional<MinionServer> findByMinionId(String minionId) {
*/
@SuppressWarnings("unchecked")
public static List<MinionServer> listMinions() {
return getSession().createCriteria(MinionServer.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.list();
return getSession().createNativeQuery("""
SELECT DISTINCT * from suseMinionInfo
""", MinionServer.class)
.getResultList();
}

/**
Expand All @@ -129,11 +142,12 @@ public static List<MinionServer> listMinions() {
* @return a list of minions ids belonging to the given organization
*/
public static List<String> findMinionIdsByOrgId(Long orgId) {
return getSession().createCriteria(MinionServer.class)
.setProjection(Projections.property("minionId"))
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.add(Restrictions.eq("org.id", orgId))
.list();
return getSession().createNativeQuery("""
SELECT DISTINCT minion_id * from suseMinionInfo
WHERE org_id = :org
""", String.class)
.setParameter("org", orgId, LongType.INSTANCE)
.getResultList();
}

/**
Expand Down Expand Up @@ -168,10 +182,12 @@ public static List<MinionServer> lookupByMinionIds(Set<String> minionIds) {
return emptyList();
}
else {
return HibernateFactory.getSession().createCriteria(MinionServer.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.add(Restrictions.in("minionId", minionIds))
.list();
return getSession().createNativeQuery("""
SELECT DISTINCT * from suseMinionInfo
WHERE minion_id IN (:minions)
""", MinionServer.class)
.setParameterList("minions", minionIds, StringType.INSTANCE)
.getResultList();
}
}

Expand All @@ -180,11 +196,18 @@ public static List<MinionServer> lookupByMinionIds(Set<String> minionIds) {
* @return map of SSH minion id and its contact method
*/
public static List<MinionServer> listSSHMinions() {
return HibernateFactory.getSession().createCriteria(MinionServer.class)
.createAlias("contactMethod", "m")
.add(Restrictions.in("m.label",
"ssh-push", "ssh-push-tunnel"))
.list();
List<Long> contacts = getSession().createNativeQuery("""
SELECT id from suseServerContactMethod
WHERE label IN (:labels)
""", Long.class)
.setParameterList("labels", List.of("ssh-push", "ssh-push-tunnel"), StringType.INSTANCE)
.getResultList();
return getSession().createNativeQuery("""
SELECT * from rhnServer
WHERE contact_method_id IN (:contacts)
""", MinionServer.class)
.setParameterList("contact", contacts, LongType.INSTANCE)
.getResultList();
}

/**
Expand Down Expand Up @@ -273,16 +296,21 @@ public static List<MinionServer> findEmptyProfilesByHwAddrs(Set<String> hwAddrs)
return emptyList();
}

CriteriaBuilder builder = getSession().getCriteriaBuilder();
CriteriaQuery<MinionServer> query = builder.createQuery(MinionServer.class);
Root<MinionServer> root = query.distinct(true).from(MinionServer.class);

Join<MinionServer, NetworkInterface> nicJoin = root.join("networkInterfaces", JoinType.INNER);
Predicate hwAddrPredicate = nicJoin.get("hwaddr").in(hwAddrs);

query.where(hwAddrPredicate);

return getSession().createQuery(query).stream()
List<Long> serverIds = getSession().createNativeQuery("""
SELECT server_id from rhnServerNetInterface
WHERE hw_addr IN (:hwaddr)
""", Long.class)
.setParameterList("hwaddr", hwAddrs, StringType.INSTANCE)
.getResultList();

List<MinionServer> servers = getSession().createNativeQuery("""
SELECT * from rhnServer
WHERE id IN (:ids)
""", MinionServer.class)
.setParameterList("ids", serverIds, StringType.INSTANCE)
.getResultList();

return servers.stream()
.filter(s -> s.hasEntitlement(EntitlementManager.BOOTSTRAP))
.collect(toList());
}
Expand All @@ -294,12 +322,14 @@ public static List<MinionServer> findEmptyProfilesByHwAddrs(Set<String> hwAddrs)
* @return the List of MinionServer matching given hostname
*/
public static List<MinionServer> findEmptyProfilesByHostName(String hostname) {
CriteriaBuilder builder = getSession().getCriteriaBuilder();
CriteriaQuery<MinionServer> query = builder.createQuery(MinionServer.class);
Root<MinionServer> root = query.from(MinionServer.class);
query.where(builder.equal(root.get("hostname"), hostname));

return getSession().createQuery(query).stream()
List<MinionServer> servers = getSession().createNativeQuery("""
SELECT * from rhnServer
WHERE hostname = :hostname
""", MinionServer.class)
.setParameter("hostname", hostname, StringType.INSTANCE)
.getResultList();

return servers.stream()
.filter(s -> s.hasEntitlement(EntitlementManager.BOOTSTRAP))
.collect(toList());
}
Expand Down

0 comments on commit 578e5d5

Please sign in to comment.