summaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/dal/AsyncInstantiationRepository.kt
blob: 79c7297a33588e3f75c843f4f21b3b8c30a252fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.dal

import org.onap.portalsdk.core.domain.support.DomainVo
import org.onap.portalsdk.core.service.DataAccessService
import org.onap.vid.dao.JobRequest
import org.onap.vid.exceptions.GenericUncheckedException
import org.onap.vid.exceptions.NotFoundException
import org.onap.vid.job.Job
import org.onap.vid.model.JobAuditStatus
import org.onap.vid.model.ResourceInfo
import org.onap.vid.model.ServiceInfo
import org.onap.vid.model.serviceInstantiation.ServiceInstantiation
import org.onap.vid.utils.DaoUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository
import java.sql.Timestamp
import java.time.LocalDateTime
import java.util.*
import kotlin.collections.HashMap

@Repository
class AsyncInstantiationRepository @Autowired constructor(val dataAccessService:DataAccessService) {

    fun addJobRequest(jobUuid: UUID, request:ServiceInstantiation) {
        save(JobRequest(jobUuid, request))
    }

    fun getJobRequest(jobUuid: UUID):ServiceInstantiation? {
        return getSingleItem(JobRequest::class.java, "JOB_ID", jobUuid).request
    }

    fun saveResourceInfo(resource:ResourceInfo) {
        save(resource)
    }

    fun getResourceInfoByRootJobId(rootJobId: UUID): Map<String, ResourceInfo> {
        val resourceInfoList:List<ResourceInfo> = getResultList(ResourceInfo::class.java, "ROOT_JOB_ID", rootJobId)

        if (resourceInfoList.isEmpty()) {
            throw GenericUncheckedException("Failed to retrieve resource info with rootJobId " + rootJobId + " from ResourceInfo table. no resource found")
        }
        return resourceInfoList.fold(HashMap(), { accumulator, item ->
            accumulator.put(item.trackById, item); accumulator})
    }

    fun getResourceInfoByTrackId(trackById: String):ResourceInfo {
        return getSingleItem(ResourceInfo::class.java, "TRACK_BY_ID", trackById)
    }

    fun saveServiceInfo(serviceInfo: ServiceInfo) {
        save(serviceInfo)
    }

    fun getServiceInfoByJobId(jobUuid: UUID): ServiceInfo {
        return getSingleItem(ServiceInfo::class.java, "jobId", jobUuid)
    }

    fun getServiceInfoByTemplateIdAndJobStatus(templateId: UUID, jobStatus: Job.JobStatus): List<ServiceInfo> {
        return getResultList(ServiceInfo::class.java, mapOf("templateId" to templateId, "jobStatus" to jobStatus), "AND")
    }

    fun getAllServicesInfo(): List<ServiceInfo> {
        return dataAccessService.getList(ServiceInfo::class.java, filterByCreationDateAndNotDeleted(), orderByCreatedDateAndStatus(), null) as List<ServiceInfo>
    }

    private fun filterByCreationDateAndNotDeleted(): String {
        val minus3Months = LocalDateTime.now().minusMonths(3)
        val filterDate = Timestamp.valueOf(minus3Months)
        return " WHERE" +
                "   hidden = false" +
                "   and deleted_at is null" +  // don't fetch deleted

                "   and created >= '" + filterDate + "' "
    }

    private fun orderByCreatedDateAndStatus(): String {
        return " createdBulkDate DESC ,\n" +
                "  (CASE jobStatus\n" +
                "   WHEN 'COMPLETED' THEN 0\n" +
                "   WHEN 'FAILED' THEN 0\n" +
                "   WHEN 'COMPLETED_WITH_ERRORS' THEN 0\n" +
                "   WHEN 'IN_PROGRESS' THEN 1\n" +
                "   WHEN 'PAUSE' THEN 2\n" +
                "   WHEN 'PENDING' THEN 3\n" +
                "   WHEN 'STOPPED' THEN 3 END),\n" +
                "  statusModifiedDate "
    }

    fun getAuditStatuses(jobUUID: UUID, source: JobAuditStatus.SourceStatus): List<JobAuditStatus> {
        // order by ORDINAL.
        // CREATED_DATE is kept for backward compatibility: when all Ordinals are zero
        return getResultList(JobAuditStatus::class.java, mapOf("SOURCE" to source, "JOB_ID" to jobUUID), "AND", " ORDINAL, CREATED_DATE ")
    }

    fun addJobAudiStatus(jobAuditStatus:JobAuditStatus) {
        save(jobAuditStatus)
    }

    private fun <T: DomainVo> save(item:T) {
        dataAccessService.saveDomainObject(item, DaoUtils.getPropsMap())
    }

    private fun <T> getSingleItem(className:Class<T>, filterKey:String, filterValue:Any): T {
        val resultList:List<T> = getResultList(className, filterKey, filterValue)
        if (resultList.size < 1) {
            throw NotFoundException("Failed to retrieve $className with $filterKey $filterValue from table. no resource found")
        }else if (resultList.size > 1) {
            throw GenericUncheckedException("Failed to retrieve $className with $filterKey $filterValue from table. found more than 1 resources")
        }
        return resultList[0]
    }

    private fun <T> getResultList(className:Class<T>, filterKey:String, filterValue:Any): List<T> {
        return getResultList(className, mapOf(filterKey to filterValue), "AND", null)
    }

    private fun <T> getResultList(className:Class<T>, filters: Map<String, Any>, conditionType: String): List<T> {
        return getResultList(className, filters, conditionType, null)
    }

    private fun <T> getResultList(className:Class<T>, filters: Map<String, Any>, conditionType: String, orderBy: String?): List<T> {
        var condition:String = filters
                .map{f -> f.key + " = '" + f.value + "'"}
                .joinToString(" $conditionType ")
        return dataAccessService.getList(className, " WHERE $condition", orderBy, null) as List<T>
    }
}