use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class DataCenterDaoImpl method allocatePrivateIpAddress.
@Override
public Pair<String, Long> allocatePrivateIpAddress(final long dcId, final long podId, final long instanceId, final String reservationId) {
_ipAllocDao.releaseIpAddress(instanceId);
final DataCenterIpAddressVO vo = _ipAllocDao.takeIpAddress(dcId, podId, instanceId, reservationId);
if (vo == null) {
return null;
}
return new Pair<>(vo.getIpAddress(), vo.getMacAddress());
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class CapacityDaoImpl method orderClustersByAggregateCapacity.
@Override
public Pair<List<Long>, Map<Long, Double>> orderClustersByAggregateCapacity(final long id, final short capacityTypeForOrdering, final boolean isZone) {
final TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
final List<Long> result = new ArrayList<>();
final Map<Long, Double> clusterCapacityMap = new HashMap<>();
final StringBuilder sql = new StringBuilder();
if (capacityTypeForOrdering != Capacity.CAPACITY_TYPE_CPU && capacityTypeForOrdering != Capacity.CAPACITY_TYPE_MEMORY) {
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_PART1);
} else {
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_OVERCOMMIT_CAPACITY_PART1);
}
if (isZone) {
sql.append(" data_center_id = ?");
} else {
sql.append(" pod_id = ?");
}
if (capacityTypeForOrdering != Capacity.CAPACITY_TYPE_CPU && capacityTypeForOrdering != Capacity.CAPACITY_TYPE_MEMORY) {
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_PART2);
} else {
sql.append(ORDER_CLUSTERS_BY_AGGREGATE_OVERCOMMIT_CAPACITY_PART2);
}
try {
pstmt = txn.prepareAutoCloseStatement(sql.toString());
pstmt.setLong(1, id);
pstmt.setShort(2, capacityTypeForOrdering);
if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU) {
pstmt.setString(3, "cpuOvercommitRatio");
} else if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
pstmt.setString(3, "memoryOvercommitRatio");
}
final ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
final Long clusterId = rs.getLong(1);
result.add(clusterId);
clusterCapacityMap.put(clusterId, rs.getDouble(2));
}
return new Pair<>(result, clusterCapacityMap);
} catch (final SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + sql, e);
}
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class GenericDaoBase method setField.
@DB()
protected void setField(final Object entity, final ResultSet rs, final ResultSetMetaData meta, final int index) throws SQLException {
Attribute attr = _allColumns.get(new Pair<>(meta.getTableName(index), meta.getColumnName(index)));
if (attr == null) {
// work around for mysql bug to return original table name instead of view name in db view case
final Table tbl = entity.getClass().getSuperclass().getAnnotation(Table.class);
if (tbl != null) {
attr = _allColumns.get(new Pair<>(tbl.name(), meta.getColumnLabel(index)));
}
}
assert (attr != null) : "How come I can't find " + meta.getCatalogName(index) + "." + meta.getColumnName(index);
setField(entity, attr.field, rs, index);
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class SqlGenerator method buildDiscriminatorClause.
/**
* buildDiscriminatorClause builds the join clause when there are multiple tables.
*
* @return
*/
public Pair<StringBuilder, Map<String, Object>> buildDiscriminatorClause() {
final StringBuilder sql = new StringBuilder();
final Map<String, Object> values = new HashMap<>();
for (final Class<?> table : _tables) {
final DiscriminatorValue dv = table.getAnnotation(DiscriminatorValue.class);
if (dv != null) {
final Class<?> parent = table.getSuperclass();
final String tableName = DbUtil.getTableName(parent);
final DiscriminatorColumn dc = parent.getAnnotation(DiscriminatorColumn.class);
assert (dc != null) : "Parent does not have discrminator column: " + parent.getName();
sql.append(tableName);
sql.append(".");
sql.append(dc.name()).append("=");
Object value = null;
if (dc.discriminatorType() == DiscriminatorType.INTEGER) {
sql.append(dv.value());
value = Integer.parseInt(dv.value());
} else if (dc.discriminatorType() == DiscriminatorType.CHAR) {
sql.append(dv.value());
value = dv.value().charAt(0);
} else if (dc.discriminatorType() == DiscriminatorType.STRING) {
String v = dv.value();
v = v.substring(0, v.length() < dc.length() ? v.length() : dc.length());
sql.append("'").append(v).append("'");
value = v;
}
values.put(dc.name(), value);
sql.append(" AND ");
}
}
return new Pair<>(sql, values);
}
use of com.cloud.legacymodel.utils.Pair in project cosmic by MissionCriticalCloud.
the class ApiServer method handleAsyncJobPublishEvent.
@MessageHandler(topic = AsyncJob.Topics.JOB_EVENT_PUBLISH)
private void handleAsyncJobPublishEvent(final String subject, final String senderAddress, final Object args) {
assert (args != null);
final Pair<AsyncJob, String> eventInfo = (Pair<AsyncJob, String>) args;
final AsyncJob job = eventInfo.first();
final String jobEvent = eventInfo.second();
if (s_logger.isTraceEnabled()) {
s_logger.trace("Handle asyjob publish event " + jobEvent);
}
final EventBus eventBus;
try {
eventBus = ComponentContext.getComponent(EventBus.class);
} catch (final NoSuchBeanDefinitionException nbe) {
// no provider is configured to provide events bus, so just return
return;
}
if (!job.getDispatcher().equalsIgnoreCase("ApiAsyncJobDispatcher")) {
return;
}
final User userJobOwner = _accountMgr.getUserIncludingRemoved(job.getUserId());
final Account jobOwner = _accountMgr.getAccount(userJobOwner.getAccountId());
// Get the event type from the cmdInfo json string
final String info = job.getCmdInfo();
String cmdEventType = "unknown";
if (info != null) {
final Type type = new TypeToken<Map<String, String>>() {
}.getType();
final Map<String, String> cmdInfo = ApiGsonHelper.getBuilder().create().fromJson(info, type);
final String eventTypeObj = cmdInfo.get("cmdEventType");
if (eventTypeObj != null) {
cmdEventType = eventTypeObj;
if (s_logger.isDebugEnabled()) {
s_logger.debug("Retrieved cmdEventType from job info: " + cmdEventType);
}
} else {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Unable to locate cmdEventType marker in job info. publish as unknown event");
}
}
}
// For some reason, the instanceType / instanceId are not abstract, which means we may get null values.
final String instanceType = job.getInstanceType() != null ? job.getInstanceType() : "unknown";
final String instanceUuid = job.getInstanceId() != null ? ApiDBUtils.findJobInstanceUuid(job) : "";
final Event event = new Event("management-server", EventCategory.ASYNC_JOB_CHANGE_EVENT.getName(), jobEvent, instanceType, instanceUuid);
final Map<String, String> eventDescription = new HashMap<>();
eventDescription.put("command", job.getCmd());
eventDescription.put("user", userJobOwner.getUuid());
eventDescription.put("account", jobOwner.getUuid());
eventDescription.put("processStatus", "" + job.getProcessStatus());
eventDescription.put("resultCode", "" + job.getResultCode());
eventDescription.put("instanceUuid", instanceUuid);
eventDescription.put("instanceType", instanceType);
eventDescription.put("commandEventType", cmdEventType);
eventDescription.put("jobId", job.getUuid());
eventDescription.put("jobResult", job.getResult());
eventDescription.put("cmdInfo", job.getCmdInfo());
eventDescription.put("status", "" + job.getStatus());
// If the event.accountinfo boolean value is set, get the human readable value for the username / domainname
final Map<String, String> configs = _configDao.getConfiguration("management-server", new HashMap<String, String>());
if (Boolean.valueOf(configs.get("event.accountinfo"))) {
final DomainVO domain = _domainDao.findById(jobOwner.getDomainId());
eventDescription.put("username", userJobOwner.getUsername());
eventDescription.put("accountname", jobOwner.getAccountName());
eventDescription.put("domainname", domain.getName());
}
event.setDescription(eventDescription);
try {
eventBus.publish(event);
} catch (final EventBusException evx) {
final String errMsg = "Failed to publish async job event on the the event bus.";
s_logger.warn(errMsg, evx);
}
}
Aggregations