Skip to content

Commit 80de0de

Browse files
committed
differ acquire/release behaviors between STL and ASTF mode
Signed-off-by: Leo Ma <[email protected]>
1 parent 3979997 commit 80de0de

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

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

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ private String call(String json) {
8888
}
8989

9090
private List<TRexCommand> buildRemoveCaptureCommand(List<Integer> captureIds) {
91-
return captureIds
92-
.stream()
91+
return captureIds.stream()
9392
.map(
9493
captureId -> {
9594
Map<String, Object> parameters = new HashMap<>();
@@ -108,8 +107,7 @@ private List<TRexCommand> buildRemoveCaptureCommand(List<Integer> captureIds) {
108107
public List<Port> getPorts() {
109108
LOGGER.debug("Getting ports list.");
110109
List<Port> ports = getSystemInfo().getPorts();
111-
ports
112-
.stream()
110+
ports.stream()
113111
.forEach(
114112
port -> {
115113
TRexClientResult<PortStatus> result = getPortStatus(port.getIndex());
@@ -136,8 +134,7 @@ public Port getPortByIndex(int portIndex) {
136134
LOGGER.error(
137135
String.format("Port with index %s was not found. Returning empty port", portIndex));
138136
}
139-
return getPorts()
140-
.stream()
137+
return getPorts().stream()
141138
.filter(p -> p.getIndex() == portIndex)
142139
.findFirst()
143140
.orElse(new Port());
@@ -511,23 +508,6 @@ public SystemInfo getSystemInfo() {
511508
return GSON.fromJson(getResultFromResponse(json), SystemInfo.class);
512509
}
513510

514-
/**
515-
* Release Port
516-
*
517-
* @param portIndex
518-
* @return PortStatus
519-
*/
520-
public PortStatus releasePort(int portIndex) {
521-
Map<String, Object> payload = createPayload(portIndex);
522-
payload.put("user", userName);
523-
String result = callMethod("release", payload);
524-
if (result.contains("must acquire the context")) {
525-
LOGGER.info("Port is not owned by this session, already released or never acquired");
526-
}
527-
portHandlers.remove(portIndex);
528-
return getPortStatus(portIndex).get();
529-
}
530-
531511
protected Map<String, Object> createPayload(int portIndex) {
532512
Map<String, Object> payload = new HashMap<>();
533513
payload.put(PORT_ID, portIndex);
@@ -552,13 +532,4 @@ protected Map<String, Object> createPayload(int portIndex, String profileId) {
552532
}
553533

554534
protected abstract void serverAPISync() throws TRexConnectionException;
555-
556-
/**
557-
* Acquire Port to be able to apply configuration to it
558-
*
559-
* @param port
560-
* @param force
561-
* @return PortStatus
562-
*/
563-
public abstract PortStatus acquirePort(int port, Boolean force);
564535
}

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.cisco.trex.stateful.model.stats.MetaData;
99
import com.cisco.trex.stateless.exception.TRexConnectionException;
1010
import com.cisco.trex.stateless.model.ApiVersionHandler;
11-
import com.cisco.trex.stateless.model.PortStatus;
1211
import com.cisco.trex.stateless.model.TRexClientResult;
1312
import com.cisco.trex.util.Constants;
1413
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -219,12 +218,15 @@ public void updateLatencyTrafficRate(int mult) {
219218
this.callMethod("update_latency", payload);
220219
}
221220

222-
@Override
223-
public PortStatus acquirePort(int portIndex, Boolean force) {
221+
/**
222+
* In ASTF mode all ports will be acquired in a single call, not support to acquire a single port
223+
*
224+
* @param force
225+
*/
226+
public void acquirePorts(Boolean force) {
224227
Map<String, Object> payload = createPayload();
225228
payload.put("user", userName);
226229
payload.put("force", force);
227-
payload.put(PORT_ID, portIndex);
228230
String json = callMethod("acquire", payload);
229231
Set<Entry<String, JsonElement>> entrySet;
230232
try {
@@ -243,7 +245,21 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
243245
portHandlers.put(Integer.parseInt(entry.getKey()), entry.getValue().getAsString());
244246
}
245247
LOGGER.info("portHandlers is: {} ", portHandlers);
246-
return getPortStatus(portIndex).get();
248+
}
249+
250+
/** Release Ports */
251+
public void releasePorts() {
252+
if (StringUtils.isEmpty(masterHandler)) {
253+
LOGGER.debug("No handler assigned, ports are not acquired.");
254+
} else {
255+
Map<String, Object> payload = createPayload();
256+
payload.put("user", userName);
257+
String result = callMethod("release", payload);
258+
if (result.contains("must acquire the context")) {
259+
LOGGER.info("Ports are not owned by this session, already released or never acquired");
260+
}
261+
portHandlers.clear();
262+
}
247263
}
248264

249265
/**
@@ -306,6 +322,9 @@ public void clearProfile(String profileId) {
306322
* @return profile id list
307323
*/
308324
public List<String> getProfileIds() {
325+
if (StringUtils.isEmpty(masterHandler)) {
326+
return Collections.emptyList();
327+
}
309328
Map<String, Object> payload = createPayload();
310329
String json = callMethod("get_profile_list", payload);
311330
JsonArray ids = getResultFromResponse(json).getAsJsonArray();

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ protected void serverAPISync() throws TRexConnectionException {
122122
LOGGER.info("Received api_H: {}", apiH);
123123
}
124124

125-
@Deprecated
126-
@Override
127125
public PortStatus acquirePort(int portIndex, Boolean force) {
128126
Map<String, Object> payload = createPayload(portIndex);
129127
payload.put("session_id", SESSON_ID);
@@ -135,6 +133,27 @@ public PortStatus acquirePort(int portIndex, Boolean force) {
135133
return getPortStatus(portIndex).get();
136134
}
137135

136+
/**
137+
* Release Port
138+
*
139+
* @param portIndex
140+
* @return PortStatus
141+
*/
142+
public PortStatus releasePort(int portIndex) {
143+
if (!portHandlers.containsKey(portIndex)) {
144+
LOGGER.debug("No handler assigned, port is not acquired.");
145+
} else {
146+
Map<String, Object> payload = createPayload(portIndex);
147+
payload.put("user", userName);
148+
String result = callMethod("release", payload);
149+
if (result.contains("must acquire the context")) {
150+
LOGGER.info("Port is not owned by this session, already released or never acquired");
151+
}
152+
portHandlers.remove(portIndex);
153+
}
154+
return getPortStatus(portIndex).get();
155+
}
156+
138157
/**
139158
* Reset port stop traffic, remove all streams, remove rx queue, disable service mode and release
140159
* port

0 commit comments

Comments
 (0)