Skip to content

Commit 2bdff26

Browse files
authored
Merge pull request #71 from enxxjmn/MJE-15700
support template group stats RPCs
2 parents 885593f + 256680d commit 2bdff26

File tree

3 files changed

+151
-4
lines changed

3 files changed

+151
-4
lines changed

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

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.cisco.trex.stateful;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.google.gson.JsonObject;
5+
6+
import java.io.IOException;
37
import java.nio.charset.StandardCharsets;
48
import java.security.MessageDigest;
59
import java.security.NoSuchAlgorithmException;
10+
import java.util.ArrayList;
611
import java.util.HashMap;
12+
import java.util.HashSet;
13+
import java.util.LinkedHashMap;
714
import java.util.List;
815
import java.util.Map;
916
import java.util.Map.Entry;
@@ -24,7 +31,6 @@
2431
import com.google.gson.JsonArray;
2532
import com.google.gson.JsonElement;
2633
import com.google.gson.JsonParser;
27-
import com.google.gson.JsonObject;
2834
import com.cisco.trex.stateful.model.stats.LatencyStats;
2935
import com.cisco.trex.stateful.model.stats.LatencyPortData;
3036

@@ -252,7 +258,7 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
252258
* @param profile
253259
*/
254260
public void loadProfile(String profile) {
255-
loadProfile(profile, "");
261+
loadProfile("", profile);
256262
}
257263

258264
/**
@@ -319,7 +325,7 @@ public List<String> getProfileIds() {
319325

320326
/**
321327
* Get ASTF counters of profile associated with specified profile id
322-
*
328+
*
323329
* @param profileId
324330
* @return AstfStatistics
325331
*/
@@ -331,7 +337,7 @@ public AstfStatistics getAstfStatistics(String profileId) {
331337

332338
/**
333339
* Get ASTF total counters for all profiles
334-
*
340+
*
335341
* @return AstfStatistics
336342
*/
337343
public AstfStatistics getAstfTotalStatistics() {
@@ -390,4 +396,95 @@ public String getVersion() {
390396
}
391397
}
392398

399+
/**
400+
* get template group names
401+
*
402+
* @return template group names
403+
*/
404+
public List<String> getTemplateGroupNames() {
405+
return this.getTemplateGroupNames("");
406+
}
407+
408+
/**
409+
* get template group names
410+
*
411+
* @param profileId
412+
* @return template group names
413+
*/
414+
public List<String> getTemplateGroupNames(String profileId) {
415+
Map<String, Object> payload = createPayload(profileId);
416+
payload.put("initialized", false);
417+
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()
420+
.get("tg_names").getAsJsonArray();
421+
return StreamSupport.stream(names.spliterator(), false)
422+
.map(JsonElement::getAsString)
423+
.collect(Collectors.toList());
424+
}
425+
426+
/**
427+
* get template group statistics
428+
*
429+
* @param tgNames
430+
* @return
431+
*/
432+
public Map<String, AstfStatistics> getTemplateGroupStatistics(List<String> tgNames) {
433+
return getTemplateGroupStatistics("", tgNames);
434+
}
435+
436+
/**
437+
* get template group statistics
438+
*
439+
* @param profileId
440+
* @param tgNames
441+
* @return Map key:tgName, value:AstfStatistics
442+
*/
443+
public Map<String, AstfStatistics> getTemplateGroupStatistics(String profileId, List<String> tgNames) {
444+
445+
//remove duplicated tgNames in input list
446+
tgNames = new ArrayList<>(new HashSet<>(tgNames));
447+
Map<String, AstfStatistics> stats = new LinkedHashMap<>(tgNames.size());
448+
449+
Map<String, Object> payload = createPayload(profileId);
450+
payload.put("epoch", 1);
451+
Map<String, Integer> name2Id = translateNames2Ids(profileId, tgNames);
452+
payload.put("tg_ids", new ArrayList<>(name2Id.values()));
453+
454+
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();
457+
MetaData metaData = getAstfStatsMetaData();
458+
name2Id.forEach((tgName, tgId) ->{
459+
try {
460+
AstfStatistics astfStatistics = new ObjectMapper().readValue(result.get(tgId.toString()).toString(), AstfStatistics.class);
461+
astfStatistics.setCounterNames(metaData);
462+
stats.put(tgName, astfStatistics);
463+
} catch (IOException e) {
464+
LOGGER.error("Error occurred during processing output of get_tg_id_stats method", e);
465+
}
466+
});
467+
468+
return stats;
469+
}
470+
471+
/**
472+
* translate template group names to ids
473+
* getTemplateGroupNames with return all template group names, this method will map an id for each name,
474+
* the id is an increasing integer starting at 1. and filter names by input name list
475+
* @param profileId
476+
* @param tgNames
477+
* @return Map key:tgName, value:tgId
478+
*/
479+
private Map<String, Integer> translateNames2Ids(String profileId, List<String> tgNames) {
480+
Map<String, Integer> name2Id = new LinkedHashMap<>(tgNames.size());
481+
List<String> allTgNames = getTemplateGroupNames(profileId);
482+
for (int i = 0; i < allTgNames.size(); i++) {
483+
if(tgNames.contains(allTgNames.get(i)))
484+
name2Id.put(allTgNames.get(i), i+1);
485+
}
486+
487+
return name2Id;
488+
}
489+
393490
}

src/main/java/com/cisco/trex/stateful/api/lowlevel/ASTFProfile.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.JsonArray;
44
import com.google.gson.JsonObject;
5+
import java.util.LinkedHashMap;
56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

@@ -26,6 +27,9 @@ public class ASTFProfile {
2627
private ASTFGlobalInfo astfServerGlobalInfo;
2728
private List<ASTFTemplate> astfTemplateList;
2829
private List<ASTFCapInfo> astfCapInfoList;
30+
31+
private Map<String, Integer> tgName2TgId = new LinkedHashMap<>(); //template group name -> template group id
32+
2933
/**
3034
* construct
3135
*
@@ -55,6 +59,19 @@ public ASTFProfile(ASTFIpGen defaultIpGen, ASTFGlobalInfo astfClientGlobalInfo,
5559
this.astfTemplateList = astfTemplateList;
5660
this.astfCapInfoList = astfCapInfoList;
5761

62+
for (ASTFTemplate template : astfTemplateList) {
63+
if (template.getTgName() == null) {
64+
template.setTgId(0);
65+
continue;
66+
}
67+
68+
String tgName = template.getTgName();
69+
if (!tgName2TgId.containsKey(tgName)) {
70+
tgName2TgId.put(tgName, tgName2TgId.size() + 1);
71+
}
72+
template.setTgId(tgName2TgId.get(tgName));
73+
}
74+
5875
/**
5976
* for pcap file parse scenario
6077
*/
@@ -165,6 +182,11 @@ public JsonObject toJson() {
165182
jsonArray.add(astfTemplate.toJson());
166183
}
167184
json.add("templates", jsonArray);
185+
186+
JsonArray tgNames = new JsonArray();
187+
tgName2TgId.keySet().forEach(name -> tgNames.add(name));
188+
json.add("tg_names", tgNames);
189+
168190
return json;
169191
}
170192

src/main/java/com/cisco/trex/stateful/api/lowlevel/ASTFTemplate.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
* </p>
3434
*/
3535
public class ASTFTemplate {
36+
3637
private ASTFTCPClientTemplate astfTcpClientTemplate;
3738
private ASTFTCPServerTemplate astfTcpServerTemplate;
39+
private String tgName;
40+
private Integer tgId;
3841

3942
/**
4043
* construct
@@ -43,11 +46,35 @@ public class ASTFTemplate {
4346
* @param astfTcpServerTemplate
4447
*/
4548
public ASTFTemplate(ASTFTCPClientTemplate astfTcpClientTemplate, ASTFTCPServerTemplate astfTcpServerTemplate) {
49+
this(astfTcpClientTemplate, astfTcpServerTemplate, null);
50+
}
51+
52+
/**
53+
* construct
54+
*
55+
* @param astfTcpClientTemplate
56+
* @param astfTcpServerTemplate
57+
* @param tgName
58+
*/
59+
public ASTFTemplate(ASTFTCPClientTemplate astfTcpClientTemplate, ASTFTCPServerTemplate astfTcpServerTemplate, String tgName) {
4660
if (astfTcpClientTemplate.isStream() != astfTcpServerTemplate.isStream()) {
4761
throw new IllegalStateException(String.format(" Client template stream mode is %s and different from server template mode %s", astfTcpClientTemplate.isStream(), astfTcpServerTemplate.isStream()));
4862
}
4963
this.astfTcpClientTemplate = astfTcpClientTemplate;
5064
this.astfTcpServerTemplate = astfTcpServerTemplate;
65+
this.tgName = tgName;
66+
}
67+
68+
public String getTgName() {
69+
return tgName;
70+
}
71+
72+
Integer getTgId() {
73+
return tgId;
74+
}
75+
76+
void setTgId(Integer tgId) {
77+
this.tgId = tgId;
5178
}
5279

5380
/**
@@ -59,6 +86,7 @@ public JsonObject toJson() {
5986
JsonObject json = new JsonObject();
6087
json.add("client_template", astfTcpClientTemplate.toJson());
6188
json.add("server_template", astfTcpServerTemplate.toJson());
89+
json.addProperty("tg_id", tgId);
6290
return json;
6391
}
6492
}

0 commit comments

Comments
 (0)