Skip to content

Commit f669c69

Browse files
HongJiangEricleomaatEric
authored andcommitted
Add Util to get Trex server mode --fix comments
1 parent 007ce64 commit f669c69

File tree

7 files changed

+66
-25
lines changed

7 files changed

+66
-25
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.cisco.trex.stateless;
2+
3+
import com.cisco.trex.util.TRexClientUtil;
4+
import com.cisco.trex.util.TRexServerMode;
5+
import org.junit.Test;
6+
7+
public class TRexClientUtilTest {
8+
@Test
9+
public void getModeTest() throws Exception {
10+
TRexServerMode mode = TRexClientUtil.getMode("trex-host", "4501");
11+
System.out.println(mode);
12+
}
13+
}

src/e2e-test/java/com/cisco/trex/stateless/TrexClientUtilTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cisco.trex.stateless;
22

3-
import com.cisco.trex.util.TrexClientUtil;
43
import com.cisco.trex.util.TrexServerMode;
54
import org.junit.Test;
65

src/main/java/com/cisco/trex/ClientBase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.stream.Collectors;
1414
import java.util.stream.StreamSupport;
1515

16+
import com.cisco.trex.util.Constants;
1617
import org.apache.commons.lang.StringUtils;
1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
@@ -49,7 +50,6 @@
4950
public abstract class ClientBase {
5051

5152
protected static final Logger LOGGER = LoggerFactory.getLogger(ClientBase.class);
52-
protected static final String JSON_RPC_VERSION = "2.0";
5353
protected static final Gson GSON = buildGson();
5454
protected String host;
5555
protected String port;
@@ -74,7 +74,7 @@ private String buildRequest(String methodName, Map<String, Object> payload) {
7474
}
7575
Map<String, Object> parameters = new HashMap<>();
7676
parameters.put("id", "aggogxls");
77-
parameters.put("jsonrpc", JSON_RPC_VERSION);
77+
parameters.put("jsonrpc", Constants.JSON_RPC_VERSION);
7878
parameters.put("method", methodName);
7979
payload.put("api_h", apiH);
8080
parameters.put("params", payload);
@@ -232,7 +232,7 @@ public TRexCommand buildCommand(String methodName, Map<String, Object> parameter
232232
Map<String, Object> payload = new HashMap<>();
233233
int cmdId = randomizer.nextInt() & Integer.MAX_VALUE; //get a positive random value
234234
payload.put("id", cmdId);
235-
payload.put("jsonrpc", JSON_RPC_VERSION);
235+
payload.put("jsonrpc", Constants.JSON_RPC_VERSION);
236236
payload.put("method", methodName);
237237
if (!StringUtils.isEmpty(this.masterHandler)) {
238238
payload.put("handler", this.masterHandler);

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.stream.Collectors;
1212
import java.util.stream.StreamSupport;
1313

14+
import com.cisco.trex.util.Constants;
1415
import org.apache.commons.lang.StringUtils;
1516

1617
import com.cisco.trex.ClientBase;
@@ -31,8 +32,6 @@
3132
*/
3233
public class TRexAstfClient extends ClientBase {
3334

34-
private static final Integer API_VERSION_MAJOR = 1;
35-
private static final Integer API_VERSION_MINOR = 7;
3635
private static final String ASTF = "ASTF";
3736

3837
/**
@@ -52,14 +51,14 @@ public TRexAstfClient(String host, String port, String userName) {
5251
protected void serverAPISync() throws TRexConnectionException {
5352
LOGGER.info("Sync API with the TRex");
5453
Map<String, Object> apiVers = new HashMap<>();
55-
apiVers.put("major", API_VERSION_MAJOR);
56-
apiVers.put("minor", API_VERSION_MINOR);
54+
apiVers.put("major", Constants.API_VERSION_MAJOR);
55+
apiVers.put("minor", Constants.ASTF_API_VERSION_MINOR);
5756
apiVers.put("name", ASTF);
5857
TRexClientResult<ApiVersionHandler> result = callMethod("api_sync_v2", apiVers, ApiVersionHandler.class);
5958
if (result.get() == null) {
6059
TRexConnectionException e = new TRexConnectionException(
61-
"Unable to connect to TRex server. Required API version is " + API_VERSION_MAJOR + "."
62-
+ API_VERSION_MINOR);
60+
"Unable to connect to TRex server. Required API version is " + Constants.API_VERSION_MAJOR + "."
61+
+ Constants.ASTF_API_VERSION_MINOR);
6362
LOGGER.error("Unable to sync client with TRex server due to: API_H is null.", e.getMessage());
6463
throw e;
6564
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.cisco.trex.util;
2+
3+
/**
4+
* Class for constants
5+
*/
6+
public final class Constants {
7+
public static final String JSON_RPC_VERSION = "2.0";
8+
public static Integer API_VERSION_MAJOR = 1;
9+
public static Integer ASTF_API_VERSION_MINOR = 7;
10+
}

src/main/java/com/cisco/trex/util/TrexClientUtil.java renamed to src/main/java/com/cisco/trex/util/TRexClientUtil.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,37 @@
1515
/**
1616
* Util class for Trex Client
1717
*/
18-
public final class TrexClientUtil {
18+
public final class TRexClientUtil {
19+
20+
private static final Logger LOGGER = LoggerFactory.getLogger(TRexClientUtil.class);
1921

20-
private static final Logger LOGGER = LoggerFactory.getLogger(TrexClientUtil.class);
21-
private static final String JSON_RPC_VERSION = "2.0";
2222
private static final Random randomizer = new Random();
23-
private static Integer API_VERSION_MAJOR = 1;
24-
private static Integer API_VERSION_MINOR = 7;
23+
2524
private static TRexTransport transport;
2625

27-
private TrexClientUtil() {
26+
private TRexClientUtil() {
2827
}
2928

30-
public static TrexServerMode getMode(String host) {
31-
transport = new TRexTransport(host, "4501", 3000);
29+
public static TRexServerMode getMode(String host, String port) {
30+
transport = new TRexTransport(host, port, 3000);
3231
Map<String, Object> parameters = new HashMap<>();
3332
parameters.put("name", "ASTF");
34-
parameters.put("major", API_VERSION_MAJOR);
35-
parameters.put("minor", API_VERSION_MINOR);
33+
parameters.put("major", Constants.API_VERSION_MAJOR);
34+
parameters.put("minor", Constants.ASTF_API_VERSION_MINOR);
3635
RPCResponse response = null;
3736
try {
3837
response = transport.sendCommand(buildCommand("api_sync_v2", parameters));
3938
} catch (IOException | NullPointerException e) {
4039
LOGGER.debug("Unable to sync client with TRex server .", e.getMessage());
41-
return TrexServerMode.UNKNOWN;
40+
return TRexServerMode.UNKNOWN;
4241
}
4342
if (!response.isFailed()) {
44-
return TrexServerMode.ASTF;
43+
return TRexServerMode.ASTF;
4544
}
4645
if ("RPC configuration mismatch - server RPC configuration: 'STL', client RPC configuration: 'ASTF'".equalsIgnoreCase(response.getError().getSpecificErr())) {
47-
return TrexServerMode.STL;
46+
return TRexServerMode.STL;
4847
}
49-
return TrexServerMode.UNKNOWN;
48+
return TRexServerMode.UNKNOWN;
5049
}
5150

5251
/**
@@ -64,7 +63,7 @@ private static TRexCommand buildCommand(String methodName, Map<String, Object> p
6463
Map<String, Object> payload = new HashMap<>();
6564
int cmdId = randomizer.nextInt() & Integer.MAX_VALUE; //get a positive random value
6665
payload.put("id", cmdId);
67-
payload.put("jsonrpc", JSON_RPC_VERSION);
66+
payload.put("jsonrpc", Constants.JSON_RPC_VERSION);
6867
payload.put("method", methodName);
6968
payload.put("params", parameters);
7069
return new TRexCommand(cmdId, methodName, payload);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.cisco.trex.util;
2+
3+
/**
4+
* Class represent for Trex Server Mode
5+
*/
6+
public enum TRexServerMode {
7+
ASTF("ASTF"),
8+
STL("STL"),
9+
UNKNOWN("UNKNOWN");
10+
11+
private String serverMode;
12+
13+
TRexServerMode(String serverMode) {
14+
this.serverMode = serverMode;
15+
}
16+
17+
public String getServerMode() {
18+
return this.serverMode;
19+
}
20+
21+
}

0 commit comments

Comments
 (0)