}
@DB
@Override
public Answer simulate(Command cmd, String hostGuid) {
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.SIMULATOR_DB);
try {
MockHost host = _mockHost.findByGuid(hostGuid);
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);
SimulatorInfo info = new SimulatorInfo();
info.setHostUuid(hostGuid);
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);
} catch (NumberFormatException e) {
s_logger.debug("invalid timeout parameter: " + e.toString());
} catch (InterruptedException e) {
s_logger.debug("thread is interrupted: " + e.toString());
}
}
}
}
if (cmd instanceof GetHostStatsCommand) {
return _mockAgentMgr.getHostStatistic((GetHostStatsCommand) cmd);
} else if (cmd instanceof CheckHealthCommand) {
return _mockAgentMgr.checkHealth((CheckHealthCommand) cmd);
} else if (cmd instanceof PingTestCommand) {
return _mockAgentMgr.pingTest((PingTestCommand) cmd);
} else if (cmd instanceof PrepareForMigrationCommand) {
return _mockVmMgr.prepareForMigrate((PrepareForMigrationCommand) cmd);
} else if (cmd instanceof MigrateCommand) {
return _mockVmMgr.Migrate((MigrateCommand) cmd, info);
} else if (cmd instanceof StartCommand) {
return _mockVmMgr.startVM((StartCommand) cmd, info);
} else if (cmd instanceof CheckSshCommand) {
return _mockVmMgr.checkSshCommand((CheckSshCommand) cmd);
} else if (cmd instanceof CheckVirtualMachineCommand) {
return _mockVmMgr.checkVmState((CheckVirtualMachineCommand) cmd);
} else if (cmd instanceof SetStaticNatRulesCommand) {
return _mockNetworkMgr.SetStaticNatRules((SetStaticNatRulesCommand) cmd);
} else if (cmd instanceof SetFirewallRulesCommand) {
return _mockNetworkMgr.SetFirewallRules((SetFirewallRulesCommand) cmd);
} else if (cmd instanceof SetPortForwardingRulesCommand) {
return _mockNetworkMgr.SetPortForwardingRules((SetPortForwardingRulesCommand) cmd);
} else if (cmd instanceof NetworkUsageCommand) {
return _mockNetworkMgr.getNetworkUsage((NetworkUsageCommand) cmd);
} else if (cmd instanceof IpAssocCommand) {
return _mockNetworkMgr.IpAssoc((IpAssocCommand) cmd);
} else if (cmd instanceof LoadBalancerConfigCommand) {
return _mockNetworkMgr.LoadBalancerConfig((LoadBalancerConfigCommand) cmd);
} else if (cmd instanceof DhcpEntryCommand) {
return _mockNetworkMgr.AddDhcpEntry((DhcpEntryCommand) cmd);
} else if (cmd instanceof VmDataCommand) {
return _mockVmMgr.setVmData((VmDataCommand) cmd);
} else if (cmd instanceof CleanupNetworkRulesCmd) {
return _mockVmMgr.CleanupNetworkRules((CleanupNetworkRulesCmd) cmd, info);
} else if (cmd instanceof CheckNetworkCommand) {
return _mockAgentMgr.checkNetworkCommand((CheckNetworkCommand) cmd);
}else if (cmd instanceof StopCommand) {
return _mockVmMgr.stopVM((StopCommand)cmd);
} else if (cmd instanceof RebootCommand) {
return _mockVmMgr.rebootVM((RebootCommand) cmd);
} else if (cmd instanceof GetVncPortCommand) {
return _mockVmMgr.getVncPort((GetVncPortCommand)cmd);
} else if (cmd instanceof CheckConsoleProxyLoadCommand) {
return _mockVmMgr.CheckConsoleProxyLoad((CheckConsoleProxyLoadCommand)cmd);
} else if (cmd instanceof WatchConsoleProxyLoadCommand) {
return _mockVmMgr.WatchConsoleProxyLoad((WatchConsoleProxyLoadCommand)cmd);
} else if (cmd instanceof SecurityGroupRulesCmd) {
return _mockVmMgr.AddSecurityGroupRules((SecurityGroupRulesCmd)cmd, info);
} else if (cmd instanceof SavePasswordCommand) {
return _mockVmMgr.SavePassword((SavePasswordCommand)cmd);
} else if (cmd instanceof PrimaryStorageDownloadCommand) {
return _mockStorageMgr.primaryStorageDownload((PrimaryStorageDownloadCommand)cmd);
} else if (cmd instanceof CreateCommand) {
return _mockStorageMgr.createVolume((CreateCommand)cmd);
} else if (cmd instanceof AttachVolumeCommand) {
return _mockStorageMgr.AttachVolume((AttachVolumeCommand)cmd);
} else if (cmd instanceof AttachIsoCommand) {
return _mockStorageMgr.AttachIso((AttachIsoCommand)cmd);
} else if (cmd instanceof DeleteStoragePoolCommand) {
return _mockStorageMgr.DeleteStoragePool((DeleteStoragePoolCommand)cmd);
} else if (cmd instanceof ModifyStoragePoolCommand) {
return _mockStorageMgr.ModifyStoragePool((ModifyStoragePoolCommand)cmd);
} else if (cmd instanceof CreateStoragePoolCommand) {
return _mockStorageMgr.CreateStoragePool((CreateStoragePoolCommand)cmd);
} else if (cmd instanceof SecStorageSetupCommand) {
return _mockStorageMgr.SecStorageSetup((SecStorageSetupCommand)cmd);
} else if (cmd instanceof ListTemplateCommand) {
return _mockStorageMgr.ListTemplates((ListTemplateCommand)cmd);
} else if (cmd instanceof ListVolumeCommand) {
return _mockStorageMgr.ListVolumes((ListVolumeCommand)cmd);
} else if (cmd instanceof DestroyCommand) {
return _mockStorageMgr.Destroy((DestroyCommand)cmd);
} else if (cmd instanceof DownloadProgressCommand) {
return _mockStorageMgr.DownloadProcess((DownloadProgressCommand)cmd);
} else if (cmd instanceof DownloadCommand) {
return _mockStorageMgr.Download((DownloadCommand)cmd);
} else if (cmd instanceof GetStorageStatsCommand) {
return _mockStorageMgr.GetStorageStats((GetStorageStatsCommand)cmd);
} else if (cmd instanceof ManageSnapshotCommand) {
return _mockStorageMgr.ManageSnapshot((ManageSnapshotCommand)cmd);
} else if (cmd instanceof BackupSnapshotCommand) {
return _mockStorageMgr.BackupSnapshot((BackupSnapshotCommand)cmd, info);
} else if (cmd instanceof CreateVolumeFromSnapshotCommand) {
return _mockStorageMgr.CreateVolumeFromSnapshot((CreateVolumeFromSnapshotCommand)cmd);
} else if (cmd instanceof DeleteCommand) {
return _mockStorageMgr.Delete((DeleteCommand)cmd);
} else if (cmd instanceof SecStorageVMSetupCommand) {
return _mockStorageMgr.SecStorageVMSetup((SecStorageVMSetupCommand)cmd);
} else if (cmd instanceof CreatePrivateTemplateFromSnapshotCommand) {
return _mockStorageMgr.CreatePrivateTemplateFromSnapshot((CreatePrivateTemplateFromSnapshotCommand)cmd);
} else if (cmd instanceof ComputeChecksumCommand) {
return _mockStorageMgr.ComputeChecksum((ComputeChecksumCommand)cmd);
} else if (cmd instanceof CreatePrivateTemplateFromVolumeCommand) {
return _mockStorageMgr.CreatePrivateTemplateFromVolume((CreatePrivateTemplateFromVolumeCommand)cmd);
} else if (cmd instanceof MaintainCommand) {
return _mockAgentMgr.maintain((MaintainCommand)cmd);
} else if (cmd instanceof GetVmStatsCommand) {
return _mockVmMgr.getVmStats((GetVmStatsCommand)cmd);
} else if (cmd instanceof CheckRouterCommand) {
return _mockVmMgr.checkRouter((CheckRouterCommand) cmd);
} else if (cmd instanceof BumpUpPriorityCommand) {
return _mockVmMgr.bumpPriority((BumpUpPriorityCommand) cmd);
} else if (cmd instanceof GetDomRVersionCmd) {
return _mockVmMgr.getDomRVersion((GetDomRVersionCmd) cmd);
} else if (cmd instanceof ClusterSyncCommand) {
return new Answer(cmd);
} else if (cmd instanceof CopyVolumeCommand) {
return _mockStorageMgr.CopyVolume((CopyVolumeCommand) cmd);
} else if (cmd instanceof PlugNicCommand) {
return _mockNetworkMgr.plugNic((PlugNicCommand) cmd);
} else if (cmd instanceof UnPlugNicCommand) {
return _mockNetworkMgr.unplugNic((UnPlugNicCommand) cmd);
} else if (cmd instanceof IpAssocVpcCommand) {
return _mockNetworkMgr.ipAssoc((IpAssocVpcCommand) cmd);
} else if (cmd instanceof SetSourceNatCommand) {
return _mockNetworkMgr.setSourceNat((SetSourceNatCommand) cmd);
} else if (cmd instanceof SetNetworkACLCommand) {
return _mockNetworkMgr.setNetworkAcl((SetNetworkACLCommand) cmd);
} else if (cmd instanceof SetupGuestNetworkCommand) {
return _mockNetworkMgr.setUpGuestNetwork((SetupGuestNetworkCommand) cmd);
} else if (cmd instanceof SetPortForwardingRulesVpcCommand) {
return _mockNetworkMgr.setVpcPortForwards((SetPortForwardingRulesVpcCommand) cmd);
} else if (cmd instanceof SetStaticNatRulesCommand) {
return _mockNetworkMgr.setVPCStaticNatRules((SetStaticNatRulesCommand) cmd);
} else if (cmd instanceof SetStaticRouteCommand) {
return _mockNetworkMgr.setStaticRoute((SetStaticRouteCommand) cmd);
} else if (cmd instanceof Site2SiteVpnCfgCommand) {
return _mockNetworkMgr.siteToSiteVpn((Site2SiteVpnCfgCommand) cmd);
} else if (cmd instanceof CheckS2SVpnConnectionsCommand) {
return _mockNetworkMgr.checkSiteToSiteVpnConnection((CheckS2SVpnConnectionsCommand) cmd);
} else if (cmd instanceof CreateVMSnapshotCommand) {
return _mockVmMgr.createVmSnapshot((CreateVMSnapshotCommand) cmd);
} else if (cmd instanceof DeleteVMSnapshotCommand) {
return _mockVmMgr.deleteVmSnapshot((DeleteVMSnapshotCommand) cmd);
} else if (cmd instanceof RevertToVMSnapshotCommand) {
return _mockVmMgr.revertVmSnapshot((RevertToVMSnapshotCommand) cmd);
} else if (cmd instanceof NetworkRulesVmSecondaryIpCommand) {
return _mockVmMgr.plugSecondaryIp((NetworkRulesVmSecondaryIpCommand) cmd);
} else if (cmd instanceof ScaleVmCommand) {
return _mockVmMgr.scaleVm((ScaleVmCommand) cmd);
} else if (cmd instanceof PvlanSetupCommand) {
return _mockNetworkMgr.setupPVLAN((PvlanSetupCommand) cmd);
} else if (cmd instanceof StorageSubSystemCommand) {
return this.storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
} else if (cmd instanceof VpnUsersCfgCommand || cmd instanceof RemoteAccessVpnCfgCommand || cmd instanceof SetMonitorServiceCommand) {
return new Answer(cmd);
} else {
s_logger.error("Simulator does not implement command of type "+cmd.toString());
return Answer.createUnsupportedCommandAnswer(cmd);
}
} catch(Exception e) {
s_logger.error("Failed execute cmd: ", e);
txn.rollback();
return new Answer(cmd, false, e.toString());
} finally {
txn.close();
txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
txn.close();
}
}