1
|
package eu.dnetlib.administration.uis.modules.workflows;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.Collections;
|
7
|
import java.util.HashMap;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
import java.util.stream.Collectors;
|
11
|
|
12
|
import javax.servlet.http.HttpServletRequest;
|
13
|
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.apache.commons.lang3.StringUtils;
|
16
|
import org.apache.commons.lang3.math.NumberUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.dom4j.Document;
|
20
|
import org.dom4j.DocumentException;
|
21
|
import org.dom4j.Element;
|
22
|
import org.dom4j.Node;
|
23
|
import org.dom4j.io.SAXReader;
|
24
|
import org.springframework.beans.factory.annotation.Autowired;
|
25
|
import org.springframework.core.io.ClassPathResource;
|
26
|
import org.springframework.ui.ModelMap;
|
27
|
import org.springframework.web.bind.annotation.PathVariable;
|
28
|
import org.springframework.web.bind.annotation.RequestBody;
|
29
|
import org.springframework.web.bind.annotation.RequestMapping;
|
30
|
import org.springframework.web.bind.annotation.RequestMethod;
|
31
|
import org.springframework.web.bind.annotation.RequestParam;
|
32
|
import org.springframework.web.bind.annotation.ResponseBody;
|
33
|
import org.springframework.web.bind.annotation.RestController;
|
34
|
import org.springframework.web.client.RestTemplate;
|
35
|
|
36
|
import eu.dnetlib.administration.uis.annotations.Authorization;
|
37
|
import eu.dnetlib.administration.uis.annotations.MenuEntry;
|
38
|
import eu.dnetlib.administration.uis.annotations.MenuGroup;
|
39
|
import eu.dnetlib.administration.uis.annotations.PermissionLevel;
|
40
|
import eu.dnetlib.administration.uis.annotations.ProtocolParamFunction;
|
41
|
import eu.dnetlib.administration.uis.annotations.SimpleParamFunction;
|
42
|
import eu.dnetlib.administration.uis.functions.ParamValuesFunction;
|
43
|
import eu.dnetlib.administration.uis.functions.Value;
|
44
|
import eu.dnetlib.administration.uis.modules.datasources.AbstractDsModule;
|
45
|
import eu.dnetlib.administration.uis.modules.datasources.DatasourceVocabularies;
|
46
|
import eu.dnetlib.administration.uis.modules.workflows.objects.WorkflowManagementInfo;
|
47
|
import eu.dnetlib.administration.uis.modules.workflows.objects.WorkflowNotificationInfo;
|
48
|
import eu.dnetlib.clients.dsManager.DatasourceDesc;
|
49
|
import eu.dnetlib.clients.dsManager.DsManagerClient;
|
50
|
import eu.dnetlib.clients.dsManager.IfaceDesc;
|
51
|
import eu.dnetlib.clients.is.InformationServiceClient;
|
52
|
import eu.dnetlib.clients.locators.ServiceClientFactory;
|
53
|
import eu.dnetlib.clients.msro.MsroClient;
|
54
|
import eu.dnetlib.msro.workflows.NotificationCondition;
|
55
|
import eu.dnetlib.msro.workflows.StartMode;
|
56
|
import eu.dnetlib.msro.workflows.WorkflowProcessInfo;
|
57
|
import eu.dnetlib.msro.workflows.WorkflowRef;
|
58
|
import eu.dnetlib.msro.workflows.WorkflowStatus;
|
59
|
import eu.dnetlib.msro.workflows.WorkflowsConstants;
|
60
|
import eu.dnetlib.msro.workflows.logs.WfLogEntry;
|
61
|
|
62
|
@RestController
|
63
|
@RequestMapping("/ajax/wfs")
|
64
|
@MenuEntry(value = "Infrastructure Management", urlSection = "wfs", order = 10, group = MenuGroup.HIDDEN)
|
65
|
@Authorization(PermissionLevel.IS_ADMIN)
|
66
|
public class WorkflowsModule extends AbstractDsModule {
|
67
|
|
68
|
private static final Log log = LogFactory.getLog(WorkflowsModule.class);
|
69
|
|
70
|
@Autowired
|
71
|
private DsManagerClient dsClient;
|
72
|
|
73
|
@Autowired
|
74
|
private InformationServiceClient isClient;
|
75
|
|
76
|
@Autowired
|
77
|
private WorkflowsUIUtils wfUtils;
|
78
|
|
79
|
@Autowired
|
80
|
private ServiceClientFactory clientFactory;
|
81
|
|
82
|
@Autowired(required = false)
|
83
|
private List<ParamValuesFunction> paramValuesFunctions = new ArrayList<>();
|
84
|
|
85
|
@Autowired
|
86
|
private DatasourceVocabularies vocs;
|
87
|
|
88
|
@Override
|
89
|
public void populateModelMap(final ModelMap map, final HttpServletRequest request) throws Exception {
|
90
|
map.addAttribute("compatibilityLevels", fetchVocabularyTermsAsJson(vocs.getCompatibilityLevels()));
|
91
|
}
|
92
|
|
93
|
@RequestMapping("list")
|
94
|
public List<WorkflowItem> listWorkflowsBySection(@RequestParam final String section) throws Exception {
|
95
|
return wfUtils.listWorflowsForSection(section);
|
96
|
}
|
97
|
|
98
|
@RequestMapping("forApi")
|
99
|
public List<WorkflowItem> listWorkflowsForApi(@RequestParam final String dsId, @RequestParam final String ifaceId) throws Exception {
|
100
|
return wfUtils.listWorflowsForApi(dsId, ifaceId);
|
101
|
}
|
102
|
|
103
|
@RequestMapping(value = "workflowManagementParams", method = RequestMethod.GET)
|
104
|
public WorkflowManagementInfo getWorkflowManagementInfo(@RequestParam final String wfId) throws Exception {
|
105
|
|
106
|
final String xml = isClient.getProfile(wfId);
|
107
|
|
108
|
final Document doc = (new SAXReader()).read(new StringReader(xml));
|
109
|
|
110
|
final WorkflowManagementInfo info = new WorkflowManagementInfo();
|
111
|
info.setWfId(wfId);
|
112
|
info.setMode(StartMode.valueOf(doc.valueOf("//CONFIGURATION/@start")));
|
113
|
info.setPriority(NumberUtils.toInt(doc.valueOf("//WORKFLOW_PRIORITY"), WorkflowsConstants.DEFAULT_WF_PRIORITY));
|
114
|
|
115
|
info.setScheduled("true".equalsIgnoreCase(doc.valueOf("//SCHEDULING/@enabled")));
|
116
|
info.setCron(doc.valueOf("//SCHEDULING/CRON"));
|
117
|
info.setInterval(NumberUtils.toInt(doc.valueOf("//SCHEDULING/MININTERVAL"), 120)); // Default: 120 minutes
|
118
|
|
119
|
for (final Object o : doc.selectNodes("//NOTIFICATIONS/EMAIL")) {
|
120
|
info.getNotifications().add(new WorkflowNotificationInfo(
|
121
|
((Element) o).valueOf("@address"),
|
122
|
((Element) o).valueOf("@messageProfileId"),
|
123
|
NotificationCondition.valueOf(((Element) o).valueOf("@condition"))));
|
124
|
}
|
125
|
|
126
|
return info;
|
127
|
}
|
128
|
|
129
|
@RequestMapping(value = "workflowManagementParams", method = RequestMethod.POST)
|
130
|
public WorkflowManagementInfo saveWorkflowManagementInfo(@RequestParam final String wfId, @RequestBody final WorkflowManagementInfo info) throws Exception {
|
131
|
|
132
|
log.info("Updating workflow " + info.getWfId());
|
133
|
|
134
|
final String xml = isClient.getProfile(info.getWfId());
|
135
|
|
136
|
final Document doc = new SAXReader().read(new StringReader(xml));
|
137
|
|
138
|
doc.selectSingleNode("//CONFIGURATION/@start").setText(info.getMode().toString());
|
139
|
|
140
|
final Element notificationsNode = (Element) doc.selectSingleNode("//NOTIFICATIONS");
|
141
|
notificationsNode.clearContent();
|
142
|
if (info.getNotifications() != null) {
|
143
|
for (final WorkflowNotificationInfo n : info.getNotifications()) {
|
144
|
final Element emailNode = notificationsNode.addElement("EMAIL");
|
145
|
emailNode.addAttribute("address", n.getEmail());
|
146
|
emailNode.addAttribute("messageProfileId", n.getMessageProfileId());
|
147
|
emailNode.addAttribute("condition", n.getCondition().toString());
|
148
|
}
|
149
|
}
|
150
|
|
151
|
doc.selectSingleNode("//WORKFLOW_PRIORITY").setText(Integer.toString(info.getPriority()));
|
152
|
|
153
|
final Node node = doc.selectSingleNode("//SCHEDULING");
|
154
|
((Element) node).addAttribute("enabled", Boolean.toString(info.isScheduled()));
|
155
|
if (info.isScheduled()) {
|
156
|
node.selectSingleNode("./CRON").setText(info.getCron() != null ? info.getCron() : "");
|
157
|
node.selectSingleNode("./MININTERVAL").setText(Integer.toString(info.getInterval()));
|
158
|
}
|
159
|
isClient.updateProfile(wfId, doc.asXML());
|
160
|
|
161
|
return info;
|
162
|
}
|
163
|
|
164
|
@RequestMapping(value = "workflowParams", method = RequestMethod.POST)
|
165
|
public boolean saveWorkflowParams(@RequestParam final String wfId, @RequestBody final Map<String, String> params) throws Exception {
|
166
|
|
167
|
final String xml = isClient.getProfile(wfId);
|
168
|
|
169
|
final Document doc = new SAXReader().read(new StringReader(xml));
|
170
|
for (final Map.Entry<String, String> e : params.entrySet()) {
|
171
|
final Node node = doc.selectSingleNode("//CONFIGURATION/PARAMETERS/PARAM[@name='" + e.getKey() + "']");
|
172
|
if (node == null) {
|
173
|
log.error("Param " + e.getKey() + " not found in profile " + xml);
|
174
|
} else {
|
175
|
final String val = e.getValue() != null ? e.getValue() : "";
|
176
|
if (node.valueOf("@managedBy").equalsIgnoreCase("user")) {
|
177
|
node.setText(val);
|
178
|
} else {
|
179
|
log.debug("Param " + e.getKey() + " not updated, it is not editable by user");
|
180
|
}
|
181
|
|
182
|
}
|
183
|
}
|
184
|
|
185
|
// Update the Workflow status
|
186
|
if (doc.selectNodes("//CONFIGURATION/PARAMETERS/PARAM[@required='true' and string-length(normalize-space(.)) = 0]").isEmpty()) {
|
187
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.EXECUTABLE.name());
|
188
|
} else {
|
189
|
((Element) doc.selectSingleNode("//CONFIGURATION")).addAttribute("status", WorkflowStatus.WAIT_USER_SETTINGS.name());
|
190
|
}
|
191
|
|
192
|
isClient.updateProfile(wfId, doc.asXML());
|
193
|
|
194
|
return true;
|
195
|
}
|
196
|
|
197
|
@RequestMapping(value = "subWorkflows")
|
198
|
public @ResponseBody List<Map<String, String>> obtainSubWorkflows(@RequestParam final String wfId) {
|
199
|
|
200
|
try {
|
201
|
final ClassPathResource resource = new ClassPathResource("/xquery/listSubWorkflows.xquery");
|
202
|
final String query = IOUtils.toString(resource.getInputStream()).replaceAll("@wfId@", wfId);
|
203
|
|
204
|
final List<String> list = isClient.find(query);
|
205
|
|
206
|
final SAXReader reader = new SAXReader();
|
207
|
return list.stream()
|
208
|
.map(s -> {
|
209
|
try {
|
210
|
final Map<String, String> map = new HashMap<String, String>();
|
211
|
for (final Object o : reader.read(new StringReader(s)).selectNodes("/res/*")) {
|
212
|
map.put(((Node) o).getName(), ((Node) o).getText());
|
213
|
}
|
214
|
return map;
|
215
|
} catch (final DocumentException e) {
|
216
|
throw new RuntimeException(e);
|
217
|
}
|
218
|
})
|
219
|
// TODO AGGIUNGERE DAL LOGGER
|
220
|
/*
|
221
|
* .map(map -> { if ("true".equalsIgnoreCase(map.get("isTemplate"))) { final Map<String, Object> query = new
|
222
|
* HashMap<>(); query.put(WorkflowsConstants.LOG_WF_PARENT, wfId);
|
223
|
* query.put(WorkflowsConstants.LOG_WF_PROFILE_TEMPLATE_ID, map.get("id")); final Map<String, String> log =
|
224
|
* dnetLogger.findOne(query); if ((log != null) && !log.isEmpty()) { map.put("lastDate",
|
225
|
* log.get(WorkflowsConstants.LOG_WF_PROCESS_END_DATE)); map.put("lastStatus",
|
226
|
* log.get(WorkflowsConstants.LOG_WF_PROCESS_STATUS)); } } return map: })
|
227
|
*/
|
228
|
.collect(Collectors.toList());
|
229
|
|
230
|
} catch (final Throwable e) {
|
231
|
log.error("Error obtaining subWorkflows of " + wfId, e);
|
232
|
return new ArrayList<>();
|
233
|
}
|
234
|
|
235
|
}
|
236
|
|
237
|
@RequestMapping("startWorkflow")
|
238
|
public WorkflowRef startWorkflow(@RequestParam final String wfId) throws Exception {
|
239
|
return clientFactory.getClient(MsroClient.class).startWorkflow(wfId);
|
240
|
}
|
241
|
|
242
|
@RequestMapping("startWorkflowTemplate")
|
243
|
public @ResponseBody WorkflowRef startWorkflowTemplate(
|
244
|
@RequestParam final String node,
|
245
|
@RequestParam final String parent) throws Exception {
|
246
|
|
247
|
return clientFactory.getClient(MsroClient.class).startWorkflowTemplate(node, parent);
|
248
|
}
|
249
|
|
250
|
@RequestMapping(value = "proc/{procId}", method = RequestMethod.GET)
|
251
|
public WorkflowProcessInfo getProcess(@PathVariable final String procId, @RequestParam final String worker) throws IOException {
|
252
|
final String url = String.format("%s/proc/%s", worker, procId);
|
253
|
|
254
|
log.debug("Invoking url: " + url);
|
255
|
|
256
|
return (new RestTemplate()).getForObject(url, WorkflowProcessInfo.class);
|
257
|
}
|
258
|
|
259
|
@RequestMapping(value = "kill/{procId}", method = RequestMethod.GET)
|
260
|
public boolean killProcess(@PathVariable final String procId, @RequestParam final String worker) throws IOException {
|
261
|
return (new RestTemplate()).getForObject(worker + "/kill/" + procId, Boolean.class);
|
262
|
}
|
263
|
|
264
|
@RequestMapping(value = "validValues/{param}", method = RequestMethod.POST)
|
265
|
public List<Value> obtainValidValues(@PathVariable final String param, @RequestBody final Map<String, String> params) throws IOException {
|
266
|
|
267
|
return paramValuesFunctions.stream()
|
268
|
.filter(f -> f.getClass().isAnnotationPresent(SimpleParamFunction.class))
|
269
|
.filter(f -> f.getClass().getAnnotation(SimpleParamFunction.class).value().equals(param))
|
270
|
.findFirst()
|
271
|
.map(f -> {
|
272
|
try {
|
273
|
final List<Value> list = f.findValues(params);
|
274
|
Collections.sort(list);
|
275
|
return list;
|
276
|
} catch (final Exception e) {
|
277
|
log.error("Error obtaining values for " + param, e);
|
278
|
return new ArrayList<Value>();
|
279
|
}
|
280
|
})
|
281
|
.orElseGet(() -> {
|
282
|
log.error("ParamValuesFunction not found for param: " + param);
|
283
|
return new ArrayList<>();
|
284
|
});
|
285
|
}
|
286
|
|
287
|
@RequestMapping(value = "validValues/{protocol}/{param}", method = RequestMethod.POST)
|
288
|
public List<Value> obtainValidValues(@PathVariable final String protocol, final String param, @RequestBody final Map<String, String> params)
|
289
|
throws IOException {
|
290
|
|
291
|
return paramValuesFunctions.stream()
|
292
|
.filter(f -> f.getClass().isAnnotationPresent(ProtocolParamFunction.class))
|
293
|
.filter(f -> f.getClass().getAnnotation(ProtocolParamFunction.class).protocol().equals(protocol))
|
294
|
.filter(f -> f.getClass().getAnnotation(ProtocolParamFunction.class).param().equals(param))
|
295
|
.findFirst()
|
296
|
.map(f -> {
|
297
|
try {
|
298
|
final List<Value> list = f.findValues(params);
|
299
|
Collections.sort(list);
|
300
|
return list;
|
301
|
} catch (final Exception e) {
|
302
|
log.error("Error obtaining values for " + param, e);
|
303
|
return new ArrayList<Value>();
|
304
|
}
|
305
|
})
|
306
|
.orElseGet(() -> {
|
307
|
log.error("ParamValuesFunction not found for param: " + protocol + " -> " + param);
|
308
|
return new ArrayList<>();
|
309
|
});
|
310
|
}
|
311
|
|
312
|
@RequestMapping("ds/compliance/override/{level}")
|
313
|
public boolean updateRepoApiCompliance(@RequestParam final String dsId, @RequestParam final String ifaceId, @PathVariable final String level)
|
314
|
throws Exception {
|
315
|
|
316
|
log.debug("SET COMPLIANCE TO " + level);
|
317
|
|
318
|
dsClient.updateLevelOfCompliance(dsId, ifaceId, level, true);
|
319
|
|
320
|
return true;
|
321
|
}
|
322
|
|
323
|
@RequestMapping("ds/compliance/reset")
|
324
|
public boolean resetRepoApiCompliance(@RequestParam final String dsId, @RequestParam final String ifaceId) throws Exception {
|
325
|
|
326
|
log.debug("RESET COMPLIANCE");
|
327
|
|
328
|
dsClient.resetLevelOfCompliance(dsId, ifaceId);
|
329
|
|
330
|
return true;
|
331
|
}
|
332
|
|
333
|
@RequestMapping("protocolParameters")
|
334
|
public List<Map<String, String>> getProtocolParameters(@RequestParam final String dsId, @RequestParam final String ifaceId) throws Exception {
|
335
|
|
336
|
final DatasourceDesc ds = dsClient.getDatasource(dsId);
|
337
|
final IfaceDesc iface = ds.getInterfaces().stream().filter(ifc -> ifc.getId().equals(ifaceId)).findFirst().get();
|
338
|
final String protocol = iface.getAccessProtocol();
|
339
|
|
340
|
final List<Map<String, String>> list = new ArrayList<>();
|
341
|
final Map<String, String> baseUrlParam = new HashMap<>();
|
342
|
|
343
|
baseUrlParam.put("name", "baseUrl");
|
344
|
baseUrlParam.put("type", "TEXT");
|
345
|
baseUrlParam.put("value", iface.getBaseUrl());
|
346
|
baseUrlParam.put("regex", "^(http|https|ftp|ftps|sftp|file):\\/\\/");
|
347
|
list.add(baseUrlParam);
|
348
|
|
349
|
wfUtils.listParametersForProtocol(protocol).stream().map(pp -> {
|
350
|
final Map<String, String> res = new HashMap<>();
|
351
|
res.put("name", pp.getName());
|
352
|
res.put("type", pp.getType());
|
353
|
res.put("value", iface.getAccessParams().get(pp.getName()));
|
354
|
|
355
|
if (StringUtils.isNotBlank(pp.getRegex())) {
|
356
|
res.put("regex", pp.getRegex());
|
357
|
}
|
358
|
if (pp.isFunctionPopulated()) {
|
359
|
res.put("functionPopulated", "true");
|
360
|
}
|
361
|
if (pp.isOptional()) {
|
362
|
res.put("optional", "true");
|
363
|
}
|
364
|
|
365
|
return res;
|
366
|
}).forEach(list::add);
|
367
|
|
368
|
return list;
|
369
|
}
|
370
|
|
371
|
@RequestMapping("logs")
|
372
|
public List<WfLogEntry> getWorkflowLogs(@RequestParam final String wf) throws Exception {
|
373
|
return clientFactory.getClient(MsroClient.class).listWfLogs(wf);
|
374
|
}
|
375
|
|
376
|
}
|