|
1 | 1 | package com.cisco.trex.stateful; |
2 | 2 |
|
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | +import java.security.MessageDigest; |
| 5 | +import java.security.NoSuchAlgorithmException; |
3 | 6 | import java.util.HashMap; |
4 | 7 | import java.util.Map; |
5 | 8 | import java.util.Map.Entry; |
@@ -65,6 +68,20 @@ private Map<String, Object> createPayload() { |
65 | 68 | return payload; |
66 | 69 | } |
67 | 70 |
|
| 71 | + private static String calculateMd5(String profile) { |
| 72 | + try { |
| 73 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 74 | + byte[] hashInBytes = md.digest(profile.getBytes(StandardCharsets.UTF_8)); |
| 75 | + StringBuilder sb = new StringBuilder(); |
| 76 | + for (byte b : hashInBytes) { |
| 77 | + sb.append(String.format("%02x", b)); |
| 78 | + } |
| 79 | + return sb.toString(); |
| 80 | + } catch (NoSuchAlgorithmException e) { |
| 81 | + throw new IllegalStateException("Could not generate MD5", e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
68 | 85 | /** |
69 | 86 | * start traffic on all ports on the last loaded profile |
70 | 87 | * |
@@ -179,22 +196,31 @@ public PortStatus acquirePort(int portIndex, Boolean force) { |
179 | 196 | } |
180 | 197 |
|
181 | 198 | /** |
182 | | - * @param fragFirst |
183 | | - * @param fragLast |
184 | | - * @param fragmentData |
185 | | - * @param totalSize |
| 199 | + * Load profile object as string and upload in fragments |
| 200 | + * |
| 201 | + * @param profile |
186 | 202 | */ |
187 | | - public void loadProfile(boolean fragFirst, boolean fragLast, String fragmentData, long totalSize) { |
188 | | - Map<String, Object> payload = createPayload(); |
189 | | - if (fragFirst) { |
190 | | - payload.put("frag_first", true); |
191 | | - payload.put("total_size", totalSize); |
192 | | - } |
193 | | - if (fragLast) { |
194 | | - payload.put("frag_last", true); |
| 203 | + public void loadProfile(String profile) { |
| 204 | + int indexStart = 0; |
| 205 | + int fragmentLength = 1000; //shorter length the first time |
| 206 | + int totalLength = profile.length(); |
| 207 | + while (totalLength > indexStart) { |
| 208 | + int indexEnd = indexStart + fragmentLength; |
| 209 | + Map<String, Object> payload = createPayload(); |
| 210 | + if (indexStart == 0) { //is first fragment |
| 211 | + payload.put("frag_first", true); |
| 212 | + payload.put("total_size", totalLength); |
| 213 | + payload.put("md5", calculateMd5(profile)); |
| 214 | + } |
| 215 | + if (indexEnd >= totalLength) { |
| 216 | + payload.put("frag_last", true); |
| 217 | + indexEnd = totalLength; |
| 218 | + } |
| 219 | + payload.put("fragment", profile.subSequence(indexStart, indexEnd)); |
| 220 | + this.callMethod("profile_fragment", payload); |
| 221 | + indexStart = indexEnd; |
| 222 | + fragmentLength = 500000; //larger fragments after first fragment |
195 | 223 | } |
196 | | - payload.put("fragment", fragmentData); |
197 | | - this.callMethod("profile_fragment", payload); |
198 | 224 | } |
199 | 225 |
|
200 | 226 | /** |
|
0 commit comments