30969f1f6744082c28da17d6d163ba6940f54ca4
[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.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 resolution_key = :key
81             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
82             AND artifact_name = :artifactName AND occurrence BETWEEN :begin AND :end
83             ORDER BY occurrence DESC, creation_date DESC
84     """,
85         nativeQuery = true
86     )
87     fun findOccurrencesWithinRange(
88         @Param("key")key: String,
89         @Param("blueprintName")blueprintName: String,
90         @Param("blueprintVersion")blueprintVersion: String,
91         @Param("artifactName")artifactName: String,
92         @Param("begin")begin: Int,
93         @Param("end")end: Int
94     ): List<ResourceResolution>
95
96     @Query(
97         value = """
98         SELECT max(occurrence) FROM RESOURCE_RESOLUTION WHERE resolution_key = :key
99             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
100             AND artifact_name = :artifactName
101     """,
102         nativeQuery = true
103     )
104     fun findMaxOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
105         @Param("key")key: String,
106         @Param("blueprintName")blueprintName: String,
107         @Param("blueprintVersion")blueprintVersion: String,
108         @Param("artifactName")artifactName: String
109     ): Int?
110
111     @Query(
112         value = """
113         SELECT max(occurrence) FROM RESOURCE_RESOLUTION WHERE blueprint_name = :blueprintName
114             AND blueprint_version = :blueprintVersion AND resource_id = :resourceId
115             AND resource_type = :resourceType
116     """,
117         nativeQuery = true
118     )
119     fun findMaxOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
120         @Param("blueprintName")blueprintName: String,
121         @Param("blueprintVersion")blueprintVersion: String,
122         @Param("resourceId")resourceId: String,
123         @Param("resourceType")resourceType: String
124     ): Int?
125
126     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
127         resolutionKey: String,
128         blueprintName: String,
129         blueprintVersion: String,
130         artifactPrefix: String
131     ): List<ResourceResolution>
132
133     fun findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
134         blueprintName: String,
135         blueprintVersion: String,
136         resourceId: String,
137         resourceType: String
138     ): List<ResourceResolution>
139
140     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
141         blueprintName: String?,
142         blueprintVersion: String?,
143         artifactName: String,
144         resolutionKey: String,
145         occurrence: Int
146     ): List<ResourceResolution>
147
148     fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
149         blueprintName: String?,
150         blueprintVersion: String?,
151         artifactName: String,
152         resourceId: String,
153         resourceType: String,
154         occurrence: Int
155     ): List<ResourceResolution>
156
157     @Transactional
158     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
159         blueprintName: String,
160         blueprintVersion: String,
161         artifactName: String,
162         resolutionKey: String
163     ): Int
164
165     @Transactional
166     fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceTypeAndResourceId(
167         blueprintName: String,
168         blueprintVersion: String,
169         artifactName: String,
170         resourceType: String,
171         resourceId: String
172     ): Int
173
174     @Transactional
175     @Modifying
176     @Query(
177         value = """
178         DELETE FROM RESOURCE_RESOLUTION
179         WHERE resolution_key = :resolutionKey AND blueprint_name = :blueprintName
180             AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName
181             AND occurrence > (
182                 SELECT max(occurrence) - :lastN FROM RESOURCE_RESOLUTION
183                 WHERE resolution_key = :resolutionKey AND blueprint_name = :blueprintName
184                     AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName)
185     """,
186         nativeQuery = true
187     )
188     fun deleteLastNOccurences(
189         @Param("blueprintName") blueprintName: String,
190         @Param("blueprintVersion") blueprintVersion: String,
191         @Param("artifactName") artifactName: String,
192         @Param("resolutionKey") resolutionKey: String,
193         @Param("lastN") lastN: Int
194     ): Int
195
196     @Transactional
197     @Modifying
198     @Query(
199         value = """
200         DELETE FROM RESOURCE_RESOLUTION
201         WHERE resource_type = :resourceType AND resource_id = :resourceId
202             AND blueprint_name = :blueprintName
203             AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName
204             AND occurrence > (
205                 SELECT max(occurrence) - :lastN FROM RESOURCE_RESOLUTION
206                 WHERE resource_type = :resourceType AND resource_id = :resourceId
207                     AND blueprint_name = :blueprintName
208                     AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName)
209     """,
210         nativeQuery = true
211     )
212     fun deleteLastNOccurences(
213         @Param("blueprintName") blueprintName: String,
214         @Param("blueprintVersion") blueprintVersion: String,
215         @Param("artifactName") artifactName: String,
216         @Param("resourceType") resourceType: String,
217         @Param("resourceId") resourceId: String,
218         @Param("lastN") lastN: Int
219     ): Int
220 }