diff options
author | Alexandre Montplaisir | 2014-08-14 21:21:24 +0000 |
---|---|---|
committer | Alexandre Montplaisir | 2014-08-19 19:32:25 +0000 |
commit | 5bbb3ba41be50cea6b73c0dee8258066a07fdbc3 (patch) | |
tree | 497303aa4adca7ecd9deb98658a2b1ca2a1cb2e5 | |
parent | f830337dca82a6ddd9da7ac781a05a63765bd348 (diff) | |
download | org.eclipse.linuxtools-5bbb3ba41be50cea6b73c0dee8258066a07fdbc3.tar.gz org.eclipse.linuxtools-5bbb3ba41be50cea6b73c0dee8258066a07fdbc3.tar.xz org.eclipse.linuxtools-5bbb3ba41be50cea6b73c0dee8258066a07fdbc3.zip |
pcap: Cleanup protocol enums
- Convert the Layer interface to an enum, merge with PcapProtocol
- Rework TmfPcapProtocol to not use .name() and .valueOf().
Change-Id: I3f61acf97ccdfbd9469a1f40fc3e9d74857e7afd
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/31915
Tested-by: Hudson CI
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
10 files changed, 97 insertions, 149 deletions
diff --git a/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java index 73c051c51c..bedc50f0e0 100644 --- a/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java +++ b/lttng/org.eclipse.linuxtools.pcap.core.tests/src/org/eclipse/linuxtools/pcap/core/tests/protocol/ProtocolTest.java @@ -13,13 +13,14 @@ package org.eclipse.linuxtools.pcap.core.tests.protocol; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol; -import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocolValues; import org.junit.Test; /** @@ -37,7 +38,7 @@ public class ProtocolTest { public void TestProtocolAttributes() { assertEquals(PcapProtocol.PCAP.getName(), "Packet Capture"); assertEquals(PcapProtocol.PCAP.getShortName(), "pcap"); - assertEquals(PcapProtocol.PCAP.getLayer(), PcapProtocolValues.LAYER_0); + assertEquals(PcapProtocol.PCAP.getLayer(), PcapProtocol.Layer.LAYER_0); } /** @@ -45,34 +46,33 @@ public class ProtocolTest { */ @Test public void TestgetProtocols() { - List<PcapProtocol> list = new ArrayList<>(); List<PcapProtocol> manualListLayer = new ArrayList<>(); - for (int i = PcapProtocolValues.LAYER_0; i <= PcapProtocolValues.LAYER_7; i++) { - List<PcapProtocol> listLayer = PcapProtocol.getProtocolsOnLayer(i); - list.addAll(listLayer); + for (PcapProtocol.Layer layer : PcapProtocol.Layer.values()) { + assertNotNull(layer); + Collection<PcapProtocol> listLayer = PcapProtocol.getProtocolsOnLayer(layer); manualListLayer.clear(); - switch (i) { - case PcapProtocolValues.LAYER_0: + switch (layer) { + case LAYER_0: manualListLayer.add(PcapProtocol.PCAP); break; - case PcapProtocolValues.LAYER_1: + case LAYER_1: break; - case PcapProtocolValues.LAYER_2: + case LAYER_2: manualListLayer.add(PcapProtocol.ETHERNET_II); break; - case PcapProtocolValues.LAYER_3: + case LAYER_3: manualListLayer.add(PcapProtocol.IPV4); break; - case PcapProtocolValues.LAYER_4: + case LAYER_4: manualListLayer.add(PcapProtocol.TCP); manualListLayer.add(PcapProtocol.UDP); break; - case PcapProtocolValues.LAYER_5: + case LAYER_5: break; - case PcapProtocolValues.LAYER_6: + case LAYER_6: break; - case PcapProtocolValues.LAYER_7: + case LAYER_7: manualListLayer.add(PcapProtocol.UNKNOWN); break; default: @@ -80,8 +80,6 @@ public class ProtocolTest { } assertEquals(manualListLayer, listLayer); } - assertEquals(PcapProtocol.getAllProtocols(), list); - } } diff --git a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/endpoint/ProtocolEndpoint.java b/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/endpoint/ProtocolEndpoint.java index 1ba0fe0d98..79fdf72ddf 100644 --- a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/endpoint/ProtocolEndpoint.java +++ b/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/endpoint/ProtocolEndpoint.java @@ -51,7 +51,7 @@ public abstract class ProtocolEndpoint { * packet. */ public ProtocolEndpoint(Packet packet, boolean isSourceEndpoint) { - @Nullable Packet parentPacket = packet.getParentPacket(); + Packet parentPacket = packet.getParentPacket(); if (parentPacket == null) { fParentEndpoint = null; } else { diff --git a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/packet/Packet.java b/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/packet/Packet.java index 3110344c58..e499ae7bd9 100644 --- a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/packet/Packet.java +++ b/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/packet/Packet.java @@ -180,8 +180,7 @@ public abstract class Packet { * @return The most encapsulated packet. */ public Packet getMostEcapsulatedPacket() { - @NonNull - Packet packet = this; + @NonNull Packet packet = this; while (packet.getProtocol() != PcapProtocol.UNKNOWN) { Packet childPacket = packet.getChildPacket(); if (childPacket == null || childPacket.getProtocol() == PcapProtocol.UNKNOWN) { diff --git a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/protocol/PcapProtocol.java b/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/protocol/PcapProtocol.java index 4de6396fe9..d810dbf12c 100644 --- a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/protocol/PcapProtocol.java +++ b/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/protocol/PcapProtocol.java @@ -13,7 +13,7 @@ package org.eclipse.linuxtools.internal.pcap.core.protocol; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collection; import java.util.List; /** @@ -28,7 +28,7 @@ public enum PcapProtocol { * The Pcap Protocol is not a real protocol but is used as an helper to * generate Pcap packets. */ - PCAP("Packet Capture", "pcap", PcapProtocolValues.LAYER_0, false), //$NON-NLS-1$ //$NON-NLS-2$ + PCAP("Packet Capture", "pcap", Layer.LAYER_0, false), //$NON-NLS-1$ //$NON-NLS-2$ // Layer 1 // Should always be empty. @@ -37,23 +37,23 @@ public enum PcapProtocol { /** * The description of the Ethernet II Protocol. */ - ETHERNET_II("Ethernet II", "eth", PcapProtocolValues.LAYER_2, true), //$NON-NLS-1$ //$NON-NLS-2$ + ETHERNET_II("Ethernet II", "eth", Layer.LAYER_2, true), //$NON-NLS-1$ //$NON-NLS-2$ // Layer 3 /** * The description of the Internet Protocol Version 4. */ - IPV4("Internet Protocol Version 4", "ipv4", PcapProtocolValues.LAYER_3, true), //$NON-NLS-1$ //$NON-NLS-2$ + IPV4("Internet Protocol Version 4", "ipv4", Layer.LAYER_3, true), //$NON-NLS-1$ //$NON-NLS-2$ // Layer 4 /** * The description of the Transmission Control Protocol. */ - TCP("Transmission Control Protocol", "tcp", PcapProtocolValues.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$ + TCP("Transmission Control Protocol", "tcp", Layer.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$ /** * The description of the User Datagram Protocol. */ - UDP("User Datagram Protocol", "udp", PcapProtocolValues.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$ + UDP("User Datagram Protocol", "udp", Layer.LAYER_4, true), //$NON-NLS-1$ //$NON-NLS-2$ // Layer 5 @@ -66,15 +66,54 @@ public enum PcapProtocol { * a "payload packet". This is considered to be on layer 7 since its always * the most encapsulated packet if present. */ - UNKNOWN("Payload", "???", PcapProtocolValues.LAYER_7, false); //$NON-NLS-1$ //$NON-NLS-2$ + UNKNOWN("Payload", "???", Layer.LAYER_7, false); //$NON-NLS-1$ //$NON-NLS-2$ + + + /** + * Enum that lists constants related to protocols/layers. + * + * See http://en.wikipedia.org/wiki/OSI_model#Description_of_OSI_layers. + * + * @author Vincent Perot + */ + public static enum Layer { + + /** + * Layer 0. This layer is not an OSI layer but is used as an helper to store + * the pseudo-protocol PCAP. + */ + LAYER_0, + + /** Layer 1 of the OSI model */ + LAYER_1, + + /** Layer 2 of the OSI model */ + LAYER_2, + + /** Layer 3 of the OSI model */ + LAYER_3, + + /** Layer 4 of the OSI model */ + LAYER_4, + + /** Layer 5 of the OSI model */ + LAYER_5, + + /** Layer 6 of the OSI model */ + LAYER_6, + + /** Layer 7 of the OSI model */ + LAYER_7; + } + // Fields private final String fName; private final String fShortName; - private final int fLayer; + private final Layer fLayer; private final boolean fSupportsStream; - private PcapProtocol(String name, String shortName, int layer, boolean supportsStream) { + private PcapProtocol(String name, String shortName, Layer layer, boolean supportsStream) { fName = name; fShortName = shortName; fLayer = layer; @@ -104,7 +143,7 @@ public enum PcapProtocol { * * @return The layer of the protocol. */ - public int getLayer() { + public Layer getLayer() { return fLayer; } @@ -128,12 +167,7 @@ public enum PcapProtocol { * The layer of the protocols. * @return The protocols on that layer. */ - public static List<PcapProtocol> getProtocolsOnLayer(int layer) { - - if (layer > PcapProtocolValues.LAYER_7 || layer < PcapProtocolValues.LAYER_0) { - throw new IllegalArgumentException("The layer is invalid."); //$NON-NLS-1$ - } - + public static Collection<PcapProtocol> getProtocolsOnLayer(Layer layer) { List<PcapProtocol> protocolsOnLayer = new ArrayList<>(); for (PcapProtocol p : PcapProtocol.values()) { if (p.getLayer() == layer) { @@ -142,13 +176,4 @@ public enum PcapProtocol { } return protocolsOnLayer; } - - /** - * Method that returns all the protocol defined. - * - * @return A list containing all the protocols. - */ - public static List<PcapProtocol> getAllProtocols() { - return new ArrayList<>(Arrays.asList(PcapProtocol.values())); - } } diff --git a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/protocol/PcapProtocolValues.java b/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/protocol/PcapProtocolValues.java deleted file mode 100644 index 5ca17ed7bb..0000000000 --- a/lttng/org.eclipse.linuxtools.pcap.core/src/org/eclipse/linuxtools/internal/pcap/core/protocol/PcapProtocolValues.java +++ /dev/null @@ -1,51 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2014 Ericsson - * - * All rights reserved. This program and the accompanying materials are - * made available under the terms of the Eclipse Public License v1.0 which - * accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Vincent Perot - Initial API and implementation - *******************************************************************************/ - -package org.eclipse.linuxtools.internal.pcap.core.protocol; - -/** - * Interface that lists constants related to protocols/layers. - * - * See http://en.wikipedia.org/wiki/OSI_model#Description_of_OSI_layers. - * - * @author Vincent Perot - */ -public interface PcapProtocolValues { - - /** - * Layer 0. This layer is not an OSI layer but is used as an helper to store - * the pseudo-protocol PCAP. - */ - int LAYER_0 = 0; - - /** Layer 1 of the OSI model */ - int LAYER_1 = 1; - - /** Layer 2 of the OSI model */ - int LAYER_2 = 2; - - /** Layer 3 of the OSI model */ - int LAYER_3 = 3; - - /** Layer 4 of the OSI model */ - int LAYER_4 = 4; - - /** Layer 5 of the OSI model */ - int LAYER_5 = 5; - - /** Layer 6 of the OSI model */ - int LAYER_6 = 6; - - /** Layer 7 of the OSI model */ - int LAYER_7 = 7; - -} diff --git a/lttng/org.eclipse.linuxtools.tmf.pcap.core.tests/src/org/eclipse/linuxtools/tmf/pcap/core/tests/analysis/StreamListAnalysisTest.java b/lttng/org.eclipse.linuxtools.tmf.pcap.core.tests/src/org/eclipse/linuxtools/tmf/pcap/core/tests/analysis/StreamListAnalysisTest.java index d4e26e9f28..6acf92615a 100644 --- a/lttng/org.eclipse.linuxtools.tmf.pcap.core.tests/src/org/eclipse/linuxtools/tmf/pcap/core/tests/analysis/StreamListAnalysisTest.java +++ b/lttng/org.eclipse.linuxtools.tmf.pcap.core.tests/src/org/eclipse/linuxtools/tmf/pcap/core/tests/analysis/StreamListAnalysisTest.java @@ -42,7 +42,7 @@ public class StreamListAnalysisTest { public void constructorTest() { try (StreamListAnalysis analysis = new StreamListAnalysis();) { analysis.setId(StreamListAnalysis.ID); - for (TmfPcapProtocol protocol : TmfPcapProtocol.getAllProtocols()) { + for (TmfPcapProtocol protocol : TmfPcapProtocol.values()) { if (protocol.supportsStream()) { assertNotNull(analysis.getBuilder(protocol)); } diff --git a/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/analysis/StreamListAnalysis.java b/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/analysis/StreamListAnalysis.java index 293cb2ab7e..47a2c60c05 100644 --- a/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/analysis/StreamListAnalysis.java +++ b/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/analysis/StreamListAnalysis.java @@ -52,7 +52,7 @@ public class StreamListAnalysis extends TmfAbstractAnalysisModule { public StreamListAnalysis() { super(); fBuilders = new HashMap<>(); - for (TmfPcapProtocol protocol : TmfPcapProtocol.getAllProtocols()) { + for (TmfPcapProtocol protocol : TmfPcapProtocol.values()) { if (protocol.supportsStream()) { fBuilders.put(protocol, new TmfPacketStreamBuilder(protocol)); } diff --git a/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/event/PcapEvent.java b/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/event/PcapEvent.java index 77a3bf41b7..705c521984 100644 --- a/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/event/PcapEvent.java +++ b/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/event/PcapEvent.java @@ -13,7 +13,7 @@ package org.eclipse.linuxtools.internal.tmf.pcap.core.event; import java.nio.ByteBuffer; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; @@ -50,7 +50,11 @@ public class PcapEvent extends TmfEvent { private static final String EMPTY_STRING = ""; //$NON-NLS-1$ private final Packet fPacket; - private @Nullable List<TmfPcapProtocol> fList; + + /** + * Lazy-loaded field representing all the protocols in this event + */ + private transient @Nullable Collection<TmfPcapProtocol> fProtocols; /** * Full constructor. @@ -167,15 +171,15 @@ public class PcapEvent extends TmfEvent { } /** - * Method that returns a list of all the protocols in this PcapEvent. + * Method that returns all the protocols in this PcapEvent. * * @return A list containing all the TmfProtocol. */ - public List<TmfPcapProtocol> getProtocols() { - if (fList != null) { - return fList; + public Collection<TmfPcapProtocol> getProtocols() { + if (fProtocols != null) { + return fProtocols; } - List<TmfPcapProtocol> list = new ArrayList<>(); + ImmutableList.Builder<TmfPcapProtocol> builder = new ImmutableList.Builder<>(); Packet packet = fPacket; // Go to start. @@ -186,21 +190,21 @@ public class PcapEvent extends TmfEvent { if (packet == null) { @SuppressWarnings("null") @NonNull List<TmfPcapProtocol> emptyList = Collections.EMPTY_LIST; - fList = emptyList; - return fList; + fProtocols = emptyList; + return fProtocols; } // Go through all the packets and add them to list. - list.add(ProtocolConversion.wrap(packet.getProtocol())); + builder.add(ProtocolConversion.wrap(packet.getProtocol())); while (packet != null && packet.getChildPacket() != null) { packet = packet.getChildPacket(); if (packet != null) { - list.add(ProtocolConversion.wrap(packet.getProtocol())); + builder.add(ProtocolConversion.wrap(packet.getProtocol())); } } @SuppressWarnings("null") - @NonNull ImmutableList<TmfPcapProtocol> immutableList = ImmutableList.copyOf(list); - fList = immutableList; + @NonNull ImmutableList<TmfPcapProtocol> immutableList = builder.build(); + fProtocols = immutableList; return immutableList; } diff --git a/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/protocol/TmfPcapProtocol.java b/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/protocol/TmfPcapProtocol.java index 0ae703593b..2b6a96336b 100644 --- a/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/protocol/TmfPcapProtocol.java +++ b/lttng/org.eclipse.linuxtools.tmf.pcap.core/src/org/eclipse/linuxtools/internal/tmf/pcap/core/protocol/TmfPcapProtocol.java @@ -12,11 +12,6 @@ package org.eclipse.linuxtools.internal.tmf.pcap.core.protocol; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.eclipse.jdt.annotation.NonNull; import org.eclipse.linuxtools.internal.pcap.core.protocol.PcapProtocol; /** @@ -34,7 +29,7 @@ public enum TmfPcapProtocol { * The Pcap Protocol is not a real protocol but is used as an helper to * generate Pcap packets. */ - PCAP, + PCAP(PcapProtocol.PCAP), // Layer 1 // Should always be empty. @@ -43,23 +38,23 @@ public enum TmfPcapProtocol { /** * The description of the Ethernet II Protocol. */ - ETHERNET_II, + ETHERNET_II(PcapProtocol.ETHERNET_II), // Layer 3 /** * The description of the Internet Protocol Version 4. */ - IPV4, + IPV4(PcapProtocol.IPV4), // Layer 4 /** * The description of the Transmission Control Protocol. */ - TCP, + TCP(PcapProtocol.TCP), /** * The description of the User Datagram Protocol. */ - UDP, + UDP(PcapProtocol.UDP), // Layer 5 @@ -72,20 +67,16 @@ public enum TmfPcapProtocol { * a "payload packet". This is considered to be on layer 7 since its always * the most encapsulated packet if present. */ - UNKNOWN; + UNKNOWN(PcapProtocol.UNKNOWN); private final String fName; private final String fShortName; - private final int fLayer; private final boolean fSupportsStream; - private TmfPcapProtocol() { - @SuppressWarnings("null") - @NonNull String name = this.name(); - fName = PcapProtocol.valueOf(name).getName(); - fShortName = PcapProtocol.valueOf(name).getShortName(); - fLayer = PcapProtocol.valueOf(name).getLayer(); - fSupportsStream = PcapProtocol.valueOf(name).supportsStream(); + private TmfPcapProtocol(PcapProtocol pcapProto) { + fName = pcapProto.getName(); + fShortName = pcapProto.getShortName(); + fSupportsStream = pcapProto.supportsStream(); } /** @@ -107,15 +98,6 @@ public enum TmfPcapProtocol { } /** - * Getter method for the OSI layer of the protocol. - * - * @return The layer of the protocol. - */ - public int getLayer() { - return fLayer; - } - - /** * Getter method that indicates if the protocol supports streams. * * @return Whether the protocol supports streams or not. @@ -124,13 +106,4 @@ public enum TmfPcapProtocol { return fSupportsStream; } - /** - * Method that returns all the protocol defined. - * - * @return A list containing all the protocols. - */ - public static List<TmfPcapProtocol> getAllProtocols() { - return new ArrayList<>(Arrays.asList(TmfPcapProtocol.values())); - } - } diff --git a/lttng/org.eclipse.linuxtools.tmf.pcap.ui/src/org/eclipse/linuxtools/internal/tmf/pcap/ui/stream/StreamListView.java b/lttng/org.eclipse.linuxtools.tmf.pcap.ui/src/org/eclipse/linuxtools/internal/tmf/pcap/ui/stream/StreamListView.java index 9cf3f3ec39..b55e2ea213 100644 --- a/lttng/org.eclipse.linuxtools.tmf.pcap.ui/src/org/eclipse/linuxtools/internal/tmf/pcap/ui/stream/StreamListView.java +++ b/lttng/org.eclipse.linuxtools.tmf.pcap.ui/src/org/eclipse/linuxtools/internal/tmf/pcap/ui/stream/StreamListView.java @@ -335,7 +335,7 @@ public class StreamListView extends TmfView { }); // Add items and tables for each protocol - for (TmfPcapProtocol protocol : TmfPcapProtocol.getAllProtocols()) { + for (TmfPcapProtocol protocol : TmfPcapProtocol.values()) { if (protocol.supportsStream()) { CTabItem item = new CTabItem(fTabFolder, SWT.NONE); item.setText(protocol.getName()); |