Search in sources :

Example 1 with Profile

use of com.exalttech.trex.remote.models.profiles.Profile in project trex-stateless-gui by cisco-system-traffic-generator.

the class PacketBuilderHomeController method saveStream.

private boolean saveStream() {
    try {
        String fieldEngineError = packetBuilderController.getFieldEngineError();
        if (!Strings.isNullOrEmpty(fieldEngineError)) {
            streamTabPane.getSelectionModel().select(fieldEngineTab);
            LOG.error("Unable to save stream due to errors in Field Engine:" + fieldEngineError);
            return false;
        }
        updateCurrentProfile();
        if (streamPropertiesController.isValidStreamPropertiesFields()) {
            String yamlData = trafficProfile.convertTrafficProfileToYaml(profileList.toArray(new Profile[profileList.size()]));
            FileUtils.writeStringToFile(new File(yamlFileName), yamlData);
            Util.optimizeMemory();
            return true;
        }
    } catch (Exception ex) {
        LOG.error("Error Saving yaml file", ex);
    }
    return false;
}
Also used : File(java.io.File) Profile(com.exalttech.trex.remote.models.profiles.Profile) TrafficProfile(com.exalttech.trex.util.TrafficProfile) IOException(java.io.IOException)

Example 2 with Profile

use of com.exalttech.trex.remote.models.profiles.Profile in project trex-stateless-gui by cisco-system-traffic-generator.

the class Util method tuneJSONParams.

/**
     * This method tunes the JSON Params. This method was added as the original
     * requirement was to use the VM and RX_Stat as is but it is not the case.
     *
     * @param jsonString
     * @param params
     * @param apiH
     * @return
     */
public static String tuneJSONParams(String jsonString, Params params, String apiH) {
    String tunedString = jsonString;
    if (tunedString.contains("\"params\":{")) {
        tunedString = tunedString.replace("\"params\":{", "\"params\":{ \"api_h\": \"" + apiH + "\",");
    }
    if (params instanceof Profile) {
        String vm = "\"vm\": " + "{\n" + "                    \"instructions\": [],\n" + "                    \"split_by_var\": \"\"\n" + "                }";
        String rxStats = "\"rx_stats\": " + "{\n" + "                    \"enabled\": false\n" + "                }";
        if (!tunedString.contains("\"vm\"")) {
            tunedString = tunedString.replace("\"self_start\"", vm + ",\r" + "\"self_start\"");
        }
        if (!tunedString.contains("\"rx_stats\"")) {
            tunedString = tunedString.replace("\"self_start\"", rxStats + ",\r" + "\"self_start\"");
        }
        if (tunedString.contains("\"vm\":[]")) {
            tunedString = tunedString.replace("\"vm\":[]", vm);
        }
        if (tunedString.contains("\"rx_stats\":[]")) {
            tunedString = tunedString.replace("\"rx_stats\":[]", rxStats);
        }
        // Add Split by vars in the VM if missing.
        if (!tunedString.contains("split_by_var")) {
            tunedString = tunedString.replace("\"vm\":{", "\"vm\":{\n" + "                    \"split_by_var\": \"\",\n");
        }
        // Remove quotes from uInt32
        String pattern = "\"0x[^\"]*\"";
        String originalString = tunedString;
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(originalString);
        while (m.find()) {
            String hexString = m.group(0).replace("\"", "").replace("0x", "");
            int hexValue = (int) Integer.valueOf(hexString, SHORT_LENGTH);
            tunedString = tunedString.replace(m.group(0), "" + hexValue);
        }
        return tunedString;
    }
    return tunedString;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Profile(com.exalttech.trex.remote.models.profiles.Profile)

Example 3 with Profile

use of com.exalttech.trex.remote.models.profiles.Profile in project trex-stateless-gui by cisco-system-traffic-generator.

the class TrafficProfile method getTrafficProfile.

/**
     * @param yamlFile File containing the Traffic Profile Yaml.
     * @return parsed Yaml file
     * @throws java.io.IOException
     */
public Profile[] getTrafficProfile(File yamlFile) throws IOException {
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    Profile[] trafficProfileArray = mapper.readValue(yamlFile, Profile[].class);
    int i = 0;
    for (Profile profile : trafficProfileArray) {
        Map<String, Object> streamAdditionalProperties = profile.getStream().getAdditionalProperties();
        if (streamAdditionalProperties.containsKey("vm")) {
            profile.getStream().setVmRaw(streamAdditionalProperties.get("vm").toString());
        }
        if (streamAdditionalProperties.containsKey("rx_stats")) {
            profile.getStream().setRxStatsRaw(streamAdditionalProperties.get("rx_stats").toString());
        }
        // Check the Binary is in the Yaml File
        if (profile.getStream().getPacket().getBinary() == null) {
            String absolutePath = yamlFile.getAbsolutePath();
            String filePath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));
            String pacpFile = profile.getStream().getPacket().getPcap();
            String encodedPcap = encodePcapFile(filePath + File.separator + pacpFile);
            profile.getStream().getPacket().setBinary(encodedPcap);
        }
        profile.getStream().getPacket().setPcap(null);
        if (profile.getName() == null) {
            profile.setName("Stream" + i++);
        }
    }
    return trafficProfileArray;
}
Also used : YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Profile(com.exalttech.trex.remote.models.profiles.Profile)

Example 4 with Profile

use of com.exalttech.trex.remote.models.profiles.Profile in project trex-stateless-gui by cisco-system-traffic-generator.

the class TrafficProfile method convertStreamNameToInteger.

/**
     * Convert stream name to integer
     *
     * @param trafficProfileArray
     * @return
     */
public Map<String, Integer> convertStreamNameToInteger(Profile[] trafficProfileArray) {
    HashMap<String, Integer> streamNameMap = new HashMap<>();
    int streamID = 0;
    for (Profile profile : trafficProfileArray) {
        if (!streamNameMap.containsKey(profile.getName())) {
            streamNameMap.put(profile.getName(), streamID++);
        }
    }
    return streamNameMap;
}
Also used : HashMap(java.util.HashMap) Profile(com.exalttech.trex.remote.models.profiles.Profile)

Example 5 with Profile

use of com.exalttech.trex.remote.models.profiles.Profile in project trex-stateless-gui by cisco-system-traffic-generator.

the class TrafficProfile method convertTrafficProfileToJson.

/**
     * @param trafficProfileArray
     * @param portID
     * @param handler
     * @return returns a formatted JSON Array of Streams for a given port
     * @throws Exception
     *
     */
public String convertTrafficProfileToJson(Profile[] trafficProfileArray, int portID, String handler) throws Exception {
    Profile[] preparedTrafficProfile = prepareTrafficProfile(trafficProfileArray, portID, handler);
    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(preparedTrafficProfile);
    return Util.toPrettyFormat(jsonString);
}
Also used : Profile(com.exalttech.trex.remote.models.profiles.Profile) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Profile (com.exalttech.trex.remote.models.profiles.Profile)18 TrafficProfile (com.exalttech.trex.util.TrafficProfile)8 File (java.io.File)6 IOException (java.io.IOException)6 TableProfile (com.exalttech.trex.ui.views.models.TableProfile)5 TableProfileStream (com.exalttech.trex.ui.views.models.TableProfileStream)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 DialogWindow (com.exalttech.trex.ui.dialog.DialogWindow)2 Alert (javafx.scene.control.Alert)2 Window (javafx.stage.Window)2 IncorrectRPCMethodException (com.exalttech.trex.remote.exceptions.IncorrectRPCMethodException)1 InvalidRPCResponseException (com.exalttech.trex.remote.exceptions.InvalidRPCResponseException)1 PortAcquireException (com.exalttech.trex.remote.exceptions.PortAcquireException)1 TrafficException (com.exalttech.trex.remote.exceptions.TrafficException)1 Mode (com.exalttech.trex.remote.models.profiles.Mode)1 PacketInfo (com.exalttech.trex.remote.models.profiles.PacketInfo)1 AssignedProfile (com.exalttech.trex.ui.views.models.AssignedProfile)1 ImportPcapTableData (com.exalttech.trex.ui.views.models.ImportPcapTableData)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 EOFException (java.io.EOFException)1