44import java .security .MessageDigest ;
55import java .security .NoSuchAlgorithmException ;
66import java .util .HashMap ;
7+ import java .util .List ;
78import java .util .Map ;
89import java .util .Map .Entry ;
910import java .util .Set ;
11+ import java .util .stream .Collectors ;
12+ import java .util .stream .StreamSupport ;
1013
11- import com .cisco .trex .stateless .model .ApiVersionHandler ;
1214import org .apache .commons .lang .StringUtils ;
1315
1416import com .cisco .trex .ClientBase ;
1517import com .cisco .trex .stateless .exception .TRexConnectionException ;
18+ import com .cisco .trex .stateless .model .ApiVersionHandler ;
1619import com .cisco .trex .stateless .model .PortStatus ;
1720import com .cisco .trex .stateless .model .TRexClientResult ;
21+ import com .google .gson .JsonArray ;
1822import com .google .gson .JsonElement ;
1923import com .google .gson .JsonParser ;
2024
@@ -68,6 +72,14 @@ private Map<String, Object> createPayload() {
6872 return payload ;
6973 }
7074
75+ protected Map <String , Object > createPayload (String profileId ) {
76+ Map <String , Object > payload = createPayload ();
77+ if (profileId != null && !profileId .isEmpty ()) {
78+ payload .put ("profile_id" , profileId );
79+ }
80+ return payload ;
81+ }
82+
7183 private static String calculateMd5 (String profile ) {
7284 try {
7385 MessageDigest md = MessageDigest .getInstance ("MD5" );
@@ -83,7 +95,7 @@ private static String calculateMd5(String profile) {
8395 }
8496
8597 /**
86- * start traffic on all ports on the last loaded profile
98+ * start traffic on all ports on loaded profile associated with default profile id
8799 *
88100 * @param clientMask
89101 * @param duration
@@ -94,7 +106,23 @@ private static String calculateMd5(String profile) {
94106 */
95107 public void startTraffic (long clientMask , double duration , boolean ipv6 ,
96108 int latencyPps , int mult , boolean nc ) {
97- Map <String , Object > payload = createPayload ();
109+ startTraffic ("" , clientMask , duration , ipv6 , latencyPps , mult , nc );
110+ }
111+
112+ /**
113+ * start traffic on all ports on loaded profile associated with specified profile id
114+ *
115+ * @param profileId
116+ * @param clientMask
117+ * @param duration
118+ * @param ipv6
119+ * @param latencyPps
120+ * @param mult
121+ * @param nc
122+ */
123+ public void startTraffic (String profileId , long clientMask , double duration , boolean ipv6 ,
124+ int latencyPps , int mult , boolean nc ) {
125+ Map <String , Object > payload = createPayload (profileId );
98126 payload .put ("client_mask" , clientMask );
99127 payload .put ("duration" , duration );
100128 payload .put ("ipv6" , ipv6 );
@@ -124,13 +152,32 @@ public void startLatencyTraffic(long mask, int mult, String srcAddr, String dstA
124152 }
125153
126154 /**
127- * Stop the active traffic
155+ * Stop the active traffic associated with default profile id
128156 */
129157 public void stopTraffic () {
130- Map <String , Object > payload = createPayload ();
158+ stopTraffic ("" );
159+ }
160+
161+ /**
162+ * Stop the active traffic associated with specified profile id
163+ *
164+ * @param profileId
165+ */
166+ public void stopTraffic (String profileId ) {
167+ Map <String , Object > payload = createPayload (profileId );
131168 this .callMethod ("stop" , payload );
132169 }
133170
171+ /**
172+ * Stop all active traffic
173+ */
174+ public void stopAllTraffic () {
175+ List <String > profileIds = getProfileIds ();
176+ for (String profileId : profileIds ) {
177+ stopTraffic (profileId );
178+ }
179+ }
180+
134181 /**
135182 * Stop active latency traffic
136183 */
@@ -197,16 +244,27 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
197244
198245 /**
199246 * Load profile object as string and upload in fragments
200- *
247+ *
201248 * @param profile
202249 */
203250 public void loadProfile (String profile ) {
251+ loadProfile (profile , "" );
252+ }
253+
254+ /**
255+ * Load profile object as string and upload in fragments and associate it with specified profile
256+ * id
257+ *
258+ * @param profile
259+ * @param profileId
260+ */
261+ public void loadProfile (String profileId , String profile ) {
204262 int indexStart = 0 ;
205263 int fragmentLength = 1000 ; //shorter length the first time
206264 int totalLength = profile .length ();
207265 while (totalLength > indexStart ) {
208266 int indexEnd = indexStart + fragmentLength ;
209- Map <String , Object > payload = createPayload ();
267+ Map <String , Object > payload = createPayload (profileId );
210268 if (indexStart == 0 ) { //is first fragment
211269 payload .put ("frag_first" , true );
212270 payload .put ("total_size" , totalLength );
@@ -224,28 +282,68 @@ public void loadProfile(String profile) {
224282 }
225283
226284 /**
227- * clearProfile
285+ * clear profile on loaded state for default profile id
228286 */
229287 public void clearProfile () {
230- Map <String , Object > payload = createPayload ();
288+ clearProfile ("" );
289+ }
290+
291+ /**
292+ * clear profile on loaded state for specified profile id
293+ *
294+ * @param profileId
295+ */
296+ public void clearProfile (String profileId ) {
297+ Map <String , Object > payload = createPayload (profileId );
231298 this .callMethod ("profile_clear" , payload );
232299 }
233-
300+
301+ /**
302+ * fetch all the associated profile ids
303+ *
304+ * @return profile id list
305+ */
306+ public List <String > getProfileIds () {
307+ Map <String , Object > payload = createPayload ();
308+ String json = callMethod ("get_profile_list" , payload );
309+ JsonElement response = new JsonParser ().parse (json );
310+ JsonArray ids = response .getAsJsonArray ().get (0 ).getAsJsonObject ().get ("result" ).getAsJsonArray ();
311+ return StreamSupport .stream (ids .spliterator (), false )
312+ .map (JsonElement ::getAsString )
313+ .collect (Collectors .toList ());
314+ }
315+
234316 /**
235- * Get Counter Metadata
317+ * Get Counter Metadata of profile associated default profile id
236318 * Not finished, needs to return counter object
237319 */
238320 public void getCounterMetadata () {
239- Map <String , Object > payload = createPayload ();
321+ getCounterMetadata ("" );
322+ }
323+
324+ /**
325+ * Get Counter Metadata of profile associated specified profile id
326+ * Not finished, needs to return counter object
327+ */
328+ public void getCounterMetadata (String profileId ) {
329+ Map <String , Object > payload = createPayload (profileId );
240330 this .callMethod ("get_counter_desc" , payload );
241331 }
242332
243333 /**
244- * Get Astf Counters
334+ * Get Astf Counters of profile associated default profile id
245335 * Not finished, needs to return counter object
246336 */
247337 public void getAstfCounters () {
248- Map <String , Object > payload = this .createPayload ();
338+ getAstfCounters ("" );
339+ }
340+
341+ /**
342+ * Get Astf Counters of profile associated specified profile id
343+ * Not finished, needs to return counter object
344+ */
345+ public void getAstfCounters (String profileId ) {
346+ Map <String , Object > payload = this .createPayload (profileId );
249347 this .callMethod ("get_counter_values" , payload );
250348 }
251349
@@ -260,7 +358,7 @@ public void getLatencyStats() {
260358
261359 /**
262360 * Get Version
263- *
361+ *
264362 * @return version
265363 */
266364 public String getVersion () {
0 commit comments