Skip to content

Commit 4f5df54

Browse files
committed
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}}
1 parent f45c9f9 commit 4f5df54

2 files changed

Lines changed: 51 additions & 7 deletions

File tree

plugins/hypervisors/simulator/src/com/cloud/agent/manager/SimulatorManagerImpl.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package com.cloud.agent.manager;
1818

19+
import java.io.StringReader;
1920
import java.util.ArrayList;
2021
import java.util.Date;
2122
import java.util.HashMap;
@@ -113,6 +114,7 @@
113114
import com.cloud.api.commands.QuerySimulatorMockCmd;
114115
import com.cloud.agent.api.SecStorageFirewallCfgCommand;
115116
import com.cloud.resource.SimulatorStorageProcessor;
117+
import com.cloud.serializer.GsonHelper;
116118
import com.cloud.simulator.MockConfigurationVO;
117119
import com.cloud.simulator.MockHost;
118120
import com.cloud.simulator.MockVMVO;
@@ -128,11 +130,13 @@
128130
import com.cloud.utils.exception.CloudRuntimeException;
129131
import com.cloud.vm.VirtualMachine.State;
130132
import com.google.gson.Gson;
133+
import com.google.gson.stream.JsonReader;
131134

132135
@Component
133136
@Local(value = {SimulatorManager.class})
134137
public class SimulatorManagerImpl extends ManagerBase implements SimulatorManager, PluggableService {
135138
private static final Logger s_logger = Logger.getLogger(SimulatorManagerImpl.class);
139+
private static final Gson s_gson = GsonHelper.getGson();
136140
@Inject
137141
MockVmManager _mockVmMgr;
138142
@Inject
@@ -252,16 +256,20 @@ public Answer simulate(Command cmd, String hostGuid) {
252256
if (answer == null) {
253257
String message = config.getJsonResponse();
254258
if (message != null) {
255-
// json response looks like {"<AnswerType>":....}
256-
String answerType = message.split(":")[0].substring(1).replace("\"", "");
257-
if (answerType != null) {
259+
// json response looks like {"<Type>":....}
260+
String objectType = message.split(":")[0].substring(2).replace("\"", "");
261+
String objectData = message.substring(message.indexOf(':') + 1, message.length() - 1);
262+
if (objectType != null) {
258263
Class<?> clz = null;
259264
try {
260-
clz = Class.forName(answerType);
265+
clz = Class.forName(objectType);
261266
} catch (ClassNotFoundException e) {
262267
}
263268
if (clz != null) {
264-
answer = (Answer)new Gson().fromJson(message, clz);
269+
StringReader reader = new StringReader(objectData);
270+
JsonReader jsonReader = new JsonReader(reader);
271+
jsonReader.setLenient(true);
272+
answer = (Answer)s_gson.fromJson(jsonReader, clz);
265273
}
266274
}
267275
}

plugins/hypervisors/simulator/src/com/cloud/resource/AgentRoutingResource.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package com.cloud.resource;
1818

19+
import java.io.StringReader;
1920
import java.util.ArrayList;
2021
import java.util.HashMap;
2122
import java.util.List;
@@ -50,6 +51,7 @@
5051
import com.cloud.host.Host.Type;
5152
import com.cloud.hypervisor.Hypervisor.HypervisorType;
5253
import com.cloud.network.Networks.RouterPrivateIpStrategy;
54+
import com.cloud.serializer.GsonHelper;
5355
import com.cloud.simulator.MockConfigurationVO;
5456
import com.cloud.simulator.MockVMVO;
5557
import com.cloud.storage.Storage.StorageResourceType;
@@ -58,9 +60,12 @@
5860
import com.cloud.utils.db.TransactionLegacy;
5961
import com.cloud.vm.VirtualMachine.PowerState;
6062
import com.cloud.vm.VirtualMachine.State;
63+
import com.google.gson.Gson;
64+
import com.google.gson.stream.JsonReader;
6165

6266
public class AgentRoutingResource extends AgentStorageResource {
6367
private static final Logger s_logger = Logger.getLogger(AgentRoutingResource.class);
68+
private static final Gson s_gson = GsonHelper.getGson();
6469

6570
protected Map<String, State> _vms = new HashMap<String, State>();
6671
private Map<String, Pair<Long, Long>> _runningVms = new HashMap<String, Pair<Long, Long>>();
@@ -120,6 +125,29 @@ public PingCommand getCurrentStatus(long id) {
120125
}
121126
}
122127
}
128+
129+
config = _simMgr.getMockConfigurationDao().findByNameBottomUP(agentHost.getDataCenterId(), agentHost.getPodId(), agentHost.getClusterId(), agentHost.getId(), "PingRoutingWithNwGroupsCommand");
130+
if (config != null) {
131+
String message = config.getJsonResponse();
132+
if (message != null) {
133+
// json response looks like {"<Type>":....}
134+
String objectType = message.split(":")[0].substring(2).replace("\"", "");
135+
String objectData = message.substring(message.indexOf(':') + 1, message.length() - 1);
136+
if (objectType != null) {
137+
Class<?> clz = null;
138+
try {
139+
clz = Class.forName(objectType);
140+
} catch (ClassNotFoundException e) {
141+
}
142+
if (clz != null) {
143+
StringReader reader = new StringReader(objectData);
144+
JsonReader jsonReader = new JsonReader(reader);
145+
jsonReader.setLenient(true);
146+
return (PingCommand)s_gson.fromJson(jsonReader, clz);
147+
}
148+
}
149+
}
150+
}
123151
} catch (Exception e) {
124152
txn.rollback();
125153
} finally {
@@ -303,8 +331,16 @@ protected List<Object> getHostInfo() {
303331
protected HashMap<String, HostVmStateReportEntry> getHostVmStateReport() {
304332
HashMap<String, HostVmStateReportEntry> report = new HashMap<String, HostVmStateReportEntry>();
305333

306-
for (String vmName : _runningVms.keySet()) {
307-
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerOn, agentHost.getName()));
334+
Map<String, State> states = _simMgr.getVmStates(this.hostGuid);
335+
for (String vmName : states.keySet()) {
336+
State state = states.get(vmName);
337+
if (state == State.Running) {
338+
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerOn, agentHost.getName()));
339+
} else if (state == State.Stopped) {
340+
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerOff, agentHost.getName()));
341+
} else {
342+
report.put(vmName, new HostVmStateReportEntry(PowerState.PowerUnknown, agentHost.getName()));
343+
}
308344
}
309345

310346
return report;

0 commit comments

Comments
 (0)