-
Notifications
You must be signed in to change notification settings - Fork 10
/
ExperimentManagerService.java
159 lines (143 loc) · 5.99 KB
/
ExperimentManagerService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*******************************************************************************
* Copyright (c) 2018 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.incubator.internal.trace.server.jersey.rest.core.services;
import java.util.List;
import java.util.UUID;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
import org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal;
import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
/**
* Service to manage experiments
*
* @author Loic Prieur-Drevon
*/
@Path("/experiments")
public class ExperimentManagerService {
private static final String EXPERIMENTS = "experiments"; //$NON-NLS-1$
/**
* Getter for the list of experiments from the trace manager
*
* @return The set of opened experiments
*/
@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getExperiments() {
return Response.ok(Collections2.filter(TmfTraceManager.getInstance().getOpenedTraces(), TmfExperiment.class::isInstance)).build();
}
/**
* Getter for an experiment by {@link UUID}.
*
* @param uuid
* UUID of the experiment to search for
*
* @return The experiment with the queried {@link UUID} if it exists.
*/
@GET
@Path("/{uuid}")
@Produces({ MediaType.APPLICATION_JSON })
public Response getExperiment(@PathParam("uuid") UUID uuid) {
ITmfTrace experiment = TraceManagerService.getTraceByUUID(uuid);
if (experiment instanceof TmfExperiment) {
return Response.ok(experiment).build();
}
return Response.status(Status.NOT_FOUND).build();
}
@GET
@Path("/{uuid}/outputs")
@Produces(MediaType.APPLICATION_JSON)
public Response getOutputs(@PathParam("uuid") UUID uuid) {
return Response.status(Status.NOT_IMPLEMENTED).entity("Not implemented for " + uuid).build();
}
/**
* Delete an experiment by {@link UUID}.
*
* @param uuid
* UUID of the experiment to delete
*
* @return The experiment with the queried {@link UUID} if it exists.
*/
@DELETE
@Path("/{uuid}")
@Produces({ MediaType.APPLICATION_JSON })
public Response deleteExperiment(@PathParam("uuid") UUID uuid) {
ITmfTrace experiment = TraceManagerService.getTraceByUUID(uuid);
if (experiment instanceof TmfExperiment) {
TmfSignalManager.dispatchSignal(new TmfTraceClosedSignal(this, experiment));
experiment.dispose();
return Response.ok(experiment).build();
}
return Response.status(Status.NOT_FOUND).build();
}
/**
* Post a new experiment encapsulating the traces from the list of
* {@link UUID}s.
*
* @param name
* name for the experiment.
* @param traceUUIDs
* {@link UUID}s of the traces to add to the experiment
* @return no content response if one of the trace {@link UUID}s does not map to
* any trace.
*/
@POST
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED })
@Produces({ MediaType.APPLICATION_JSON })
public Response postExperiment(@FormParam("name") String name,
@FormParam("traces") List<UUID> traceUUIDs) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath ipath = root.getLocation().append(EXPERIMENTS).append(name);
IResource resource = root.findMember(ipath);
if (Iterables.any(TmfTraceManager.getInstance().getOpenedTraces(), t -> t.getPath().equals(ipath.toOSString()))) {
return Response.status(Status.CONFLICT).entity("There is already an experiment named: " + name).build(); //$NON-NLS-1$
}
ITmfTrace[] array = new ITmfTrace[traceUUIDs.size()];
int i = 0;
for (UUID uuid : traceUUIDs) {
ITmfTrace trace = TraceManagerService.getTraceByUUID(uuid);
if (trace == null) {
return Response.noContent().build();
}
// traces in the experiment need to be cloned.
try {
ITmfTrace clone = trace.getClass().newInstance();
clone.initTrace(trace.getResource(), trace.getPath(), ITmfEvent.class, trace.getName(), trace.getTraceTypeId());
array[i++] = clone;
} catch (InstantiationException | IllegalAccessException | TmfTraceException e) {
return Response.serverError().entity(e.getMessage()).build();
}
}
TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, ipath.toOSString(), array, TmfExperiment.DEFAULT_INDEX_PAGE_SIZE, resource);
TmfSignalManager.dispatchSignal(new TmfTraceOpenedSignal(this, experiment, null));
return Response.ok(experiment).build();
}
}