<commit_before>/**
 * *****************************************************************************
 * Copyright (c) 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************
 */
package com.exalttech.trex.util;

import com.exalttech.trex.remote.models.profiles.*;
import com.exalttech.trex.ui.views.models.TableProfileStream;
import com.exalttech.trex.util.files.FileManager;
import com.exalttech.trex.util.files.FileType;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import javafx.stage.Window;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.pcap4j.core.NotOpenException;
import org.pcap4j.core.PcapHandle;
import org.pcap4j.core.PcapNativeException;
import org.pcap4j.core.Pcaps;
import org.pcap4j.packet.*;
import org.pcap4j.packet.Packet;
import org.slf4j.helpers.MessageFormatter;

import javax.xml.bind.DatatypeConverter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 *
 * @author GeorgeKh
 */
public class TrafficProfile {

    private static final Logger LOG = Logger.getLogger(TrafficProfile.class.getName());
    public static final String DEFAULT_STREAM_NAME = "Stream";

    /**
     *
     */
    public TrafficProfile() {
        super();

    }

    /**
     *
     * @param packet
     * @return
     */
    public PacketInfo getPacketTypeText(Packet packet) {
        PacketInfo packetInfo = new PacketInfo();

        String packetType = "";
        // Default values for packet info
        packetInfo.setType("Unknown");
        packetInfo.setLength(packet.length());
        if (packet != null) {
            // EthernetPacket
            if (packet.get(EthernetPacket.class) != null) {
                packetType += "Ethernet/";
            }
            // IPPacket
            if (packet.get(IpV4Packet.class) != null) {
                packetType += "IPV4/";
            }
            // TCPPacket
            if (packet.get(TcpPacket.class) != null) {
                packetType += "TCP/";
            }
            // UDPPacket
            if (packet.get(UdpPacket.class) != null) {
                packetType += "UDP";
            }
            if (packetType.endsWith("/")) {
                packetType = packetType.substring(0, packetType.length() - 1);
            }
            packetInfo.setType(packetType);
        }
        return packetInfo;
    }

    /**
     *
     * @param packetBinary
     * @return
     */
    public PacketInfo getPacketInfo(String packetBinary) {

        PacketInfo packetInfo = null;

        try {
            byte[] pkt = Base64.decodeBase64(packetBinary);
            Packet packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
            packetInfo = getPacketTypeText(packet);
        } catch (IllegalRawDataException ex) {
            LOG.error("Error reading packet info", ex);
        }

        return packetInfo;

    }

    /**
     * @param binaryFile
     * @return Encodes the bytes array of a PCAP File using Base64
     */
    public String encodePcapFile(String binaryFile) {

        try {
            PcapHandle handle = Pcaps.openOffline(binaryFile);
            Packet packet = handle.getNextPacketEx();
            handle.close();
            byte[] pkt = packet.getRawData();
            byte[] bytesEncoded = Base64.encodeBase64(pkt);

            return new String(bytesEncoded);
        } catch (IOException | PcapNativeException | TimeoutException | NotOpenException ex) {
            LOG.error("Error encoding pcap file", ex);
            return binaryFile;
        }

    }

    /**
     * @param binaryPacket
     * @throws java.io.IOException
     * @
     * @return decodes the bytes array of a PCAP File using Base64
     */
    public File decodePcapBinary(String binaryPacket) throws IOException {

        byte[] decodedBytes = Base64.decodeBase64(binaryPacket);
        File pcapFile = File.createTempFile("temp-file-name", ".pcap");
        FileUtils.writeByteArrayToFile(pcapFile, decodedBytes);
        return pcapFile;
    }

    /**
     *
     * @param hexPacket
     * @return Converts a Hex packet to a base64 encoded packet.
     */
    public String encodeBinaryFromHexString(String hexPacket) {
        if (!Util.isNullOrEmpty(hexPacket)) {
            byte[] rawData = DatatypeConverter.parseHexBinary(hexPacket);
            return new String(Base64.encodeBase64(rawData));
        }
        return null;
    }

    /**
     * @param yamlFile File containing the Traffic Profile Yaml.
     * @return parsed Yaml file
     * @throws java.io.IOException
     */
    public Profile[] getTrafficProfile(File yamlFile) throws IOException {
        Profile[] trafficProfileArray = parseYamlProfileOrStream(yamlFile);
        adjustProfiles(yamlFile, trafficProfileArray);
        return trafficProfileArray;
    }

    private void adjustProfiles(File yamlFile, Profile[] trafficProfileArray) {
        for (int i=0; i<trafficProfileArray.length; i++) {
            Profile profile = trafficProfileArray[i];

            if (profile.getName() == null) {
                StringBuilder profileName = new StringBuilder(DEFAULT_STREAM_NAME);
                profileName.append("_").append(i);
                profile.setName(profileName.toString());
            }

            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());
            }
            if (profile.getStream().getPacket().getBinary() == null &&
                    profile.getStream().getPacket().getPcap() != null) {
                setBinaryFromPcap(yamlFile, profile);
            }
        }
    }
<commit_msg>Added correct format to vmRaw property<commit_after>/**
 * *****************************************************************************
 * Copyright (c) 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************
 */
package com.exalttech.trex.util;

import com.exalttech.trex.remote.models.profiles.*;
import com.exalttech.trex.ui.views.models.TableProfileStream;
import com.exalttech.trex.util.files.FileManager;
import com.exalttech.trex.util.files.FileType;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.gson.Gson;
import javafx.stage.Window;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.pcap4j.core.NotOpenException;
import org.pcap4j.core.PcapHandle;
import org.pcap4j.core.PcapNativeException;
import org.pcap4j.core.Pcaps;
import org.pcap4j.packet.*;
import org.pcap4j.packet.Packet;
import org.slf4j.helpers.MessageFormatter;

import javax.xml.bind.DatatypeConverter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 *
 * @author GeorgeKh
 */
public class TrafficProfile {

    private static final Logger LOG = Logger.getLogger(TrafficProfile.class.getName());
    public static final String DEFAULT_STREAM_NAME = "Stream";

    /**
     *
     */
    public TrafficProfile() {
        super();

    }

    /**
     *
     * @param packet
     * @return
     */
    public PacketInfo getPacketTypeText(Packet packet) {
        PacketInfo packetInfo = new PacketInfo();

        String packetType = "";
        // Default values for packet info
        packetInfo.setType("Unknown");
        packetInfo.setLength(packet.length());
        if (packet != null) {
            // EthernetPacket
            if (packet.get(EthernetPacket.class) != null) {
                packetType += "Ethernet/";
            }
            // IPPacket
            if (packet.get(IpV4Packet.class) != null) {
                packetType += "IPV4/";
            }
            // TCPPacket
            if (packet.get(TcpPacket.class) != null) {
                packetType += "TCP/";
            }
            // UDPPacket
            if (packet.get(UdpPacket.class) != null) {
                packetType += "UDP";
            }
            if (packetType.endsWith("/")) {
                packetType = packetType.substring(0, packetType.length() - 1);
            }
            packetInfo.setType(packetType);
        }
        return packetInfo;
    }

    /**
     *
     * @param packetBinary
     * @return
     */
    public PacketInfo getPacketInfo(String packetBinary) {

        PacketInfo packetInfo = null;

        try {
            byte[] pkt = Base64.decodeBase64(packetBinary);
            Packet packet = EthernetPacket.newPacket(pkt, 0, pkt.length);
            packetInfo = getPacketTypeText(packet);
        } catch (IllegalRawDataException ex) {
            LOG.error("Error reading packet info", ex);
        }

        return packetInfo;

    }

    /**
     * @param binaryFile
     * @return Encodes the bytes array of a PCAP File using Base64
     */
    public String encodePcapFile(String binaryFile) {

        try {
            PcapHandle handle = Pcaps.openOffline(binaryFile);
            Packet packet = handle.getNextPacketEx();
            handle.close();
            byte[] pkt = packet.getRawData();
            byte[] bytesEncoded = Base64.encodeBase64(pkt);

            return new String(bytesEncoded);
        } catch (IOException | PcapNativeException | TimeoutException | NotOpenException ex) {
            LOG.error("Error encoding pcap file", ex);
            return binaryFile;
        }

    }

    /**
     * @param binaryPacket
     * @throws java.io.IOException
     * @
     * @return decodes the bytes array of a PCAP File using Base64
     */
    public File decodePcapBinary(String binaryPacket) throws IOException {

        byte[] decodedBytes = Base64.decodeBase64(binaryPacket);
        File pcapFile = File.createTempFile("temp-file-name", ".pcap");
        FileUtils.writeByteArrayToFile(pcapFile, decodedBytes);
        return pcapFile;
    }

    /**
     *
     * @param hexPacket
     * @return Converts a Hex packet to a base64 encoded packet.
     */
    public String encodeBinaryFromHexString(String hexPacket) {
        if (!Util.isNullOrEmpty(hexPacket)) {
            byte[] rawData = DatatypeConverter.parseHexBinary(hexPacket);
            return new String(Base64.encodeBase64(rawData));
        }
        return null;
    }

    /**
     * @param yamlFile File containing the Traffic Profile Yaml.
     * @return parsed Yaml file
     * @throws java.io.IOException
     */
    public Profile[] getTrafficProfile(File yamlFile) throws IOException {
        Profile[] trafficProfileArray = parseYamlProfileOrStream(yamlFile);
        adjustProfiles(yamlFile, trafficProfileArray);
        return trafficProfileArray;
    }

    private void adjustProfiles(File yamlFile, Profile[] trafficProfileArray) {
        for (int i=0; i<trafficProfileArray.length; i++) {
            Profile profile = trafficProfileArray[i];

            if (profile.getName() == null) {
                StringBuilder profileName = new StringBuilder(DEFAULT_STREAM_NAME);
                profileName.append("_").append(i);
                profile.setName(profileName.toString());
            }

            Map<String, Object> streamAdditionalProperties = profile.getStream().getAdditionalProperties();
            if (streamAdditionalProperties.containsKey("vm")) {
                Gson gson = new Gson();
                Object vm = streamAdditionalProperties.get("vm");
                profile.getStream().setVmRaw(gson.toJson(vm));
            }
            if (streamAdditionalProperties.containsKey("rx_stats")) {
                profile.getStream().setRxStatsRaw(streamAdditionalProperties.get("rx_stats").toString());
            }
            if (profile.getStream().getPacket().getBinary() == null &&
                    profile.getStream().getPacket().getPcap() != null) {
                setBinaryFromPcap(yamlFile, profile);
            }
        }
    }

{'fraction_non_alphanumeric': 0.12494645152077681, 'fraction_numerical': 0.006568613451377981, 'mean_word_length': 2.551470588235294, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1369084', 'n_tokens_mistral': 3885, 'n_tokens_neox': 3688, 'n_words': 852}