use of com.cloud.utils.db.Filter in project cloudstack by apache.
the class NetworkACLServiceImpl method listNetworkACLItems.
@Override
public Pair<List<? extends NetworkACLItem>, Integer> listNetworkACLItems(final ListNetworkACLsCmd cmd) {
final Long networkId = cmd.getNetworkId();
final Long id = cmd.getId();
Long aclId = cmd.getAclId();
final String trafficType = cmd.getTrafficType();
final String protocol = cmd.getProtocol();
final String action = cmd.getAction();
final Map<String, String> tags = cmd.getTags();
final Account caller = CallContext.current().getCallingAccount();
final Filter filter = new Filter(NetworkACLItemVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal());
final SearchBuilder<NetworkACLItemVO> sb = _networkACLItemDao.createSearchBuilder();
sb.and("id", sb.entity().getId(), Op.EQ);
sb.and("aclId", sb.entity().getAclId(), Op.EQ);
sb.and("trafficType", sb.entity().getTrafficType(), Op.EQ);
sb.and("protocol", sb.entity().getProtocol(), Op.EQ);
sb.and("action", sb.entity().getAction(), Op.EQ);
if (tags != null && !tags.isEmpty()) {
final SearchBuilder<ResourceTagVO> tagSearch = _resourceTagDao.createSearchBuilder();
for (int count = 0; count < tags.size(); count++) {
tagSearch.or().op("key" + String.valueOf(count), tagSearch.entity().getKey(), Op.EQ);
tagSearch.and("value" + String.valueOf(count), tagSearch.entity().getValue(), Op.EQ);
tagSearch.cp();
}
tagSearch.and("resourceType", tagSearch.entity().getResourceType(), Op.EQ);
sb.groupBy(sb.entity().getId());
sb.join("tagSearch", tagSearch, sb.entity().getId(), tagSearch.entity().getResourceId(), JoinBuilder.JoinType.INNER);
}
if (aclId == null) {
//Join with network_acl table when aclId is not specified to list acl_items within permitted VPCs
final SearchBuilder<NetworkACLVO> vpcSearch = _networkACLDao.createSearchBuilder();
vpcSearch.and("vpcId", vpcSearch.entity().getVpcId(), Op.IN);
sb.join("vpcSearch", vpcSearch, sb.entity().getAclId(), vpcSearch.entity().getId(), JoinBuilder.JoinType.INNER);
}
final SearchCriteria<NetworkACLItemVO> sc = sb.create();
if (id != null) {
sc.setParameters("id", id);
}
if (networkId != null) {
final Network network = _networkDao.findById(networkId);
aclId = network.getNetworkACLId();
if (aclId == null) {
//Return empty list
return new Pair(new ArrayList<NetworkACLItem>(), 0);
}
}
if (trafficType != null) {
sc.setParameters("trafficType", trafficType);
}
if (aclId != null) {
// Get VPC and check access
final NetworkACL acl = _networkACLDao.findById(aclId);
if (acl.getVpcId() != 0) {
final Vpc vpc = _vpcDao.findById(acl.getVpcId());
if (vpc == null) {
throw new InvalidParameterValueException("Unable to find VPC associated with acl");
}
_accountMgr.checkAccess(caller, null, true, vpc);
}
sc.setParameters("aclId", aclId);
} else {
//ToDo: Add accountId to network_acl_item table for permission check
// aclId is not specified
// List permitted VPCs and filter aclItems
final List<Long> permittedAccounts = new ArrayList<Long>();
Long domainId = cmd.getDomainId();
boolean isRecursive = cmd.isRecursive();
final String accountName = cmd.getAccountName();
final Long projectId = cmd.getProjectId();
final boolean listAll = cmd.listAll();
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(domainId, isRecursive, null);
_accountMgr.buildACLSearchParameters(caller, id, accountName, projectId, permittedAccounts, domainIdRecursiveListProject, listAll, false);
domainId = domainIdRecursiveListProject.first();
isRecursive = domainIdRecursiveListProject.second();
final ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
final SearchBuilder<VpcVO> sbVpc = _vpcDao.createSearchBuilder();
_accountMgr.buildACLSearchBuilder(sbVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
final SearchCriteria<VpcVO> scVpc = sbVpc.create();
_accountMgr.buildACLSearchCriteria(scVpc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
final List<VpcVO> vpcs = _vpcDao.search(scVpc, null);
final List<Long> vpcIds = new ArrayList<Long>();
for (final VpcVO vpc : vpcs) {
vpcIds.add(vpc.getId());
}
//Add vpc_id 0 to list acl_items in default ACL
vpcIds.add(0L);
sc.setJoinParameters("vpcSearch", "vpcId", vpcIds.toArray());
}
if (protocol != null) {
sc.setParameters("protocol", protocol);
}
if (action != null) {
sc.setParameters("action", action);
}
if (tags != null && !tags.isEmpty()) {
int count = 0;
sc.setJoinParameters("tagSearch", "resourceType", ResourceObjectType.NetworkACL.toString());
for (final String key : tags.keySet()) {
sc.setJoinParameters("tagSearch", "key" + String.valueOf(count), key);
sc.setJoinParameters("tagSearch", "value" + String.valueOf(count), tags.get(key));
count++;
}
}
final Pair<List<NetworkACLItemVO>, Integer> result = _networkACLItemDao.searchAndCount(sc, filter);
final List<NetworkACLItemVO> aclItemVOs = result.first();
for (final NetworkACLItemVO item : aclItemVOs) {
_networkACLItemDao.loadCidrs(item);
}
return new Pair<List<? extends NetworkACLItem>, Integer>(aclItemVOs, result.second());
}
use of com.cloud.utils.db.Filter in project cloudstack by apache.
the class SecurityGroupManagerImpl method listSecurityGroupRulesByVM.
private Pair<List<SecurityGroupJoinVO>, Integer> listSecurityGroupRulesByVM(long vmId, long pageInd, long pageSize) {
Filter sf = new Filter(SecurityGroupVMMapVO.class, null, true, pageInd, pageSize);
Pair<List<SecurityGroupVMMapVO>, Integer> sgVmMappingPair = _securityGroupVMMapDao.listByInstanceId(vmId, sf);
Integer count = sgVmMappingPair.second();
if (count.intValue() == 0) {
// handle empty result cases
return new Pair<List<SecurityGroupJoinVO>, Integer>(new ArrayList<SecurityGroupJoinVO>(), count);
}
List<SecurityGroupVMMapVO> sgVmMappings = sgVmMappingPair.first();
Long[] sgIds = new Long[sgVmMappings.size()];
int i = 0;
for (SecurityGroupVMMapVO sgVm : sgVmMappings) {
sgIds[i++] = sgVm.getSecurityGroupId();
}
List<SecurityGroupJoinVO> sgs = _securityGroupJoinDao.searchByIds(sgIds);
return new Pair<List<SecurityGroupJoinVO>, Integer>(sgs, count);
}
use of com.cloud.utils.db.Filter in project cloudstack by apache.
the class VersionDaoImpl method getCurrentVersion.
@Override
@DB
public String getCurrentVersion() {
try (Connection conn = TransactionLegacy.getStandaloneConnection()) {
s_logger.debug("Checking to see if the database is at a version before it was the version table is created");
try (PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'version'");
ResultSet rs = pstmt.executeQuery()) {
if (!rs.next()) {
try (PreparedStatement pstmt_nics = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
ResultSet rs_nics = pstmt_nics.executeQuery()) {
if (!rs_nics.next()) {
try (PreparedStatement pstmt_domain = conn.prepareStatement("SELECT domain_id FROM account_vlan_map LIMIT 1")) {
pstmt_domain.executeQuery();
return "2.1.8";
} catch (final SQLException e) {
s_logger.debug("Assuming the exception means domain_id is not there.");
s_logger.debug("No version table and no nics table, returning 2.1.7");
return "2.1.7";
}
} else {
try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT is_static_nat from firewall_rules");
ResultSet rs_static_nat = pstmt_static_nat.executeQuery()) {
return "2.2.1";
} catch (final SQLException e) {
s_logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
return "2.2.2";
}
}
}
}
} catch (final SQLException e) {
throw new CloudRuntimeException("Unable to get the current version", e);
}
SearchCriteria<String> sc = CurrentVersionSearch.create();
sc.setParameters("step", Step.Complete);
Filter filter = new Filter(VersionVO.class, "id", false, 0l, 1l);
final List<String> upgradedVersions = customSearch(sc, filter);
if (upgradedVersions.isEmpty()) {
// Check if there are records in Version table
filter = new Filter(VersionVO.class, "id", false, 0l, 1l);
sc = CurrentVersionSearch.create();
final List<String> vers = customSearch(sc, filter);
if (!vers.isEmpty()) {
throw new CloudRuntimeException("Version table contains records for which upgrade wasn't completed");
}
// Use nics table information and is_static_nat field from firewall_rules table to determine version information
s_logger.debug("Version table exists, but it's empty; have to confirm that version is 2.2.2");
try (PreparedStatement pstmt = conn.prepareStatement("SHOW TABLES LIKE 'nics'");
ResultSet rs = pstmt.executeQuery()) {
if (!rs.next()) {
throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, nics table doesn't exist");
} else {
try (PreparedStatement pstmt_static_nat = conn.prepareStatement("SELECT is_static_nat from firewall_rules")) {
pstmt_static_nat.executeQuery();
throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, " + "nics table doesn't exist, is_static_nat field exists in firewall_rules table");
} catch (final SQLException e) {
s_logger.debug("Assuming the exception means static_nat field doesn't exist in firewall_rules table, returning version 2.2.2");
return "2.2.2";
}
}
} catch (final SQLException e) {
throw new CloudRuntimeException("Unable to determine the current version, version table exists and empty, query for nics table yields SQL exception", e);
}
} else {
return upgradedVersions.get(0);
}
} catch (final SQLException e) {
throw new CloudRuntimeException("Unable to get the current version", e);
}
}
use of com.cloud.utils.db.Filter in project cloudstack by apache.
the class UsageJobDaoImpl method getLastHeartbeat.
@Override
public Date getLastHeartbeat() {
Filter filter = new Filter(UsageJobVO.class, "heartbeat", false, Long.valueOf(0), Long.valueOf(1));
SearchCriteria<UsageJobVO> sc = createSearchCriteria();
List<UsageJobVO> jobs = search(sc, filter);
if ((jobs == null) || jobs.isEmpty()) {
return null;
}
return jobs.get(0).getHeartbeat();
}
use of com.cloud.utils.db.Filter in project cloudstack by apache.
the class UsageJobDaoImpl method getNextRecurringJob.
private UsageJobVO getNextRecurringJob() {
Filter filter = new Filter(UsageJobVO.class, "id", false, Long.valueOf(0), Long.valueOf(1));
SearchCriteria<UsageJobVO> sc = createSearchCriteria();
sc.addAnd("endMillis", SearchCriteria.Op.EQ, Long.valueOf(0));
sc.addAnd("jobType", SearchCriteria.Op.EQ, Integer.valueOf(UsageJobVO.JOB_TYPE_RECURRING));
List<UsageJobVO> jobs = search(sc, filter);
if ((jobs == null) || jobs.isEmpty()) {
return null;
}
return jobs.get(0);
}
Aggregations