1 package edu.uci.iotproject.detection;
3 import org.pcap4j.core.PcapPacket;
5 import java.util.ArrayList;
7 import java.util.Objects;
10 * Base class for classes that search a traffic trace for sequences of packets that "belong to" a given cluster (in
11 * other words, classes that attempt to classify traffic as pertaining to a given cluster).
13 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
14 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
16 abstract public class AbstractClusterMatcher {
19 * The cluster that describes the sequence of packets that this {@link AbstractClusterMatcher} is trying to detect
20 * in the observed traffic.
22 protected final List<List<PcapPacket>> mCluster;
25 * Observers registered for callbacks from this {@link AbstractClusterMatcher}.
27 protected final List<ClusterMatcherObserver> mObservers;
29 protected AbstractClusterMatcher(List<List<PcapPacket>> cluster, boolean isRangeBased) {
30 // ===================== PRECONDITION SECTION =====================
31 cluster = Objects.requireNonNull(cluster, "cluster cannot be null");
32 if (cluster.isEmpty() || cluster.stream().anyMatch(inner -> inner.isEmpty())) {
33 throw new IllegalArgumentException("cluster is empty (or contains an empty inner List)");
35 for (List<PcapPacket> clusterMember : cluster) {
36 if (clusterMember.size() != cluster.get(0).size()) {
37 throw new IllegalArgumentException("All sequences in cluster must contain the same number of packets");
40 // ================================================================
41 // Let the subclass prune the provided cluster---only if it is not range-based
43 mCluster = pruneCluster(cluster);
47 mObservers = new ArrayList<>();
51 * Register for callbacks from this cluster matcher.
52 * @param observer The target of the callbacks.
54 public final void addObserver(ClusterMatcherObserver observer) {
55 mObservers.add(observer);
59 * Deregister for callbacks from this cluster matcher.
60 * @param observer The callback target that is to be deregistered.
62 public final void removeObserver(ClusterMatcherObserver observer) {
63 mObservers.remove(observer);
67 * Allows subclasses to specify how to prune the input cluster provided to the constructor.
68 * @param cluster The input cluster provided to the constructor.
69 * @return The pruned cluster to use in place of the input cluster.
71 abstract protected List<List<PcapPacket>> pruneCluster(List<List<PcapPacket>> cluster);
73 // TODO: move Direction outside Conversation so that this is less confusing.
74 // abstract protected Conversation.Direction[] getPacketDirections(List<PcapPacket> packets);