Skip to content

Commit 5b78462

Browse files
committed
Uplift api version
1 parent c42c69d commit 5b78462

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.cisco.trex.stateless.model.ApiVersionHandler;
1111
import com.cisco.trex.stateless.model.TRexClientResult;
1212
import com.cisco.trex.util.Constants;
13+
import com.cisco.trex.util.TRexClientUtil;
14+
import com.cisco.trex.util.TRexServerMode;
1315
import com.fasterxml.jackson.databind.ObjectMapper;
1416
import com.google.gson.JsonArray;
1517
import com.google.gson.JsonElement;
@@ -49,17 +51,17 @@ public TRexAstfClient(String host, String port, String userName) {
4951
protected void serverAPISync() throws TRexConnectionException {
5052
LOGGER.info("Sync API with the TRex");
5153
Map<String, Object> apiVers = new HashMap<>();
52-
apiVers.put("major", Constants.ASTF_API_VERSION_MAJOR);
53-
apiVers.put("minor", Constants.ASTF_API_VERSION_MINOR);
54+
int majorVersion = Constants.ASTF_API_VERSION_MAJOR;
55+
int minorVersion = Constants.ASTF_API_VERSION_MINOR;
56+
apiVers.put("major", majorVersion);
57+
apiVers.put("minor", minorVersion);
5458
apiVers.put("name", ASTF);
5559
TRexClientResult<ApiVersionHandler> result =
5660
callMethod("api_sync_v2", apiVers, ApiVersionHandler.class);
5761

58-
int majorVersion = Constants.ASTF_API_VERSION_MAJOR;
59-
int minorVersion = Constants.ASTF_API_VERSION_MINOR;
60-
// Currently the etrex server has the NBC issue to uplift the ASTF_API_VERSION_MAJOR version
62+
// Currently the TRex server has the NBC issue to uplift the ASTF_API_VERSION_MAJOR version
6163
// This if-block is a temporary solution to support uplift the ASTF_API_VERSION_MAJOR version ,
62-
// if the etrex server does not uplift its version ,the cilent will continue use the old api, if
64+
// if the TRex server does not uplift its version ,the client will continue use the old api, if
6365
// the server uplift, the client will use the new api.
6466
if (!StringUtils.isBlank(result.getError()) && result.getError().contains("Version mismatch")) {
6567
String regrexString = "server: '([0-9]+)\\.([0-9]+)', client: '([0-9]+)\\.([0-9]+)'";
@@ -68,6 +70,13 @@ protected void serverAPISync() throws TRexConnectionException {
6870
if (matcher.find()) {
6971
majorVersion = Integer.parseInt(matcher.group(1));
7072
minorVersion = Integer.parseInt(matcher.group(2));
73+
if (!TRexClientUtil.isVersionCorrect(TRexServerMode.ASTF, majorVersion, minorVersion)) {
74+
new TRexConnectionException(
75+
"Unable to connect to TRex server. Required API version is "
76+
+ majorVersion
77+
+ "."
78+
+ minorVersion);
79+
}
7180
apiVers.put("major", majorVersion);
7281
apiVers.put("minor", minorVersion);
7382
result = callMethod("api_sync_v2", apiVers, ApiVersionHandler.class);

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.cisco.trex.stateless.model.stats.PGIdStatsRPCResult;
1919
import com.cisco.trex.stateless.model.vm.VMInstruction;
2020
import com.cisco.trex.util.Constants;
21+
import com.cisco.trex.util.TRexClientUtil;
22+
import com.cisco.trex.util.TRexServerMode;
2123
import com.google.gson.JsonArray;
2224
import com.google.gson.JsonElement;
2325
import com.google.gson.JsonObject;
@@ -104,17 +106,18 @@ protected void serverAPISync() throws TRexConnectionException {
104106

105107
Map<String, Object> parameters = new HashMap<>();
106108
parameters.put("name", "STL");
107-
parameters.put("major", Constants.STL_API_VERSION_MAJOR);
108-
parameters.put("minor", Constants.STL_API_VERSION_MINOR);
109+
int majorVersion = Constants.STL_API_VERSION_MAJOR;
110+
int minorVersion = Constants.STL_API_VERSION_MINOR;
111+
112+
parameters.put("major", majorVersion);
113+
parameters.put("minor", minorVersion);
109114

110115
TRexClientResult<ApiVersionHandler> result =
111116
callMethod("api_sync_v2", parameters, ApiVersionHandler.class);
112117

113-
int majorVersion = Constants.STL_API_VERSION_MAJOR;
114-
int minorVersion = Constants.STL_API_VERSION_MINOR;
115-
// Currently the etrex server has the NBC issue to uplift the STL_API_VERSION_MAJOR version
118+
// Currently the TRex server has the NBC issue to uplift the STL_API_VERSION_MAJOR version
116119
// This if-block is a temporary solution to support uplift the STL_API_VERSION_MAJOR version ,
117-
// if the etrex server does not uplift its version ,the cilent will continue use the old api, if
120+
// if the TRex server does not uplift its version ,the client will continue use the old api, if
118121
// the server uplift, the client will use the new api.
119122
if (!StringUtils.isBlank(result.getError()) && result.getError().contains("Version mismatch")) {
120123
String regrexString = "server: '([0-9]+)\\.([0-9]+)', client: '([0-9]+)\\.([0-9]+)'";
@@ -123,6 +126,13 @@ protected void serverAPISync() throws TRexConnectionException {
123126
if (matcher.find()) {
124127
majorVersion = Integer.parseInt(matcher.group(1));
125128
minorVersion = Integer.parseInt(matcher.group(2));
129+
if (!TRexClientUtil.isVersionCorrect(TRexServerMode.ASTF, majorVersion, minorVersion)) {
130+
new TRexConnectionException(
131+
"Unable to connect to TRex server. Required API version is "
132+
+ majorVersion
133+
+ "."
134+
+ minorVersion);
135+
}
126136
parameters.put("major", majorVersion);
127137
parameters.put("minor", minorVersion);
128138
result = callMethod("api_sync_v2", parameters, ApiVersionHandler.class);

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

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

33
import com.cisco.trex.stateless.TRexCommand;
44
import com.cisco.trex.stateless.TRexTransport;
5+
import com.cisco.trex.stateless.exception.TRexConnectionException;
56
import com.cisco.trex.stateless.model.RPCResponse;
67
import java.io.IOException;
78
import java.util.HashMap;
@@ -25,18 +26,20 @@ public static TRexServerMode getMode(String host, String port) {
2526
TRexTransport transport = new TRexTransport(host, port, 3000);
2627
Map<String, Object> parameters = new HashMap<>();
2728
parameters.put("name", "ASTF");
28-
parameters.put("major", Constants.ASTF_API_VERSION_MAJOR);
29-
parameters.put("minor", Constants.ASTF_API_VERSION_MINOR);
29+
int majorVersion = Constants.ASTF_API_VERSION_MAJOR;
30+
int minorVersion = Constants.ASTF_API_VERSION_MINOR;
31+
parameters.put("major", majorVersion);
32+
parameters.put("minor", minorVersion);
3033
RPCResponse response = null;
3134
try {
3235
TRexCommand command = buildCommand("api_sync_v2", parameters);
3336
response = transport.sendCommand(command);
3437

35-
// Currently the etrex server has the NBC issue to uplift the ASTF_API_VERSION_MAJOR
38+
// Currently the TRex server has the NBC issue to uplift the ASTF_API_VERSION_MAJOR
3639
// version
3740
// This if-block is a temporary solution to support uplift the ASTF_API_VERSION_MAJOR version
3841
// ,
39-
// if the etrex server does not uplift its version ,the cilent will continue use the old api,
42+
// if the TRex server does not uplift its version ,the client will continue use the old api,
4043
// if the server uplift, the client will use the new api.
4144
String errorMessage =
4245
response.getError() == null ? null : response.getError().getSpecificErr();
@@ -45,8 +48,18 @@ public static TRexServerMode getMode(String host, String port) {
4548
Pattern pattern = Pattern.compile(regrexString);
4649
Matcher matcher = pattern.matcher(errorMessage);
4750
if (matcher.find()) {
48-
parameters.put("major", Integer.parseInt(matcher.group(1)));
49-
parameters.put("minor", Integer.parseInt(matcher.group(2)));
51+
majorVersion = Integer.parseInt(matcher.group(1));
52+
minorVersion = Integer.parseInt(matcher.group(2));
53+
if (!TRexClientUtil.isVersionCorrect(TRexServerMode.ASTF, majorVersion, minorVersion)) {
54+
new TRexConnectionException(
55+
"Unable to connect to TRex server. Required API version is "
56+
+ majorVersion
57+
+ "."
58+
+ minorVersion);
59+
}
60+
parameters.put("major", majorVersion);
61+
parameters.put("minor", minorVersion);
62+
5063
command = buildCommand("api_sync_v2", parameters);
5164
response = transport.sendCommand(command);
5265
}
@@ -88,4 +101,22 @@ private static TRexCommand buildCommand(String methodName, Map<String, Object> p
88101
payload.put("params", parameters);
89102
return new TRexCommand(cmdId, methodName, payload);
90103
}
104+
105+
public static boolean isVersionCorrect(TRexServerMode mode, int major, int minor) {
106+
switch (mode) {
107+
// STL mode support only version not small than 4.6
108+
case STL:
109+
if (major < 4 || (major == 4 && minor < 6)) {
110+
return false;
111+
}
112+
break;
113+
// ASTF mode support only version not small than 1.7
114+
case ASTF:
115+
if (major < 1 || (major == 1 && minor < 7)) {
116+
return false;
117+
}
118+
break;
119+
}
120+
return true;
121+
}
91122
}

0 commit comments

Comments
 (0)