Use join instead of views (#365)

* Use join instead of views for filtering volumes

* Use join instead of views for filtering events

* Use join instead of views for filtering accounts

* Use join instead of views for filtering domains

* Use join instead of views for filtering hosts

* Use join instead of views for filtering storage pools

* Use join instead of views for filtering service offerings

* Use join instead of views for filtering disk offerings

* Remove unused code

* Fix unit test

* Use disk_offering instead of disk_offering_view in service_offering_view

* Fixup

* Fix listing of diskoffering & serviceoffering

* Use constants instead of strings

* Make changes to prevent sql injection

* Remove commented code

* Prevent n+1 queries for template's response

* remove unused import

* refactor some code

* Add missing check for service offering's join with disk offering

* Fix n+1 queries for stoage pool metrics

* Remove n+1 queries from list accounts

* Remove unused imports

* remove todo

* Remove unused import

* Fixup query generation for nested joins

* Fixups

* Fix DB exception on ClientPreparedStatement

* events,alerts: Add missing indexes (#366)

* Fixup
This commit is contained in:
Vishesh 2024-03-14 17:49:35 +05:30 committed by GitHub
parent bf4ea0d59f
commit 4c6c8216d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
45 changed files with 1828 additions and 679 deletions

View File

@ -102,7 +102,7 @@ public interface ServiceOffering extends InfrastructureEntity, InternalIdentity,
boolean getDefaultUse();
String getSystemVmType();
String getVmType();
String getDeploymentPlanner();

View File

@ -40,6 +40,8 @@ public interface RoleService {
*/
Role findRole(Long id);
List<Role> findRoles(List<Long> ids, boolean ignorePrivateRoles);
Role createRole(String name, RoleType roleType, String description);
Role createRole(String name, Role role, String description);

View File

@ -25,4 +25,18 @@ import java.io.Serializable;
public interface InternalIdentity extends Serializable {
long getId();
/*
Helper method to add conditions in joins where some column name is equal to a string value
*/
default Object setString(String str) {
return null;
}
/*
Helper method to add conditions in joins where some column name is equal to a long value
*/
default Object setLong(Long l) {
return null;
}
}

View File

@ -194,7 +194,7 @@ public class ServiceOfferingVO implements ServiceOffering {
limitCpuUse = offering.getLimitCpuUse();
volatileVm = offering.isVolatileVm();
hostTag = offering.getHostTag();
vmType = offering.getSystemVmType();
vmType = offering.getVmType();
systemUse = offering.isSystemUse();
dynamicScalingEnabled = offering.isDynamicScalingEnabled();
diskOfferingStrictness = offering.diskOfferingStrictness;
@ -278,7 +278,7 @@ public class ServiceOfferingVO implements ServiceOffering {
}
@Override
public String getSystemVmType() {
public String getVmType() {
return vmType;
}

View File

@ -21,6 +21,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
public class DatabaseAccessObject {
@ -85,8 +86,8 @@ public class DatabaseAccessObject {
return columnExists;
}
public String generateIndexName(String tableName, String columnName) {
return String.format("i_%s__%s", tableName, columnName);
public String generateIndexName(String tableName, String... columnNames) {
return String.format("i_%s__%s", tableName, StringUtils.join(columnNames, "__"));
}
public boolean indexExists(Connection conn, String tableName, String indexName) {
@ -101,8 +102,8 @@ public class DatabaseAccessObject {
return false;
}
public void createIndex(Connection conn, String tableName, String columnName, String indexName) {
String stmt = String.format("CREATE INDEX %s on %s (%s)", indexName, tableName, columnName);
public void createIndex(Connection conn, String tableName, String indexName, String... columnNames) {
String stmt = String.format("CREATE INDEX %s ON %s (%s)", indexName, tableName, StringUtils.join(columnNames, ", "));
s_logger.debug("Statement: " + stmt);
try (PreparedStatement pstmt = conn.prepareStatement(stmt)) {
pstmt.execute();

View File

@ -23,11 +23,11 @@ public class DbUpgradeUtils {
private static DatabaseAccessObject dao = new DatabaseAccessObject();
public static void addIndexIfNeeded(Connection conn, String tableName, String columnName) {
String indexName = dao.generateIndexName(tableName, columnName);
public static void addIndexIfNeeded(Connection conn, String tableName, String... columnNames) {
String indexName = dao.generateIndexName(tableName, columnNames);
if (!dao.indexExists(conn, tableName, indexName)) {
dao.createIndex(conn, tableName, columnName, indexName);
dao.createIndex(conn, tableName, indexName, columnNames);
}
}

View File

@ -245,6 +245,11 @@ public class Upgrade41800to41810 implements DbUpgrade, DbUpgradeSystemVmTemplate
}
private void addIndexes(Connection conn) {
DbUpgradeUtils.addIndexIfNeeded(conn, "alert", "archived", "created");
DbUpgradeUtils.addIndexIfNeeded(conn, "alert", "type", "data_center_id", "pod_id");
DbUpgradeUtils.addIndexIfNeeded(conn, "event", "resource_type", "resource_id");
DbUpgradeUtils.addIndexIfNeeded(conn, "cluster_details", "name");
}
}

View File

@ -35,4 +35,6 @@ public interface RoleDao extends GenericDao<RoleVO, Long> {
RoleVO findByNameAndType(String roleName, RoleType type);
Pair<List<RoleVO>, Integer> findAllByRoleType(RoleType type, Long offset, Long limit);
List<RoleVO> searchByIds(Long... ids);
}

View File

@ -28,10 +28,13 @@ import org.apache.cloudstack.acl.RoleVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
@Component
public class RoleDaoImpl extends GenericDaoBase<RoleVO, Long> implements RoleDao {
private final SearchBuilder<RoleVO> RoleByIdsSearch;
private final SearchBuilder<RoleVO> RoleByNameSearch;
private final SearchBuilder<RoleVO> RoleByTypeSearch;
private final SearchBuilder<RoleVO> RoleByNameAndTypeSearch;
@ -39,6 +42,10 @@ public class RoleDaoImpl extends GenericDaoBase<RoleVO, Long> implements RoleDao
public RoleDaoImpl() {
super();
RoleByIdsSearch = createSearchBuilder();
RoleByIdsSearch.and("idIN", RoleByIdsSearch.entity().getId(), SearchCriteria.Op.IN);
RoleByIdsSearch.done();
RoleByNameSearch = createSearchBuilder();
RoleByNameSearch.and("roleName", RoleByNameSearch.entity().getName(), SearchCriteria.Op.LIKE);
RoleByNameSearch.done();
@ -96,4 +103,14 @@ public class RoleDaoImpl extends GenericDaoBase<RoleVO, Long> implements RoleDao
sc.setParameters("roleType", type);
return findOneBy(sc);
}
@Override
public List<RoleVO> searchByIds(Long... ids) {
if (ids == null || ids.length == 0) {
return Collections.emptyList();
}
SearchCriteria<RoleVO> sc = RoleByIdsSearch.create();
sc.setParameters("idIN", ids);
return listBy(sc);
}
}

View File

@ -65,6 +65,10 @@ public class DiskOfferingDetailVO implements ResourceDetail {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String getValue() {
return value;

View File

@ -49,4 +49,6 @@ public interface ImageStoreDao extends GenericDao<ImageStoreVO, Long> {
List<ImageStoreVO> findByProtocol(String protocol);
ImageStoreVO findOneByZoneAndProtocol(long zoneId, String protocol);
List<ImageStoreVO> listByIds(List<Long> ids);
}

View File

@ -18,12 +18,14 @@
*/
package org.apache.cloudstack.storage.datastore.db;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.naming.ConfigurationException;
import com.cloud.utils.db.Filter;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
@ -42,6 +44,7 @@ public class ImageStoreDaoImpl extends GenericDaoBase<ImageStoreVO, Long> implem
private SearchBuilder<ImageStoreVO> storeSearch;
private SearchBuilder<ImageStoreVO> protocolSearch;
private SearchBuilder<ImageStoreVO> zoneProtocolSearch;
private SearchBuilder<ImageStoreVO> IdsSearch;
public ImageStoreDaoImpl() {
super();
@ -55,6 +58,10 @@ public class ImageStoreDaoImpl extends GenericDaoBase<ImageStoreVO, Long> implem
zoneProtocolSearch.and("protocol", zoneProtocolSearch.entity().getProtocol(), SearchCriteria.Op.EQ);
zoneProtocolSearch.and("role", zoneProtocolSearch.entity().getRole(), SearchCriteria.Op.EQ);
zoneProtocolSearch.done();
IdsSearch = createSearchBuilder();
IdsSearch.and("ids", IdsSearch.entity().getId(), SearchCriteria.Op.IN);
IdsSearch.done();
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@ -191,4 +198,14 @@ public class ImageStoreDaoImpl extends GenericDaoBase<ImageStoreVO, Long> implem
List<ImageStoreVO> results = listBy(sc, filter);
return results.size() == 0 ? null : results.get(0);
}
@Override
public List<ImageStoreVO> listByIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
SearchCriteria<ImageStoreVO> sc = IdsSearch.create();
sc.setParameters("ids", ids.toArray());
return listBy(sc);
}
}

View File

@ -23,6 +23,8 @@ import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.ScopeType;
import com.cloud.storage.Storage;
import com.cloud.storage.StoragePoolStatus;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDao;
/**
@ -134,4 +136,10 @@ public interface PrimaryDataStoreDao extends GenericDao<StoragePoolVO, Long> {
List<StoragePoolVO> findPoolsByStorageType(Storage.StoragePoolType storageType);
List<StoragePoolVO> listStoragePoolsWithActiveVolumesByOfferingId(long offeringid);
Pair<List<Long>, Integer> searchForIdsAndCount(Long storagePoolId, String storagePoolName, Long zoneId,
String path, Long podId, Long clusterId, String address, ScopeType scopeType, StoragePoolStatus status,
String keyword, Filter searchFilter);
List<StoragePoolVO> listByIds(List<Long> ids);
}

View File

@ -20,12 +20,16 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import org.apache.commons.collections.CollectionUtils;
import com.cloud.host.Status;
@ -58,6 +62,7 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
private final SearchBuilder<StoragePoolVO> DcLocalStorageSearch;
private final GenericSearchBuilder<StoragePoolVO, Long> StatusCountSearch;
private final SearchBuilder<StoragePoolVO> ClustersSearch;
private final SearchBuilder<StoragePoolVO> IdsSearch;
@Inject
private StoragePoolDetailsDao _detailsDao;
@ -144,6 +149,11 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
ClustersSearch = createSearchBuilder();
ClustersSearch.and("clusterIds", ClustersSearch.entity().getClusterId(), Op.IN);
ClustersSearch.and("status", ClustersSearch.entity().getStatus(), Op.EQ);
ClustersSearch.done();
IdsSearch = createSearchBuilder();
IdsSearch.and("ids", IdsSearch.entity().getId(), SearchCriteria.Op.IN);
IdsSearch.done();
}
@ -616,4 +626,92 @@ public class PrimaryDataStoreDaoImpl extends GenericDaoBase<StoragePoolVO, Long>
throw new CloudRuntimeException("Caught: " + sql, e);
}
}
@Override
public Pair<List<Long>, Integer> searchForIdsAndCount(Long storagePoolId, String storagePoolName, Long zoneId,
String path, Long podId, Long clusterId, String address, ScopeType scopeType, StoragePoolStatus status,
String keyword, Filter searchFilter) {
SearchCriteria<StoragePoolVO> sc = createStoragePoolSearchCriteria(storagePoolId, storagePoolName, zoneId, path, podId, clusterId, address, scopeType, status, keyword);
Pair<List<StoragePoolVO>, Integer> uniquePair = searchAndCount(sc, searchFilter);
List<Long> idList = uniquePair.first().stream().map(StoragePoolVO::getId).collect(Collectors.toList());
return new Pair<>(idList, uniquePair.second());
}
@Override
public List<StoragePoolVO> listByIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
SearchCriteria<StoragePoolVO> sc = IdsSearch.create();
sc.setParameters("ids", ids.toArray());
return listBy(sc);
}
private SearchCriteria<StoragePoolVO> createStoragePoolSearchCriteria(Long storagePoolId, String storagePoolName,
Long zoneId, String path, Long podId, Long clusterId, String address, ScopeType scopeType,
StoragePoolStatus status, String keyword) {
SearchBuilder<StoragePoolVO> sb = createSearchBuilder();
sb.select(null, SearchCriteria.Func.DISTINCT, sb.entity().getId()); // select distinct
// ids
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
sb.and("path", sb.entity().getPath(), SearchCriteria.Op.EQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
sb.and("hostAddress", sb.entity().getHostAddress(), SearchCriteria.Op.EQ);
sb.and("scope", sb.entity().getScope(), SearchCriteria.Op.EQ);
sb.and("status", sb.entity().getStatus(), SearchCriteria.Op.EQ);
sb.and("parent", sb.entity().getParent(), SearchCriteria.Op.EQ);
SearchCriteria<StoragePoolVO> sc = sb.create();
if (keyword != null) {
SearchCriteria<StoragePoolVO> ssc = createSearchCriteria();
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("poolType", SearchCriteria.Op.LIKE, new Storage.StoragePoolType("%" + keyword + "%"));
sc.addAnd("name", SearchCriteria.Op.SC, ssc);
}
if (storagePoolId != null) {
sc.setParameters("id", storagePoolId);
}
if (storagePoolName != null) {
sc.setParameters("name", storagePoolName);
}
if (path != null) {
sc.setParameters("path", path);
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (podId != null) {
SearchCriteria<StoragePoolVO> ssc = createSearchCriteria();
ssc.addOr("podId", SearchCriteria.Op.EQ, podId);
ssc.addOr("podId", SearchCriteria.Op.NULL);
sc.addAnd("podId", SearchCriteria.Op.SC, ssc);
}
if (address != null) {
sc.setParameters("hostAddress", address);
}
if (clusterId != null) {
SearchCriteria<StoragePoolVO> ssc = createSearchCriteria();
ssc.addOr("clusterId", SearchCriteria.Op.EQ, clusterId);
ssc.addOr("clusterId", SearchCriteria.Op.NULL);
sc.addAnd("clusterId", SearchCriteria.Op.SC, ssc);
}
if (scopeType != null) {
sc.setParameters("scope", scopeType.toString());
}
if (status != null) {
sc.setParameters("status", status.toString());
}
sc.setParameters("parent", 0);
return sc;
}
}

View File

@ -48,6 +48,102 @@ WHERE so.default_use = 1 AND so.vm_type IN ('domainrouter', 'secondarystoragevm'
UPDATE `cloud`.`guest_os_hypervisor` SET guest_os_name = 'rhel9_64Guest' WHERE guest_os_name = 'rhel9_64Guest,';
CALL `cloud`.`IDEMPOTENT_ADD_COLUMN`('cloud.guest_os', 'display', 'tinyint(1) DEFAULT ''1'' COMMENT ''should this guest_os be shown to the end user'' ');
--
DROP VIEW IF EXISTS `cloud`.`service_offering_view`;
CREATE VIEW `cloud`.`service_offering_view` AS
SELECT
`service_offering`.`id` AS `id`,
`service_offering`.`uuid` AS `uuid`,
`service_offering`.`name` AS `name`,
`service_offering`.`display_text` AS `display_text`,
`disk_offering`.`provisioning_type` AS `provisioning_type`,
`service_offering`.`created` AS `created`,
`disk_offering`.`tags` AS `tags`,
`service_offering`.`removed` AS `removed`,
`disk_offering`.`use_local_storage` AS `use_local_storage`,
`service_offering`.`system_use` AS `system_use`,
`disk_offering`.`id` AS `disk_offering_id`,
`disk_offering`.`name` AS `disk_offering_name`,
`disk_offering`.`uuid` AS `disk_offering_uuid`,
`disk_offering`.`display_text` AS `disk_offering_display_text`,
`disk_offering`.`customized_iops` AS `customized_iops`,
`disk_offering`.`min_iops` AS `min_iops`,
`disk_offering`.`max_iops` AS `max_iops`,
`disk_offering`.`hv_ss_reserve` AS `hv_ss_reserve`,
`disk_offering`.`bytes_read_rate` AS `bytes_read_rate`,
`disk_offering`.`bytes_read_rate_max` AS `bytes_read_rate_max`,
`disk_offering`.`bytes_read_rate_max_length` AS `bytes_read_rate_max_length`,
`disk_offering`.`bytes_write_rate` AS `bytes_write_rate`,
`disk_offering`.`bytes_write_rate_max` AS `bytes_write_rate_max`,
`disk_offering`.`bytes_write_rate_max_length` AS `bytes_write_rate_max_length`,
`disk_offering`.`iops_read_rate` AS `iops_read_rate`,
`disk_offering`.`iops_read_rate_max` AS `iops_read_rate_max`,
`disk_offering`.`iops_read_rate_max_length` AS `iops_read_rate_max_length`,
`disk_offering`.`iops_write_rate` AS `iops_write_rate`,
`disk_offering`.`iops_write_rate_max` AS `iops_write_rate_max`,
`disk_offering`.`iops_write_rate_max_length` AS `iops_write_rate_max_length`,
`disk_offering`.`cache_mode` AS `cache_mode`,
`disk_offering`.`disk_size` AS `root_disk_size`,
`disk_offering`.`encrypt` AS `encrypt_root`,
`service_offering`.`cpu` AS `cpu`,
`service_offering`.`speed` AS `speed`,
`service_offering`.`ram_size` AS `ram_size`,
`service_offering`.`nw_rate` AS `nw_rate`,
`service_offering`.`mc_rate` AS `mc_rate`,
`service_offering`.`ha_enabled` AS `ha_enabled`,
`service_offering`.`limit_cpu_use` AS `limit_cpu_use`,
`service_offering`.`host_tag` AS `host_tag`,
`service_offering`.`default_use` AS `default_use`,
`service_offering`.`vm_type` AS `vm_type`,
`service_offering`.`sort_key` AS `sort_key`,
`service_offering`.`is_volatile` AS `is_volatile`,
`service_offering`.`deployment_planner` AS `deployment_planner`,
`service_offering`.`dynamic_scaling_enabled` AS `dynamic_scaling_enabled`,
`service_offering`.`disk_offering_strictness` AS `disk_offering_strictness`,
`vsphere_storage_policy`.`value` AS `vsphere_storage_policy`,
GROUP_CONCAT(DISTINCT(domain.id)) AS domain_id,
GROUP_CONCAT(DISTINCT(domain.uuid)) AS domain_uuid,
GROUP_CONCAT(DISTINCT(domain.name)) AS domain_name,
GROUP_CONCAT(DISTINCT(domain.path)) AS domain_path,
GROUP_CONCAT(DISTINCT(zone.id)) AS zone_id,
GROUP_CONCAT(DISTINCT(zone.uuid)) AS zone_uuid,
GROUP_CONCAT(DISTINCT(zone.name)) AS zone_name,
IFNULL(`min_compute_details`.`value`, `cpu`) AS min_cpu,
IFNULL(`max_compute_details`.`value`, `cpu`) AS max_cpu,
IFNULL(`min_memory_details`.`value`, `ram_size`) AS min_memory,
IFNULL(`max_memory_details`.`value`, `ram_size`) AS max_memory
FROM
`cloud`.`service_offering`
INNER JOIN
`cloud`.`disk_offering` ON service_offering.disk_offering_id = disk_offering.id AND `disk_offering`.`state`='Active'
LEFT JOIN
`cloud`.`service_offering_details` AS `domain_details` ON `domain_details`.`service_offering_id` = `service_offering`.`id` AND `domain_details`.`name`='domainid'
LEFT JOIN
`cloud`.`domain` AS `domain` ON FIND_IN_SET(`domain`.`id`, `domain_details`.`value`)
LEFT JOIN
`cloud`.`service_offering_details` AS `zone_details` ON `zone_details`.`service_offering_id` = `service_offering`.`id` AND `zone_details`.`name`='zoneid'
LEFT JOIN
`cloud`.`data_center` AS `zone` ON FIND_IN_SET(`zone`.`id`, `zone_details`.`value`)
LEFT JOIN
`cloud`.`service_offering_details` AS `min_compute_details` ON `min_compute_details`.`service_offering_id` = `service_offering`.`id`
AND `min_compute_details`.`name` = 'mincpunumber'
LEFT JOIN
`cloud`.`service_offering_details` AS `max_compute_details` ON `max_compute_details`.`service_offering_id` = `service_offering`.`id`
AND `max_compute_details`.`name` = 'maxcpunumber'
LEFT JOIN
`cloud`.`service_offering_details` AS `min_memory_details` ON `min_memory_details`.`service_offering_id` = `service_offering`.`id`
AND `min_memory_details`.`name` = 'minmemory'
LEFT JOIN
`cloud`.`service_offering_details` AS `max_memory_details` ON `max_memory_details`.`service_offering_id` = `service_offering`.`id`
AND `max_memory_details`.`name` = 'maxmemory'
LEFT JOIN
`cloud`.`service_offering_details` AS `vsphere_storage_policy` ON `vsphere_storage_policy`.`service_offering_id` = `service_offering`.`id`
AND `vsphere_storage_policy`.`name` = 'storagepolicy'
WHERE
`service_offering`.`state`='Active'
GROUP BY
`service_offering`.`id`;
-- Idempotent ADD COLUMN
DROP PROCEDURE IF EXISTS `cloud`.`IDEMPOTENT_ADD_COLUMN`;
CREATE PROCEDURE `cloud`.`IDEMPOTENT_ADD_COLUMN` (

View File

@ -91,8 +91,14 @@ public class DatabaseAccessObjectTest {
@Test
public void generateIndexNameTest() {
String indexName = dao.generateIndexName("mytable","mycolumn");
Assert.assertEquals( "i_mytable__mycolumn", indexName);
String indexName = dao.generateIndexName("mytable","mycolumn1", "mycolumn2");
Assert.assertEquals( "i_mytable__mycolumn1__mycolumn2", indexName);
}
@Test
public void generateIndexNameTestSingleColumn() {
String indexName = dao.generateIndexName("mytable","mycolumn1");
Assert.assertEquals( "i_mytable__mycolumn1", indexName);
}
@Test
@ -134,10 +140,11 @@ public class DatabaseAccessObjectTest {
Connection conn = connectionMock;
String tableName = "mytable";
String columnName = "mycolumn";
String columnName1 = "mycolumn1";
String columnName2 = "mycolumn2";
String indexName = "myindex";
dao.createIndex(conn, tableName, columnName, indexName);
dao.createIndex(conn, tableName, indexName, columnName1, columnName2);
verify(connectionMock, times(1)).prepareStatement(anyString());
verify(preparedStatementMock, times(1)).execute();
verify(preparedStatementMock, times(1)).close();

View File

@ -81,6 +81,7 @@ public class Attribute {
protected String table;
protected String columnName;
protected Object value;
protected Field field;
protected int flags;
protected Column column;
@ -100,6 +101,10 @@ public class Attribute {
this.column = null;
}
public Attribute(Object value) {
this.value = value;
}
protected void setupColumnInfo(Class<?> clazz, AttributeOverride[] overrides, String tableName, boolean isEmbedded, boolean isId) {
flags = Flag.Selectable.setTrue(flags);
GeneratedValue gv = field.getAnnotation(GeneratedValue.class);
@ -214,6 +219,10 @@ public class Attribute {
return field;
}
public Object getValue() {
return value;
}
public Object get(Object entity) {
try {
return field.get(entity);

View File

@ -284,4 +284,6 @@ public interface GenericDao<T, ID extends Serializable> {
Pair<List<T>, Integer> searchAndDistinctCount(final SearchCriteria<T> sc, final Filter filter, final String[] distinctColumns);
Integer countAll();
List<T> findByUuids(String... uuidArray);
}

View File

@ -59,6 +59,7 @@ import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import com.amazonaws.util.CollectionUtils;
import org.apache.log4j.Logger;
import com.cloud.utils.DateUtil;
@ -378,10 +379,11 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
Collection<JoinBuilder<SearchCriteria<?>>> joins = null;
List<Attribute> joinAttrList = null;
if (sc != null) {
joins = sc.getJoins();
if (joins != null) {
addJoins(str, joins);
joinAttrList = addJoins(str, joins);
}
}
@ -401,6 +403,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
try {
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 1;
if (!CollectionUtils.isNullOrEmpty(joinAttrList)) {
for (Attribute attr : joinAttrList) {
prepareAttribute(i++, pstmt, attr, null);
}
}
if (clause != null) {
for (final Pair<Attribute, Object> value : sc.getValues()) {
prepareAttribute(i++, pstmt, value.first(), value.second());
@ -453,8 +462,9 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
Collection<JoinBuilder<SearchCriteria<?>>> joins = null;
joins = sc.getJoins();
List<Attribute> joinAttrList = null;
if (joins != null) {
addJoins(str, joins);
joinAttrList = addJoins(str, joins);
}
List<Object> groupByValues = addGroupBy(str, sc);
@ -467,6 +477,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
try {
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 1;
if (!CollectionUtils.isNullOrEmpty(joinAttrList)) {
for (Attribute attr : joinAttrList) {
prepareAttribute(i++, pstmt, attr, null);
}
}
if (clause != null) {
for (final Pair<Attribute, Object> value : sc.getValues()) {
prepareAttribute(i++, pstmt, value.first(), value.second());
@ -1270,12 +1287,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
@DB()
protected void addJoins(StringBuilder str, Collection<JoinBuilder<SearchCriteria<?>>> joins) {
addJoins(str, joins, new HashMap<>());
protected List<Attribute> addJoins(StringBuilder str, Collection<JoinBuilder<SearchCriteria<?>>> joins) {
return addJoins(str, joins, new HashMap<>());
}
@DB()
protected void addJoins(StringBuilder str, Collection<JoinBuilder<SearchCriteria<?>>> joins, Map<String, Integer> joinedTableNames) {
protected List<Attribute> addJoins(StringBuilder str, Collection<JoinBuilder<SearchCriteria<?>>> joins, Map<String, String> joinedTableNames) {
List<Attribute> joinAttrList = new ArrayList<>();
boolean hasWhereClause = true;
int fromIndex = str.lastIndexOf("WHERE");
if (fromIndex == -1) {
@ -1286,8 +1304,14 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
for (JoinBuilder<SearchCriteria<?>> join : joins) {
String joinTableName = join.getSecondAttribute().table;
String joinTableAlias = findNextJoinTableName(joinTableName, joinedTableNames);
String joinTableName = join.getSecondAttribute()[0].table;
String joinTableAlias;
if (StringUtils.isNotEmpty(join.getName())) {
joinTableAlias = join.getName();
joinedTableNames.put(joinTableName, joinTableAlias);
} else {
joinTableAlias = joinedTableNames.getOrDefault(joinTableName, joinTableName);
}
StringBuilder onClause = new StringBuilder();
onClause.append(" ")
.append(join.getType().getName())
@ -1296,21 +1320,36 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
if (!joinTableAlias.equals(joinTableName)) {
onClause.append(" ").append(joinTableAlias);
}
onClause.append(" ON ")
.append(join.getFirstAttribute().table)
.append(".")
.append(join.getFirstAttribute().columnName)
.append("=");
if(!joinTableAlias.equals(joinTableName)) {
onClause.append(joinTableAlias);
} else {
onClause.append(joinTableName);
onClause.append(" ON ");
for (int i = 0; i < join.getFirstAttributes().length; i++) {
if (i > 0) {
onClause.append(join.getCondition().getName());
}
if (join.getFirstAttributes()[i].getValue() != null) {
onClause.append("?");
joinAttrList.add(join.getFirstAttributes()[i]);
} else {
onClause.append(joinedTableNames.getOrDefault(join.getFirstAttributes()[i].table, join.getFirstAttributes()[i].table))
.append(".")
.append(join.getFirstAttributes()[i].columnName);
}
onClause.append("=");
if (join.getSecondAttribute()[i].getValue() != null) {
onClause.append("?");
joinAttrList.add(join.getSecondAttribute()[i]);
} else {
if(!joinTableAlias.equals(joinTableName)) {
onClause.append(joinTableAlias);
} else {
onClause.append(joinTableName);
}
onClause.append(".")
.append(join.getSecondAttribute()[i].columnName);
}
}
onClause.append(".")
.append(join.getSecondAttribute().columnName)
.append(" ");
onClause.append(" ");
str.insert(fromIndex, onClause);
String whereClause = join.getT().getWhereClause();
String whereClause = join.getT().getWhereClause(joinTableAlias);
if (StringUtils.isNotEmpty(whereClause)) {
if (!hasWhereClause) {
str.append(" WHERE ");
@ -1327,20 +1366,10 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
for (JoinBuilder<SearchCriteria<?>> join : joins) {
if (join.getT().getJoins() != null) {
addJoins(str, join.getT().getJoins(), joinedTableNames);
joinAttrList.addAll(addJoins(str, join.getT().getJoins(), joinedTableNames));
}
}
}
protected static String findNextJoinTableName(String tableName, Map<String, Integer> usedTableNames) {
if (usedTableNames.containsKey(tableName)) {
Integer tableCounter = usedTableNames.get(tableName);
usedTableNames.put(tableName, ++tableCounter);
tableName = tableName + tableCounter;
} else {
usedTableNames.put(tableName, 0);
}
return tableName;
return joinAttrList;
}
private void removeAndClause(StringBuilder sql) {
@ -1593,13 +1622,29 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
return;
}
}
if(attr.field.getDeclaredAnnotation(Convert.class) != null) {
if (attr.getValue() != null && attr.getValue() instanceof String) {
pstmt.setString(j, (String)attr.getValue());
} else if (attr.getValue() != null && attr.getValue() instanceof Long) {
pstmt.setLong(j, (Long)attr.getValue());
} else if(attr.field.getDeclaredAnnotation(Convert.class) != null) {
Object val = _conversionSupport.convertToDatabaseColumn(attr.field, value);
pstmt.setObject(j, val);
} else if (attr.field.getType() == String.class) {
final String str = (String)value;
if (str == null) {
pstmt.setString(j, null);
final String str;
try {
str = (String) value;
if (str == null) {
pstmt.setString(j, null);
return;
}
} catch (ClassCastException ex) {
// This happens when we pass in an integer, long or any other object which can't be cast to String.
// Converting to string in case of integer or long can result in different results. Required specifically for details tables.
// So, we set the value for the object directly.
s_logger.debug("ClassCastException when casting value to String. Setting the value of the object directly.");
pstmt.setObject(j, value);
return;
}
final Column column = attr.field.getAnnotation(Column.class);
@ -2019,10 +2064,11 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
Collection<JoinBuilder<SearchCriteria<?>>> joins = null;
List<Attribute> joinAttrList = null;
if (sc != null) {
joins = sc.getJoins();
if (joins != null) {
addJoins(str, joins);
joinAttrList = addJoins(str, joins);
}
}
@ -2035,6 +2081,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
try {
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 1;
if (!CollectionUtils.isNullOrEmpty(joinAttrList)) {
for (Attribute attr : joinAttrList) {
prepareAttribute(i++, pstmt, attr, null);
}
}
if (clause != null) {
for (final Pair<Attribute, Object> value : sc.getValues()) {
prepareAttribute(i++, pstmt, value.first(), value.second());
@ -2082,10 +2135,11 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
Collection<JoinBuilder<SearchCriteria<?>>> joins = null;
List<Attribute> joinAttrList = null;
if (sc != null) {
joins = sc.getJoins();
if (joins != null) {
addJoins(str, joins);
joinAttrList = addJoins(str, joins);
}
}
@ -2094,6 +2148,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
try (PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql)) {
int i = 1;
if (!CollectionUtils.isNullOrEmpty(joinAttrList)) {
for (Attribute attr : joinAttrList) {
prepareAttribute(i++, pstmt, attr, null);
}
}
if (clause != null) {
for (final Pair<Attribute, Object> value : sc.getValues()) {
prepareAttribute(i++, pstmt, value.first(), value.second());
@ -2120,6 +2181,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
return getCount(null);
}
@Override
public List<T> findByUuids(String... uuidArray) {
SearchCriteria<T> sc = createSearchCriteria();
sc.addAnd("uuid", SearchCriteria.Op.IN, uuidArray);
return search(sc, null);
}
public Integer getCount(SearchCriteria<T> sc) {
sc = checkAndSetRemovedIsNull(sc);
return getCountIncludingRemoved(sc);
@ -2137,10 +2205,11 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
}
Collection<JoinBuilder<SearchCriteria<?>>> joins = null;
List<Attribute> joinAttrList = null;
if (sc != null) {
joins = sc.getJoins();
if (joins != null) {
addJoins(str, joins);
joinAttrList = addJoins(str, joins);
}
}
@ -2151,6 +2220,13 @@ public abstract class GenericDaoBase<T, ID extends Serializable> extends Compone
try {
pstmt = txn.prepareAutoCloseStatement(sql);
int i = 1;
if (!CollectionUtils.isNullOrEmpty(joinAttrList)) {
for (Attribute attr : joinAttrList) {
prepareAttribute(i++, pstmt, attr, null);
}
}
if (clause != null) {
for (final Pair<Attribute, Object> value : sc.getValues()) {
prepareAttribute(i++, pstmt, value.first(), value.second());

View File

@ -80,6 +80,12 @@ public class GenericSearchBuilder<T, K> extends SearchBase<GenericSearchBuilder<
return this;
}
public GenericSearchBuilder<T, K> and(String joinName, String name, Object field, Op op) {
SearchBase<?, ?, ?> join = _joins.get(joinName).getT();
constructCondition(joinName, name, " AND ", join._specifiedAttrs.get(0), op);
return this;
}
/**
* Adds an AND condition. Some prefer this method because it looks like
* the actual SQL query.
@ -134,6 +140,12 @@ public class GenericSearchBuilder<T, K> extends SearchBase<GenericSearchBuilder<
return this;
}
protected GenericSearchBuilder<T, K> left(String joinName, Object field, Op op, String name) {
SearchBase<?, ?, ?> joinSb = _joins.get(joinName).getT();
constructCondition(joinName, name, " ( ", joinSb._specifiedAttrs.get(0), op);
return this;
}
protected Preset left(Object field, Op op) {
Condition condition = constructCondition(UUID.randomUUID().toString(), " ( ", _specifiedAttrs.get(0), op);
return new Preset(this, condition);
@ -169,6 +181,10 @@ public class GenericSearchBuilder<T, K> extends SearchBase<GenericSearchBuilder<
return left(field, op, name);
}
public GenericSearchBuilder<T, K> op(String joinName, String name, Object field, Op op) {
return left(joinName, field, op, name);
}
/**
* Adds an OR condition to the SearchBuilder.
*
@ -182,6 +198,12 @@ public class GenericSearchBuilder<T, K> extends SearchBase<GenericSearchBuilder<
return this;
}
public GenericSearchBuilder<T, K> or(String joinName, String name, Object field, Op op) {
SearchBase<?, ?, ?> join = _joins.get(joinName).getT();
constructCondition(joinName, name, " OR ", join._specifiedAttrs.get(0), op);
return this;
}
/**
* Adds an OR condition
*

View File

@ -31,19 +31,57 @@ public class JoinBuilder<T> {
return _name;
}
}
public enum JoinCondition {
AND(" AND "), OR(" OR ");
private final String name;
JoinCondition(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private final T t;
private final String name;
private JoinType type;
private Attribute firstAttribute;
private Attribute secondAttribute;
public JoinBuilder(T t, Attribute firstAttribute, Attribute secondAttribute, JoinType type) {
private JoinCondition condition;
private Attribute[] firstAttributes;
private Attribute[] secondAttribute;
public JoinBuilder(String name, T t, Attribute firstAttributes, Attribute secondAttribute, JoinType type) {
this.name = name;
this.t = t;
this.firstAttribute = firstAttribute;
this.firstAttributes = new Attribute[]{firstAttributes};
this.secondAttribute = new Attribute[]{secondAttribute};
this.type = type;
}
public JoinBuilder(String name, T t, Attribute[] firstAttributes, Attribute[] secondAttribute, JoinType type) {
this.name = name;
this.t = t;
this.firstAttributes = firstAttributes;
this.secondAttribute = secondAttribute;
this.type = type;
}
public JoinBuilder(String name, T t, Attribute[] firstAttributes, Attribute[] secondAttribute, JoinType type, JoinCondition condition) {
this.name = name;
this.t = t;
this.firstAttributes = firstAttributes;
this.secondAttribute = secondAttribute;
this.type = type;
this.condition = condition;
}
public String getName() {
return name;
}
public T getT() {
return t;
}
@ -56,19 +94,23 @@ public class JoinBuilder<T> {
this.type = type;
}
public Attribute getFirstAttribute() {
return firstAttribute;
public JoinCondition getCondition() {
return condition;
}
public void setFirstAttribute(Attribute firstAttribute) {
this.firstAttribute = firstAttribute;
public Attribute[] getFirstAttributes() {
return firstAttributes;
}
public Attribute getSecondAttribute() {
public void setFirstAttributes(Attribute[] firstAttributes) {
this.firstAttributes = firstAttributes;
}
public Attribute[] getSecondAttribute() {
return secondAttribute;
}
public void setSecondAttribute(Attribute secondAttribute) {
public void setSecondAttribute(Attribute[] secondAttribute) {
this.secondAttribute = secondAttribute;
}

View File

@ -36,6 +36,7 @@ import com.cloud.utils.exception.CloudRuntimeException;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.commons.lang3.StringUtils;
/**
* SearchBase contains the methods that are used to build up search
@ -70,6 +71,14 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
init(entityType, resultType);
}
public SearchBase<?, ?, ?> getJoinSB(String name) {
JoinBuilder<SearchBase<?, ?, ?>> jb = null;
if (_joins != null) {
jb = _joins.get(name);
}
return jb == null ? null : jb.getT();
}
protected void init(final Class<T> entityType, final Class<K> resultType) {
_dao = (GenericDaoBase<? extends T, ? extends Serializable>)GenericDaoBase.getDao(entityType);
if (_dao == null) {
@ -194,15 +203,45 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
* @param joinType type of join
* @return itself
*/
@SuppressWarnings("unchecked")
public J join(final String name, final SearchBase<?, ?, ?> builder, final Object joinField1, final Object joinField2, final JoinBuilder.JoinType joinType) {
assert _entity != null : "SearchBuilder cannot be modified once it has been setup";
assert _specifiedAttrs.size() == 1 : "You didn't select the attribute.";
assert builder._entity != null : "SearchBuilder cannot be modified once it has been setup";
assert builder._specifiedAttrs.size() == 1 : "You didn't select the attribute.";
assert builder != this : "You can't add yourself, can you? Really think about it!";
if (_specifiedAttrs.size() != 1)
throw new CloudRuntimeException("You didn't select the attribute.");
if (builder._specifiedAttrs.size() != 1)
throw new CloudRuntimeException("You didn't select the attribute.");
final JoinBuilder<SearchBase<?, ?, ?>> t = new JoinBuilder<SearchBase<?, ?, ?>>(builder, _specifiedAttrs.get(0), builder._specifiedAttrs.get(0), joinType);
return join(name, builder, joinType, null, joinField1, joinField2);
}
/**
* joins this search with another search with multiple conditions in the join clause
*
* @param name name given to the other search. used for setJoinParameters.
* @param builder The other search
* @param joinType type of join
* @param condition condition to be used for multiple conditions in the join clause
* @param joinFields fields the first and second table used to perform the join.
* The fields should be in the order of the checks between the two tables.
*
* @return
*/
public J join(final String name, final SearchBase<?, ?, ?> builder, final JoinBuilder.JoinType joinType, final
JoinBuilder.JoinCondition condition, final Object... joinFields) {
if (_entity == null)
throw new CloudRuntimeException("SearchBuilder cannot be modified once it has been setup");
if (_specifiedAttrs.isEmpty())
throw new CloudRuntimeException("Attribute not specified.");
if (builder._entity == null)
throw new CloudRuntimeException("SearchBuilder cannot be modified once it has been setup");
if (builder._specifiedAttrs.isEmpty())
throw new CloudRuntimeException("Attribute not specified.");
if (builder == this)
throw new CloudRuntimeException("Can't join with itself. Create a new SearchBuilder for the same entity and use that.");
if (_specifiedAttrs.size() != builder._specifiedAttrs.size())
throw new CloudRuntimeException("Number of attributes to join on must be the same.");
final JoinBuilder<SearchBase<?, ?, ?>> t = new JoinBuilder<>(name, builder, _specifiedAttrs.toArray(new Attribute[0]),
builder._specifiedAttrs.toArray(new Attribute[0]), joinType, condition);
if (_joins == null) {
_joins = new HashMap<String, JoinBuilder<SearchBase<?, ?, ?>>>();
}
@ -223,6 +262,16 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
_specifiedAttrs.add(attr);
}
/*
Allows to set conditions in join where one entity is equivalent to a string or a long
e.g. join("vm", vmSearch, VmDetailVO.class, entity.getName(), "vm.id", SearchCriteria.Op.EQ);
will create a condition 'vm.name = "vm.id"'
*/
protected void setAttr(final Object obj) {
final Attribute attr = new Attribute(obj);
_specifiedAttrs.add(attr);
}
/**
* @return entity object. This allows the caller to use the entity return
* to specify the field to be selected in many of the search parameters.
@ -242,17 +291,26 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
return _specifiedAttrs;
}
protected Condition constructCondition(final String conditionName, final String cond, final Attribute attr, final Op op) {
protected Condition constructCondition(final String joinName, final String conditionName, final String cond, final Attribute attr, final Op op) {
assert _entity != null : "SearchBuilder cannot be modified once it has been setup";
assert op == null || _specifiedAttrs.size() == 1 : "You didn't select the attribute.";
assert op != Op.SC : "Call join";
final Condition condition = new Condition(conditionName, cond, attr, op);
if (StringUtils.isNotEmpty(joinName)) {
condition.setJoinName(joinName);
}
_conditions.add(condition);
_specifiedAttrs.clear();
return condition;
}
protected Condition constructCondition(final String conditionName, final String cond, final Attribute attr, final Op op) {
return constructCondition(null, conditionName, cond, attr, op);
}
/**
* creates the SearchCriteria so the actual values can be filled in.
*
@ -364,6 +422,7 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
protected static class Condition {
protected final String name;
protected final String cond;
protected String joinName;
protected final Op op;
protected final Attribute attr;
protected Object[] presets;
@ -388,11 +447,15 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
this.presets = presets;
}
public void setJoinName(final String joinName) {
this.joinName = joinName;
}
public Object[] getPresets() {
return presets;
}
public void toSql(final StringBuilder sql, final Object[] params, final int count) {
public void toSql(final StringBuilder sql, String tableAlias, final Object[] params, final int count) {
if (count > 0) {
sql.append(cond);
}
@ -414,7 +477,15 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
sql.append(" FIND_IN_SET(?, ");
}
sql.append(attr.table).append(".").append(attr.columnName).append(op.toString());
if (tableAlias == null) {
if (joinName != null) {
tableAlias = joinName;
} else {
tableAlias = attr.table;
}
}
sql.append(tableAlias).append(".").append(attr.columnName).append(op.toString());
if (op == Op.IN && params.length == 1) {
sql.delete(sql.length() - op.toString().length(), sql.length());
sql.append("=?");
@ -485,6 +556,8 @@ public abstract class SearchBase<J extends SearchBase<?, T, K>, T, K> {
final String fieldName = Character.toLowerCase(name.charAt(2)) + name.substring(3);
set(fieldName);
return null;
} else if (name.equals("setLong") || name.equals("setString")) {
setAttr(args[0]);
} else {
final Column ann = method.getAnnotation(Column.class);
if (ann != null) {

View File

@ -106,7 +106,7 @@ public class SearchCriteria<K> {
for (Map.Entry<String, JoinBuilder<SearchBase<?, ?, ?>>> entry : sb._joins.entrySet()) {
JoinBuilder<SearchBase<?, ?, ?>> value = entry.getValue();
_joins.put(entry.getKey(),
new JoinBuilder<SearchCriteria<?>>(value.getT().create(), value.getFirstAttribute(), value.getSecondAttribute(), value.getType()));
new JoinBuilder<SearchCriteria<?>>(entry.getKey(), value.getT().create(), value.getFirstAttributes(), value.getSecondAttribute(), value.getType(), value.getCondition()));
}
}
_selects = sb._selects;
@ -250,7 +250,7 @@ public class SearchCriteria<K> {
_additionals.add(condition);
}
public String getWhereClause() {
public String getWhereClause(String tableAlias) {
StringBuilder sql = new StringBuilder();
int i = 0;
for (Condition condition : _conditions) {
@ -259,7 +259,7 @@ public class SearchCriteria<K> {
}
Object[] params = _params.get(condition.name);
if ((condition.op == null || condition.op.params == 0) || (params != null)) {
condition.toSql(sql, params, i++);
condition.toSql(sql, tableAlias, params, i++);
}
}
@ -269,13 +269,17 @@ public class SearchCriteria<K> {
}
Object[] params = _params.get(condition.name);
if ((condition.op.params == 0) || (params != null)) {
condition.toSql(sql, params, i++);
condition.toSql(sql, tableAlias, params, i++);
}
}
return sql.toString();
}
public String getWhereClause() {
return getWhereClause(null);
}
public List<Pair<Attribute, Object>> getValues() {
ArrayList<Pair<Attribute, Object>> params = new ArrayList<Pair<Attribute, Object>>(_params.size());
for (Condition condition : _conditions) {

View File

@ -20,8 +20,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Before;
@ -229,12 +227,16 @@ public class GenericDaoBaseTest {
Attribute attr2 = new Attribute("table2", "column2");
Attribute attr3 = new Attribute("table3", "column1");
Attribute attr4 = new Attribute("table4", "column2");
Attribute attr5 = new Attribute("table3", "column1");
Attribute attr6 = new Attribute("XYZ");
joins.add(new JoinBuilder<>(dbTestDao.createSearchCriteria(), attr1, attr2, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>(dbTestDao.createSearchCriteria(), attr3, attr4, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>("", dbTestDao.createSearchCriteria(), attr1, attr2, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>("", dbTestDao.createSearchCriteria(),
new Attribute[]{attr3, attr5}, new Attribute[]{attr4, attr6}, JoinBuilder.JoinType.INNER, JoinBuilder.JoinCondition.OR));
dbTestDao.addJoins(joinString, joins);
Assert.assertEquals(" INNER JOIN table2 ON table1.column1=table2.column2 INNER JOIN table4 ON table3.column1=table4.column2 ", joinString.toString());
Assert.assertEquals(" INNER JOIN table2 ON table1.column1=table2.column2 " +
" INNER JOIN table4 ON table3.column1=table4.column2 OR table3.column1=? ", joinString.toString());
}
@Test
@ -248,22 +250,17 @@ public class GenericDaoBaseTest {
Attribute tBc2 = new Attribute("tableB", "column2");
Attribute tCc3 = new Attribute("tableC", "column3");
Attribute tDc4 = new Attribute("tableD", "column4");
Attribute tDc5 = new Attribute("tableD", "column5");
Attribute attr = new Attribute(123);
joins.add(new JoinBuilder<>(dbTestDao.createSearchCriteria(), tBc2, tAc1, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>(dbTestDao.createSearchCriteria(), tCc3, tAc2, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>(dbTestDao.createSearchCriteria(), tDc4, tAc3, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>("tableA1Alias", dbTestDao.createSearchCriteria(), tBc2, tAc1, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>("tableA2Alias", dbTestDao.createSearchCriteria(), tCc3, tAc2, JoinBuilder.JoinType.INNER));
joins.add(new JoinBuilder<>("tableA3Alias", dbTestDao.createSearchCriteria(),
new Attribute[]{tDc4, tDc5}, new Attribute[]{tAc3, attr}, JoinBuilder.JoinType.INNER, JoinBuilder.JoinCondition.AND));
dbTestDao.addJoins(joinString, joins);
Assert.assertEquals(" INNER JOIN tableA ON tableB.column2=tableA.column1 INNER JOIN tableA tableA1 ON tableC.column3=tableA1.column2 INNER JOIN tableA tableA2 ON tableD.column4=tableA2.column3 ", joinString.toString());
}
@Test
public void findNextTableNameTest() {
Map<String, Integer> usedTables = new HashMap<>();
Assert.assertEquals("tableA", GenericDaoBase.findNextJoinTableName("tableA", usedTables));
Assert.assertEquals("tableA1", GenericDaoBase.findNextJoinTableName("tableA", usedTables));
Assert.assertEquals("tableA2", GenericDaoBase.findNextJoinTableName("tableA", usedTables));
Assert.assertEquals("tableA3", GenericDaoBase.findNextJoinTableName("tableA", usedTables));
Assert.assertEquals(" INNER JOIN tableA tableA1Alias ON tableB.column2=tableA1Alias.column1 " +
" INNER JOIN tableA tableA2Alias ON tableC.column3=tableA2Alias.column2 " +
" INNER JOIN tableA tableA3Alias ON tableD.column4=tableA3Alias.column3 AND tableD.column5=? ", joinString.toString());
}
}

View File

@ -27,9 +27,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import javax.inject.Inject;
import com.cloud.dc.ClusterVO;
import org.apache.cloudstack.api.ApiErrorCode;
import org.apache.cloudstack.api.ListClustersMetricsCmd;
import org.apache.cloudstack.api.ListDbMetricsCmd;
@ -626,6 +628,7 @@ public class MetricsServiceImpl extends MutualExclusiveIdsManagerBase implements
@Override
public List<StoragePoolMetricsResponse> listStoragePoolMetrics(List<StoragePoolResponse> poolResponses) {
final List<StoragePoolMetricsResponse> metricsResponses = new ArrayList<>();
Map<String, Long> clusterUuidToIdMap = clusterDao.findByUuids(poolResponses.stream().map(StoragePoolResponse::getClusterId).toArray(String[]::new)).stream().collect(Collectors.toMap(ClusterVO::getUuid, ClusterVO::getId));
for (final StoragePoolResponse poolResponse: poolResponses) {
StoragePoolMetricsResponse metricsResponse = new StoragePoolMetricsResponse();
@ -635,11 +638,7 @@ public class MetricsServiceImpl extends MutualExclusiveIdsManagerBase implements
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate storagepool metrics response");
}
Long poolClusterId = null;
final Cluster cluster = clusterDao.findByUuid(poolResponse.getClusterId());
if (cluster != null) {
poolClusterId = cluster.getId();
}
Long poolClusterId = clusterUuidToIdMap.get(poolResponse.getClusterId());
final Double storageThreshold = AlertManager.StorageCapacityThreshold.valueIn(poolClusterId);
final Double storageDisableThreshold = CapacityManager.StorageCapacityDisableThreshold.valueIn(poolClusterId);

View File

@ -16,75 +16,6 @@
// under the License.
package com.cloud.api;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.cloudstack.acl.Role;
import org.apache.cloudstack.acl.RoleService;
import org.apache.cloudstack.affinity.AffinityGroup;
import org.apache.cloudstack.affinity.AffinityGroupResponse;
import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants.DomainDetails;
import org.apache.cloudstack.api.ApiConstants.HostDetails;
import org.apache.cloudstack.api.ApiConstants.VMDetails;
import org.apache.cloudstack.api.ResponseObject.ResponseView;
import org.apache.cloudstack.api.response.AccountResponse;
import org.apache.cloudstack.api.response.AsyncJobResponse;
import org.apache.cloudstack.api.response.BackupOfferingResponse;
import org.apache.cloudstack.api.response.BackupResponse;
import org.apache.cloudstack.api.response.BackupScheduleResponse;
import org.apache.cloudstack.api.response.DiskOfferingResponse;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.DomainRouterResponse;
import org.apache.cloudstack.api.response.EventResponse;
import org.apache.cloudstack.api.response.HostForMigrationResponse;
import org.apache.cloudstack.api.response.HostResponse;
import org.apache.cloudstack.api.response.HostTagResponse;
import org.apache.cloudstack.api.response.ImageStoreResponse;
import org.apache.cloudstack.api.response.InstanceGroupResponse;
import org.apache.cloudstack.api.response.NetworkOfferingResponse;
import org.apache.cloudstack.api.response.ProjectAccountResponse;
import org.apache.cloudstack.api.response.ProjectInvitationResponse;
import org.apache.cloudstack.api.response.ProjectResponse;
import org.apache.cloudstack.api.response.ResourceIconResponse;
import org.apache.cloudstack.api.response.ResourceTagResponse;
import org.apache.cloudstack.api.response.SecurityGroupResponse;
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
import org.apache.cloudstack.api.response.StoragePoolResponse;
import org.apache.cloudstack.api.response.StorageTagResponse;
import org.apache.cloudstack.api.response.TemplateResponse;
import org.apache.cloudstack.api.response.UserResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.api.response.VolumeResponse;
import org.apache.cloudstack.api.response.VpcOfferingResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.backup.Backup;
import org.apache.cloudstack.backup.BackupOffering;
import org.apache.cloudstack.backup.BackupSchedule;
import org.apache.cloudstack.backup.dao.BackupDao;
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
import org.apache.cloudstack.backup.dao.BackupScheduleDao;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.framework.jobs.AsyncJob;
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
import org.apache.cloudstack.framework.jobs.dao.AsyncJobDao;
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import com.cloud.agent.api.VgpuTypesInfo;
import com.cloud.api.query.dao.AccountJoinDao;
import com.cloud.api.query.dao.AffinityGroupJoinDao;
@ -344,6 +275,75 @@ import com.cloud.vm.dao.UserVmDetailsDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.cloud.vm.snapshot.VMSnapshot;
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
import org.apache.cloudstack.acl.Role;
import org.apache.cloudstack.acl.RoleService;
import org.apache.cloudstack.affinity.AffinityGroup;
import org.apache.cloudstack.affinity.AffinityGroupResponse;
import org.apache.cloudstack.affinity.dao.AffinityGroupDao;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.ApiConstants.DomainDetails;
import org.apache.cloudstack.api.ApiConstants.HostDetails;
import org.apache.cloudstack.api.ApiConstants.VMDetails;
import org.apache.cloudstack.api.ResponseObject.ResponseView;
import org.apache.cloudstack.api.response.AccountResponse;
import org.apache.cloudstack.api.response.AsyncJobResponse;
import org.apache.cloudstack.api.response.BackupOfferingResponse;
import org.apache.cloudstack.api.response.BackupResponse;
import org.apache.cloudstack.api.response.BackupScheduleResponse;
import org.apache.cloudstack.api.response.DiskOfferingResponse;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.DomainRouterResponse;
import org.apache.cloudstack.api.response.EventResponse;
import org.apache.cloudstack.api.response.HostForMigrationResponse;
import org.apache.cloudstack.api.response.HostResponse;
import org.apache.cloudstack.api.response.HostTagResponse;
import org.apache.cloudstack.api.response.ImageStoreResponse;
import org.apache.cloudstack.api.response.InstanceGroupResponse;
import org.apache.cloudstack.api.response.NetworkOfferingResponse;
import org.apache.cloudstack.api.response.ProjectAccountResponse;
import org.apache.cloudstack.api.response.ProjectInvitationResponse;
import org.apache.cloudstack.api.response.ProjectResponse;
import org.apache.cloudstack.api.response.ResourceIconResponse;
import org.apache.cloudstack.api.response.ResourceTagResponse;
import org.apache.cloudstack.api.response.SecurityGroupResponse;
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
import org.apache.cloudstack.api.response.StoragePoolResponse;
import org.apache.cloudstack.api.response.StorageTagResponse;
import org.apache.cloudstack.api.response.TemplateResponse;
import org.apache.cloudstack.api.response.UserResponse;
import org.apache.cloudstack.api.response.UserVmResponse;
import org.apache.cloudstack.api.response.VolumeResponse;
import org.apache.cloudstack.api.response.VpcOfferingResponse;
import org.apache.cloudstack.api.response.ZoneResponse;
import org.apache.cloudstack.backup.Backup;
import org.apache.cloudstack.backup.BackupOffering;
import org.apache.cloudstack.backup.BackupSchedule;
import org.apache.cloudstack.backup.dao.BackupDao;
import org.apache.cloudstack.backup.dao.BackupOfferingDao;
import org.apache.cloudstack.backup.dao.BackupScheduleDao;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.framework.jobs.AsyncJob;
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
import org.apache.cloudstack.framework.jobs.dao.AsyncJobDao;
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ApiDBUtils {
private static ManagementServer s_ms;
@ -792,7 +792,7 @@ public class ApiDBUtils {
s_configDao = configDao;
s_consoleProxyDao = consoleProxyDao;
s_firewallCidrsDao = firewallCidrsDao;
s_firewallDcidrsDao = firewalDcidrsDao;
s_firewallDcidrsDao = firewalDcidrsDao;
s_vmDao = vmDao;
s_resourceLimitMgr = resourceLimitMgr;
s_projectMgr = projectMgr;
@ -917,7 +917,8 @@ public class ApiDBUtils {
return s_resourceLimitMgr.findCorrectResourceLimitForDomain(domain, type, null);
}
public static long findCorrectResourceLimitForDomain(Long limit, boolean isRootDomain, ResourceType type, long domainId) {
public static long findCorrectResourceLimitForDomain(Long limit, boolean isRootDomain, ResourceType type,
long domainId) {
long max = Resource.RESOURCE_UNLIMITED; // if resource limit is not found, then we treat it as unlimited
// No limits for Root domain
@ -997,7 +998,7 @@ public class ApiDBUtils {
return s_statsCollector.getManagementServerHostStats(mgmtSrvrUuid);
}
public static Map<String,Object> getDbStatistics() {
public static Map<String, Object> getDbStatistics() {
return s_statsCollector.getDbStats();
}
@ -1077,6 +1078,7 @@ public class ApiDBUtils {
ServiceOfferingVO off = s_serviceOfferingDao.findServiceOfferingByComputeOnlyDiskOffering(diskOfferingId);
return off;
}
public static DomainVO findDomainById(Long domainId) {
return s_domainDao.findByIdIncludingRemoved(domainId);
}
@ -1249,7 +1251,7 @@ public class ApiDBUtils {
return s_volumeDao.getHypervisorType(volumeId);
}
public static HypervisorType getHypervisorTypeFromFormat(long dcId, ImageFormat format){
public static HypervisorType getHypervisorTypeFromFormat(long dcId, ImageFormat format) {
HypervisorType type = s_storageMgr.getHypervisorTypeFromFormat(format);
if (format == ImageFormat.VHD) {
// Xenserver and Hyperv both support vhd format. Additionally hyperv is only supported
@ -1260,7 +1262,8 @@ public class ApiDBUtils {
if (xenClusters.isEmpty()) {
type = HypervisorType.Hyperv;
}
} if (format == ImageFormat.RAW) {
}
if (format == ImageFormat.RAW) {
// Currently, KVM only supports RBD and PowerFlex images of type RAW.
// This results in a weird collision with OVM volumes which
// can only be raw, thus making KVM RBD volumes show up as OVM
@ -1271,16 +1274,16 @@ public class ApiDBUtils {
// This would be better implemented at a cluster level.
List<StoragePoolVO> pools = s_storagePoolDao.listByDataCenterId(dcId);
ListIterator<StoragePoolVO> itr = pools.listIterator();
while(itr.hasNext()) {
while (itr.hasNext()) {
StoragePoolVO pool = itr.next();
if(pool.getPoolType() == StoragePoolType.RBD ||
pool.getPoolType() == StoragePoolType.PowerFlex ||
pool.getPoolType() == StoragePoolType.CLVM ||
pool.getPoolType() == StoragePoolType.Linstor) {
// This case will note the presence of non-qcow2 primary stores, suggesting KVM without NFS. Otherwse,
// If this check is not passed, the hypervisor type will remain OVM.
type = HypervisorType.KVM;
break;
if (pool.getPoolType() == StoragePoolType.RBD ||
pool.getPoolType() == StoragePoolType.PowerFlex ||
pool.getPoolType() == StoragePoolType.CLVM ||
pool.getPoolType() == StoragePoolType.Linstor) {
// This case will note the presence of non-qcow2 primary stores, suggesting KVM without NFS. Otherwse,
// If this check is not passed, the hypervisor type will remain OVM.
type = HypervisorType.KVM;
break;
} else if (pool.getHypervisor() == HypervisorType.Custom) {
type = HypervisorType.Custom;
break;
@ -1428,7 +1431,7 @@ public class ApiDBUtils {
public static boolean isExtractionDisabled() {
String disableExtractionString = s_configDao.getValue(Config.DisableExtraction.toString());
boolean disableExtraction = (disableExtractionString == null) ? false : Boolean.parseBoolean(disableExtractionString);
boolean disableExtraction = (disableExtractionString == null) ? false : Boolean.parseBoolean(disableExtractionString);
return disableExtraction;
}
@ -1444,7 +1447,7 @@ public class ApiDBUtils {
return s_firewallCidrsDao.getSourceCidrs(id);
}
public static List<String> findFirewallDestCidrs(long id){
public static List<String> findFirewallDestCidrs(long id) {
return s_firewallDcidrsDao.getDestCidrs(id);
}
@ -1554,7 +1557,8 @@ public class ApiDBUtils {
return conditions;
}
public static void getAutoScaleVmGroupPolicyIds(long vmGroupId, List<Long> scaleUpPolicyIds, List<Long> scaleDownPolicyIds) {
public static void getAutoScaleVmGroupPolicyIds(long vmGroupId, List<Long> scaleUpPolicyIds,
List<Long> scaleDownPolicyIds) {
List<AutoScaleVmGroupPolicyMapVO> vos = s_asVmGroupPolicyMapDao.listByVmGroupId(vmGroupId);
for (AutoScaleVmGroupPolicyMapVO vo : vos) {
AutoScalePolicy autoScalePolicy = s_asPolicyDao.findById(vo.getPolicyId());
@ -1575,11 +1579,12 @@ public class ApiDBUtils {
return null;
}
public static UserVmDetailVO findPublicKeyByVmId(long vmId) {
public static UserVmDetailVO findPublicKeyByVmId(long vmId) {
return s_userVmDetailsDao.findDetail(vmId, VmDetailConstants.SSH_PUBLIC_KEY);
}
public static void getAutoScaleVmGroupPolicies(long vmGroupId, List<AutoScalePolicy> scaleUpPolicies, List<AutoScalePolicy> scaleDownPolicies) {
public static void getAutoScaleVmGroupPolicies(long vmGroupId, List<AutoScalePolicy> scaleUpPolicies,
List<AutoScalePolicy> scaleDownPolicies) {
List<AutoScaleVmGroupPolicyMapVO> vos = s_asVmGroupPolicyMapDao.listByVmGroupId(vmGroupId);
for (AutoScaleVmGroupPolicyMapVO vo : vos) {
AutoScalePolicy autoScalePolicy = s_asPolicyDao.findById(vo.getPolicyId());
@ -1691,7 +1696,7 @@ public class ApiDBUtils {
jobInstanceId = template.getUuid();
}
} else if (jobInstanceType == ApiCommandResourceType.VirtualMachine || jobInstanceType == ApiCommandResourceType.ConsoleProxy ||
jobInstanceType == ApiCommandResourceType.SystemVm || jobInstanceType == ApiCommandResourceType.DomainRouter) {
jobInstanceType == ApiCommandResourceType.SystemVm || jobInstanceType == ApiCommandResourceType.DomainRouter) {
VMInstanceVO vm = ApiDBUtils.findVMInstanceById(job.getInstanceId());
if (vm != null) {
jobInstanceId = vm.getUuid();
@ -1788,7 +1793,7 @@ public class ApiDBUtils {
}
} else if (jobInstanceType == ApiCommandResourceType.Network) {
NetworkVO networkVO = ApiDBUtils.findNetworkById(job.getInstanceId());
if(networkVO != null) {
if (networkVO != null) {
jobInstanceId = networkVO.getUuid();
}
} else if (jobInstanceType != ApiCommandResourceType.None) {
@ -1815,11 +1820,13 @@ public class ApiDBUtils {
return s_domainRouterJoinDao.newDomainRouterView(vr);
}
public static UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoinVO userVm, Set<VMDetails> details, Account caller) {
public static UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoinVO userVm,
Set<VMDetails> details, Account caller) {
return s_userVmJoinDao.newUserVmResponse(view, objectName, userVm, details, null, null, caller);
}
public static UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoinVO userVm, Set<VMDetails> details, Boolean accumulateStats,
public static UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoinVO userVm,
Set<VMDetails> details, Boolean accumulateStats,
Boolean showUserData, Account caller) {
return s_userVmJoinDao.newUserVmResponse(view, objectName, userVm, details, accumulateStats, showUserData, caller);
}
@ -1836,7 +1843,8 @@ public class ApiDBUtils {
return s_securityGroupJoinDao.newSecurityGroupResponse(vsg, caller);
}
public static SecurityGroupResponse fillSecurityGroupDetails(SecurityGroupResponse vsgData, SecurityGroupJoinVO sg) {
public static SecurityGroupResponse fillSecurityGroupDetails(SecurityGroupResponse vsgData,
SecurityGroupJoinVO sg) {
return s_securityGroupJoinDao.setSecurityGroupResponse(vsgData, sg);
}
@ -1886,12 +1894,12 @@ public class ApiDBUtils {
public static UserResponse newUserResponse(UserAccountJoinVO usr, Long domainId) {
UserResponse response = s_userAccountJoinDao.newUserResponse(usr);
if(!AccountManager.UseSecretKeyInResponse.value()){
if (!AccountManager.UseSecretKeyInResponse.value()) {
response.setSecretKey(null);
}
// Populate user account role information
if (usr.getAccountRoleId() != null) {
Role role = s_roleService.findRole( usr.getAccountRoleId());
Role role = s_roleService.findRole(usr.getAccountRoleId());
if (role != null) {
response.setRoleId(role.getUuid());
response.setRoleType(role.getRoleType());
@ -1987,7 +1995,8 @@ public class ApiDBUtils {
return s_poolJoinDao.newStoragePoolForMigrationResponse(vr);
}
public static StoragePoolResponse fillStoragePoolForMigrationDetails(StoragePoolResponse vrData, StoragePoolJoinVO vr) {
public static StoragePoolResponse fillStoragePoolForMigrationDetails(StoragePoolResponse vrData,
StoragePoolJoinVO vr) {
return s_poolJoinDao.setStoragePoolForMigrationResponse(vrData, vr);
}
@ -2011,7 +2020,8 @@ public class ApiDBUtils {
return s_domainJoinDao.newDomainResponse(view, details, ve);
}
public static AccountResponse newAccountResponse(ResponseView view, EnumSet<DomainDetails> details, AccountJoinVO ve) {
public static AccountResponse newAccountResponse(ResponseView view, EnumSet<DomainDetails> details,
AccountJoinVO ve) {
AccountResponse response = s_accountJoinDao.newAccountResponse(view, details, ve);
// Populate account role information
if (ve.getRoleId() != null) {
@ -2025,6 +2035,30 @@ public class ApiDBUtils {
return response;
}
public static List<AccountResponse> newAccountResponses(ResponseView view, EnumSet<DomainDetails> details,
AccountJoinVO... accounts) {
List<AccountResponse> responseList = new ArrayList<>();
List<Long> roleIdList = Arrays.stream(accounts).map(AccountJoinVO::getRoleId).collect(Collectors.toList());
Map<Long, Role> roleIdMap = s_roleService.findRoles(roleIdList, false).stream().collect(Collectors.toMap(Role::getId, Function.identity()));
for (AccountJoinVO account : accounts) {
AccountResponse response = s_accountJoinDao.newAccountResponse(view, details, account);
// Populate account role information
if (account.getRoleId() != null) {
Role role = roleIdMap.get(account.getRoleId());
if (role != null) {
response.setRoleId(role.getUuid());
response.setRoleType(role.getRoleType());
response.setRoleName(role.getName());
}
}
responseList.add(response);
}
return responseList;
}
public static AccountJoinVO newAccountView(Account e) {
return s_accountJoinDao.newAccountView(e);
}
@ -2073,7 +2107,8 @@ public class ApiDBUtils {
return s_serviceOfferingJoinDao.newServiceOfferingView(offering);
}
public static ZoneResponse newDataCenterResponse(ResponseView view, DataCenterJoinVO dc, Boolean showCapacities, Boolean showResourceImage) {
public static ZoneResponse newDataCenterResponse(ResponseView view, DataCenterJoinVO dc, Boolean showCapacities,
Boolean showResourceImage) {
return s_dcJoinDao.newDataCenterResponse(view, dc, showCapacities, showResourceImage);
}
@ -2093,7 +2128,8 @@ public class ApiDBUtils {
return s_templateJoinDao.newUpdateResponse(vr);
}
public static TemplateResponse newTemplateResponse(EnumSet<DomainDetails> detailsView, ResponseView view, TemplateJoinVO vr) {
public static TemplateResponse newTemplateResponse(EnumSet<DomainDetails> detailsView, ResponseView view,
TemplateJoinVO vr) {
return s_templateJoinDao.newTemplateResponse(detailsView, view, vr);
}
@ -2101,7 +2137,8 @@ public class ApiDBUtils {
return s_templateJoinDao.newIsoResponse(vr);
}
public static TemplateResponse fillTemplateDetails(EnumSet<DomainDetails> detailsView, ResponseView view, TemplateResponse vrData, TemplateJoinVO vr) {
public static TemplateResponse fillTemplateDetails(EnumSet<DomainDetails> detailsView, ResponseView view,
TemplateResponse vrData, TemplateJoinVO vr) {
return s_templateJoinDao.setTemplateResponse(detailsView, view, vrData, vr);
}
@ -2121,7 +2158,8 @@ public class ApiDBUtils {
return s_affinityGroupJoinDao.newAffinityGroupResponse(group);
}
public static AffinityGroupResponse fillAffinityGroupDetails(AffinityGroupResponse resp, AffinityGroupJoinVO group) {
public static AffinityGroupResponse fillAffinityGroupDetails(AffinityGroupResponse resp,
AffinityGroupJoinVO group) {
return s_affinityGroupJoinDao.setAffinityGroupResponse(resp, group);
}
@ -2149,7 +2187,8 @@ public class ApiDBUtils {
return s_accountService.isAdmin(account.getId());
}
public static List<ResourceTagJoinVO> listResourceTagViewByResourceUUID(String resourceUUID, ResourceObjectType resourceType) {
public static List<ResourceTagJoinVO> listResourceTagViewByResourceUUID(String resourceUUID,
ResourceObjectType resourceType) {
return s_tagJoinDao.listBy(resourceUUID, resourceType);
}

File diff suppressed because it is too large Load Diff

View File

@ -536,11 +536,7 @@ public class ViewResponseHelper {
}
public static List<AccountResponse> createAccountResponse(ResponseView view, EnumSet<DomainDetails> details, AccountJoinVO... accounts) {
List<AccountResponse> respList = new ArrayList<AccountResponse>();
for (AccountJoinVO vt : accounts){
respList.add(ApiDBUtils.newAccountResponse(view, details, vt));
}
return respList;
return ApiDBUtils.newAccountResponses(view, details, accounts);
}
public static List<AsyncJobResponse> createAsyncJobResponse(AsyncJobJoinVO... jobs) {

View File

@ -17,6 +17,7 @@
package com.cloud.api.query.dao;
import java.util.EnumSet;
import java.util.List;
import org.apache.cloudstack.api.ApiConstants.DomainDetails;
import org.apache.cloudstack.api.ResponseObject.ResponseView;
@ -35,4 +36,5 @@ public interface AccountJoinDao extends GenericDao<AccountJoinVO, Long> {
void setResourceLimits(AccountJoinVO account, boolean accountIsAdmin, ResourceLimitAndCountResponse response);
List<AccountJoinVO> searchByIds(Long... ids);
}

View File

@ -16,11 +16,13 @@
// under the License.
package com.cloud.api.query.dao;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import javax.inject.Inject;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -46,12 +48,19 @@ import com.cloud.utils.db.SearchCriteria;
public class AccountJoinDaoImpl extends GenericDaoBase<AccountJoinVO, Long> implements AccountJoinDao {
public static final Logger s_logger = Logger.getLogger(AccountJoinDaoImpl.class);
@Inject
private ConfigurationDao configDao;
private final SearchBuilder<AccountJoinVO> acctIdSearch;
private final SearchBuilder<AccountJoinVO> domainSearch;
@Inject
AccountManager _acctMgr;
protected AccountJoinDaoImpl() {
domainSearch = createSearchBuilder();
domainSearch.and("idIN", domainSearch.entity().getId(), SearchCriteria.Op.IN);
domainSearch.done();
acctIdSearch = createSearchBuilder();
acctIdSearch.and("id", acctIdSearch.entity().getId(), SearchCriteria.Op.EQ);
acctIdSearch.done();
@ -232,6 +241,50 @@ public class AccountJoinDaoImpl extends GenericDaoBase<AccountJoinVO, Long> impl
response.setSecondaryStorageAvailable(secondaryStorageAvail);
}
@Override
public List<AccountJoinVO> searchByIds(Long... accountIds) {
// set detail batch query size
int DETAILS_BATCH_SIZE = 2000;
String batchCfg = configDao.getValue("detail.batch.query.size");
if (batchCfg != null) {
DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
}
List<AccountJoinVO> uvList = new ArrayList<>();
// query details by batches
int curr_index = 0;
if (accountIds.length > DETAILS_BATCH_SIZE) {
while ((curr_index + DETAILS_BATCH_SIZE) <= accountIds.length) {
Long[] ids = new Long[DETAILS_BATCH_SIZE];
for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
ids[k] = accountIds[j];
}
SearchCriteria<AccountJoinVO> sc = domainSearch.create();
sc.setParameters("idIN", ids);
List<AccountJoinVO> accounts = searchIncludingRemoved(sc, null, null, false);
if (accounts != null) {
uvList.addAll(accounts);
}
curr_index += DETAILS_BATCH_SIZE;
}
}
if (curr_index < accountIds.length) {
int batch_size = (accountIds.length - curr_index);
// set the ids value
Long[] ids = new Long[batch_size];
for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
ids[k] = accountIds[j];
}
SearchCriteria<AccountJoinVO> sc = domainSearch.create();
sc.setParameters("idIN", ids);
List<AccountJoinVO> accounts = searchIncludingRemoved(sc, null, null, false);
if (accounts != null) {
uvList.addAll(accounts);
}
}
return uvList;
}
@Override
public AccountJoinVO newAccountView(Account acct) {
SearchCriteria<AccountJoinVO> sc = acctIdSearch.create();

View File

@ -33,4 +33,6 @@ public interface DiskOfferingJoinDao extends GenericDao<DiskOfferingJoinVO, Long
DiskOfferingResponse newDiskOfferingResponse(DiskOfferingJoinVO dof);
DiskOfferingJoinVO newDiskOfferingView(DiskOffering dof);
List<DiskOfferingJoinVO> searchByIds(Long... idArray);
}

View File

@ -16,6 +16,7 @@
// under the License.
package com.cloud.api.query.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -26,6 +27,7 @@ import org.apache.cloudstack.annotation.dao.AnnotationDao;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.response.DiskOfferingResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -51,9 +53,12 @@ public class DiskOfferingJoinDaoImpl extends GenericDaoBase<DiskOfferingJoinVO,
@Inject
private AnnotationDao annotationDao;
@Inject
private ConfigurationDao configDao;
@Inject
private AccountManager accountManager;
private final SearchBuilder<DiskOfferingJoinVO> dofIdSearch;
private SearchBuilder<DiskOfferingJoinVO> diskOfferingSearch;
private final Attribute _typeAttr;
protected DiskOfferingJoinDaoImpl() {
@ -62,6 +67,11 @@ public class DiskOfferingJoinDaoImpl extends GenericDaoBase<DiskOfferingJoinVO,
dofIdSearch.and("id", dofIdSearch.entity().getId(), SearchCriteria.Op.EQ);
dofIdSearch.done();
diskOfferingSearch = createSearchBuilder();
diskOfferingSearch.and("idIN", diskOfferingSearch.entity().getId(), SearchCriteria.Op.IN);
diskOfferingSearch.done();
_typeAttr = _allAttributes.get("type");
_count = "select count(distinct id) from disk_offering_view WHERE ";
@ -154,4 +164,48 @@ public class DiskOfferingJoinDaoImpl extends GenericDaoBase<DiskOfferingJoinVO,
assert offerings != null && offerings.size() == 1 : "No disk offering found for offering id " + offering.getId();
return offerings.get(0);
}
@Override
public List<DiskOfferingJoinVO> searchByIds(Long... offeringIds) {
// set detail batch query size
int DETAILS_BATCH_SIZE = 2000;
String batchCfg = configDao.getValue("detail.batch.query.size");
if (batchCfg != null) {
DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
}
List<DiskOfferingJoinVO> uvList = new ArrayList<>();
// query details by batches
int curr_index = 0;
if (offeringIds.length > DETAILS_BATCH_SIZE) {
while ((curr_index + DETAILS_BATCH_SIZE) <= offeringIds.length) {
Long[] ids = new Long[DETAILS_BATCH_SIZE];
for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
ids[k] = offeringIds[j];
}
SearchCriteria<DiskOfferingJoinVO> sc = diskOfferingSearch.create();
sc.setParameters("idIN", ids);
List<DiskOfferingJoinVO> accounts = searchIncludingRemoved(sc, null, null, false);
if (accounts != null) {
uvList.addAll(accounts);
}
curr_index += DETAILS_BATCH_SIZE;
}
}
if (curr_index < offeringIds.length) {
int batch_size = (offeringIds.length - curr_index);
// set the ids value
Long[] ids = new Long[batch_size];
for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
ids[k] = offeringIds[j];
}
SearchCriteria<DiskOfferingJoinVO> sc = diskOfferingSearch.create();
sc.setParameters("idIN", ids);
List<DiskOfferingJoinVO> accounts = searchIncludingRemoved(sc, null, null, false);
if (accounts != null) {
uvList.addAll(accounts);
}
}
return uvList;
}
}

View File

@ -17,6 +17,7 @@
package com.cloud.api.query.dao;
import java.util.EnumSet;
import java.util.List;
import org.apache.cloudstack.api.ApiConstants.DomainDetails;
import org.apache.cloudstack.api.ResponseObject.ResponseView;
@ -35,4 +36,5 @@ public interface DomainJoinDao extends GenericDao<DomainJoinVO, Long> {
void setResourceLimits(DomainJoinVO domain, boolean isRootDomain, ResourceLimitAndCountResponse response);
List<DomainJoinVO> searchByIds(Long... domainIds);
}

View File

@ -16,6 +16,7 @@
// under the License.
package com.cloud.api.query.dao;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@ -29,6 +30,7 @@ import org.apache.cloudstack.api.ResponseObject.ResponseView;
import org.apache.cloudstack.api.response.DomainResponse;
import org.apache.cloudstack.api.response.ResourceLimitAndCountResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -47,10 +49,13 @@ public class DomainJoinDaoImpl extends GenericDaoBase<DomainJoinVO, Long> implem
public static final Logger s_logger = Logger.getLogger(DomainJoinDaoImpl.class);
private SearchBuilder<DomainJoinVO> domainIdSearch;
private SearchBuilder<DomainJoinVO> domainSearch;
@Inject
private AnnotationDao annotationDao;
@Inject
private ConfigurationDao configDao;
@Inject
private AccountManager accountManager;
protected DomainJoinDaoImpl() {
@ -59,6 +64,10 @@ public class DomainJoinDaoImpl extends GenericDaoBase<DomainJoinVO, Long> implem
domainIdSearch.and("id", domainIdSearch.entity().getId(), SearchCriteria.Op.EQ);
domainIdSearch.done();
domainSearch = createSearchBuilder();
domainSearch.and("idIN", domainSearch.entity().getId(), SearchCriteria.Op.IN);
domainSearch.done();
this._count = "select count(distinct id) from domain_view WHERE ";
}
@ -207,6 +216,50 @@ public class DomainJoinDaoImpl extends GenericDaoBase<DomainJoinVO, Long> implem
response.setSecondaryStorageAvailable(secondaryStorageAvail);
}
@Override
public List<DomainJoinVO> searchByIds(Long... domainIds) {
// set detail batch query size
int DETAILS_BATCH_SIZE = 2000;
String batchCfg = configDao.getValue("detail.batch.query.size");
if (batchCfg != null) {
DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
}
List<DomainJoinVO> uvList = new ArrayList<>();
// query details by batches
int curr_index = 0;
if (domainIds.length > DETAILS_BATCH_SIZE) {
while ((curr_index + DETAILS_BATCH_SIZE) <= domainIds.length) {
Long[] ids = new Long[DETAILS_BATCH_SIZE];
for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
ids[k] = domainIds[j];
}
SearchCriteria<DomainJoinVO> sc = domainSearch.create();
sc.setParameters("idIN", ids);
List<DomainJoinVO> domains = searchIncludingRemoved(sc, null, null, false);
if (domains != null) {
uvList.addAll(domains);
}
curr_index += DETAILS_BATCH_SIZE;
}
}
if (curr_index < domainIds.length) {
int batch_size = (domainIds.length - curr_index);
// set the ids value
Long[] ids = new Long[batch_size];
for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
ids[k] = domainIds[j];
}
SearchCriteria<DomainJoinVO> sc = domainSearch.create();
sc.setParameters("idIN", ids);
List<DomainJoinVO> domains = searchIncludingRemoved(sc, null, null, false);
if (domains != null) {
uvList.addAll(domains);
}
}
return uvList;
}
@Override
public DomainJoinVO newDomainView(Domain domain) {
SearchCriteria<DomainJoinVO> sc = domainIdSearch.create();

View File

@ -31,4 +31,6 @@ public interface ServiceOfferingJoinDao extends GenericDao<ServiceOfferingJoinVO
ServiceOfferingResponse newServiceOfferingResponse(ServiceOfferingJoinVO offering);
ServiceOfferingJoinVO newServiceOfferingView(ServiceOffering offering);
List<ServiceOfferingJoinVO> searchByIds(Long... id);
}

View File

@ -16,6 +16,7 @@
// under the License.
package com.cloud.api.query.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -28,6 +29,7 @@ import com.cloud.storage.DiskOfferingVO;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -50,10 +52,14 @@ public class ServiceOfferingJoinDaoImpl extends GenericDaoBase<ServiceOfferingJo
@Inject
private AnnotationDao annotationDao;
@Inject
private ConfigurationDao configDao;
@Inject
private AccountManager accountManager;
private SearchBuilder<ServiceOfferingJoinVO> sofIdSearch;
private SearchBuilder<ServiceOfferingJoinVO> srvOfferingSearch;
/**
* Constant used to convert GB into Bytes (or the other way around).
* GB * MB * KB = Bytes //
@ -67,6 +73,10 @@ public class ServiceOfferingJoinDaoImpl extends GenericDaoBase<ServiceOfferingJo
sofIdSearch.and("id", sofIdSearch.entity().getId(), SearchCriteria.Op.EQ);
sofIdSearch.done();
srvOfferingSearch = createSearchBuilder();
srvOfferingSearch.and("idIN", srvOfferingSearch.entity().getId(), SearchCriteria.Op.IN);
srvOfferingSearch.done();
this._count = "select count(distinct service_offering_view.id) from service_offering_view WHERE ";
}
@ -165,4 +175,48 @@ public class ServiceOfferingJoinDaoImpl extends GenericDaoBase<ServiceOfferingJo
assert offerings != null && offerings.size() == 1 : "No service offering found for offering id " + offering.getId();
return offerings.get(0);
}
@Override
public List<ServiceOfferingJoinVO> searchByIds(Long... offeringIds) {
// set detail batch query size
int DETAILS_BATCH_SIZE = 2000;
String batchCfg = configDao.getValue("detail.batch.query.size");
if (batchCfg != null) {
DETAILS_BATCH_SIZE = Integer.parseInt(batchCfg);
}
List<ServiceOfferingJoinVO> uvList = new ArrayList<>();
// query details by batches
int curr_index = 0;
if (offeringIds.length > DETAILS_BATCH_SIZE) {
while ((curr_index + DETAILS_BATCH_SIZE) <= offeringIds.length) {
Long[] ids = new Long[DETAILS_BATCH_SIZE];
for (int k = 0, j = curr_index; j < curr_index + DETAILS_BATCH_SIZE; j++, k++) {
ids[k] = offeringIds[j];
}
SearchCriteria<ServiceOfferingJoinVO> sc = srvOfferingSearch.create();
sc.setParameters("idIN", ids);
List<ServiceOfferingJoinVO> accounts = searchIncludingRemoved(sc, null, null, false);
if (accounts != null) {
uvList.addAll(accounts);
}
curr_index += DETAILS_BATCH_SIZE;
}
}
if (curr_index < offeringIds.length) {
int batch_size = (offeringIds.length - curr_index);
// set the ids value
Long[] ids = new Long[batch_size];
for (int k = 0, j = curr_index; j < curr_index + batch_size; j++, k++) {
ids[k] = offeringIds[j];
}
SearchCriteria<ServiceOfferingJoinVO> sc = srvOfferingSearch.create();
sc.setParameters("idIN", ids);
List<ServiceOfferingJoinVO> accounts = searchIncludingRemoved(sc, null, null, false);
if (accounts != null) {
uvList.addAll(accounts);
}
}
return uvList;
}
}

View File

@ -18,10 +18,6 @@ package com.cloud.api.query.dao;
import java.util.List;
import com.cloud.storage.ScopeType;
import com.cloud.storage.StoragePoolStatus;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Filter;
import org.apache.cloudstack.api.response.StoragePoolResponse;
import com.cloud.api.query.vo.StoragePoolJoinVO;
@ -42,6 +38,4 @@ public interface StoragePoolJoinDao extends GenericDao<StoragePoolJoinVO, Long>
List<StoragePoolJoinVO> searchByIds(Long... spIds);
Pair<List<StoragePoolJoinVO>, Integer> searchAndCount(Long storagePoolId, String storagePoolName, Long zoneId, String path, Long podId, Long clusterId, String address, ScopeType scopeType, StoragePoolStatus status, String keyword, Filter searchFilter);
}

View File

@ -40,15 +40,11 @@ import com.cloud.api.ApiDBUtils;
import com.cloud.api.query.vo.StoragePoolJoinVO;
import com.cloud.capacity.CapacityManager;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.ScopeType;
import com.cloud.storage.Storage;
import com.cloud.storage.StoragePool;
import com.cloud.storage.StoragePoolStatus;
import com.cloud.storage.StorageStats;
import com.cloud.user.AccountManager;
import com.cloud.utils.Pair;
import com.cloud.utils.StringUtils;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
@ -296,76 +292,4 @@ public class StoragePoolJoinDaoImpl extends GenericDaoBase<StoragePoolJoinVO, Lo
}
return uvList;
}
@Override
public Pair<List<StoragePoolJoinVO>, Integer> searchAndCount(Long storagePoolId, String storagePoolName, Long zoneId, String path, Long podId, Long clusterId, String address, ScopeType scopeType, StoragePoolStatus status, String keyword, Filter searchFilter) {
SearchCriteria<StoragePoolJoinVO> sc = createStoragePoolSearchCriteria(storagePoolId, storagePoolName, zoneId, path, podId, clusterId, address, scopeType, status, keyword);
return searchAndCount(sc, searchFilter);
}
private SearchCriteria<StoragePoolJoinVO> createStoragePoolSearchCriteria(Long storagePoolId, String storagePoolName, Long zoneId, String path, Long podId, Long clusterId, String address, ScopeType scopeType, StoragePoolStatus status, String keyword) {
SearchBuilder<StoragePoolJoinVO> sb = createSearchBuilder();
sb.select(null, SearchCriteria.Func.DISTINCT, sb.entity().getId()); // select distinct
// ids
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
sb.and("path", sb.entity().getPath(), SearchCriteria.Op.EQ);
sb.and("dataCenterId", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
sb.and("hostAddress", sb.entity().getHostAddress(), SearchCriteria.Op.EQ);
sb.and("scope", sb.entity().getScope(), SearchCriteria.Op.EQ);
sb.and("status", sb.entity().getStatus(), SearchCriteria.Op.EQ);
sb.and("parent", sb.entity().getParent(), SearchCriteria.Op.EQ);
SearchCriteria<StoragePoolJoinVO> sc = sb.create();
if (keyword != null) {
SearchCriteria<StoragePoolJoinVO> ssc = createSearchCriteria();
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("poolType", SearchCriteria.Op.LIKE, new Storage.StoragePoolType("%" + keyword + "%"));
sc.addAnd("name", SearchCriteria.Op.SC, ssc);
}
if (storagePoolId != null) {
sc.setParameters("id", storagePoolId);
}
if (storagePoolName != null) {
sc.setParameters("name", storagePoolName);
}
if (path != null) {
sc.setParameters("path", path);
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (podId != null) {
SearchCriteria<StoragePoolJoinVO> ssc = createSearchCriteria();
ssc.addOr("podId", SearchCriteria.Op.EQ, podId);
ssc.addOr("podId", SearchCriteria.Op.NULL);
sc.addAnd("podId", SearchCriteria.Op.SC, ssc);
}
if (address != null) {
sc.setParameters("hostAddress", address);
}
if (clusterId != null) {
SearchCriteria<StoragePoolJoinVO> ssc = createSearchCriteria();
ssc.addOr("clusterId", SearchCriteria.Op.EQ, clusterId);
ssc.addOr("clusterId", SearchCriteria.Op.NULL);
sc.addAnd("clusterId", SearchCriteria.Op.SC, ssc);
}
if (scopeType != null) {
sc.setParameters("scope", scopeType.toString());
}
if (status != null) {
sc.setParameters("status", status.toString());
}
sc.setParameters("parent", 0);
return sc;
}
}

View File

@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
@ -171,13 +172,17 @@ public class TemplateJoinDaoImpl extends GenericDaoBaseWithTagInformation<Templa
List<ImageStoreVO> storesInZone = dataStoreDao.listStoresByZoneId(template.getDataCenterId());
Long[] storeIds = storesInZone.stream().map(ImageStoreVO::getId).toArray(Long[]::new);
List<TemplateDataStoreVO> templatesInStore = _templateStoreDao.listByTemplateNotBypassed(template.getId(), storeIds);
List<Long> dataStoreIdList = templatesInStore.stream().map(TemplateDataStoreVO::getDataStoreId).collect(Collectors.toList());
Map<Long, ImageStoreVO> imageStoreMap = dataStoreDao.listByIds(dataStoreIdList).stream().collect(Collectors.toMap(ImageStoreVO::getId, imageStore -> imageStore));
List<Map<String, String>> downloadProgressDetails = new ArrayList<>();
HashMap<String, String> downloadDetailInImageStores = null;
for (TemplateDataStoreVO templateInStore : templatesInStore) {
downloadDetailInImageStores = new HashMap<>();
ImageStoreVO datastore = dataStoreDao.findById(templateInStore.getDataStoreId());
if (datastore != null) {
downloadDetailInImageStores.put("datastore", datastore.getName());
ImageStoreVO imageStore = imageStoreMap.get(templateInStore.getDataStoreId());
if (imageStore != null) {
downloadDetailInImageStores.put("datastore", imageStore.getName());
downloadDetailInImageStores.put("downloadPercent", Integer.toString(templateInStore.getDownloadPercent()));
downloadDetailInImageStores.put("downloadState", (templateInStore.getDownloadState() != null ? templateInStore.getDownloadState().toString() : ""));
downloadProgressDetails.add(downloadDetailInImageStores);

View File

@ -7387,7 +7387,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
networkRate = offering.getRateMbps();
} else {
// for domain router service offering, get network rate from
if (offering.getSystemVmType() != null && offering.getSystemVmType().equalsIgnoreCase(VirtualMachine.Type.DomainRouter.toString())) {
if (offering.getVmType() != null && offering.getVmType().equalsIgnoreCase(VirtualMachine.Type.DomainRouter.toString())) {
networkRate = NetworkOrchestrationService.NetworkThrottlingRate.valueIn(dataCenterId);
} else {
networkRate = Integer.parseInt(_configDao.getValue(Config.VmNetworkThrottlingRate.key()));

View File

@ -4153,9 +4153,9 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService, C
}
final String virtualMachineDomainRouterType = VirtualMachine.Type.DomainRouter.toString();
if (!virtualMachineDomainRouterType.equalsIgnoreCase(serviceOffering.getSystemVmType())) {
if (!virtualMachineDomainRouterType.equalsIgnoreCase(serviceOffering.getVmType())) {
throw new InvalidParameterValueException(String.format("The specified service offering [%s] is of type [%s]. Virtual routers can only be created with service offering "
+ "of type [%s].", serviceOffering, serviceOffering.getSystemVmType(), virtualMachineDomainRouterType.toLowerCase()));
+ "of type [%s].", serviceOffering, serviceOffering.getVmType(), virtualMachineDomainRouterType.toLowerCase()));
}
}

View File

@ -94,6 +94,33 @@ public class RoleManagerImpl extends ManagerBase implements RoleService, Configu
return RoleService.EnableDynamicApiChecker.value();
}
private boolean isCallerRootAdmin() {
return accountManager.isRootAdmin(getCurrentAccount().getId());
}
@Override
public List<Role> findRoles(List<Long> ids, boolean ignorePrivateRoles) {
List<Role> result = new ArrayList<>();
if (CollectionUtils.isEmpty(ids)) {
logger.trace(String.format("Role IDs are invalid [%s]", ids));
return result;
}
List<RoleVO> roles = roleDao.searchByIds(ids.toArray(new Long[0]));
if (CollectionUtils.isEmpty(roles)) {
logger.trace(String.format("Roles not found [ids=%s]", ids));
return result;
}
for (Role role : roles) {
if (!isCallerRootAdmin() && (RoleType.Admin == role.getRoleType() || ignorePrivateRoles)) {
logger.debug(String.format("Role [id=%s, name=%s] is either of 'Admin' type or is private and is only visible to 'Root admins'.", role.getId(), role.getName()));
continue;
}
result.add(role);
}
return result;
}
@Override
public Role findRole(Long id) {
if (id == null || id < 1L) {

View File

@ -23,6 +23,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import com.cloud.event.EventVO;
import com.cloud.event.dao.EventDao;
import org.apache.cloudstack.acl.SecurityChecker;
import org.apache.cloudstack.api.ApiCommandResourceType;
import org.apache.cloudstack.api.command.user.event.ListEventsCmd;
@ -73,6 +75,10 @@ public class QueryManagerImplTest {
EntityManager entityManager;
@Mock
AccountManager accountManager;
@Mock
EventDao eventDao;
@Mock
EventJoinDao eventJoinDao;
@Mock
@ -102,13 +108,12 @@ public class QueryManagerImplTest {
Mockito.any(Project.ListProjectResourcesCriteria.class));
Mockito.doNothing().when(accountManager).buildACLViewSearchCriteria(Mockito.any(), Mockito.anyLong(), Mockito.anyBoolean(), Mockito.anyList(),
Mockito.any(Project.ListProjectResourcesCriteria.class));
final SearchBuilder<EventJoinVO> searchBuilder = Mockito.mock(SearchBuilder.class);
final SearchCriteria<EventJoinVO> searchCriteria = Mockito.mock(SearchCriteria.class);
final EventJoinVO eventJoinVO = Mockito.mock(EventJoinVO.class);
when(searchBuilder.entity()).thenReturn(eventJoinVO);
when(searchBuilder.create()).thenReturn(searchCriteria);
Mockito.when(eventJoinDao.createSearchBuilder()).thenReturn(searchBuilder);
Mockito.when(eventJoinDao.createSearchCriteria()).thenReturn(searchCriteria);
final SearchBuilder<EventVO> eventSearchBuilder = Mockito.mock(SearchBuilder.class);
final SearchCriteria<EventVO> eventSearchCriteria = Mockito.mock(SearchCriteria.class);
final EventVO eventVO = Mockito.mock(EventVO.class);
when(eventSearchBuilder.entity()).thenReturn(eventVO);
when(eventSearchBuilder.create()).thenReturn(eventSearchCriteria);
Mockito.when(eventDao.createSearchBuilder()).thenReturn(eventSearchBuilder);
}
private ListEventsCmd setupMockListEventsCmd() {
@ -124,19 +129,24 @@ public class QueryManagerImplTest {
String uuid = UUID.randomUUID().toString();
Mockito.when(cmd.getResourceId()).thenReturn(uuid);
Mockito.when(cmd.getResourceType()).thenReturn(ApiCommandResourceType.Network.toString());
List<EventJoinVO> events = new ArrayList<>();
events.add(Mockito.mock(EventJoinVO.class));
events.add(Mockito.mock(EventJoinVO.class));
events.add(Mockito.mock(EventJoinVO.class));
Pair<List<EventJoinVO>, Integer> pair = new Pair<>(events, events.size());
List<EventVO> events = new ArrayList<>();
events.add(Mockito.mock(EventVO.class));
events.add(Mockito.mock(EventVO.class));
events.add(Mockito.mock(EventVO.class));
Pair<List<EventVO>, Integer> pair = new Pair<>(events, events.size());
List<EventJoinVO> eventJoins = new ArrayList<>();
eventJoins.add(Mockito.mock(EventJoinVO.class));
eventJoins.add(Mockito.mock(EventJoinVO.class));
eventJoins.add(Mockito.mock(EventJoinVO.class));
NetworkVO network = Mockito.mock(NetworkVO.class);
Mockito.when(network.getId()).thenReturn(1L);
Mockito.when(network.getAccountId()).thenReturn(account.getId());
Mockito.when(entityManager.findByUuidIncludingRemoved(Network.class, uuid)).thenReturn(network);
Mockito.doNothing().when(accountManager).checkAccess(account, SecurityChecker.AccessType.ListEntry, true, network);
Mockito.when(eventJoinDao.searchAndCount(Mockito.any(), Mockito.any(Filter.class))).thenReturn(pair);
Mockito.when(eventDao.searchAndCount(Mockito.any(), Mockito.any(Filter.class))).thenReturn(pair);
List<EventResponse> respList = new ArrayList<EventResponse>();
for (EventJoinVO vt : events) {
for (EventJoinVO vt : eventJoins) {
respList.add(eventJoinDao.newEventResponse(vt));
}
PowerMockito.mockStatic(ViewResponseHelper.class);

View File

@ -753,10 +753,10 @@ public class NetworkServiceImplTest {
public void validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouterTestMustThrowInvalidParameterValueExceptionWhenSystemVmTypeIsNotDomainRouter() {
doReturn(serviceOfferingVoMock).when(serviceOfferingDaoMock).findById(anyLong());
doReturn(ServiceOffering.State.Active).when(serviceOfferingVoMock).getState();
doReturn(VirtualMachine.Type.ElasticLoadBalancerVm.toString()).when(serviceOfferingVoMock).getSystemVmType();
doReturn(VirtualMachine.Type.ElasticLoadBalancerVm.toString()).when(serviceOfferingVoMock).getVmType();
String expectedMessage = String.format("The specified service offering [%s] is of type [%s]. Virtual routers can only be created with service offering of type [%s].",
serviceOfferingVoMock, serviceOfferingVoMock.getSystemVmType(), VirtualMachine.Type.DomainRouter.toString().toLowerCase());
serviceOfferingVoMock, serviceOfferingVoMock.getVmType(), VirtualMachine.Type.DomainRouter.toString().toLowerCase());
InvalidParameterValueException assertThrows = Assert.assertThrows(expectedException, () -> {
service.validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter(1l);
});

View File

@ -293,6 +293,11 @@ public class MockUsageEventDao implements UsageEventDao{
return null;
}
@Override
public List<UsageEventVO> findByUuids(String... uuids) {
return null;
}
@Override
public List<UsageEventVO> listLatestEvents(Date endDate) {
return null;