Some fixes in the simulator

1. Fixed JSON response deserialization. While creating a mock a JSON can be passed which will be deserialized into a response object and returned from agent layer.
For e.g. for a mock corresponding to StopCommand, a response like "{"com.cloud.agent.api.StopAnswer":{"result":false,"wait":0}}" can be passed.
2. Ability to mock PingCommand (returned as part of getCurrentStatus() agent method). As a part of this a mocked VM state report can be returned.
For e.g. {"com.cloud.agent.api.PingRoutingWithNwGroupsCommand":{"newGroupStates":{},"newStates":{},"_hostVmStateReport":{"v-2-VM":{"state":"PowerOn","host":"SimulatedAgent.e6df7732-69b2-429b-9b6a-3e24dddfa2e0"},"i-2-5-VM":{"state":"PowerOff","host":"SimulatedAgent.e6df7732-69b2-429b-9b6a-3e24dddfa2e0"}},"_gatewayAccessible":true,"_vnetAccessible":true,"hostType":"Routing","hostId":3,"contextMap":{},"wait":0}}
This commit is contained in:
Koushik Das 2014-07-01 14:54:28 +05:30
parent f45c9f9d2f
commit 4f5df54d5c
2 changed files with 51 additions and 7 deletions

View File

@ -16,6 +16,7 @@
// under the License.
package com.cloud.agent.manager;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@ -113,6 +114,7 @@ import com.cloud.api.commands.ConfigureSimulatorCmd;
import com.cloud.api.commands.QuerySimulatorMockCmd;
import com.cloud.agent.api.SecStorageFirewallCfgCommand;
import com.cloud.resource.SimulatorStorageProcessor;
import com.cloud.serializer.GsonHelper;
import com.cloud.simulator.MockConfigurationVO;
import com.cloud.simulator.MockHost;
import com.cloud.simulator.MockVMVO;
@ -128,11 +130,13 @@ import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.VirtualMachine.State;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
@Component
@Local(value = {SimulatorManager.class})
public class SimulatorManagerImpl extends ManagerBase implements SimulatorManager, PluggableService {
private static final Logger s_logger = Logger.getLogger(SimulatorManagerImpl.class);
private static final Gson s_gson = GsonHelper.getGson();
@Inject
MockVmManager _mockVmMgr;
@Inject
@ -252,16 +256,20 @@ public class SimulatorManagerImpl extends ManagerBase implements SimulatorManage
if (answer == null) {
String message = config.getJsonResponse();
if (message != null) {
// json response looks like {"<AnswerType>":....}
String answerType = message.split(":")[0].substring(1).replace("\"", "");
if (answerType != null) {
// json response looks like {"<Type>":....}
String objectType = message.split(":")[0].substring(2).replace("\"", "");
String objectData = message.substring(message.indexOf(':') + 1, message.length() - 1);
if (objectType != null) {
Class<?> clz = null;
try {
clz = Class.forName(answerType);
clz = Class.forName(objectType);
} catch (ClassNotFoundException e) {
}
if (clz != null) {
answer = (Answer)new Gson().fromJson(message, clz);
StringReader reader = new StringReader(objectData);
JsonReader jsonReader = new JsonReader(reader);
jsonReader.setLenient(true);
answer = (Answer)s_gson.fromJson(jsonReader, clz);
}
}
}

View File

@ -16,6 +16,7 @@
// under the License.
package com.cloud.resource;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -50,6 +51,7 @@ import com.cloud.host.Host;
import com.cloud.host.Host.Type;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.Networks.RouterPrivateIpStrategy;
import com.cloud.serializer.GsonHelper;
import com.cloud.simulator.MockConfigurationVO;
import com.cloud.simulator.MockVMVO;
import com.cloud.storage.Storage.StorageResourceType;
@ -58,9 +60,12 @@ import com.cloud.utils.Pair;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.vm.VirtualMachine.PowerState;
import com.cloud.vm.VirtualMachine.State;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
public class AgentRoutingResource extends AgentStorageResource {
private static final Logger s_logger = Logger.getLogger(AgentRoutingResource.class);
private static final Gson s_gson = GsonHelper.getGson();
protected Map<String, State> _vms = new HashMap<String, State>();
private Map<String, Pair<Long, Long>> _runningVms = new HashMap<String, Pair<Long, Long>>();
@ -120,6 +125,29 @@ public class AgentRoutingResource extends AgentStorageResource {
}
}
}
config = _simMgr.getMockConfigurationDao().findByNameBottomUP(agentHost.getDataCenterId(), agentHost.getPodId(), agentHost.getClusterId(), agentHost.getId(), "PingRoutingWithNwGroupsCommand");
if (config != null) {
String message = config.getJsonResponse();
if (message != null) {
// json response looks like {"<Type>":....}
String objectType = message.split(":")[0].substring(2).replace("\"", "");
String objectData = message.substring(message.indexOf(':') + 1, message.length() - 1);
if (objectType != null) {
Class<?> clz = null;
try {
clz = Class.forName(objectType);
} catch (ClassNotFoundException e) {
}
if (clz != null) {
StringReader reader = new StringReader(objectData);
JsonReader jsonReader = new JsonReader(reader);
jsonReader.setLenient(true);
return (PingCommand)s_gson.fromJson(jsonReader, clz);
}
}
}
}
} catch (Exception e) {
txn.rollback();
} finally {
@ -303,8 +331,16 @@ public class AgentRoutingResource extends AgentStorageResource {
protected HashMap<String, HostVmStateReportEntry> getHostVmStateReport() {
HashMap<String, HostVmStateReportEntry> report = new HashMap<String, HostVmStateReportEntry>();
for (String vmName : _runningVms.keySet()) {
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerOn, agentHost.getName()));
Map<String, State> states = _simMgr.getVmStates(this.hostGuid);
for (String vmName : states.keySet()) {
State state = states.get(vmName);
if (state == State.Running) {
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerOn, agentHost.getName()));
} else if (state == State.Stopped) {
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerOff, agentHost.getName()));
} else {
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerUnknown, agentHost.getName()));
}
}
return report;