[Simulator] Add API to configure simualtor: you can configure which command can be simulated, or how long it will wait for each command, for which zone/pod/cluster/host etc.

e.g: command=configuresimulator&name=SecurityIngressRulesCmd&zoneid=1&value=enabled:true|timeout=30, means enable command SecurityIngressRulesCmd for zone 1, and wait for 30 seconds.
This commit is contained in:
Edison Su 2011-09-30 18:16:25 -07:00
parent 4ef20e91ed
commit eacb9dde26
5 changed files with 78 additions and 19 deletions

View File

@ -136,15 +136,26 @@ public class SimulatorManagerImpl implements SimulatorManager {
try {
MockHost host = _mockHost.findByGuid(hostGuid);
MockConfigurationVO config = _mockConfigDao.findByCommand(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), cmd.toString());
if (config == null) {
config = _mockConfigDao.findByGlobal(cmd.toString());
String cmdName = cmd.toString();
int index = cmdName.lastIndexOf(".");
if (index != -1) {
cmdName = cmdName.substring(index + 1);
}
MockConfigurationVO config = _mockConfigDao.findByNameBottomUP(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), cmdName);
if (config != null) {
Map<String, String> configParameters = config.getParameters();
if (configParameters.get("enabled").equalsIgnoreCase("false")) {
if ("false".equalsIgnoreCase(configParameters.get("enabled"))) {
return new Answer(cmd, false, "cmd is disabled");
} else if (configParameters.get("timeout") != null) {
try {
int timeout = Integer.valueOf(configParameters.get("timeout"));
Thread.sleep(timeout * 1000);
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
} catch (InterruptedException e) {
s_logger.debug("thread is interrupted: " + e.toString());
}
}
}
@ -257,14 +268,20 @@ public class SimulatorManagerImpl implements SimulatorManager {
@Override
public boolean configureSimulator(Long zoneId, Long podId, Long clusterId, Long hostId, String command, String values) {
MockConfigurationVO config = new MockConfigurationVO();
config.setClusterId(clusterId);
config.setDataCenterId(zoneId);
config.setPodId(podId);
config.setHostId(hostId);
config.setName(command);
config.setValues(values);
_mockConfigDao.persist(config);
MockConfigurationVO config = _mockConfigDao.findByCommand(zoneId, podId, clusterId, hostId, command);
if (config == null) {
config = new MockConfigurationVO();
config.setClusterId(clusterId);
config.setDataCenterId(zoneId);
config.setPodId(podId);
config.setHostId(hostId);
config.setName(command);
config.setValues(values);
_mockConfigDao.persist(config);
} else {
config.setValues(values);
_mockConfigDao.update(config.getId(), config);
}
return true;
}

View File

@ -5,9 +5,12 @@ import org.apache.log4j.Logger;
import com.cloud.agent.manager.SimulatorManager;
import com.cloud.api.ApiConstants;
import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.repsonse.ConfigureSimulatorResponse;
import com.cloud.api.response.BaseResponse;
import com.cloud.api.response.SuccessResponse;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.ResourceAllocationException;
@ -16,6 +19,7 @@ import com.cloud.server.ManagementService;
import com.cloud.user.Account;
import com.cloud.utils.component.ComponentLocator;
@Implementation(description="configure simulator", responseObject=SuccessResponse.class)
public class ConfigureSimulatorCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(ConfigureSimulatorCmd.class.getName());
private static final String s_name = "configuresimulatorresponse";
@ -47,8 +51,7 @@ public class ConfigureSimulatorCmd extends BaseCmd {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to configure simulator");
}
BaseResponse response = new BaseResponse();
response.setResponseName(getCommandName());
SuccessResponse response = new SuccessResponse(getCommandName());
this.setResponseObject(response);
}

View File

@ -90,9 +90,9 @@ public class MockConfigurationVO {
return maps;
}
String[] vals = this.values.split(";");
String[] vals = this.values.split("\\|");
for (String val : vals) {
String[] paras = val.split("=");
String[] paras = val.split(":");
maps.put(paras[0], paras[1]);
}
return maps;

View File

@ -6,5 +6,6 @@ import com.cloud.utils.db.GenericDao;
public interface MockConfigurationDao extends GenericDao<MockConfigurationVO, Long> {
MockConfigurationVO findByCommand(Long dcId, Long podId, Long clusterId, Long hostId, String name);
MockConfigurationVO findByGlobal(String name);
MockConfigurationVO findByNameBottomUP(Long dcId, Long podId,
Long clusterId, Long hostId, String name);
}

View File

@ -1,11 +1,19 @@
package com.cloud.simulator.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import javax.ejb.Local;
import com.cloud.simulator.MockConfigurationVO;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
@Local(value={MockConfigurationDao.class})
public class MockConfigurationDaoImpl extends GenericDaoBase<MockConfigurationVO, Long> implements MockConfigurationDao {
@ -14,21 +22,30 @@ public class MockConfigurationDaoImpl extends GenericDaoBase<MockConfigurationVO
private SearchBuilder<MockConfigurationVO> _searchByDcIDPodIdClusterIdName;
private SearchBuilder<MockConfigurationVO> _searchByDcIDPodIdClusterIdHostIdName;
private SearchBuilder<MockConfigurationVO> _searchByGlobalName;
public MockConfigurationDaoImpl() {
_searchByGlobalName = createSearchBuilder();
_searchByGlobalName.and("dcId", _searchByGlobalName.entity().getDataCenterId(), SearchCriteria.Op.NULL);
_searchByGlobalName.and("podId", _searchByGlobalName.entity().getPodId(), SearchCriteria.Op.NULL);
_searchByGlobalName.and("clusterId", _searchByGlobalName.entity().getClusterId(), SearchCriteria.Op.NULL);
_searchByGlobalName.and("hostId", _searchByGlobalName.entity().getHostId(), SearchCriteria.Op.NULL);
_searchByGlobalName.and("name", _searchByGlobalName.entity().getName(), SearchCriteria.Op.EQ);
_searchByGlobalName.done();
_searchByDcIdName = createSearchBuilder();
_searchByDcIdName.and("dcId", _searchByDcIdName.entity().getDataCenterId(), SearchCriteria.Op.EQ);
_searchByDcIdName.and("podId", _searchByDcIdName.entity().getPodId(), SearchCriteria.Op.NULL);
_searchByDcIdName.and("clusterId", _searchByDcIdName.entity().getClusterId(), SearchCriteria.Op.NULL);
_searchByDcIdName.and("hostId", _searchByDcIdName.entity().getHostId(), SearchCriteria.Op.NULL);
_searchByDcIdName.and("name", _searchByDcIdName.entity().getName(), SearchCriteria.Op.EQ);
_searchByDcIdName.done();
_searchByDcIDPodIdName = createSearchBuilder();
_searchByDcIDPodIdName.and("dcId", _searchByDcIDPodIdName.entity().getDataCenterId(), SearchCriteria.Op.EQ);
_searchByDcIDPodIdName.and("podId", _searchByDcIDPodIdName.entity().getPodId(), SearchCriteria.Op.EQ);
_searchByDcIDPodIdName.and("clusterId", _searchByDcIDPodIdName.entity().getClusterId(), SearchCriteria.Op.NULL);
_searchByDcIDPodIdName.and("hostId", _searchByDcIDPodIdName.entity().getHostId(), SearchCriteria.Op.NULL);
_searchByDcIDPodIdName.and("name", _searchByDcIDPodIdName.entity().getName(), SearchCriteria.Op.EQ);
_searchByDcIDPodIdName.done();
@ -36,6 +53,7 @@ public class MockConfigurationDaoImpl extends GenericDaoBase<MockConfigurationVO
_searchByDcIDPodIdClusterIdName.and("dcId", _searchByDcIDPodIdClusterIdName.entity().getDataCenterId(), SearchCriteria.Op.EQ);
_searchByDcIDPodIdClusterIdName.and("podId", _searchByDcIDPodIdClusterIdName.entity().getPodId(), SearchCriteria.Op.EQ);
_searchByDcIDPodIdClusterIdName.and("clusterId", _searchByDcIDPodIdClusterIdName.entity().getClusterId(), SearchCriteria.Op.EQ);
_searchByDcIDPodIdClusterIdName.and("hostId", _searchByDcIDPodIdClusterIdName.entity().getHostId(), SearchCriteria.Op.NULL);
_searchByDcIDPodIdClusterIdName.and("name", _searchByDcIDPodIdClusterIdName.entity().getName(), SearchCriteria.Op.EQ);
_searchByDcIDPodIdClusterIdName.done();
@ -84,8 +102,28 @@ public class MockConfigurationDaoImpl extends GenericDaoBase<MockConfigurationVO
}
@Override
public MockConfigurationVO findByGlobal(String name) {
return this.findByCommand(null, null, null, null, name);
public MockConfigurationVO findByNameBottomUP(Long dcId, Long podId, Long clusterId, Long hostId, String name) {
Transaction txn = Transaction.currentTxn();
StringBuilder search = new StringBuilder();
Formatter formatter = new Formatter(search);
formatter.format("select * from mockconfiguration where (name='%s') and ((data_center_id = %d and pod_id = %d and cluster_id = %d and host_id = %d)", name, dcId, podId, clusterId, hostId);
formatter.format(" or (data_center_id = %d and pod_id = %d and cluster_id = %d and host_id is null)", dcId, podId, clusterId);
formatter.format(" or (data_center_id = %d and pod_id = %d and cluster_id is null and host_id is null)", dcId, podId);
formatter.format(" or (data_center_id = %d and pod_id is null and cluster_id is null and host_id is null)", dcId);
formatter.format(" or (data_center_id is null and pod_id is null and cluster_id is null and host_id is null)) LIMIT 1");
PreparedStatement pstmt = null;
try {
String sql = search.toString();
pstmt = txn.prepareAutoCloseStatement(sql);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return toEntityBean(rs, false);
}
} catch (Exception e) {
}
return null;
}
}