Skip to content

Commit b7d184f

Browse files
committed
Support both new and old JSON RPC response style
Signed-off-by: Leo Ma <[email protected]>
1 parent 9626c7b commit b7d184f

File tree

5 files changed

+34
-33
lines changed

5 files changed

+34
-33
lines changed

src/e2e-test/java/com/cisco/trex/stateless/TRexClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class TRexClientTest {
3636

3737
@BeforeClass
3838
public static void setUp() throws TRexConnectionException, TRexTimeoutException {
39-
client = new TRexClient("trex-host", "4501", CLIENT_USER);
39+
client = new TRexClient("10.76.176.8", "4501", CLIENT_USER);
4040
client.connect();
4141
}
4242

src/main/java/com/cisco/trex/ClientBase.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.gson.GsonBuilder;
4242
import com.google.gson.JsonArray;
4343
import com.google.gson.JsonElement;
44+
import com.google.gson.JsonObject;
4445
import com.google.gson.JsonParser;
4546
import com.google.gson.reflect.TypeToken;
4647

@@ -294,12 +295,21 @@ public List<String> getSupportedCommands() {
294295
Map<String, Object> payload = new HashMap<>();
295296
payload.put("api_h", apiH);
296297
String json = callMethod("get_supported_cmds", payload);
297-
JsonElement response = new JsonParser().parse(json);
298-
JsonArray cmds = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonArray();
298+
JsonArray cmds = getResultFromResponse(json).getAsJsonArray();
299299
return StreamSupport.stream(cmds.spliterator(), false)
300300
.map(JsonElement::getAsString)
301301
.collect(Collectors.toList());
302302
}
303+
304+
protected JsonElement getResultFromResponse(String json) {
305+
JsonElement response = new JsonParser().parse(json);
306+
if (response.isJsonArray()) {
307+
// for versions of TRex before v2.61, single entry response also wrapped with json array
308+
return response.getAsJsonArray().get(0).getAsJsonObject().get("result");
309+
}
310+
311+
return response.getAsJsonObject().get("result");
312+
}
303313

304314
/**
305315
* Get Port Status
@@ -505,8 +515,7 @@ public TRexClientResult<StubResult> setVlan(int portIdx, List<Integer> vlanIds)
505515
*/
506516
public SystemInfo getSystemInfo() {
507517
String json = callMethod("get_system_info", null);
508-
SystemInfoResponse response = GSON.fromJson(json, SystemInfoResponse[].class)[0];
509-
return response.getResult();
518+
return GSON.fromJson(getResultFromResponse(json), SystemInfo.class);
510519
}
511520

512521
/**

src/main/java/com/cisco/trex/stateful/TRexAstfClient.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,11 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
231231
payload.put("force", force);
232232
payload.put("port_id", portIndex);
233233
String json = callMethod("acquire", payload);
234-
JsonElement response = new JsonParser().parse(json);
235234
Set<Entry<String, JsonElement>> entrySet;
236235
try {
237-
this.masterHandler = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonObject()
236+
this.masterHandler = getResultFromResponse(json).getAsJsonObject()
238237
.get("handler").getAsString();
239-
entrySet = response.getAsJsonArray().get(0).getAsJsonObject().get("result")
238+
entrySet = getResultFromResponse(json)
240239
.getAsJsonObject()
241240
.get("ports").getAsJsonObject().entrySet();
242241
} catch (NullPointerException e) {
@@ -316,8 +315,7 @@ public void clearProfile(String profileId) {
316315
public List<String> getProfileIds() {
317316
Map<String, Object> payload = createPayload();
318317
String json = callMethod("get_profile_list", payload);
319-
JsonElement response = new JsonParser().parse(json);
320-
JsonArray ids = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonArray();
318+
JsonArray ids = getResultFromResponse(json).getAsJsonArray();
321319
return StreamSupport.stream(ids.spliterator(), false)
322320
.map(JsonElement::getAsString)
323321
.collect(Collectors.toList());
@@ -359,8 +357,7 @@ private MetaData getAstfStatsMetaData() {
359357
public LatencyStats getLatencyStats() {
360358
Map<String, Object> payload = this.createPayload();
361359
String json = this.callMethod("get_latency_stats", payload);
362-
JsonElement response = new JsonParser().parse(json);
363-
JsonElement latencyStatsJsonElement = response.getAsJsonArray().get(0).getAsJsonObject().get("result");
360+
JsonElement latencyStatsJsonElement = getResultFromResponse(json);
364361
//only can parse a part of data, LatencyPortData need to be parsed manually.
365362
LatencyStats latencyStats = GSON.fromJson(latencyStatsJsonElement, LatencyStats.class);
366363
JsonElement latencyDataJsonElement = latencyStatsJsonElement.getAsJsonObject().get("data");
@@ -387,9 +384,8 @@ public LatencyStats getLatencyStats() {
387384
public String getVersion() {
388385
Map<String, Object> payload = this.createPayload();
389386
String json = callMethod("get_version", payload);
390-
JsonElement response = new JsonParser().parse(json);
391387
try {
392-
return response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonObject()
388+
return getResultFromResponse(json).getAsJsonObject()
393389
.get("version").getAsString();
394390
} catch (NullPointerException e) {
395391
throw new IllegalStateException("could not parse version", e);
@@ -415,8 +411,7 @@ public List<String> getTemplateGroupNames(String profileId) {
415411
Map<String, Object> payload = createPayload(profileId);
416412
payload.put("initialized", false);
417413
String json = callMethod("get_tg_names", payload);
418-
JsonElement response = new JsonParser().parse(json);
419-
JsonArray names = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonObject()
414+
JsonArray names = getResultFromResponse(json).getAsJsonObject()
420415
.get("tg_names").getAsJsonArray();
421416
return StreamSupport.stream(names.spliterator(), false)
422417
.map(JsonElement::getAsString)
@@ -452,8 +447,7 @@ public Map<String, AstfStatistics> getTemplateGroupStatistics(String profileId,
452447
payload.put("tg_ids", new ArrayList<>(name2Id.values()));
453448

454449
String json = callMethod("get_tg_id_stats", payload);
455-
JsonElement response = new JsonParser().parse(json);
456-
JsonObject result = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonObject();
450+
JsonObject result = getResultFromResponse(json).getAsJsonObject();
457451
MetaData metaData = getAstfStatsMetaData();
458452
name2Id.forEach((tgName, tgId) ->{
459453
try {

src/main/java/com/cisco/trex/stateless/TRexClient.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
108108
payload.put("user", userName);
109109
payload.put("force", force);
110110
String json = callMethod("acquire", payload);
111-
JsonElement response = new JsonParser().parse(json);
112-
String handler = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsString();
111+
String handler = getResultFromResponse(json).getAsString();
113112
portHandlers.put(portIndex, handler);
114113
return getPortStatus(portIndex).get();
115114
}
@@ -163,8 +162,7 @@ public Stream getStream(int portIndex, int streamId) {
163162

164163
String json = callMethod("get_stream", payload);
165164
JsonElement response = new JsonParser().parse(json);
166-
JsonObject stream = response.getAsJsonArray().get(0)
167-
.getAsJsonObject().get("result")
165+
JsonObject stream = getResultFromResponse(json)
168166
.getAsJsonObject().get("stream")
169167
.getAsJsonObject();
170168
return GSON.fromJson(stream, Stream.class);
@@ -199,8 +197,7 @@ public List<Stream> getAllStreams(int portIndex, String profileId) {
199197
Map<String, Object> payload = createPayload(portIndex, profileId);
200198
String json = callMethod("get_all_streams", payload);
201199
JsonElement response = new JsonParser().parse(json);
202-
JsonObject streams = response.getAsJsonArray().get(0)
203-
.getAsJsonObject().get("result")
200+
JsonObject streams = getResultFromResponse(json)
204201
.getAsJsonObject().get("streams")
205202
.getAsJsonObject();
206203
ArrayList<Stream> streamList = new ArrayList<>();
@@ -219,8 +216,7 @@ public List<Integer> getStreamIds(int portIndex) {
219216
public List<Integer> getStreamIds(int portIndex, String profileId) {
220217
Map<String, Object> payload = createPayload(portIndex, profileId);
221218
String json = callMethod("get_stream_list", payload);
222-
JsonElement response = new JsonParser().parse(json);
223-
JsonArray ids = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonArray();
219+
JsonArray ids = getResultFromResponse(json).getAsJsonArray();
224220
return StreamSupport.stream(ids.spliterator(), false)
225221
.map(JsonElement::getAsInt)
226222
.collect(Collectors.toList());
@@ -250,8 +246,7 @@ public void updateStreams(int portIndex, List<Integer> streams, boolean force,
250246
public List<String> getProfileIds(int portIndex) {
251247
Map<String, Object> payload = createPayload(portIndex);
252248
String json = callMethod("get_profile_list", payload);
253-
JsonElement response = new JsonParser().parse(json);
254-
JsonArray ids = response.getAsJsonArray().get(0).getAsJsonObject().get("result").getAsJsonArray();
249+
JsonArray ids = getResultFromResponse(json).getAsJsonArray();
255250
return StreamSupport.stream(ids.spliterator(), false)
256251
.map(JsonElement::getAsString)
257252
.collect(Collectors.toList());
@@ -542,9 +537,7 @@ public List<EthernetPacket> getRxQueue(int portIndex, Predicate<EthernetPacket>
542537

543538
Map<String, Object> payload = createPayload(portIndex);
544539
String json = callMethod("get_rx_queue_pkts", payload);
545-
JsonElement response = new JsonParser().parse(json);
546-
JsonArray pkts = response.getAsJsonArray().get(0)
547-
.getAsJsonObject().get("result")
540+
JsonArray pkts = getResultFromResponse(json)
548541
.getAsJsonObject()
549542
.getAsJsonArray("pkts");
550543
return StreamSupport.stream(pkts.spliterator(), false)

src/main/java/com/cisco/trex/stateless/TRexTransport.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ public TRexTransport(String host, String port, int timeout) {
5252
public RPCResponse sendCommand(TRexCommand command) throws IOException {
5353
String json = new ObjectMapper().writeValueAsString(command.getParameters());
5454
String response = sendJson(json);
55-
RPCResponse[] rpcResult = new ObjectMapper().readValue(response, RPCResponse[].class);
56-
return rpcResult[0];
55+
ObjectMapper objectMapper = new ObjectMapper();
56+
if (objectMapper.readTree(response).isArray()) {
57+
// for versions of TRex before v2.61, single entry response also wrapped with json array
58+
RPCResponse[] rpcResult = objectMapper.readValue(response, RPCResponse[].class);
59+
return rpcResult[0];
60+
}
61+
return objectMapper.readValue(response, RPCResponse.class);
5762
}
5863

5964
public RPCResponse[] sendCommands(List<TRexCommand> commands) throws IOException {

0 commit comments

Comments
 (0)