Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / BaseWatchingCommand.java
1 package org.onap.vid.job.command;
2
3 import org.apache.commons.lang3.ObjectUtils;
4 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
5 import org.onap.vid.job.Job;
6 import org.onap.vid.job.JobCommand;
7 import org.onap.vid.job.NextCommand;
8 import org.onap.vid.job.impl.JobSharedData;
9 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
10
11 import javax.inject.Inject;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 public abstract class BaseWatchingCommand extends BaseInstantiationCommand implements JobCommand {
18
19     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(BaseWatchingCommand.class);
20
21     @Inject
22     protected AsyncInstantiationBusinessLogic asyncInstantiationBL;
23
24     @Inject
25     private WatchChildrenJobsBL watchChildrenJobsBL;
26
27     private List<String> childrenJobsIds;
28
29     protected boolean isService;
30
31     public BaseWatchingCommand() {}
32
33     public BaseWatchingCommand(JobSharedData sharedData, List<String> childrenJobsIds, boolean isService) {
34         init(sharedData, childrenJobsIds, isService);
35     }
36
37     @Override
38     public NextCommand call() {
39         Job.JobStatus cumulativeJobsStatus =  watchChildrenJobsBL.cumulateJobStatus(
40                 watchChildrenJobsBL.retrieveChildrenJobsStatus(childrenJobsIds),
41                 Job.JobStatus.COMPLETED);
42         return getNextCommand(cumulativeJobsStatus);
43     }
44
45     protected abstract NextCommand getNextCommand(Job.JobStatus cumulativeJobsStatus);
46
47     @Override
48     public BaseWatchingCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
49         return init(
50                 sharedData,
51                 (List<String>) commandData.get("childrenJobs"),
52                 (boolean) commandData.get("isService")
53         );
54     }
55
56     protected BaseWatchingCommand init(JobSharedData sharedData, List<String> childrenJobsIds, boolean isService) {
57         super.init(sharedData);
58         this.childrenJobsIds = ObjectUtils.defaultIfNull(childrenJobsIds, new ArrayList<>());
59         this.isService = isService;
60         return this;
61     }
62
63     @Override
64     public Map<String, Object> getData() {
65         Map<String, Object> data = new HashMap<>();
66         data.put("childrenJobs", childrenJobsIds);
67         data.put("isService", isService);
68         return data;
69     }
70 }