tmf: Have TmfTrace implement ITmfEventParser
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / stubs / org / eclipse / tracecompass / tmf / tests / stubs / trace / xml / TmfXmlTraceStub.java
CommitLineData
1d8ab692 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
1d8ab692
GB
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 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
12
2bdf0193 13package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml;
1d8ab692
GB
14
15import java.io.File;
16import java.io.IOException;
17import java.io.InputStream;
18import java.net.URL;
69ff4f26 19import java.util.Collection;
1d8ab692
GB
20
21import javax.xml.XMLConstants;
22import javax.xml.transform.Source;
23import javax.xml.transform.stream.StreamSource;
24import javax.xml.validation.Schema;
25import javax.xml.validation.SchemaFactory;
26import javax.xml.validation.Validator;
27
28import org.eclipse.core.resources.IProject;
29import org.eclipse.core.resources.IResource;
30import org.eclipse.core.runtime.IStatus;
31import org.eclipse.core.runtime.Status;
56e9f830 32import org.eclipse.jdt.annotation.NonNull;
69ff4f26 33import org.eclipse.jdt.annotation.Nullable;
1d8ab692 34import org.eclipse.osgi.util.NLS;
2bdf0193
AM
35import org.eclipse.tracecompass.internal.tmf.core.Activator;
36import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
37import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
38import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
39import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
40import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
41import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
69ff4f26
GB
42import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
43import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
44import org.eclipse.tracecompass.tmf.core.event.aspect.TmfEventFieldAspect;
2bdf0193
AM
45import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
46import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomEventContent;
47import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlEvent;
48import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
49import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
50import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
51import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
12ca2c10 52import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
2bdf0193
AM
53import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
54import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
55import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
56import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
1d8ab692
GB
57import org.xml.sax.SAXException;
58
69ff4f26
GB
59import com.google.common.collect.ImmutableList;
60import com.google.common.primitives.Ints;
61
1d8ab692
GB
62/**
63 * An XML development trace using a custom XML trace definition and schema.
64 *
65 * This class will typically be used to build custom traces to unit test more
66 * complex functionalities like analyzes or to develop and test data-driven
67 * analyzes.
68 *
69 * This class wraps a custom XML trace and rewrites the returned events in the
70 * getNext() method so that event's fields are the ones defined in <field ... />
71 * elements instead of those defined in the custom XML parser. This way, each
72 * event can have a different set of fields. This class can, for example, mimic
73 * a CTF trace.
74 *
75 * @author Geneviève Bastien
76 */
77public class TmfXmlTraceStub extends TmfTrace {
78
79 private static final String DEVELOPMENT_TRACE_PARSER_PATH = "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
80 private static final String DEVELOPMENT_TRACE_XSD = "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
81 private static final String EMPTY = ""; //$NON-NLS-1$
82
83 /* XML elements and attributes names */
84 private static final String EVENT_NAME_FIELD = "Message"; //$NON-NLS-1$
85 private static final String FIELD_NAMES_FIELD = "fields"; //$NON-NLS-1$
1d8ab692
GB
86 private static final String VALUES_FIELD = "values"; //$NON-NLS-1$
87 private static final String TYPES_FIELD = "type"; //$NON-NLS-1$
88 private static final String VALUES_SEPARATOR = " \\| "; //$NON-NLS-1$
89 private static final String TYPE_INTEGER = "int"; //$NON-NLS-1$
90 private static final String TYPE_LONG = "long"; //$NON-NLS-1$
69ff4f26
GB
91 private static final String ASPECT_SPECIAL_EVENT = "set_aspects";
92 private static final String ASPECT_CPU = "cpu";
1d8ab692 93
12ca2c10
GB
94 private static final Long SECONDS_TO_NS = 1000000000L;
95
1d8ab692
GB
96 private final CustomXmlTrace fTrace;
97
69ff4f26
GB
98 private Collection<ITmfEventAspect> fAspects;
99
1d8ab692
GB
100 /**
101 * Constructor. Constructs the custom XML trace with the appropriate
102 * definition.
103 */
104 public TmfXmlTraceStub() {
105
106 /* Load custom XML definition */
107 try (InputStream in = TmfXmlTraceStub.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH);) {
108 CustomXmlTraceDefinition[] definitions = CustomXmlTraceDefinition.loadAll(in);
109 if (definitions.length == 0) {
110 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
111 }
112 fTrace = new CustomXmlTrace(definitions[0]);
113 /* Deregister the custom XML trace */
114 TmfSignalManager.deregister(fTrace);
69ff4f26
GB
115
116 Collection<ITmfEventAspect> aspects = TmfTrace.BASE_ASPECTS;
117 fAspects = aspects;
1d8ab692
GB
118 } catch (IOException e) {
119 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
120 }
121
122 }
123
124 @Override
69ff4f26 125 public void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class<? extends ITmfEvent> type) throws TmfTraceException {
1d8ab692
GB
126 super.initTrace(resource, path, type);
127 fTrace.initTrace(resource, path, type);
128 ITmfContext ctx;
129 /* Set the start and (current) end times for this trace */
130 ctx = seekEvent(0L);
69ff4f26
GB
131 if (ctx == null) {
132 return;
133 }
1d8ab692
GB
134 ITmfEvent event = getNext(ctx);
135 if (event != null) {
136 final ITmfTimestamp curTime = event.getTimestamp();
137 this.setStartTime(curTime);
138 this.setEndTime(curTime);
139 }
140 }
141
5733be39
AM
142 @Override
143 public @Nullable ITmfEvent parseEvent(@Nullable ITmfContext context) {
144 return fTrace.parseEvent(context);
145 }
146
1d8ab692 147 @Override
69ff4f26 148 public @Nullable ITmfLocation getCurrentLocation() {
1d8ab692
GB
149 return fTrace.getCurrentLocation();
150 }
151
152 @Override
69ff4f26 153 public double getLocationRatio(@Nullable ITmfLocation location) {
1d8ab692
GB
154 return fTrace.getLocationRatio(location);
155 }
156
157 @Override
69ff4f26 158 public @Nullable ITmfContext seekEvent(@Nullable ITmfLocation location) {
1d8ab692
GB
159 return fTrace.seekEvent(location);
160 }
161
162 @Override
69ff4f26 163 public @Nullable ITmfContext seekEvent(double ratio) {
1d8ab692
GB
164 return fTrace.seekEvent(ratio);
165 }
166
167 @Override
69ff4f26 168 public IStatus validate(@Nullable IProject project, @Nullable String path) {
1d8ab692
GB
169 File xmlFile = new File(path);
170 if (!xmlFile.exists() || !xmlFile.isFile() || !xmlFile.canRead()) {
2bdf0193 171 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_FileNotFound, path));
1d8ab692
GB
172 }
173 /* Does the XML file validate with the XSD */
174 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
175 Source xmlSource = new StreamSource(xmlFile);
176
177 try {
178 URL url = TmfXmlTraceStub.class.getResource(DEVELOPMENT_TRACE_XSD);
179 Schema schema = schemaFactory.newSchema(url);
180
181 Validator validator = schema.newValidator();
182 validator.validate(xmlSource);
183 } catch (SAXException e) {
2bdf0193 184 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_ValidationError, path), e);
1d8ab692 185 } catch (IOException e) {
2bdf0193 186 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_IoError, path), e);
1d8ab692 187 }
69ff4f26 188 @SuppressWarnings("null")
12ca2c10
GB
189 @NonNull
190 IStatus status = Status.OK_STATUS;
69ff4f26 191 return status;
1d8ab692
GB
192 }
193
69ff4f26 194 private static String getStringValue(ITmfEventField content, String fieldName) {
56e9f830
GB
195 ITmfEventField field = content.getField(fieldName);
196 if (field == null) {
197 return EMPTY;
198 }
199 Object val = field.getValue();
200 if (!(val instanceof String)) {
201 return EMPTY;
202 }
203 return (String) val;
204 }
205
1d8ab692 206 @Override
69ff4f26
GB
207 public synchronized @Nullable ITmfEvent getNext(@Nullable ITmfContext context) {
208 if (context == null) {
209 return null;
210 }
1d8ab692
GB
211 final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
212 CustomXmlEvent event = fTrace.getNext(context);
213 if (event == null) {
214 return null;
215 }
216
217 /* Translate the content of the event */
218 /* The "fields" field contains a | separated list of field names */
219 /* The "values" field contains a | separated list of field values */
220 /* the "type" field contains a | separated list of field types */
221 ITmfEventField content = event.getContent();
56e9f830
GB
222 if (content == null) {
223 return null;
224 }
69ff4f26 225
56e9f830
GB
226 String fieldString = getStringValue(content, FIELD_NAMES_FIELD);
227 String valueString = getStringValue(content, VALUES_FIELD);
228 String typeString = getStringValue(content, TYPES_FIELD);
1d8ab692
GB
229
230 String[] fields = fieldString.split(VALUES_SEPARATOR);
231 String[] values = valueString.split(VALUES_SEPARATOR);
232 String[] types = typeString.split(VALUES_SEPARATOR);
233 ITmfEventField[] fieldsArray = new TmfEventField[fields.length];
234
235 for (int i = 0; i < fields.length; i++) {
236 String value = EMPTY;
237 if (values.length > i) {
238 value = values[i];
239 }
240 String type = null;
241 if (types.length > i) {
242 type = types[i];
243 }
244 Object val = value;
245 if (type != null) {
246 switch (type) {
247 case TYPE_INTEGER: {
248 try {
249 val = Integer.valueOf(value);
250 } catch (NumberFormatException e) {
251 Activator.logError(String.format("Get next XML event: cannot cast value %s to integer", value), e); //$NON-NLS-1$
252 val = 0;
253 }
254 break;
255 }
256 case TYPE_LONG: {
257 try {
258 val = Long.valueOf(value);
259 } catch (NumberFormatException e) {
260 Activator.logError(String.format("Get next XML event: cannot cast value %s to long", value), e); //$NON-NLS-1$
261 val = 0L;
262 }
263 break;
264 }
265 default:
266 break;
267 }
268 }
269 fieldsArray[i] = new TmfEventField(fields[i], val, null);
270 }
271
69ff4f26
GB
272 /* Generate the aspects for this trace if it is the aspects special event */
273 String eventName = getStringValue(content, EVENT_NAME_FIELD);
274 if (eventName.equals(ASPECT_SPECIAL_EVENT)) {
275 generateAspects(fieldsArray);
276 return getNext(context);
277 }
278
1d8ab692
GB
279 /* Create a new event with new fields and name */
280 ITmfEventType customEventType = event.getType();
69ff4f26 281 TmfEventType eventType = new TmfEventType(eventName, customEventType.getRootField());
1d8ab692 282 ITmfEventField eventFields = new CustomEventContent(content.getName(), content.getValue(), fieldsArray);
12ca2c10
GB
283 /*
284 * TODO: Timestamps for these traces are in nanos, but since the
285 * CustomXmlTrace does not support this format, the timestamp of the
286 * original is in second and we need to convert it. We should do that at
287 * the source when it is supported
288 */
289 ITmfTimestamp timestamp = new TmfNanoTimestamp(event.getTimestamp().getValue() / SECONDS_TO_NS);
290 TmfEvent newEvent = new TmfEvent(this, ITmfContext.UNKNOWN_RANK, timestamp, eventType, eventFields);
1d8ab692
GB
291 updateAttributes(savedContext, event.getTimestamp());
292 context.increaseRank();
293
294 return newEvent;
295 }
296
69ff4f26
GB
297 private static final class XmlStubCpuAspect extends TmfCpuAspect {
298
299 private final TmfEventFieldAspect fAspect;
300
301 public XmlStubCpuAspect(TmfEventFieldAspect aspect) {
302 fAspect = aspect;
303 }
304
69ff4f26 305 @Override
2a28075c 306 public @Nullable Integer resolve(ITmfEvent event) {
69ff4f26
GB
307 Integer cpu = Ints.tryParse(fAspect.resolve(event));
308 if (cpu == null) {
2a28075c 309 return null;
69ff4f26
GB
310 }
311 return cpu;
312 }
313
314 }
315
316 private void generateAspects(ITmfEventField[] fieldsArray) {
317 ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>();
318
319 /* Initialize the first default trace aspects */
320 builder.add(ITmfEventAspect.BaseAspects.TIMESTAMP);
321 builder.add(ITmfEventAspect.BaseAspects.EVENT_TYPE);
322
323 /* Add custom aspects in between */
324 for (ITmfEventField field : fieldsArray) {
325 String name = field.getName();
326 if (name == null) {
327 break;
328 }
329 ITmfEventAspect aspect = new TmfEventFieldAspect(name, name);
330 if (name.equals(ASPECT_CPU)) {
331 aspect = new XmlStubCpuAspect((TmfEventFieldAspect) aspect);
332 }
333 builder.add(aspect);
334 }
335
336 /* Add the big content aspect */
337 builder.add(ITmfEventAspect.BaseAspects.CONTENTS);
338
339 @SuppressWarnings("null")
340 @NonNull Collection<ITmfEventAspect> aspectList = builder.build();
341 fAspects = aspectList;
342 }
343
344 @Override
345 public Iterable<ITmfEventAspect> getEventAspects() {
346 return fAspects;
347 }
348
1d8ab692 349}
This page took 0.15092 seconds and 5 git commands to generate.