Skip to content

Commit

Permalink
fixup! fixup! refactor MinionServerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mbussolotto committed Jan 10, 2025
1 parent 7670247 commit ca22059
Showing 1 changed file with 58 additions and 42 deletions.
100 changes: 58 additions & 42 deletions java/code/src/com/redhat/rhn/domain/server/MinionServerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,9 @@

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;
Expand All @@ -42,12 +39,6 @@
import java.util.stream.Stream;

import javax.persistence.NoResultException;
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;

/**
* MinionFactory - the singleton class used to fetch and store
Expand All @@ -63,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 @@ -93,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 Down Expand Up @@ -141,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 @@ -194,11 +196,18 @@ WHERE minion_id IN (:minions)
* @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 @@ -287,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);
List<Long> server_ids = getSession().createNativeQuery("""
SELECT server_id from rhnServerNetInterface
WHERE hw_addr IN (:hwaddr)
""", Long.class)
.setParameterList("hwaddr", hwAddrs, StringType.INSTANCE)
.getResultList();

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

return getSession().createQuery(query).stream()
return servers.stream()
.filter(s -> s.hasEntitlement(EntitlementManager.BOOTSTRAP))
.collect(toList());
}
Expand All @@ -308,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));
List<MinionServer> servers = getSession().createNativeQuery("""
SELECT * from rhnServer
WHERE hostname = :hostname
""", MinionServer.class)
.setParameter("hostname", hostname, StringType.INSTANCE)
.getResultList();

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

0 comments on commit ca22059

Please sign in to comment.