502d8344d192e6716780ff9af4895390af1fbc7c
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / WatchChildrenJobsBL.kt
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.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
31 import java.util.*
32 import java.util.stream.Collectors
33 import java.util.stream.Stream
34
35
36 @Service
37 class WatchChildrenJobsBL @Autowired
38 constructor(private val dataAccessService: DataAccessService) {
39
40     fun retrieveChildrenJobsStatus(childrenJobsIds: List<String>): Job.JobStatus {
41         val jobs = getAllChildrenJobs(childrenJobsIds)
42
43         val jobsStatuses = childrenJobsIds.stream()
44                 .map<JobDaoImpl> { jobId -> jobs[UUID.fromString(jobId)] }
45                 .map {when {
46                     (it == null || it.status == null) -> Job.JobStatus.FAILED
47                     else -> it.status
48                 }}
49
50         return cumulateJobStatus(jobsStatuses)
51
52     }
53
54     fun cumulateJobStatus(childrenComulatedStatus: Job.JobStatus, fatherJobStatus: Job.JobStatus): Job.JobStatus {
55         return cumulateJobStatus(Stream.of(childrenComulatedStatus, fatherJobStatus))
56     }
57
58     private fun cumulateJobStatus(jobsStatuses: Stream<Job.JobStatus>): Job.JobStatus {
59
60         return jobsStatuses.reduce{ a, b ->
61             when {
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
70             }
71         } .orElse(COMPLETED_WITH_NO_ACTION)
72   }
73
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 }))
77     }
78
79     private fun filterByJobIds(childrenJobsIds: List<String>): String {
80         return " WHERE JOB_ID IN('" + StringUtils.join(childrenJobsIds, "', '") + "')"
81     }
82 }