/
AiravataManager.java
286 lines (245 loc) · 13.6 KB
/
AiravataManager.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package org.seagrid.desktop.connectors.airavata;
import org.apache.airavata.api.Airavata;
import org.apache.airavata.model.appcatalog.appdeployment.ApplicationModule;
import org.apache.airavata.model.appcatalog.appinterface.ApplicationInterfaceDescription;
import org.apache.airavata.model.appcatalog.computeresource.ComputeResourceDescription;
import org.apache.airavata.model.appcatalog.userresourceprofile.UserComputeResourcePreference;
import org.apache.airavata.model.data.replica.DataProductModel;
import org.apache.airavata.model.data.replica.DataReplicaLocationModel;
import org.apache.airavata.model.error.*;
import org.apache.airavata.model.experiment.ExperimentModel;
import org.apache.airavata.model.experiment.ExperimentSearchFields;
import org.apache.airavata.model.experiment.ExperimentSummaryModel;
import org.apache.airavata.model.security.AuthzToken;
import org.apache.airavata.model.status.JobStatus;
import org.apache.airavata.model.workspace.Notification;
import org.apache.airavata.model.workspace.Project;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.seagrid.desktop.util.SEAGridContext;
import org.seagrid.desktop.util.messaging.SEAGridEvent;
import org.seagrid.desktop.util.messaging.SEAGridEventBus;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
public class AiravataManager {
private static AiravataManager instance;
private Airavata.Client airavataClient;
private AiravataCache<String, Object> airavataCache;
private AiravataManager() throws AiravataClientException {
try {
this.airavataClient = createAiravataClient();
this.airavataCache = new AiravataCache<>(200, 500, 50);
} catch (Exception e) {
e.printStackTrace();
throw new AiravataClientException(AiravataErrorType.UNKNOWN);
}
}
public static AiravataManager getInstance() throws AiravataClientException {
if (AiravataManager.instance == null) {
AiravataManager.instance = new AiravataManager();
}
return AiravataManager.instance;
}
private Airavata.Client createAiravataClient() throws TTransportException {
String host = SEAGridContext.getInstance().getAiravataHost();
int port = SEAGridContext.getInstance().getAiravataPort();
String applicationDataDir = System.getProperty("user.home") + File.separator + "SEAGrid" + File.separator;
TSSLTransportFactory.TSSLTransportParameters params = new TSSLTransportFactory.TSSLTransportParameters();
params.setTrustStore(applicationDataDir + "client-truststore.jks", "airavata");
//System.setProperty("javax.net.ssl.trustStore","clientTrustStore.key");
System.setProperty("javax.net.ssl.trustStore", applicationDataDir + "client-truststore.jks");
System.setProperty("javax.net.ssl.trustStorePassword","airavata");
TTransport transport = TSSLTransportFactory.getClientSocket(host, port, 10000, params);
//TTransport transport = TSSLTransportFactory.getClientSocket(host, port, 10000);
TProtocol protocol = new TBinaryProtocol(transport);
//SharingRegistryService.Client sharingServiceClient = new SharingRegistryService.Client(protocol);
return new Airavata.Client(protocol);
}
//params.setKeyStore("/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home/conf/security/java.security", "airavata");
private Airavata.Client getClient() throws AiravataClientException, TTransportException {
try{
airavataClient.getAPIVersion();
} catch (Exception e) {
try{
airavataClient = createAiravataClient();
airavataClient.getAPIVersion();
//airavataClient.getAPIVersion(getAuthzToken());
} catch (Exception e1) {
SEAGridEventBus.getInstance().post(new SEAGridEvent(SEAGridEvent.SEAGridEventType.LOGOUT, null));
AiravataClientException clientError = new AiravataClientException();
//clientError.setParameter("Unable to connect to the server at " + host + ":" + port);
clientError.setParameter("Unable to connect to the server ");
throw clientError;
//SEAGridEventBus.getInstance().post(new SEAGridEvent(SEAGridEvent.SEAGridEventType.LOGOUT, null));
}
}
return airavataClient;
}
private AuthzToken getAuthzToken() {
AuthzToken authzToken = new AuthzToken(SEAGridContext.getInstance().getOAuthToken());
authzToken.setClaimsMap(new HashMap<>());
authzToken.getClaimsMap().put("gatewayID", SEAGridContext.getInstance().getAiravataGatewayId());
authzToken.getClaimsMap().put("userName", SEAGridContext.getInstance().getUserName());
return authzToken;
}
private String getGatewayId() {
return SEAGridContext.getInstance().getAiravataGatewayId();
}
private String getUserName() {
return SEAGridContext.getInstance().getUserName();
}
public synchronized List<ExperimentSummaryModel> getExperimentSummaries(Map<ExperimentSearchFields, String> filters,
int limit, int offset) throws TException {
List<ExperimentSummaryModel> exp = getClient().searchExperiments(
getAuthzToken(), getGatewayId(), getUserName(), filters, limit, offset);
return exp;
}
public synchronized List<ExperimentSummaryModel> getExperimentSummariesInProject(String projectId) throws TException {
List<ExperimentSummaryModel> exp;
Map<ExperimentSearchFields, String> filters = new HashMap<>();
filters.put(ExperimentSearchFields.PROJECT_ID, projectId);
exp = getClient().searchExperiments(
getAuthzToken(), getGatewayId(), getUserName(), filters, -1, 0);
return exp;
}
public synchronized List<ExperimentSummaryModel> getRecentExperimentSummaries(int limit) throws TException {
List<ExperimentSummaryModel> exp;
Map<ExperimentSearchFields, String> filters = new HashMap<>();
exp = getClient().searchExperiments(
getAuthzToken(), getGatewayId(), getUserName(), filters, limit, 0);
return exp;
}
public synchronized List<Project> getProjects() throws TException {
List<Project> projects;
try{
projects = getClient().getUserProjects(
getAuthzToken(), getGatewayId(), getUserName(), -1, 0);
}catch (Exception ex){
//FIXME If the user is new getProjects will fail
getClient().createProject(getAuthzToken(),getGatewayId(),new Project("", getUserName(),
SEAGridContext.getInstance().getAiravataGatewayId(), "Default Project"));
projects = getClient().getUserProjects(
getAuthzToken(), getGatewayId(), getUserName(), -1, 0);
}
return projects;
}
public synchronized Project createProject(String projectName, String projectDescription) throws TException {
Project project;
project = new Project("no-id", getUserName(), SEAGridContext.getInstance().getAiravataGatewayId(), projectName);
if (projectDescription != null)
project.setDescription(projectDescription);
String projectId = getClient().createProject(
getAuthzToken(), getGatewayId(), project);
project.setProjectID(projectId);
return project;
}
public synchronized ExperimentModel getExperiment(String experimentId) throws TException {
return getClient().getExperiment(getAuthzToken(), experimentId);
}
public synchronized ComputeResourceDescription getComputeResource(String resourceId) throws TException {
ComputeResourceDescription computeResourceDescription;
if (airavataCache.get(resourceId) != null) {
computeResourceDescription = (ComputeResourceDescription) airavataCache.get(resourceId);
} else {
computeResourceDescription = getClient().getComputeResource(getAuthzToken(), resourceId);
airavataCache.put(resourceId, computeResourceDescription);
}
return computeResourceDescription;
}
public synchronized ApplicationInterfaceDescription getApplicationInterface(String interfaceId) throws TException {
ApplicationInterfaceDescription applicationInterfaceDescription = null;
if (airavataCache.get(interfaceId) != null) {
applicationInterfaceDescription = (ApplicationInterfaceDescription) airavataCache.get(interfaceId);
} else {
applicationInterfaceDescription = getClient().getApplicationInterface(getAuthzToken(), interfaceId);
airavataCache.put(interfaceId, applicationInterfaceDescription);
}
return applicationInterfaceDescription;
}
public synchronized Project getProject(String projectId) throws TException {
Project project;
if (airavataCache.get(projectId) != null) {
project = (Project) airavataCache.get(projectId);
} else {
project = getClient().getProject(getAuthzToken(), projectId);
airavataCache.put(projectId, project);
}
return project;
}
public synchronized Map<String, JobStatus> getJobStatuses(String expId) throws TException {
Map<String, JobStatus> jobStatuses;
jobStatuses = getClient().getJobStatuses(getAuthzToken(), expId);
return jobStatuses;
}
public synchronized List<ApplicationInterfaceDescription> getAllApplicationInterfaces() throws TException {
List<ApplicationInterfaceDescription> allApplicationInterfaces;
allApplicationInterfaces = getClient().getAllApplicationInterfaces(getAuthzToken(), getGatewayId());
Collections.sort(allApplicationInterfaces, (o1, o2) -> o1.getApplicationName().compareTo(o2.getApplicationName()));
return allApplicationInterfaces;
}
public synchronized List<ComputeResourceDescription> getAvailableComputeResourcesForApp(String applicationInterfaceId)
throws TException {
List<ComputeResourceDescription> availableComputeResources;
Map<String, String> temp = getClient().getAvailableAppInterfaceComputeResources(getAuthzToken(), applicationInterfaceId);
availableComputeResources = new ArrayList<>();
for (String resourceId : temp.keySet()) {
availableComputeResources.add(getComputeResource(resourceId));
}
return availableComputeResources;
}
public synchronized String createExperiment(ExperimentModel experimentModel) throws TException {
return getClient().createExperiment(getAuthzToken(), getGatewayId(), experimentModel);
}
public synchronized void updateExperiment(ExperimentModel experimentModel) throws TException {
getClient().updateExperiment(getAuthzToken(), experimentModel.getExperimentId(), experimentModel);
}
public synchronized void launchExperiment(String experimentId) throws TException {
getClient().launchExperiment(getAuthzToken(), experimentId, getGatewayId());
}
public synchronized void deleteExperiment(String experimentId) throws TException {
getClient().deleteExperiment(getAuthzToken(), experimentId);
}
public synchronized void cancelExperiment(String experimentId) throws TException {
getClient().terminateExperiment(getAuthzToken(),experimentId, getGatewayId());
}
public synchronized String cloneExperiment(String experimentId, String newExpName, String projectId) throws TException {
return getClient().cloneExperiment(getAuthzToken(), experimentId, newExpName, projectId);
}
public String registerDataProduct(DataProductModel dpModel) throws TException {
return getClient().registerDataProduct(getAuthzToken(), dpModel);
}
public List<DataReplicaLocationModel> getDataReplicas(String uri) throws TException {
return getClient().getDataProduct(getAuthzToken(), uri).getReplicaLocations();
}
public List<Notification> getNotifications() throws TException {
//We don't need security token for calling getNotifications API method
java.util.List<Notification> temp = (createAiravataClient()).getAllNotifications(new AuthzToken("dummy-token"),
SEAGridContext.getInstance().getAiravataGatewayId());
java.util.List<Notification> messages = new ArrayList<>();
if(temp != null){
messages.addAll(temp.stream().filter(m -> m.getExpirationTime() > System.currentTimeMillis()
|| m.getExpirationTime() == 0).collect(Collectors.toList()));
}
return messages;
}
public UserComputeResourcePreference getUserComputeResourcePrefs(String computeResourceId) throws TException {
UserComputeResourcePreference userComputeResourcePreference = null;
try{
userComputeResourcePreference = getClient().getUserComputeResourcePreference(getAuthzToken(),
SEAGridContext.getInstance().getUserName(), SEAGridContext.getInstance().getAiravataGatewayId(),
computeResourceId);
return userComputeResourcePreference;
}catch (Exception ex) {
//User compute resource pref does not exists
return null;
}
}
public List<ApplicationModule> getAccessibleAppModules() throws TException {
return getClient().getAccessibleAppModules(getAuthzToken(), getGatewayId());
}
}