Aharoni, Pavel (pa0916) | 8c70f07 | 2018-11-18 00:07:12 +0200 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 | package org.onap.osam.job.command;
|
| 21 |
|
| 22 | import lombok.extern.slf4j.Slf4j;
|
| 23 | import org.onap.osam.job.dao.job.JobStatus;
|
| 24 | import org.onap.osam.job.dao.job.OsamJob;
|
| 25 | import org.onap.osam.job.IJobCommand;
|
| 26 | import org.onap.osam.job.NextCommand;
|
| 27 | import org.onap.osam.job.AsyncJobService;
|
| 28 | import org.onap.osam.job.impl.JobSharedData;
|
| 29 | import org.onap.osam.job.repository.job.OsamJobRepository;
|
| 30 | import org.springframework.beans.factory.annotation.Autowired;
|
| 31 | import org.springframework.util.CollectionUtils;
|
| 32 |
|
| 33 | import java.util.ArrayList;
|
| 34 | import java.util.HashMap;
|
| 35 | import java.util.List;
|
| 36 | import java.util.Map;
|
| 37 | import java.util.UUID;
|
| 38 | import java.util.stream.Collectors;
|
| 39 |
|
| 40 | @Slf4j
|
| 41 | public abstract class BaseWatchingCommand extends CommandBase implements IJobCommand {
|
| 42 |
|
| 43 | @Autowired
|
| 44 | protected AsyncJobService asyncInstantiationBL;
|
| 45 |
|
| 46 | @Autowired
|
| 47 | private OsamJobRepository osamJobRepository;
|
| 48 |
|
| 49 | private List<UUID> childrenJobsIds;
|
| 50 |
|
| 51 | protected boolean isRoot;
|
| 52 |
|
| 53 | public BaseWatchingCommand() {}
|
| 54 |
|
| 55 | public BaseWatchingCommand(JobSharedData sharedData, List<UUID> childrenJobsIds, boolean isRoot) {
|
| 56 | init(sharedData, childrenJobsIds, isRoot);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public BaseWatchingCommand init(JobSharedData sharedData, Map<String, Object> commandData) {
|
| 61 | return init(
|
| 62 | sharedData,
|
| 63 | ((List<String>) commandData.get("childrenJobs")).stream().map(x -> UUID.fromString(x)).collect(Collectors.toList()),
|
| 64 | (boolean) commandData.get("isRoot")
|
| 65 | );
|
| 66 | }
|
| 67 |
|
| 68 | protected BaseWatchingCommand init(JobSharedData sharedData, List<UUID> childrenJobsIds, boolean isRoot) {
|
| 69 | super.init(sharedData);
|
| 70 | this.childrenJobsIds = CollectionUtils.isEmpty(childrenJobsIds) ? new ArrayList<>() : childrenJobsIds;
|
| 71 | this.isRoot = isRoot;
|
| 72 | return this;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public NextCommand call() {
|
| 77 | Map<UUID, OsamJob> jobs = getAllChildrenJobs();
|
| 78 |
|
| 79 | boolean isAllChildrenFinal = true;
|
| 80 | boolean hasFailedChild = false;
|
| 81 | for (UUID jobId: childrenJobsIds) {
|
| 82 | OsamJob job = jobs.get(jobId);
|
| 83 |
|
| 84 |
|
| 85 | //if job not found - we assume it failed
|
| 86 | if (job == null || job.getStatus() == null) {
|
| 87 | hasFailedChild = true;
|
| 88 | continue;
|
| 89 | }
|
| 90 |
|
| 91 | if (!job.getStatus().isFinal()) {
|
| 92 | isAllChildrenFinal = false;
|
| 93 | } else if (!job.getStatus().equals(JobStatus.COMPLETED)) {
|
| 94 | //if job status is final - check if it failed status
|
| 95 | hasFailedChild = true;
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | return getNextCommand(isAllChildrenFinal, hasFailedChild);
|
| 100 | }
|
| 101 |
|
| 102 | private Map<UUID, OsamJob> getAllChildrenJobs() {
|
| 103 | List<OsamJob> jobs = osamJobRepository.findAllByUuid(childrenJobsIds);
|
| 104 | return jobs.stream().collect(Collectors.toMap(OsamJob::getUuid, item -> item));
|
| 105 | }
|
| 106 |
|
| 107 | protected abstract NextCommand getNextCommand(boolean isAllChildrenFinal, boolean hasFailedChild);
|
| 108 |
|
| 109 |
|
| 110 | @Override
|
| 111 | public Map<String, Object> getData() {
|
| 112 | Map<String, Object> data = new HashMap<>();
|
| 113 | data.put("childrenJobs", childrenJobsIds);
|
| 114 | data.put("isRoot", isRoot);
|
| 115 | return data;
|
| 116 | }
|
| 117 | }
|