09357e51975849362bd9c5e69d4ba760cd92b8ff
[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.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
33 import java.util.*
34 import java.util.stream.Collectors
35 import java.util.stream.Stream
36
37
38 @Service
39 class WatchChildrenJobsBL @Autowired
40 constructor(private val dataAccessService: DataAccessService, private val featureManager: FeatureManager) {
41
42     fun retrieveChildrenJobsStatus(childrenJobsIds: List<String>): Job.JobStatus {
43         val jobs = getAllChildrenJobs(childrenJobsIds)
44
45         val jobsStatuses = childrenJobsIds.stream()
46                 .map<JobDaoImpl> { jobId -> jobs[UUID.fromString(jobId)] }
47                 .map {when {
48                     (it == null || it.status == null) -> Job.JobStatus.FAILED
49                     else -> it.status
50                 }}
51
52         return if(featureManager.isActive(Features.FLAG_2008_PAUSE_VFMODULE_INSTANTIATION_FAILURE))
53             cumulateJobStatusWithPauseOnFailure(jobsStatuses) else cumulateJobStatus(jobsStatuses)
54
55     }
56
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))
61     }
62     private fun cumulateJobStatus(jobsStatuses: Stream<Job.JobStatus>): Job.JobStatus {
63
64         return jobsStatuses.reduce{ a, b ->
65             when {
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
74             }
75         } .orElse(COMPLETED_WITH_NO_ACTION)
76   }
77     private fun cumulateJobStatusWithPauseOnFailure(jobsStatuses: Stream<Job.JobStatus>): Job.JobStatus {
78
79         return jobsStatuses.reduce{ a, b ->
80             when {
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
89             }
90         } .orElse(COMPLETED_WITH_NO_ACTION)
91     }
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 }))
95     }
96
97     private fun filterByJobIds(childrenJobsIds: List<String>): String {
98         return " WHERE JOB_ID IN('" + StringUtils.join(childrenJobsIds, "', '") + "')"
99     }
100 }