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