pcap: Rename packages to org.eclipse.tracecompass.*
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.pcap.core / src / org / eclipse / tracecompass / internal / tmf / pcap / core / event / PcapEvent.java
CommitLineData
b6eb4dce
VP
1/*******************************************************************************
2 * Copyright (c) 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Vincent Perot - Initial API and implementation
11 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.internal.tmf.pcap.core.event;
b6eb4dce
VP
14
15import java.nio.ByteBuffer;
e20e3d49 16import java.util.Collection;
b6eb4dce
VP
17import java.util.Collections;
18import java.util.List;
19import java.util.Map;
20
21import org.eclipse.jdt.annotation.NonNull;
22import org.eclipse.jdt.annotation.Nullable;
71f2817f
AM
23import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
24import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
2bdf0193
AM
25import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol;
26import org.eclipse.tracecompass.internal.tmf.pcap.core.util.ProtocolConversion;
27import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
28import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
29import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
30import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
31import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
b6eb4dce
VP
32
33import com.google.common.collect.ImmutableList;
34
35/**
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.
38 *
39 * @author Vincent Perot
40 */
41public class PcapEvent extends TmfEvent {
42
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$
49
50 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
51
52 private final Packet fPacket;
e20e3d49
AM
53
54 /**
55 * Lazy-loaded field representing all the protocols in this event
56 */
57 private transient @Nullable Collection<TmfPcapProtocol> fProtocols;
b6eb4dce
VP
58
59 /**
60 * Full constructor.
61 *
62 * @param trace
63 * the parent trace
64 * @param rank
65 * the event rank (in the trace)
66 * @param timestamp
67 * the event timestamp
68 * @param source
69 * the event source
70 * @param type
71 * the event type
72 * @param content
73 * the event content (payload)
74 * @param reference
75 * the event reference
76 * @param packet
77 * The packet contained in this event
78 */
79 public PcapEvent(ITmfTrace trace,
80 long rank,
81 ITmfTimestamp timestamp,
82 String source,
83 TmfEventType type,
84 ITmfEventField content,
85 String reference,
86 Packet packet) {
87
88 super(trace, rank, timestamp, source, type, content, reference);
89 fPacket = packet;
90 }
91
92 /**
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,
95 * use:
96 * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br>
97 * It returns null if the protocol is inexistent in the PcapEvent.
98 *
99 * @param protocol
100 * The specified protocol
101 * @return A map containing the fields.
102 */
c88feda9
AM
103 public @Nullable Map<String, String> getFields(TmfPcapProtocol protocol) {
104 PcapProtocol p = ProtocolConversion.unwrap(protocol);
b6eb4dce
VP
105 Packet packet = fPacket.getPacket(p);
106 if (packet == null) {
107 return null;
108 }
109 return packet.getFields();
110 }
111
112 /**
113 * Method that returns the payload at a certain protocol level. It returns
114 * null if the protocol is inexistent in the PcapEvent.
115 *
116 * @param protocol
117 * The specified protocol
118 * @return The payload as a ByteBuffer.
119 */
c88feda9
AM
120 public @Nullable ByteBuffer getPayload(TmfPcapProtocol protocol) {
121 PcapProtocol p = ProtocolConversion.unwrap(protocol);
b6eb4dce
VP
122 Packet packet = fPacket.getPacket(p);
123 if (packet == null) {
124 return null;
125 }
126 return packet.getPayload();
127 }
128
129 /**
130 * Method that returns the source endpoint at a certain protocol level. It
131 * returns null if the protocol is inexistent in the PcapEvent.
132 *
133 * @param protocol
134 * The specified protocol
135 * @return The source endpoint.
136 */
c88feda9
AM
137 public @Nullable String getSourceEndpoint(TmfPcapProtocol protocol) {
138 PcapProtocol p = ProtocolConversion.unwrap(protocol);
b6eb4dce
VP
139 Packet packet = fPacket.getPacket(p);
140 if (packet == null) {
141 return null;
142 }
143 return packet.getSourceEndpoint().toString();
144 }
145
146 /**
147 * Method that returns the destination endpoint at a certain protocol level.
148 * It returns null if the protocol is inexistent in the PcapEvent.
149 *
150 * @param protocol
151 * The specified protocol
152 * @return The destination endpoint.
153 */
c88feda9
AM
154 public @Nullable String getDestinationEndpoint(TmfPcapProtocol protocol) {
155 PcapProtocol p = ProtocolConversion.unwrap(protocol);
b6eb4dce
VP
156 Packet packet = fPacket.getPacket(p);
157 if (packet == null) {
158 return null;
159 }
160 return packet.getDestinationEndpoint().toString();
161 }
162
163 /**
164 * Method that returns the most encapsulated protocol in this PcapEvent. If
165 * it is an unknown protocol, it returns the last known protocol.
166 *
167 * @return The most encapsulated TmfProtocol.
168 */
c88feda9 169 public TmfPcapProtocol getMostEncapsulatedProtocol() {
b6eb4dce
VP
170 return ProtocolConversion.wrap(fPacket.getMostEcapsulatedPacket().getProtocol());
171 }
172
173 /**
e20e3d49 174 * Method that returns all the protocols in this PcapEvent.
b6eb4dce
VP
175 *
176 * @return A list containing all the TmfProtocol.
177 */
e20e3d49
AM
178 public Collection<TmfPcapProtocol> getProtocols() {
179 if (fProtocols != null) {
180 return fProtocols;
b6eb4dce 181 }
e20e3d49 182 ImmutableList.Builder<TmfPcapProtocol> builder = new ImmutableList.Builder<>();
b6eb4dce
VP
183 Packet packet = fPacket;
184
185 // Go to start.
186 while (packet != null && packet.getParentPacket() != null) {
187 packet = packet.getParentPacket();
188 }
189
190 if (packet == null) {
191 @SuppressWarnings("null")
c88feda9 192 @NonNull List<TmfPcapProtocol> emptyList = Collections.EMPTY_LIST;
e20e3d49
AM
193 fProtocols = emptyList;
194 return fProtocols;
b6eb4dce
VP
195 }
196 // Go through all the packets and add them to list.
e20e3d49 197 builder.add(ProtocolConversion.wrap(packet.getProtocol()));
b6eb4dce
VP
198 while (packet != null && packet.getChildPacket() != null) {
199 packet = packet.getChildPacket();
200 if (packet != null) {
e20e3d49 201 builder.add(ProtocolConversion.wrap(packet.getProtocol()));
b6eb4dce
VP
202 }
203 }
204
205 @SuppressWarnings("null")
e20e3d49
AM
206 @NonNull ImmutableList<TmfPcapProtocol> immutableList = builder.build();
207 fProtocols = immutableList;
b6eb4dce
VP
208 return immutableList;
209 }
210
211 /**
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
214 * packages
215 *
216 * @return The packet.
217 */
218 Packet getPacket() {
219 return fPacket;
220 }
221
222 @Override
223 public String toString() {
224 return fPacket.getGlobalSummaryString();
225 }
226
227 /**
228 * Return the signification of the PcapEvent at a specific protocol level.
229 *
230 * @param protocol
231 * The specified protocol.
232 * @return The signification as a String.
233 */
c88feda9
AM
234 public String toString(TmfPcapProtocol protocol) {
235 PcapProtocol p = ProtocolConversion.unwrap(protocol);
b6eb4dce
VP
236 Packet packet = fPacket.getPacket(p);
237 if (packet == null) {
238 return EMPTY_STRING;
239 }
240 return packet.getLocalSummaryString();
241 }
242}
This page took 0.044481 seconds and 5 git commands to generate.