2 * Copyright (C) 2019 Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
18 import kotlinx.coroutines.Dispatchers
19 import kotlinx.coroutines.withContext
20 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
24 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
25 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
26 import org.slf4j.LoggerFactory
27 import org.springframework.dao.EmptyResultDataAccessException
28 import org.springframework.stereotype.Service
32 class ResourceResolutionDBService(private val resourceResolutionRepository: ResourceResolutionRepository) {
34 private val log = LoggerFactory.getLogger(ResourceResolutionDBService::class.toString())
36 suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
37 bluePrintRuntimeService: BluePrintRuntimeService<*>,
40 artifactPrefix: String
41 ): List<ResourceResolution> {
43 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
45 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
46 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
48 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
55 } catch (e: EmptyResultDataAccessException) {
60 suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
61 bluePrintRuntimeService: BluePrintRuntimeService<*>,
65 artifactPrefix: String
66 ): List<ResourceResolution> {
69 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
71 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
72 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
74 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
82 } catch (e: EmptyResultDataAccessException) {
87 suspend fun readValue(
88 blueprintName: String,
89 blueprintVersion: String,
90 artifactPrefix: String,
91 resolutionKey: String,
93 ): ResourceResolution? = withContext(Dispatchers.IO) {
95 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
104 suspend fun readWithResolutionKey(
105 blueprintName: String,
106 blueprintVersion: String,
107 artifactPrefix: String,
108 resolutionKey: String
109 ): List<ResourceResolution> = withContext(Dispatchers.IO) {
111 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
120 * This returns the resolutions of first N 'occurrences'.
122 * @param blueprintName
123 * @param blueprintVersion
124 * @param artifactPrefix
125 * @param resolutionKey
128 suspend fun findFirstNOccurrences(
129 blueprintName: String,
130 blueprintVersion: String,
131 artifactPrefix: String,
132 resolutionKey: String,
134 ): Map<Int, List<ResourceResolution>> = withContext(Dispatchers.IO) {
136 resourceResolutionRepository.findFirstNOccurrences(
142 ).groupBy(ResourceResolution::occurrence).toSortedMap(reverseOrder())
146 * This returns the resolutions of last N 'occurrences'.
148 * @param blueprintName
149 * @param blueprintVersion
150 * @param artifactPrefix
151 * @param resolutionKey
154 suspend fun findLastNOccurrences(
155 blueprintName: String,
156 blueprintVersion: String,
157 artifactPrefix: String,
158 resolutionKey: String,
160 ): Map<Int, List<ResourceResolution>> = withContext(Dispatchers.IO) {
162 resourceResolutionRepository.findLastNOccurrences(
168 ).groupBy(ResourceResolution::occurrence).toSortedMap(reverseOrder())
172 * This returns the resolutions with 'occurrence' value between begin and end.
174 * @param blueprintName
175 * @param blueprintVersion
176 * @param artifactPrefix
177 * @param resolutionKey
181 suspend fun findOccurrencesWithinRange(
182 blueprintName: String,
183 blueprintVersion: String,
184 artifactPrefix: String,
185 resolutionKey: String,
188 ): Map<Int, List<ResourceResolution>> = withContext(Dispatchers.IO) {
190 resourceResolutionRepository.findOccurrencesWithinRange(
197 ).groupBy(ResourceResolution::occurrence).toSortedMap(reverseOrder())
200 suspend fun readWithResourceIdAndResourceType(
201 blueprintName: String,
202 blueprintVersion: String,
205 ): List<ResourceResolution> =
206 withContext(Dispatchers.IO) {
208 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
217 properties: Map<String, Any>,
218 bluePrintRuntimeService: BluePrintRuntimeService<*>,
219 artifactPrefix: String,
220 resourceAssignment: ResourceAssignment
221 ): ResourceResolution = withContext(Dispatchers.IO) {
223 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
225 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
226 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
228 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
229 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
230 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
231 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
246 blueprintName: String,
247 blueprintVersion: String,
248 resolutionKey: String,
250 resourceType: String,
251 artifactPrefix: String,
252 resourceAssignment: ResourceAssignment,
254 ): ResourceResolution = withContext(Dispatchers.IO) {
256 val resourceResolution = ResourceResolution()
257 resourceResolution.id = UUID.randomUUID().toString()
258 resourceResolution.artifactName = artifactPrefix
259 resourceResolution.occurrence = occurrence
260 resourceResolution.blueprintVersion = blueprintVersion
261 resourceResolution.blueprintName = blueprintName
262 resourceResolution.resolutionKey = resolutionKey
263 resourceResolution.resourceType = resourceType
264 resourceResolution.resourceId = resourceId
265 resourceResolution.value = resourceAssignment.property?.value?.let {
266 if (BluePrintConstants.STATUS_SUCCESS == resourceAssignment.status)
267 JacksonUtils.getValue(it).toString()
270 resourceResolution.name = resourceAssignment.name
271 resourceResolution.dictionaryName = resourceAssignment.dictionaryName
272 resourceResolution.dictionaryVersion = resourceAssignment.version
273 resourceResolution.dictionarySource = resourceAssignment.dictionarySource
274 resourceResolution.status = resourceAssignment.status ?: BluePrintConstants.STATUS_FAILURE
277 resourceResolutionRepository.saveAndFlush(resourceResolution)
278 } catch (ex: Exception) {
279 throw BluePrintException("Failed to store resource resolution result.", ex)
284 * This is a deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey method to delete resources
285 * associated to a specific resolution-key
287 * @param blueprintName name of the CBA
288 * @param blueprintVersion version of the CBA
289 * @param artifactName name of the artifact
290 * @param resolutionKey value of the resolution-key
292 suspend fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
293 blueprintName: String,
294 blueprintVersion: String,
295 artifactName: String,
296 resolutionKey: String
298 resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
306 suspend fun deleteResourceResolutionList(listResourceResolution: List<ResourceResolution>) = withContext(Dispatchers.IO) {
308 resourceResolutionRepository.deleteInBatch(listResourceResolution)
309 } catch (ex: Exception) {
310 throw BluePrintException("Failed to batch delete resource resolution", ex)
315 * This method returns the (highest occurrence + 1) of resource resolutions if present in DB, returns 1 otherwise.
316 * The 'occurrence' is used to persist new resource resolution in the DB.
318 * @param resolutionKey
319 * @param blueprintName
320 * @param blueprintVersion
321 * @param artifactPrefix
323 suspend fun findNextOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
324 resolutionKey: String,
325 blueprintName: String,
326 blueprintVersion: String,
327 artifactPrefix: String
328 ) = withContext(Dispatchers.IO) {
329 val maxOccurrence = resourceResolutionRepository.findMaxOccurrenceByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
335 maxOccurrence?.inc() ?: 1
339 * This method returns the (highest occurrence + 1) of resource resolutions if present in DB, returns 1 otherwise.
340 * The 'occurrence' is used to persist new resource resolution in the DB.
342 * @param blueprintName
343 * @param blueprintVersion
345 * @param resourceType
347 suspend fun findNextOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
348 blueprintName: String,
349 blueprintVersion: String,
352 ) = withContext(Dispatchers.IO) {
353 val maxOccurrence = resourceResolutionRepository.findMaxOccurrenceByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
359 maxOccurrence?.inc() ?: 1