4b707b04ef40ed15f0e678abb5ca5c0090341e33
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / db / ResourceResolutionRepository.kt
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
17
18 import org.springframework.data.jpa.repository.JpaRepository
19 import org.springframework.data.jpa.repository.Query
20 import org.springframework.data.repository.query.Param
21 import org.springframework.stereotype.Repository
22 import javax.transaction.Transactional
23
24 @Repository
25 interface ResourceResolutionRepository : JpaRepository<ResourceResolution, String> {
26
27     @Query(
28         value = "SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName AND name = :name ORDER BY occurrence DESC, creation_date DESC LIMIT 1",
29         nativeQuery = true
30     )
31     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
32         @Param("key")key: String,
33         @Param("blueprintName")blueprintName: String,
34         @Param("blueprintVersion")blueprintVersion: String,
35         @Param("artifactName")artifactName: String,
36         @Param("name")name: String
37     ): ResourceResolution?
38
39     @Query(
40         value = """
41         SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
42            AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
43            AND artifact_name = :artifactName AND occurrence <=  :firstN
44     """,
45         nativeQuery = true
46     )
47     fun findFirstNOccurrences(
48         @Param("key")key: String,
49         @Param("blueprintName")blueprintName: String,
50         @Param("blueprintVersion")blueprintVersion: String,
51         @Param("artifactName")artifactName: String,
52         @Param("firstN")begin: Int
53     ): List<ResourceResolution>
54
55     @Query(
56         value = """
57         SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
58             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
59             AND artifact_name = :artifactName
60             AND occurrence > (
61                 select max(occurrence) - :lastN from RESOURCE_RESOLUTION
62                 WHERE resolution_key = :key AND blueprint_name = :blueprintName
63                       AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName
64                 )
65             ORDER BY occurrence DESC, creation_date DESC
66     """,
67         nativeQuery = true
68     )
69     fun findLastNOccurrences(
70         @Param("key")key: String,
71         @Param("blueprintName")blueprintName: String,
72         @Param("blueprintVersion")blueprintVersion: String,
73         @Param("artifactName")artifactName: String,
74         @Param("lastN")begin: Int
75     ): List<ResourceResolution>
76
77     @Query(
78         value = """
79         SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
80             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
81             AND artifact_name = :artifactName AND occurrence BETWEEN :begin AND :end
82             ORDER BY occurrence DESC, creation_date DESC
83     """,
84         nativeQuery = true
85     )
86     fun findOccurrencesWithinRange(
87         @Param("key")key: String,
88         @Param("blueprintName")blueprintName: String,
89         @Param("blueprintVersion")blueprintVersion: String,
90         @Param("artifactName")artifactName: String,
91         @Param("begin")begin: Int,
92         @Param("end")end: Int
93     ): List<ResourceResolution>
94
95     @Query(
96         value = """
97         SELECT max(occurrence) FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
98             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
99             AND artifact_name = :artifactName
100     """,
101         nativeQuery = true
102     )
103     fun findMaxOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
104         @Param("key")key: String,
105         @Param("blueprintName")blueprintName: String,
106         @Param("blueprintVersion")blueprintVersion: String,
107         @Param("artifactName")artifactName: String
108     ): Int?
109
110     @Query(
111         value = """
112         SELECT max(occurrence) FROM RESOURCE_RESOLUTION WHERE blueprint_name = :blueprintName
113             AND blueprint_version = :blueprintVersion AND resource_id = :resourceId
114             AND resource_type = :resourceType
115     """,
116         nativeQuery = true
117     )
118     fun findMaxOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
119         @Param("blueprintName")blueprintName: String,
120         @Param("blueprintVersion")blueprintVersion: String,
121         @Param("resourceId")resourceId: String,
122         @Param("resourceType")resourceType: String
123     ): Int?
124
125     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
126         resolutionKey: String,
127         blueprintName: String,
128         blueprintVersion: String,
129         artifactPrefix: String
130     ): List<ResourceResolution>
131
132     fun findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
133         blueprintName: String,
134         blueprintVersion: String,
135         resourceId: String,
136         resourceType: String
137     ): List<ResourceResolution>
138
139     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
140         blueprintName: String?,
141         blueprintVersion: String?,
142         artifactName: String,
143         resolutionKey: String,
144         occurrence: Int
145     ): List<ResourceResolution>
146
147     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
148         blueprintName: String?,
149         blueprintVersion: String?,
150         artifactName: String,
151         resourceId: String,
152         resourceType: String,
153         occurrence: Int
154     ): List<ResourceResolution>
155
156     @Transactional
157     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
158         blueprintName: String?,
159         blueprintVersion: String?,
160         artifactName: String,
161         resolutionKey: String
162     )
163 }