Simulator: moving hypervisor simulator into plugin

Simulator just like any hypervisor should be a plugin.
resurrecting it to aid api refactoring tests. WIP

Signed-off-by: Prasanna Santhanam <tsp@apache.org>
This commit is contained in:
Prasanna Santhanam 2012-12-22 13:01:44 -08:00
parent 0b60dda1e6
commit d6083ce51d
50 changed files with 631 additions and 578 deletions

View File

@ -0,0 +1,45 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloudstack-plugins</artifactId>
<version>4.1.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-plugin-hypervisor-simulator</artifactId>
<version>4.1.0-SNAPSHOT</version>
<name>Apache CloudStack Plugin - Hypervisor Simulator</name>
<description>Simulator Hypervisor for Cloudstack</description>
<build>
<defaultGoal>install</defaultGoal>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
</build>
<dependencies>
<dependency>
<groupId>org.apache.cloudstack</groupId>
<artifactId>cloud-utils</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -57,7 +57,7 @@ public class MetricsCollector {
vmNames.removeAll(currentVMs); //old vms - current vms --> leave non-running vms;
for (String vm: vmNames) {
removeVM(vm);
removeVM(vm);
}
vmNames.clear();
@ -87,14 +87,14 @@ public class MetricsCollector {
newVMnames.remove(vmName);
vmNames.remove(vmName);
MockVmMetrics task = metricsMap.get(vmName);
if (task != null) {
task.stop();
boolean r1= task.getFuture().cancel(false);
metricsMap.remove(vmName);
s_logger.debug("removeVM: cancel returned " + r1 + " for VM " + vmName);
} else {
s_logger.warn("removeVM called for nonexistent VM " + vmName);
}
if (task != null) {
task.stop();
boolean r1= task.getFuture().cancel(false);
metricsMap.remove(vmName);
s_logger.debug("removeVM: cancel returned " + r1 + " for VM " + vmName);
} else {
s_logger.warn("removeVM called for nonexistent VM " + vmName);
}
}
public synchronized Set<String> getVMNames() {

View File

@ -202,4 +202,3 @@ public class MockVmMetrics implements Runnable {
this.stopped = true;
}
}

View File

@ -72,8 +72,8 @@ public class MockVmManagerImpl implements MockVmManager {
}
public String startVM(String vmName, NicTO[] nics,
int cpuHz, long ramSize,
String bootArgs, String hostGuid) {
int cpuHz, long ramSize,
String bootArgs, String hostGuid) {
Transaction txn = Transaction.open(Transaction.SIMULATOR_DB);
MockHost host = null;
@ -108,13 +108,13 @@ public class MockVmManagerImpl implements MockVmManager {
vm.setVncPort(vncPort);
vm.setHostId(host.getId());
if(vmName.startsWith("s-")) {
vm.setType("SecondaryStorageVm");
vm.setType("SecondaryStorageVm");
} else if (vmName.startsWith("v-")) {
vm.setType("ConsoleProxy");
vm.setType("ConsoleProxy");
} else if (vmName.startsWith("r-")) {
vm.setType("DomainRouter");
vm.setType("DomainRouter");
} else if (vmName.startsWith("i-")) {
vm.setType("User");
vm.setType("User");
}
txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
@ -133,18 +133,18 @@ public class MockVmManagerImpl implements MockVmManager {
if(vm.getState() == State.Stopped) {
vm.setState(State.Running);
txn = Transaction.open(Transaction.SIMULATOR_DB);
try {
txn.start();
_mockVmDao.update(vm.getId(), (MockVMVO)vm);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("unable to update vm " + vm.getName(), ex);
} finally {
txn.close();
try {
txn.start();
_mockVmDao.update(vm.getId(), (MockVMVO)vm);
txn.commit();
} catch (Exception ex) {
txn.rollback();
throw new CloudRuntimeException("unable to update vm " + vm.getName(), ex);
} finally {
txn.close();
txn = Transaction.open(Transaction.CLOUD_DB);
txn.close();
}
}
}
}
@ -370,7 +370,7 @@ public class MockVmManagerImpl implements MockVmManager {
@Override
public Answer SetFirewallRules(SetFirewallRulesCommand cmd) {
return new Answer(cmd);
return new Answer(cmd);
}
@ -519,13 +519,13 @@ public class MockVmManagerImpl implements MockVmManager {
@Override
public GetDomRVersionAnswer getDomRVersion(GetDomRVersionCmd cmd) {
return new GetDomRVersionAnswer(cmd, null, null, null);
return new GetDomRVersionAnswer(cmd, null, null, null);
}
@Override
public SecurityGroupRuleAnswer AddSecurityGroupRules(SecurityGroupRulesCmd cmd, SimulatorInfo info) {
if (!info.isEnabled()) {
return new SecurityGroupRuleAnswer(cmd, false, "Disabled", SecurityGroupRuleAnswer.FailureReason.CANNOT_BRIDGE_FIREWALL);
return new SecurityGroupRuleAnswer(cmd, false, "Disabled", SecurityGroupRuleAnswer.FailureReason.CANNOT_BRIDGE_FIREWALL);
}
Map<String, Ternary<String,Long, Long>> rules = _securityRules.get(info.getHostUuid());

View File

@ -23,7 +23,6 @@ import java.util.Map;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.StoragePoolInfo;
import com.cloud.agent.mockvm.MockVm;
import com.cloud.simulator.MockVMVO;
import com.cloud.utils.Pair;
import com.cloud.utils.component.Manager;

View File

@ -127,7 +127,7 @@ public class SimulatorManagerImpl implements SimulatorManager {
String cmdName = cmd.toString();
int index = cmdName.lastIndexOf(".");
if (index != -1) {
cmdName = cmdName.substring(index + 1);
cmdName = cmdName.substring(index + 1);
}
MockConfigurationVO config = _mockConfigDao.findByNameBottomUP(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), cmdName);
@ -137,24 +137,24 @@ public class SimulatorManagerImpl implements SimulatorManager {
if (config != null) {
Map<String, String> configParameters = config.getParameters();
for (Map.Entry<String, String> entry : configParameters.entrySet()) {
if (entry.getKey().equalsIgnoreCase("enabled")) {
info.setEnabled(Boolean.parseBoolean(entry.getValue()));
} else if (entry.getKey().equalsIgnoreCase("timeout")) {
try {
info.setTimeout(Integer.valueOf(entry.getValue()));
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
}
} else if (entry.getKey().equalsIgnoreCase("wait")) {
try {
int wait = Integer.valueOf(entry.getValue());
Thread.sleep(wait * 1000);
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
} catch (InterruptedException e) {
s_logger.debug("thread is interrupted: " + e.toString());
}
}
if (entry.getKey().equalsIgnoreCase("enabled")) {
info.setEnabled(Boolean.parseBoolean(entry.getValue()));
} else if (entry.getKey().equalsIgnoreCase("timeout")) {
try {
info.setTimeout(Integer.valueOf(entry.getValue()));
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
}
} else if (entry.getKey().equalsIgnoreCase("wait")) {
try {
int wait = Integer.valueOf(entry.getValue());
Thread.sleep(wait * 1000);
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
} catch (InterruptedException e) {
s_logger.debug("thread is interrupted: " + e.toString());
}
}
}
}
@ -165,7 +165,7 @@ public class SimulatorManagerImpl implements SimulatorManager {
} else if (cmd instanceof PingTestCommand) {
return _mockAgentMgr.pingTest((PingTestCommand)cmd);
} else if (cmd instanceof PrepareForMigrationCommand) {
return _mockAgentMgr.prepareForMigrate((PrepareForMigrationCommand)cmd);
return _mockAgentMgr.prepareForMigrate((PrepareForMigrationCommand)cmd);
} else if (cmd instanceof MigrateCommand) {
return _mockVmMgr.Migrate((MigrateCommand)cmd, info);
} else if (cmd instanceof StartCommand) {
@ -173,11 +173,11 @@ public class SimulatorManagerImpl implements SimulatorManager {
} else if (cmd instanceof CheckSshCommand) {
return _mockVmMgr.checkSshCommand((CheckSshCommand)cmd);
} else if (cmd instanceof CheckVirtualMachineCommand) {
return _mockVmMgr.checkVmState((CheckVirtualMachineCommand)cmd);
return _mockVmMgr.checkVmState((CheckVirtualMachineCommand)cmd);
} else if (cmd instanceof SetStaticNatRulesCommand) {
return _mockVmMgr.SetStaticNatRules((SetStaticNatRulesCommand)cmd);
} else if (cmd instanceof SetFirewallRulesCommand) {
return _mockVmMgr.SetFirewallRules((SetFirewallRulesCommand)cmd);
return _mockVmMgr.SetFirewallRules((SetFirewallRulesCommand)cmd);
} else if (cmd instanceof SetPortForwardingRulesCommand) {
return _mockVmMgr.SetPortForwardingRules((SetPortForwardingRulesCommand)cmd);
} else if (cmd instanceof NetworkUsageCommand) {
@ -193,7 +193,7 @@ public class SimulatorManagerImpl implements SimulatorManager {
} else if (cmd instanceof CleanupNetworkRulesCmd) {
return _mockVmMgr.CleanupNetworkRules((CleanupNetworkRulesCmd)cmd, info);
} else if (cmd instanceof CheckNetworkCommand) {
return _mockAgentMgr.checkNetworkCommand((CheckNetworkCommand) cmd);
return _mockAgentMgr.checkNetworkCommand((CheckNetworkCommand) cmd);
}else if (cmd instanceof StopCommand) {
return _mockVmMgr.stopVM((StopCommand)cmd);
} else if (cmd instanceof RebootCommand) {
@ -261,12 +261,12 @@ public class SimulatorManagerImpl implements SimulatorManager {
} else if (cmd instanceof BumpUpPriorityCommand) {
return _mockVmMgr.bumpPriority((BumpUpPriorityCommand) cmd);
} else if (cmd instanceof GetDomRVersionCmd) {
return _mockVmMgr.getDomRVersion((GetDomRVersionCmd) cmd);
return _mockVmMgr.getDomRVersion((GetDomRVersionCmd) cmd);
} else if (cmd instanceof ClusterSyncCommand) {
return new Answer(cmd);
//return new ClusterSyncAnswer(((ClusterSyncCommand) cmd).getClusterId(), this.getVmStates(hostGuid));
return new Answer(cmd);
//return new ClusterSyncAnswer(((ClusterSyncCommand) cmd).getClusterId(), this.getVmStates(hostGuid));
} else if (cmd instanceof CopyVolumeCommand) {
return _mockStorageMgr.CopyVolume((CopyVolumeCommand) cmd);
return _mockStorageMgr.CopyVolume((CopyVolumeCommand) cmd);
} else {
return Answer.createUnsupportedCommandAnswer(cmd);
}
@ -288,19 +288,19 @@ public class SimulatorManagerImpl implements SimulatorManager {
@Override
public Map<String, State> getVmStates(String hostGuid) {
return _mockVmMgr.getVmStates(hostGuid);
return _mockVmMgr.getVmStates(hostGuid);
}
@Override
public Map<String, MockVMVO> getVms(String hostGuid) {
return _mockVmMgr.getVms(hostGuid);
return _mockVmMgr.getVms(hostGuid);
}
@Override
public HashMap<String, Pair<Long, Long>> syncNetworkGroups(String hostGuid) {
SimulatorInfo info = new SimulatorInfo();
info.setHostUuid(hostGuid);
return _mockVmMgr.syncNetworkGroups(info);
SimulatorInfo info = new SimulatorInfo();
info.setHostUuid(hostGuid);
return _mockVmMgr.syncNetworkGroups(info);
}
@Override

View File

@ -111,10 +111,10 @@ public class AgentRoutingResource extends AgentStorageResource {
return null;
}
synchronized (_vms) {
if (_vms.size() == 0) {
//load vms state from database
_vms.putAll(_simMgr.getVmStates(hostGuid));
}
if (_vms.size() == 0) {
//load vms state from database
_vms.putAll(_simMgr.getVmStates(hostGuid));
}
}
final HashMap<String, State> newStates = sync();
HashMap<String, Pair<Long, Long>> nwGrpStates = _simMgr.syncNetworkGroups(hostGuid);
@ -131,10 +131,10 @@ public class AgentRoutingResource extends AgentStorageResource {
totalCpu = agentHost.getCpuCount() * agentHost.getCpuSpeed();
totalMem = agentHost.getMemorySize();
for (Map.Entry<String, MockVMVO> entry : vmsMaps.entrySet()) {
MockVMVO vm = entry.getValue();
usedCpu += vm.getCpu();
usedMem += vm.getMemory();
_runningVms.put(entry.getKey(), new Pair<Long, Long>(Long.valueOf(vm.getCpu()), vm.getMemory()));
MockVMVO vm = entry.getValue();
usedCpu += vm.getCpu();
usedMem += vm.getMemory();
_runningVms.put(entry.getKey(), new Pair<Long, Long>(Long.valueOf(vm.getCpu()), vm.getMemory()));
}
List<Object> info = getHostInfo();

View File

@ -73,14 +73,14 @@ public class AgentStorageResource extends AgentResourceBase implements Secondary
@Override
public Type getType() {
if(SecondaryStorageVm.Role.templateProcessor.toString().equals(_role))
return Host.Type.SecondaryStorage;
return Host.Type.SecondaryStorageCmdExecutor;
if(SecondaryStorageVm.Role.templateProcessor.toString().equals(_role))
return Host.Type.SecondaryStorage;
return Host.Type.SecondaryStorageCmdExecutor;
}
@Override
public StartupCommand[] initialize() {
StartupSecondaryStorageCommand cmd = new StartupSecondaryStorageCommand();
StartupSecondaryStorageCommand cmd = new StartupSecondaryStorageCommand();
cmd.setPrivateIpAddress(agentHost.getPrivateIpAddress());
cmd.setPrivateNetmask(agentHost.getPrivateNetMask());

View File

@ -208,18 +208,18 @@ public class SimulatorDiscoverer extends DiscovererBase implements Discoverer, L
}
private void associateTemplatesToZone(long hostId, long dcId){
VMTemplateZoneVO tmpltZone;
VMTemplateZoneVO tmpltZone;
List<VMTemplateVO> allTemplates = _vmTemplateDao.listAll();
for (VMTemplateVO vt: allTemplates){
if (vt.isCrossZones()) {
tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
if (tmpltZone == null) {
VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
_vmTemplateZoneDao.persist(vmTemplateZone);
}
}
}
List<VMTemplateVO> allTemplates = _vmTemplateDao.listAll();
for (VMTemplateVO vt: allTemplates){
if (vt.isCrossZones()) {
tmpltZone = _vmTemplateZoneDao.findByZoneTemplate(dcId, vt.getId());
if (tmpltZone == null) {
VMTemplateZoneVO vmTemplateZone = new VMTemplateZoneVO(dcId, vt.getId(), new Date());
_vmTemplateZoneDao.persist(vmTemplateZone);
}
}
}
}
@Override
@ -259,7 +259,7 @@ public class SimulatorDiscoverer extends DiscovererBase implements Discoverer, L
public void processConnect(HostVO host, StartupCommand cmd, boolean forRebalance) throws ConnectionException {
/*if(forRebalance)
return;
return;
if ( Host.Type.SecondaryStorage == host.getType() ) {
List<VMTemplateVO> tmplts = _vmTemplateDao.listAll();
for( VMTemplateVO tmplt : tmplts ) {
@ -329,7 +329,7 @@ public class SimulatorDiscoverer extends DiscovererBase implements Discoverer, L
@Override
public boolean stop() {
_resourceMgr.unregisterResourceStateAdapter(this.getClass().getSimpleName());
_resourceMgr.unregisterResourceStateAdapter(this.getClass().getSimpleName());
return super.stop();
}

View File

@ -48,7 +48,7 @@ public class SimulatorSecondaryDiscoverer extends SecondaryStorageDiscoverer imp
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
_agentMgr.registerForHostEvents(this, true, false, false);
_agentMgr.registerForHostEvents(this, true, false, false);
_resourceMgr.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
return super.configure(name, params);
}
@ -74,7 +74,7 @@ public class SimulatorSecondaryDiscoverer extends SecondaryStorageDiscoverer imp
//for detecting SSVM dispatch
StartupCommand firstCmd = startup[0];
if (!(firstCmd instanceof StartupSecondaryStorageCommand)) {
return null;
return null;
}
host.setType(com.cloud.host.Host.Type.SecondaryStorageVM);
@ -103,7 +103,7 @@ public class SimulatorSecondaryDiscoverer extends SecondaryStorageDiscoverer imp
@Override
public boolean stop() {
_resourceMgr.unregisterResourceStateAdapter(this.getClass().getSimpleName());
_resourceMgr.unregisterResourceStateAdapter(this.getClass().getSimpleName());
return true;
}

View File

@ -92,7 +92,7 @@ public class MockVMVO implements MockVm{
}
public String getType() {
return this.vmType;
return this.vmType;
}
public void setState(State state) {
@ -124,7 +124,7 @@ public class MockVMVO implements MockVm{
}
public void setType(String type) {
this.vmType = type;
this.vmType = type;
}
}

View File

@ -42,4 +42,3 @@ public interface MockVm {
public void setVncPort(int vncPort);
public long getId();
}

View File

@ -116,16 +116,16 @@ public class MockConfigurationDaoImpl extends GenericDaoBase<MockConfigurationVO
@Override
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");
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;
PreparedStatement pstmt = null;
try {
String sql = search.toString();
pstmt = txn.prepareAutoCloseStatement(sql);

View File

@ -38,6 +38,7 @@
<module>hypervisors/ovm</module>
<module>hypervisors/xen</module>
<module>hypervisors/kvm</module>
<module>hypervisors/simulator</module>
<module>network-elements/elastic-loadbalancer</module>
<module>network-elements/ovs</module>
<module>network-elements/nicira-nvp</module>
@ -124,6 +125,17 @@
<module>hypervisors/vmware</module>
</modules>
</profile>
<profile>
<id>kvm</id>
<activation>
<property>
<name>nonoss</name>
</property>
</activation>
<modules>
<module>hypervisors/simulator</module>
</modules>
</profile>
</profiles>
</project>