11package com .cisco .trex .stateful ;
22
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import com .google .gson .JsonObject ;
5+
6+ import java .io .IOException ;
37import java .nio .charset .StandardCharsets ;
48import java .security .MessageDigest ;
59import java .security .NoSuchAlgorithmException ;
10+ import java .util .ArrayList ;
611import java .util .HashMap ;
12+ import java .util .HashSet ;
13+ import java .util .LinkedHashMap ;
714import java .util .List ;
815import java .util .Map ;
916import java .util .Map .Entry ;
2431import com .google .gson .JsonArray ;
2532import com .google .gson .JsonElement ;
2633import com .google .gson .JsonParser ;
27- import com .google .gson .JsonObject ;
2834import com .cisco .trex .stateful .model .stats .LatencyStats ;
2935import 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}
0 commit comments