5861cf8cb1c3b01fff908bab8db1661de176d234
[ccsdk/cds.git] /
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.Modifying
20 import org.springframework.data.jpa.repository.Query
21 import org.springframework.data.repository.query.Param
22 import org.springframework.stereotype.Repository
23 import javax.transaction.Transactional
24
25 @Repository
26 interface ResourceResolutionRepository : JpaRepository<ResourceResolution, String> {
27
28     @Query(
29         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",
30         nativeQuery = true
31     )
32     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
33         @Param("key")key: String,
34         @Param("blueprintName")blueprintName: String,
35         @Param("blueprintVersion")blueprintVersion: String,
36         @Param("artifactName")artifactName: String,
37         @Param("name")name: String
38     ): ResourceResolution?
39
40     @Query(
41         value = """
42         SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
43            AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
44            AND artifact_name = :artifactName AND occurrence <=  :firstN
45     """,
46         nativeQuery = true
47     )
48     fun findFirstNOccurrences(
49         @Param("key")key: String,
50         @Param("blueprintName")blueprintName: String,
51         @Param("blueprintVersion")blueprintVersion: String,
52         @Param("artifactName")artifactName: String,
53         @Param("firstN")begin: Int
54     ): List<ResourceResolution>
55
56     @Query(
57         value = """
58         SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
59             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
60             AND artifact_name = :artifactName
61             AND occurrence > (
62                 select max(occurrence) - :lastN from RESOURCE_RESOLUTION
63                 WHERE resolution_key = :key AND blueprint_name = :blueprintName
64                       AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName
65                 )
66             ORDER BY occurrence DESC, creation_date DESC
67     """,
68         nativeQuery = true
69     )
70     fun findLastNOccurrences(
71         @Param("key")key: String,
72         @Param("blueprintName")blueprintName: String,
73         @Param("blueprintVersion")blueprintVersion: String,
74         @Param("artifactName")artifactName: String,
75         @Param("lastN")begin: Int
76     ): List<ResourceResolution>
77
78     @Query(
79         value = """
80         SELECT * FROM RESOURCE_RESOLUTION WHERE resource_id = :resourceId
81             AND resource_type =:resourceType AND blueprint_name = :blueprintName
82             AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName
83             AND occurrence > (
84                 select max(occurrence) - :lastN from RESOURCE_RESOLUTION
85                 WHERE resource_id = :resourceId
86                     AND resource_type =:resourceType AND blueprint_name = :blueprintName
87                     AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName)
88                     ORDER BY occurrence DESC, creation_date DESC
89           """,
90         nativeQuery = true
91     )
92     fun findLastNOccurrences(
93         @Param("resourceId")resourceId: String,
94         @Param("resourceType")resourceType: String,
95         @Param("blueprintName")blueprintName: String,
96         @Param("blueprintVersion")blueprintVersion: String,
97         @Param("artifactName")artifactName: String,
98         @Param("lastN")begin: Int
99     ): List<ResourceResolution>
100
101     @Query(
102         value = """
103         SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
104             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
105             AND artifact_name = :artifactName AND occurrence BETWEEN :begin AND :end
106             ORDER BY occurrence DESC, creation_date DESC
107     """,
108         nativeQuery = true
109     )
110     fun findOccurrencesWithinRange(
111         @Param("key")key: String,
112         @Param("blueprintName")blueprintName: String,
113         @Param("blueprintVersion")blueprintVersion: String,
114         @Param("artifactName")artifactName: String,
115         @Param("begin")begin: Int,
116         @Param("end")end: Int
117     ): List<ResourceResolution>
118
119     @Query(
120         value = """
121         SELECT max(occurrence) FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
122             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
123             AND artifact_name = :artifactName
124     """,
125         nativeQuery = true
126     )
127     fun findMaxOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
128         @Param("key")key: String,
129         @Param("blueprintName")blueprintName: String,
130         @Param("blueprintVersion")blueprintVersion: String,
131         @Param("artifactName")artifactName: String
132     ): Int?
133
134     @Query(
135         value = """
136         SELECT max(occurrence) FROM RESOURCE_RESOLUTION WHERE blueprint_name = :blueprintName
137             AND blueprint_version = :blueprintVersion AND resource_id = :resourceId
138             AND resource_type = :resourceType
139     """,
140         nativeQuery = true
141     )
142     fun findMaxOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
143         @Param("blueprintName")blueprintName: String,
144         @Param("blueprintVersion")blueprintVersion: String,
145         @Param("resourceId")resourceId: String,
146         @Param("resourceType")resourceType: String
147     ): Int?
148
149     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
150         resolutionKey: String,
151         blueprintName: String,
152         blueprintVersion: String,
153         artifactPrefix: String
154     ): List<ResourceResolution>
155
156     fun findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
157         blueprintName: String,
158         blueprintVersion: String,
159         resourceId: String,
160         resourceType: String
161     ): List<ResourceResolution>
162
163     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
164         blueprintName: String?,
165         blueprintVersion: String?,
166         artifactName: String,
167         resolutionKey: String,
168         occurrence: Int
169     ): List<ResourceResolution>
170
171     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
172         blueprintName: String?,
173         blueprintVersion: String?,
174         artifactName: String,
175         resourceId: String,
176         resourceType: String,
177         occurrence: Int
178     ): List<ResourceResolution>
179
180     @Transactional
181     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
182         blueprintName: String,
183         blueprintVersion: String,
184         artifactName: String,
185         resolutionKey: String
186     ): Int
187
188     @Transactional
189     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceTypeAndResourceId(
190         blueprintName: String,
191         blueprintVersion: String,
192         artifactName: String,
193         resourceType: String,
194         resourceId: String
195     ): Int
196
197     @Transactional
198     @Modifying
199     @Query(
200         value = """
201         DELETE FROM RESOURCE_RESOLUTION
202         WHERE resolution_key = :resolutionKey AND blueprint_name = :blueprintName
203             AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName
204             AND occurrence > (
205                 SELECT MAX(occurrence) - :lastN FROM (
206                     SELECT occurrence from RESOURCE_RESOLUTION
207                     WHERE resolution_key = :resolutionKey
208                         AND blueprint_name = :blueprintName
209                         AND blueprint_version = :blueprintVersion
210                         AND artifact_name = :artifactName) AS o
211                 )
212     """,
213         nativeQuery = true
214     )
215     fun deleteLastNOccurrences(
216         @Param("blueprintName") blueprintName: String,
217         @Param("blueprintVersion") blueprintVersion: String,
218         @Param("artifactName") artifactName: String,
219         @Param("resolutionKey") resolutionKey: String,
220         @Param("lastN") lastN: Int
221     ): Int
222
223     @Transactional
224     @Modifying
225     @Query(
226         value = """
227         DELETE FROM RESOURCE_RESOLUTION
228         WHERE resource_type = :resourceType AND resource_id = :resourceId
229             AND blueprint_name = :blueprintName
230             AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName
231             AND occurrence > (
232                 SELECT MAX(occurrence) - :lastN FROM (
233                     SELECT occurrence FROM RESOURCE_RESOLUTION
234                     WHERE resource_type = :resourceType
235                         AND resource_id = :resourceId
236                         AND blueprint_name = :blueprintName
237                         AND blueprint_version = :blueprintVersion
238                         AND artifact_name = :artifactName) AS o
239                 )
240     """,
241         nativeQuery = true
242     )
243     fun deleteLastNOccurrences(
244         @Param("blueprintName") blueprintName: String,
245         @Param("blueprintVersion") blueprintVersion: String,
246         @Param("artifactName") artifactName: String,
247         @Param("resourceType") resourceType: String,
248         @Param("resourceId") resourceId: String,
249         @Param("lastN") lastN: Int
250     ): Int
251 }