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.properties.Features
29 import org.onap.vid.utils.DaoUtils
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.stereotype.Service
32 import org.togglz.core.manager.FeatureManager
34 import java.util.stream.Collectors
35 import java.util.stream.Stream
39 class WatchChildrenJobsBL @Autowired
40 constructor(private val dataAccessService: DataAccessService, private val featureManager: FeatureManager) {
42 fun retrieveChildrenJobsStatus(childrenJobsIds: List<String>): Job.JobStatus {
43 val jobs = getAllChildrenJobs(childrenJobsIds)
45 val jobsStatuses = childrenJobsIds.stream()
46 .map<JobDaoImpl> { jobId -> jobs[UUID.fromString(jobId)] }
48 (it == null || it.status == null) -> Job.JobStatus.FAILED
52 return if(featureManager.isActive(Features.FLAG_2008_PAUSE_VFMODULE_INSTANTIATION_FAILURE))
53 cumulateJobStatusWithPauseOnFailure(jobsStatuses) else cumulateJobStatus(jobsStatuses)
57 fun cumulateJobStatus(childrenComulatedStatus: Job.JobStatus, fatherJobStatus: Job.JobStatus): Job.JobStatus {
58 return if(featureManager.isActive(Features.FLAG_2008_PAUSE_VFMODULE_INSTANTIATION_FAILURE))
59 cumulateJobStatusWithPauseOnFailure(Stream.of(childrenComulatedStatus, fatherJobStatus))
60 else cumulateJobStatus(Stream.of(childrenComulatedStatus, fatherJobStatus))
62 private fun cumulateJobStatus(jobsStatuses: Stream<Job.JobStatus>): Job.JobStatus {
64 return jobsStatuses.reduce{ a, b ->
66 !a.isFinal || !b.isFinal -> IN_PROGRESS
67 a == COMPLETED_WITH_ERRORS || b == COMPLETED_WITH_ERRORS-> COMPLETED_WITH_ERRORS
68 a == COMPLETED && b.isFailure -> COMPLETED_WITH_ERRORS
69 b == COMPLETED && a.isFailure -> COMPLETED_WITH_ERRORS
70 a == COMPLETED_AND_PAUSED || b == COMPLETED_AND_PAUSED -> COMPLETED_AND_PAUSED
71 a == COMPLETED || b == COMPLETED -> COMPLETED
72 a.isFailure || b.isFailure -> FAILED
73 else -> COMPLETED_WITH_NO_ACTION
75 } .orElse(COMPLETED_WITH_NO_ACTION)
77 private fun cumulateJobStatusWithPauseOnFailure(jobsStatuses: Stream<Job.JobStatus>): Job.JobStatus {
79 return jobsStatuses.reduce{ a, b ->
81 a == FAILED_AND_PAUSED || b == FAILED_AND_PAUSED-> FAILED_AND_PAUSED
82 a == COMPLETED && b.isFailure -> FAILED_AND_PAUSED
83 b == COMPLETED && a.isFailure -> FAILED_AND_PAUSED
84 !a.isFinal || !b.isFinal -> IN_PROGRESS
85 a == COMPLETED_AND_PAUSED || b == COMPLETED_AND_PAUSED -> COMPLETED_AND_PAUSED
86 a == COMPLETED || b == COMPLETED -> COMPLETED
87 a.isFailure || b.isFailure -> FAILED
88 else -> COMPLETED_WITH_NO_ACTION
90 } .orElse(COMPLETED_WITH_NO_ACTION)
92 private fun getAllChildrenJobs(childrenJobsIds: List<String>): Map<UUID, JobDaoImpl> {
93 val jobs:MutableList<JobDaoImpl> = dataAccessService.getList(JobDaoImpl::class.java, filterByJobIds(childrenJobsIds), null, DaoUtils.getPropsMap()) as MutableList<JobDaoImpl>
94 return jobs.stream().collect(Collectors.toMap( { it.uuid }, { it }))
97 private fun filterByJobIds(childrenJobsIds: List<String>): String {
98 return " WHERE JOB_ID IN('" + StringUtils.join(childrenJobsIds, "', '") + "')"