Skip to content

Commit 1f17c63

Browse files
committed
Uplift api version
1 parent 53c3322 commit 1f17c63

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818
import java.nio.charset.StandardCharsets;
1919
import java.security.MessageDigest;
2020
import java.security.NoSuchAlgorithmException;
21-
import java.util.ArrayList;
22-
import java.util.Collections;
23-
import java.util.HashMap;
24-
import java.util.HashSet;
25-
import java.util.LinkedHashMap;
26-
import java.util.List;
27-
import java.util.Map;
21+
import java.util.*;
2822
import java.util.Map.Entry;
29-
import java.util.Set;
23+
import java.util.regex.Matcher;
24+
import java.util.regex.Pattern;
3025
import java.util.stream.Collectors;
3126
import java.util.stream.StreamSupport;
3227
import org.apache.commons.lang3.StringUtils;
@@ -59,6 +54,20 @@ protected void serverAPISync() throws TRexConnectionException {
5954
apiVers.put("name", ASTF);
6055
TRexClientResult<ApiVersionHandler> result =
6156
callMethod("api_sync_v2", apiVers, ApiVersionHandler.class);
57+
58+
if (!StringUtils.isBlank(result.getError()) && result.getError().contains("Version mismatch")) {
59+
String regrexString = "server: '([0-9]*)\\.([0-9]*)', client: '([0-9]*)\\.([0-9]*)'";
60+
Pattern pattern = Pattern.compile(regrexString);
61+
Matcher matcher = pattern.matcher(result.getError());
62+
if (matcher.find()) {
63+
Constants.ASTF_API_VERSION_MAJOR = Integer.parseInt(matcher.group(1));
64+
Constants.ASTF_API_VERSION_MINOR = Integer.parseInt(matcher.group(2));
65+
apiVers.put("major", Constants.ASTF_API_VERSION_MAJOR);
66+
apiVers.put("minor", Constants.ASTF_API_VERSION_MINOR);
67+
result = callMethod("api_sync_v2", apiVers, ApiVersionHandler.class);
68+
}
69+
}
70+
6271
if (result.get() == null) {
6372
TRexConnectionException e =
6473
new TRexConnectionException(
@@ -226,6 +235,7 @@ public void updateLatencyTrafficRate(int mult) {
226235
*/
227236
public void acquirePorts(Boolean force) {
228237
Map<String, Object> payload = createPayload();
238+
payload.put("session_id", new Random().nextInt(Integer.MAX_VALUE - 1) + 1);
229239
payload.put("user", userName);
230240
payload.put("force", force);
231241
String json = callMethod("acquire", payload);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@
3838
import java.util.Queue;
3939
import java.util.Set;
4040
import java.util.function.Predicate;
41+
import java.util.regex.Matcher;
42+
import java.util.regex.Pattern;
4143
import java.util.stream.Collectors;
4244
import java.util.stream.StreamSupport;
45+
import org.apache.commons.lang3.StringUtils;
4346
import org.pcap4j.packet.ArpPacket;
4447
import org.pcap4j.packet.Dot1qVlanTagPacket;
4548
import org.pcap4j.packet.EthernetPacket;
@@ -107,6 +110,19 @@ protected void serverAPISync() throws TRexConnectionException {
107110
TRexClientResult<ApiVersionHandler> result =
108111
callMethod("api_sync_v2", parameters, ApiVersionHandler.class);
109112

113+
if (!StringUtils.isBlank(result.getError()) && result.getError().contains("Version mismatch")) {
114+
String regrexString = "server: '([0-9]*)\\.([0-9]*)', client: '([0-9]*)\\.([0-9]*)'";
115+
Pattern pattern = Pattern.compile(regrexString);
116+
Matcher matcher = pattern.matcher(result.getError());
117+
if (matcher.find()) {
118+
Constants.STL_API_VERSION_MAJOR = Integer.parseInt(matcher.group(1));
119+
Constants.STL_API_VERSION_MINOR = Integer.parseInt(matcher.group(2));
120+
parameters.put("major", Constants.STL_API_VERSION_MAJOR);
121+
parameters.put("minor", Constants.STL_API_VERSION_MINOR);
122+
result = callMethod("api_sync_v2", parameters, ApiVersionHandler.class);
123+
}
124+
}
125+
110126
if (result.get() == null) {
111127
TRexConnectionException e =
112128
new TRexConnectionException(

src/main/java/com/cisco/trex/util/Constants.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/** Class for constants */
44
public final class Constants {
55
public static final String JSON_RPC_VERSION = "2.0";
6-
public static final int ASTF_API_VERSION_MAJOR = 1;
7-
public static final int ASTF_API_VERSION_MINOR = 7;
8-
public static final int STL_API_VERSION_MAJOR = 4;
9-
public static final int STL_API_VERSION_MINOR = 6;
6+
public static int ASTF_API_VERSION_MAJOR = 2;
7+
public static int ASTF_API_VERSION_MINOR = 0;
8+
public static int STL_API_VERSION_MAJOR = 5;
9+
public static int STL_API_VERSION_MINOR = 0;
1010
}

src/main/java/com/cisco/trex/util/TRexClientUtil.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import java.util.HashMap;
88
import java.util.Map;
99
import java.util.Random;
10+
import java.util.regex.Matcher;
1011
import java.util.regex.Pattern;
12+
import org.apache.commons.lang3.StringUtils;
1113
import org.slf4j.Logger;
1214
import org.slf4j.LoggerFactory;
1315

@@ -27,7 +29,24 @@ public static TRexServerMode getMode(String host, String port) {
2729
parameters.put("minor", Constants.ASTF_API_VERSION_MINOR);
2830
RPCResponse response = null;
2931
try {
30-
response = transport.sendCommand(buildCommand("api_sync_v2", parameters));
32+
TRexCommand command = buildCommand("api_sync_v2", parameters);
33+
response = transport.sendCommand(command);
34+
35+
String errorMessage =
36+
response.getError() == null ? null : response.getError().getSpecificErr();
37+
if (!StringUtils.isBlank(errorMessage) && errorMessage.contains("Version mismatch")) {
38+
String regrexString = "server: '([0-9]*)\\.([0-9]*)', client: '([0-9]*)\\.([0-9]*)'";
39+
Pattern pattern = Pattern.compile(regrexString);
40+
Matcher matcher = pattern.matcher(errorMessage);
41+
if (matcher.find()) {
42+
Constants.ASTF_API_VERSION_MAJOR = Integer.parseInt(matcher.group(1));
43+
Constants.ASTF_API_VERSION_MINOR = Integer.parseInt(matcher.group(2));
44+
parameters.put("major", Constants.ASTF_API_VERSION_MAJOR);
45+
parameters.put("minor", Constants.ASTF_API_VERSION_MINOR);
46+
command = buildCommand("api_sync_v2", parameters);
47+
response = transport.sendCommand(command);
48+
}
49+
}
3150
} catch (IOException | NullPointerException e) {
3251
LOGGER.debug("Unable to sync client with TRex server .", e);
3352
return TRexServerMode.UNKNOWN;

0 commit comments

Comments
 (0)