22
33import com .cisco .trex .stateless .TRexCommand ;
44import com .cisco .trex .stateless .TRexTransport ;
5+ import com .cisco .trex .stateless .exception .TRexConnectionException ;
56import com .cisco .trex .stateless .model .RPCResponse ;
67import java .io .IOException ;
78import 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