1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.pcap
.core
.event
;
15 import java
.nio
.ByteBuffer
;
16 import java
.util
.Collection
;
17 import java
.util
.Collections
;
18 import java
.util
.List
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.packet
.Packet
;
24 import org
.eclipse
.linuxtools
.internal
.pcap
.core
.protocol
.PcapProtocol
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.pcap
.core
.protocol
.TmfPcapProtocol
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.pcap
.core
.util
.ProtocolConversion
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
33 import com
.google
.common
.collect
.ImmutableList
;
36 * Class that extends TmfEvent to allow TMF to use the packets from the parser.
37 * It is a simple TmfEvent that wraps a Packet.
39 * @author Vincent Perot
41 public class PcapEvent
extends TmfEvent
{
43 /** Packet Source Field ID */
44 public static final String EVENT_FIELD_PACKET_SOURCE
= ":packetsource:"; //$NON-NLS-1$
45 /** Packet Destination Field ID */
46 public static final String EVENT_FIELD_PACKET_DESTINATION
= ":packetdestination:"; //$NON-NLS-1$
47 /** Packet Protocol Field ID */
48 public static final String EVENT_FIELD_PACKET_PROTOCOL
= ":protocol:"; //$NON-NLS-1$
50 private static final String EMPTY_STRING
= ""; //$NON-NLS-1$
52 private final Packet fPacket
;
55 * Lazy-loaded field representing all the protocols in this event
57 private transient @Nullable Collection
<TmfPcapProtocol
> fProtocols
;
65 * the event rank (in the trace)
73 * the event content (payload)
77 * The packet contained in this event
79 public PcapEvent(ITmfTrace trace
,
81 ITmfTimestamp timestamp
,
84 ITmfEventField content
,
88 super(trace
, rank
, timestamp
, source
, type
, content
, reference
);
93 * Method that returns an immutable map containing all the fields of a
94 * packet at a certain protocol. For instance, to get the Source IP Address,
96 * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br>
97 * It returns null if the protocol is inexistent in the PcapEvent.
100 * The specified protocol
101 * @return A map containing the fields.
103 public @Nullable Map
<String
, String
> getFields(TmfPcapProtocol protocol
) {
104 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
105 Packet packet
= fPacket
.getPacket(p
);
106 if (packet
== null) {
109 return packet
.getFields();
113 * Method that returns the payload at a certain protocol level. It returns
114 * null if the protocol is inexistent in the PcapEvent.
117 * The specified protocol
118 * @return The payload as a ByteBuffer.
120 public @Nullable ByteBuffer
getPayload(TmfPcapProtocol protocol
) {
121 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
122 Packet packet
= fPacket
.getPacket(p
);
123 if (packet
== null) {
126 return packet
.getPayload();
130 * Method that returns the source endpoint at a certain protocol level. It
131 * returns null if the protocol is inexistent in the PcapEvent.
134 * The specified protocol
135 * @return The source endpoint.
137 public @Nullable String
getSourceEndpoint(TmfPcapProtocol protocol
) {
138 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
139 Packet packet
= fPacket
.getPacket(p
);
140 if (packet
== null) {
143 return packet
.getSourceEndpoint().toString();
147 * Method that returns the destination endpoint at a certain protocol level.
148 * It returns null if the protocol is inexistent in the PcapEvent.
151 * The specified protocol
152 * @return The destination endpoint.
154 public @Nullable String
getDestinationEndpoint(TmfPcapProtocol protocol
) {
155 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
156 Packet packet
= fPacket
.getPacket(p
);
157 if (packet
== null) {
160 return packet
.getDestinationEndpoint().toString();
164 * Method that returns the most encapsulated protocol in this PcapEvent. If
165 * it is an unknown protocol, it returns the last known protocol.
167 * @return The most encapsulated TmfProtocol.
169 public TmfPcapProtocol
getMostEncapsulatedProtocol() {
170 return ProtocolConversion
.wrap(fPacket
.getMostEcapsulatedPacket().getProtocol());
174 * Method that returns all the protocols in this PcapEvent.
176 * @return A list containing all the TmfProtocol.
178 public Collection
<TmfPcapProtocol
> getProtocols() {
179 if (fProtocols
!= null) {
182 ImmutableList
.Builder
<TmfPcapProtocol
> builder
= new ImmutableList
.Builder
<>();
183 Packet packet
= fPacket
;
186 while (packet
!= null && packet
.getParentPacket() != null) {
187 packet
= packet
.getParentPacket();
190 if (packet
== null) {
191 @SuppressWarnings("null")
192 @NonNull List
<TmfPcapProtocol
> emptyList
= Collections
.EMPTY_LIST
;
193 fProtocols
= emptyList
;
196 // Go through all the packets and add them to list.
197 builder
.add(ProtocolConversion
.wrap(packet
.getProtocol()));
198 while (packet
!= null && packet
.getChildPacket() != null) {
199 packet
= packet
.getChildPacket();
200 if (packet
!= null) {
201 builder
.add(ProtocolConversion
.wrap(packet
.getProtocol()));
205 @SuppressWarnings("null")
206 @NonNull ImmutableList
<TmfPcapProtocol
> immutableList
= builder
.build();
207 fProtocols
= immutableList
;
208 return immutableList
;
212 * Getter method that returns the packet. This is default visible since it
213 * is only used by tmf.pcap.core and thus should not be visible to other
216 * @return The packet.
223 public String
toString() {
224 return fPacket
.getGlobalSummaryString();
228 * Return the signification of the PcapEvent at a specific protocol level.
231 * The specified protocol.
232 * @return The signification as a String.
234 public String
toString(TmfPcapProtocol protocol
) {
235 PcapProtocol p
= ProtocolConversion
.unwrap(protocol
);
236 Packet packet
= fPacket
.getPacket(p
);
237 if (packet
== null) {
240 return packet
.getLocalSummaryString();