1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStream
;
19 import java
.util
.Collection
;
21 import javax
.xml
.XMLConstants
;
22 import javax
.xml
.transform
.Source
;
23 import javax
.xml
.transform
.stream
.StreamSource
;
24 import javax
.xml
.validation
.Schema
;
25 import javax
.xml
.validation
.SchemaFactory
;
26 import javax
.xml
.validation
.Validator
;
28 import org
.eclipse
.core
.resources
.IProject
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.IStatus
;
31 import org
.eclipse
.core
.runtime
.Status
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.jdt
.annotation
.Nullable
;
34 import org
.eclipse
.osgi
.util
.NLS
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfEventFieldAspect
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomEventContent
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
57 import org
.xml
.sax
.SAXException
;
59 import com
.google
.common
.collect
.ImmutableList
;
60 import com
.google
.common
.primitives
.Ints
;
63 * An XML development trace using a custom XML trace definition and schema.
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
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
75 * @author Geneviève Bastien
77 public class TmfXmlTraceStub
extends TmfTrace
{
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$
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$
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$
91 private static final String ASPECT_SPECIAL_EVENT
= "set_aspects";
92 private static final String ASPECT_CPU
= "cpu";
94 private static final Long SECONDS_TO_NS
= 1000000000L;
96 private final CustomXmlTrace fTrace
;
98 private Collection
<ITmfEventAspect
> fAspects
;
101 * Constructor. Constructs the custom XML trace with the appropriate
104 public TmfXmlTraceStub() {
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$
112 fTrace
= new CustomXmlTrace(definitions
[0]);
113 /* Deregister the custom XML trace */
114 TmfSignalManager
.deregister(fTrace
);
115 this.setParser(fTrace
);
117 Collection
<ITmfEventAspect
> aspects
= TmfTrace
.BASE_ASPECTS
;
119 } catch (IOException e
) {
120 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
126 public void initTrace(@Nullable IResource resource
, @Nullable String path
, @Nullable Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
127 super.initTrace(resource
, path
, type
);
128 fTrace
.initTrace(resource
, path
, type
);
130 /* Set the start and (current) end times for this trace */
135 ITmfEvent event
= getNext(ctx
);
137 final ITmfTimestamp curTime
= event
.getTimestamp();
138 this.setStartTime(curTime
);
139 this.setEndTime(curTime
);
144 public @Nullable ITmfLocation
getCurrentLocation() {
145 return fTrace
.getCurrentLocation();
149 public double getLocationRatio(@Nullable ITmfLocation location
) {
150 return fTrace
.getLocationRatio(location
);
154 public @Nullable ITmfContext
seekEvent(@Nullable ITmfLocation location
) {
155 return fTrace
.seekEvent(location
);
159 public @Nullable ITmfContext
seekEvent(double ratio
) {
160 return fTrace
.seekEvent(ratio
);
164 public IStatus
validate(@Nullable IProject project
, @Nullable String path
) {
165 File xmlFile
= new File(path
);
166 if (!xmlFile
.exists() || !xmlFile
.isFile() || !xmlFile
.canRead()) {
167 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_FileNotFound
, path
));
169 /* Does the XML file validate with the XSD */
170 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
171 Source xmlSource
= new StreamSource(xmlFile
);
174 URL url
= TmfXmlTraceStub
.class.getResource(DEVELOPMENT_TRACE_XSD
);
175 Schema schema
= schemaFactory
.newSchema(url
);
177 Validator validator
= schema
.newValidator();
178 validator
.validate(xmlSource
);
179 } catch (SAXException e
) {
180 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_ValidationError
, path
), e
);
181 } catch (IOException e
) {
182 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_IoError
, path
), e
);
184 @SuppressWarnings("null")
186 IStatus status
= Status
.OK_STATUS
;
190 private static String
getStringValue(ITmfEventField content
, String fieldName
) {
191 ITmfEventField field
= content
.getField(fieldName
);
195 Object val
= field
.getValue();
196 if (!(val
instanceof String
)) {
203 public synchronized @Nullable ITmfEvent
getNext(@Nullable ITmfContext context
) {
204 if (context
== null) {
207 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
208 CustomXmlEvent event
= fTrace
.getNext(context
);
213 /* Translate the content of the event */
214 /* The "fields" field contains a | separated list of field names */
215 /* The "values" field contains a | separated list of field values */
216 /* the "type" field contains a | separated list of field types */
217 ITmfEventField content
= event
.getContent();
218 if (content
== null) {
222 String fieldString
= getStringValue(content
, FIELD_NAMES_FIELD
);
223 String valueString
= getStringValue(content
, VALUES_FIELD
);
224 String typeString
= getStringValue(content
, TYPES_FIELD
);
226 String
[] fields
= fieldString
.split(VALUES_SEPARATOR
);
227 String
[] values
= valueString
.split(VALUES_SEPARATOR
);
228 String
[] types
= typeString
.split(VALUES_SEPARATOR
);
229 ITmfEventField
[] fieldsArray
= new TmfEventField
[fields
.length
];
231 for (int i
= 0; i
< fields
.length
; i
++) {
232 String value
= EMPTY
;
233 if (values
.length
> i
) {
237 if (types
.length
> i
) {
245 val
= Integer
.valueOf(value
);
246 } catch (NumberFormatException e
) {
247 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to integer", value
), e
); //$NON-NLS-1$
254 val
= Long
.valueOf(value
);
255 } catch (NumberFormatException e
) {
256 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to long", value
), e
); //$NON-NLS-1$
265 fieldsArray
[i
] = new TmfEventField(fields
[i
], val
, null);
268 /* Generate the aspects for this trace if it is the aspects special event */
269 String eventName
= getStringValue(content
, EVENT_NAME_FIELD
);
270 if (eventName
.equals(ASPECT_SPECIAL_EVENT
)) {
271 generateAspects(fieldsArray
);
272 return getNext(context
);
275 /* Create a new event with new fields and name */
276 ITmfEventType customEventType
= event
.getType();
277 TmfEventType eventType
= new TmfEventType(eventName
, customEventType
.getRootField());
278 ITmfEventField eventFields
= new CustomEventContent(content
.getName(), content
.getValue(), fieldsArray
);
280 * TODO: Timestamps for these traces are in nanos, but since the
281 * CustomXmlTrace does not support this format, the timestamp of the
282 * original is in second and we need to convert it. We should do that at
283 * the source when it is supported
285 ITmfTimestamp timestamp
= new TmfNanoTimestamp(event
.getTimestamp().getValue() / SECONDS_TO_NS
);
286 TmfEvent newEvent
= new TmfEvent(this, ITmfContext
.UNKNOWN_RANK
, timestamp
, eventType
, eventFields
);
287 updateAttributes(savedContext
, event
.getTimestamp());
288 context
.increaseRank();
293 private static final class XmlStubCpuAspect
extends TmfCpuAspect
{
295 private final TmfEventFieldAspect fAspect
;
297 public XmlStubCpuAspect(TmfEventFieldAspect aspect
) {
302 public @Nullable Integer
resolve(ITmfEvent event
) {
303 Integer cpu
= Ints
.tryParse(fAspect
.resolve(event
));
312 private void generateAspects(ITmfEventField
[] fieldsArray
) {
313 ImmutableList
.Builder
<ITmfEventAspect
> builder
= new ImmutableList
.Builder
<>();
315 /* Initialize the first default trace aspects */
316 builder
.add(ITmfEventAspect
.BaseAspects
.TIMESTAMP
);
317 builder
.add(ITmfEventAspect
.BaseAspects
.EVENT_TYPE
);
319 /* Add custom aspects in between */
320 for (ITmfEventField field
: fieldsArray
) {
321 String name
= field
.getName();
325 ITmfEventAspect aspect
= new TmfEventFieldAspect(name
, name
);
326 if (name
.equals(ASPECT_CPU
)) {
327 aspect
= new XmlStubCpuAspect((TmfEventFieldAspect
) aspect
);
332 /* Add the big content aspect */
333 builder
.add(ITmfEventAspect
.BaseAspects
.CONTENTS
);
335 @SuppressWarnings("null")
336 @NonNull Collection
<ITmfEventAspect
> aspectList
= builder
.build();
337 fAspects
= aspectList
;
341 public Iterable
<ITmfEventAspect
> getEventAspects() {