2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.command
23 import org.apache.commons.lang3.StringUtils
24 import org.onap.portalsdk.core.service.DataAccessService
25 import org.onap.vid.job.Job
26 import org.onap.vid.job.Job.JobStatus.*
27 import org.onap.vid.job.impl.JobDaoImpl
28 import org.onap.vid.utils.DaoUtils
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.stereotype.Service
32 import java.util.stream.Collectors
33 import java.util.stream.Stream
37 class WatchChildrenJobsBL @Autowired
38 constructor(private val dataAccessService: DataAccessService) {
40 fun retrieveChildrenJobsStatus(childrenJobsIds: List<String>): Job.JobStatus {
41 val jobs = getAllChildrenJobs(childrenJobsIds)
43 val jobsStatuses = childrenJobsIds.stream()
44 .map<JobDaoImpl> { jobId -> jobs[UUID.fromString(jobId)] }
46 (it == null || it.status == null) -> Job.JobStatus.FAILED
50 return cumulateJobStatus(jobsStatuses)
54 fun cumulateJobStatus(childrenComulatedStatus: Job.JobStatus, fatherJobStatus: Job.JobStatus): Job.JobStatus {
55 return cumulateJobStatus(Stream.of(childrenComulatedStatus, fatherJobStatus))
58 private fun cumulateJobStatus(jobsStatuses: Stream<Job.JobStatus>): Job.JobStatus {
60 return jobsStatuses.reduce{ a, b ->
62 !a.isFinal || !b.isFinal -> IN_PROGRESS
63 a == COMPLETED_WITH_ERRORS || b == COMPLETED_WITH_ERRORS-> COMPLETED_WITH_ERRORS
64 a == COMPLETED && b.isFailure -> COMPLETED_WITH_ERRORS
65 b == COMPLETED && a.isFailure -> COMPLETED_WITH_ERRORS
66 a == COMPLETED_AND_PAUSED || b == COMPLETED_AND_PAUSED -> COMPLETED_AND_PAUSED
67 a == COMPLETED || b == COMPLETED -> COMPLETED
68 a.isFailure || b.isFailure -> FAILED
69 else -> COMPLETED_WITH_NO_ACTION
71 } .orElse(COMPLETED_WITH_NO_ACTION)
74 private fun getAllChildrenJobs(childrenJobsIds: List<String>): Map<UUID, JobDaoImpl> {
75 val jobs:MutableList<JobDaoImpl> = dataAccessService.getList(JobDaoImpl::class.java, filterByJobIds(childrenJobsIds), null, DaoUtils.getPropsMap()) as MutableList<JobDaoImpl>
76 return jobs.stream().collect(Collectors.toMap( { it.uuid }, { it }))
79 private fun filterByJobIds(childrenJobsIds: List<String>): String {
80 return " WHERE JOB_ID IN('" + StringUtils.join(childrenJobsIds, "', '") + "')"