Fix some null warnings
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.tmf.pcap.core / src / org / eclipse / tracecompass / internal / tmf / pcap / core / event / PcapEvent.java
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
13 package org.eclipse.tracecompass.internal.tmf.pcap.core.event;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.nio.ByteBuffer;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
24 import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
25 import org.eclipse.tracecompass.internal.tmf.pcap.core.protocol.TmfPcapProtocol;
26 import org.eclipse.tracecompass.internal.tmf.pcap.core.util.ProtocolConversion;
27 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
28 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
29 import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
30 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
32
33 import 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 */
41 public 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;
53 private final String fSource;
54 private final String fReference;
55
56 /**
57 * Lazy-loaded field representing all the protocols in this event
58 */
59 private transient @Nullable Collection<TmfPcapProtocol> fProtocols;
60
61 /**
62 * Full constructor.
63 *
64 * @param trace
65 * the parent trace
66 * @param rank
67 * the event rank (in the trace)
68 * @param timestamp
69 * the event timestamp
70 * @param source
71 * the event source
72 * @param type
73 * the event type
74 * @param content
75 * the event content (payload)
76 * @param reference
77 * the event reference
78 * @param packet
79 * The packet contained in this event
80 */
81 public PcapEvent(ITmfTrace trace,
82 long rank,
83 ITmfTimestamp timestamp,
84 String source,
85 TmfEventType type,
86 ITmfEventField content,
87 String reference,
88 Packet packet) {
89
90 super(trace, rank, timestamp, type, content);
91 fPacket = packet;
92 fSource = source;
93 fReference = reference;
94 }
95
96 /**
97 * Return this event's source.
98 *
99 * @return The event's source
100 */
101 public String getSource() {
102 return fSource;
103 }
104
105 /**
106 * Return this event's reference
107 *
108 * @return The event's reference
109 */
110 public String getReference() {
111 return fReference;
112 }
113
114 /**
115 * Method that returns an immutable map containing all the fields of a
116 * packet at a certain protocol. For instance, to get the Source IP Address,
117 * use:
118 * <code>event.getFields(TmfProtocol.IPV4).get("Source IP Address");</code>. <br>
119 * It returns null if the protocol is inexistent in the PcapEvent.
120 *
121 * @param protocol
122 * The specified protocol
123 * @return A map containing the fields.
124 */
125 public @Nullable Map<String, String> getFields(TmfPcapProtocol protocol) {
126 PcapProtocol p = ProtocolConversion.unwrap(protocol);
127 Packet packet = fPacket.getPacket(p);
128 if (packet == null) {
129 return null;
130 }
131 return packet.getFields();
132 }
133
134 /**
135 * Method that returns the payload at a certain protocol level. It returns
136 * null if the protocol is inexistent in the PcapEvent.
137 *
138 * @param protocol
139 * The specified protocol
140 * @return The payload as a ByteBuffer.
141 */
142 public @Nullable ByteBuffer getPayload(TmfPcapProtocol protocol) {
143 PcapProtocol p = ProtocolConversion.unwrap(protocol);
144 Packet packet = fPacket.getPacket(p);
145 if (packet == null) {
146 return null;
147 }
148 return packet.getPayload();
149 }
150
151 /**
152 * Method that returns the source endpoint at a certain protocol level. It
153 * returns null if the protocol is inexistent in the PcapEvent.
154 *
155 * @param protocol
156 * The specified protocol
157 * @return The source endpoint.
158 */
159 public @Nullable String getSourceEndpoint(TmfPcapProtocol protocol) {
160 PcapProtocol p = ProtocolConversion.unwrap(protocol);
161 Packet packet = fPacket.getPacket(p);
162 if (packet == null) {
163 return null;
164 }
165 return packet.getSourceEndpoint().toString();
166 }
167
168 /**
169 * Method that returns the destination endpoint at a certain protocol level.
170 * It returns null if the protocol is inexistent in the PcapEvent.
171 *
172 * @param protocol
173 * The specified protocol
174 * @return The destination endpoint.
175 */
176 public @Nullable String getDestinationEndpoint(TmfPcapProtocol protocol) {
177 PcapProtocol p = ProtocolConversion.unwrap(protocol);
178 Packet packet = fPacket.getPacket(p);
179 if (packet == null) {
180 return null;
181 }
182 return packet.getDestinationEndpoint().toString();
183 }
184
185 /**
186 * Method that returns the most encapsulated protocol in this PcapEvent. If
187 * it is an unknown protocol, it returns the last known protocol.
188 *
189 * @return The most encapsulated TmfProtocol.
190 */
191 public TmfPcapProtocol getMostEncapsulatedProtocol() {
192 return ProtocolConversion.wrap(fPacket.getMostEcapsulatedPacket().getProtocol());
193 }
194
195 /**
196 * Method that returns all the protocols in this PcapEvent.
197 *
198 * @return A list containing all the TmfProtocol.
199 */
200 public Collection<TmfPcapProtocol> getProtocols() {
201 if (fProtocols != null) {
202 return fProtocols;
203 }
204 ImmutableList.Builder<TmfPcapProtocol> builder = new ImmutableList.Builder<>();
205 Packet packet = fPacket;
206
207 // Go to start.
208 while (packet != null && packet.getParentPacket() != null) {
209 packet = packet.getParentPacket();
210 }
211
212 if (packet == null) {
213 fProtocols = Collections.EMPTY_LIST;
214 return fProtocols;
215 }
216 // Go through all the packets and add them to list.
217 builder.add(ProtocolConversion.wrap(packet.getProtocol()));
218 while (packet != null && packet.getChildPacket() != null) {
219 packet = packet.getChildPacket();
220 if (packet != null) {
221 builder.add(ProtocolConversion.wrap(packet.getProtocol()));
222 }
223 }
224
225 fProtocols = checkNotNull(builder.build());
226 return fProtocols;
227 }
228
229 /**
230 * Getter method that returns the packet. This is default visible since it
231 * is only used by tmf.pcap.core and thus should not be visible to other
232 * packages
233 *
234 * @return The packet.
235 */
236 Packet getPacket() {
237 return fPacket;
238 }
239
240 @Override
241 public String toString() {
242 return fPacket.getGlobalSummaryString();
243 }
244
245 /**
246 * Return the signification of the PcapEvent at a specific protocol level.
247 *
248 * @param protocol
249 * The specified protocol.
250 * @return The signification as a String.
251 */
252 public String toString(TmfPcapProtocol protocol) {
253 PcapProtocol p = ProtocolConversion.unwrap(protocol);
254 Packet packet = fPacket.getPacket(p);
255 if (packet == null) {
256 return EMPTY_STRING;
257 }
258 return packet.getLocalSummaryString();
259 }
260 }
This page took 0.044818 seconds and 5 git commands to generate.