a0b7cd784cd29e3fa37a68c482d5e220c4b19c11
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / BaseWatchingCommand.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.job.command;
22
23 import org.apache.commons.lang3.ObjectUtils;
24 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
25 import org.onap.vid.job.Job;
26 import org.onap.vid.job.JobCommand;
27 import org.onap.vid.job.NextCommand;
28 import org.onap.vid.job.impl.JobSharedData;
29 import org.onap.vid.services.AsyncInstantiationBusinessLogic;
30
31 import javax.inject.Inject;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37 public abstract class BaseWatchingCommand extends BaseInstantiationCommand implements JobCommand {
38
39     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(BaseWatchingCommand.class);
40
41     @Inject
42     protected AsyncInstantiationBusinessLogic asyncInstantiationBL;
43
44     @Inject
45     private WatchChildrenJobsBL watchChildrenJobsBL;
46
47     private List<String> childrenJobsIds;
48
49     protected boolean isService;
50
51     public BaseWatchingCommand() {}
52
53     public BaseWatchingCommand(JobSharedData sharedData, List<String> childrenJobsIds, boolean isService) {
54         init(sharedData, childrenJobsIds, isService);
55     }
56
57     @Override
58     public NextCommand call() {
59         Job.JobStatus cumulativeJobsStatus =  watchChildrenJobsBL.cumulateJobStatus(
60                 watchChildrenJobsBL.retrieveChildrenJobsStatus(childrenJobsIds),
61                 Job.JobStatus.COMPLETED);
62         return getNextCommand(cumulativeJobsStatus);
63     }
64
65     protected abstract NextCommand getNextCommand(Job.JobStatus cumulativeJobsStatus);
66
67     @Override
68     public BaseWatchingCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
69         return init(
70                 sharedData,
71                 (List<String>) commandData.get("childrenJobs"),
72                 (boolean) commandData.get("isService")
73         );
74     }
75
76     protected BaseWatchingCommand init(JobSharedData sharedData, List<String> childrenJobsIds, boolean isService) {
77         super.init(sharedData);
78         this.childrenJobsIds = ObjectUtils.defaultIfNull(childrenJobsIds, new ArrayList<>());
79         this.isService = isService;
80         return this;
81     }
82
83     @Override
84     public Map<String, Object> getData() {
85         Map<String, Object> data = new HashMap<>();
86         data.put("childrenJobs", childrenJobsIds);
87         data.put("isService", isService);
88         return data;
89     }
90 }