pcap.core: making some classes final
[deliverable/tracecompass.git] / pcap / org.eclipse.tracecompass.tmf.pcap.core / src / org / eclipse / tracecompass / internal / tmf / pcap / core / util / PcapEventFactory.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.util;
b6eb4dce 14
5db5a3a4
AM
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
333a2acb 17import java.nio.file.Path;
b6eb4dce
VP
18import java.util.ArrayList;
19import java.util.HashMap;
20import java.util.List;
21import java.util.Map;
22
23import org.eclipse.jdt.annotation.NonNull;
24import org.eclipse.jdt.annotation.Nullable;
71f2817f
AM
25import org.eclipse.tracecompass.internal.pcap.core.packet.Packet;
26import org.eclipse.tracecompass.internal.pcap.core.protocol.PcapProtocol;
27import org.eclipse.tracecompass.internal.pcap.core.protocol.pcap.PcapPacket;
28import org.eclipse.tracecompass.internal.pcap.core.trace.PcapFile;
29import org.eclipse.tracecompass.internal.pcap.core.util.LinkTypeHelper;
30import org.eclipse.tracecompass.internal.pcap.core.util.PcapTimestampScale;
2bdf0193
AM
31import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEvent;
32import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventField;
33import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapEventType;
34import org.eclipse.tracecompass.internal.tmf.pcap.core.event.PcapRootEventField;
35import org.eclipse.tracecompass.internal.tmf.pcap.core.trace.PcapTrace;
36import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
37import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
38import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
39import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
40import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
b6eb4dce
VP
41
42/**
43 * Factory class that helps generating Pcap Events.
44 *
45 * @author Vincent Perot
46 */
1ad849b8 47public final class PcapEventFactory {
b6eb4dce 48
b6eb4dce
VP
49 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
50
c88feda9 51 private static final Map<PcapProtocol, TmfEventType> fEventTypes = new HashMap<>();
b6eb4dce
VP
52
53 private PcapEventFactory() {
54 }
55
56 /**
57 * Method that create a PcapEvent from a packet.
58 *
59 * @param pcapPacket
60 * The packet to generate the event from.
61 * @param pcap
62 * The pcap file to which the packet belongs.
63 * @param trace
64 * The trace to which this packet belongs.
65 * @return The generated PcapEvent.
66 */
67 public static @Nullable PcapEvent createEvent(PcapPacket pcapPacket, PcapFile pcap, PcapTrace trace) {
68 long rank = pcapPacket.getIndex();
69 long timestamp = pcapPacket.getTimestamp();
70 PcapTimestampScale scale = pcapPacket.getTimestampScale();
71 ITmfTimestamp tmfTimestamp;
72 switch (scale) {
73 case MICROSECOND:
6b44794a 74 long us = trace.getTimestampTransform().transform(timestamp * 1000) / 1000;
b2c971ec 75 tmfTimestamp = TmfTimestamp.fromMicros(us);
b6eb4dce
VP
76 break;
77 case NANOSECOND:
6b44794a 78 long ns = trace.getTimestampTransform().transform(timestamp);
b2c971ec 79 tmfTimestamp = TmfTimestamp.fromNanos(ns);
b6eb4dce
VP
80 break;
81 default:
82 throw new IllegalArgumentException("The timestamp precision is not valid!"); //$NON-NLS-1$
83 }
333a2acb 84 Path filePath = pcap.getPath().getFileName();
5db5a3a4 85 @NonNull String fileName = (filePath == null ? EMPTY_STRING : checkNotNull(filePath.toString()));
333a2acb 86
b6eb4dce
VP
87 String dataLink = Messages.PcapEventFactory_LinkType + ':' + LinkTypeHelper.toString((int) pcapPacket.getPcapFile().getDataLinkType());
88
89 ITmfEventField[] fields = generatePacketFields(pcapPacket);
40dfafb3 90 ITmfEventField field = new PcapRootEventField(fields, pcapPacket);
b6eb4dce
VP
91 Packet packet = pcapPacket.getMostEcapsulatedPacket();
92 if (!fEventTypes.containsKey(packet.getProtocol())) {
93 String typeIdString = PcapEventType.DEFAULT_PCAP_TYPE_ID + ':' + packet.getProtocol().getShortName();
94 fEventTypes.put(packet.getProtocol(), new PcapEventType(typeIdString, null));
95 }
96 TmfEventType eventType = fEventTypes.get(packet.getProtocol());
97 if (eventType == null) {
98 eventType = new TmfEventType();
99 }
100 return new PcapEvent(trace, rank, tmfTimestamp, dataLink, eventType, field, fileName, packet);
101
102 }
103
104 private static ITmfEventField[] generatePacketFields(Packet packet) {
105 // TODO This is SOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO slow. Must find a
106 // way to use less intermediate data structures.
107 List<ITmfEventField> fieldList = new ArrayList<>();
108 List<ITmfEventField> subfieldList = new ArrayList<>();
c88feda9 109 Packet localPacket = packet.getPacket(PcapProtocol.PCAP);
b6eb4dce
VP
110
111 while (localPacket != null) {
112 subfieldList.clear();
113 for (Map.Entry<String, String> entry : localPacket.getFields().entrySet()) {
0e4f957e 114 String key = entry.getKey();
5db5a3a4 115 String value = entry.getValue();
b6eb4dce
VP
116 subfieldList.add(new TmfEventField(key, value, null));
117 }
118 ITmfEventField[] subfieldArray = subfieldList.toArray(new ITmfEventField[subfieldList.size()]);
119 fieldList.add(new PcapEventField(localPacket.getProtocol().getName(), EMPTY_STRING, subfieldArray, localPacket));
120 localPacket = localPacket.getChildPacket();
121 }
122
95bcb6c4 123 return fieldList.toArray(new ITmfEventField[fieldList.size()]);
b6eb4dce
VP
124 }
125}
This page took 0.070113 seconds and 5 git commands to generate.