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.slf4j.LoggerFactory
25 import org.springframework.dao.DataIntegrityViolationException
26 import org.springframework.dao.EmptyResultDataAccessException
27 import org.springframework.stereotype.Service
31 class TemplateResolutionService(private val templateResolutionRepository: TemplateResolutionRepository) {
33 private val log = LoggerFactory.getLogger(TemplateResolutionService::class.toString())
35 suspend fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
36 bluePrintRuntimeService: BluePrintRuntimeService<*>,
37 artifactPrefix: String,
40 withContext(Dispatchers.IO) {
42 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
44 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
45 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
47 findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
55 suspend fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
56 blueprintName: String,
57 blueprintVersion: String,
58 artifactPrefix: String,
59 resolutionKey: String,
62 withContext(Dispatchers.IO) {
64 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
70 )?.result ?: throw EmptyResultDataAccessException(1)
73 suspend fun findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactName(
74 bluePrintRuntimeService: BluePrintRuntimeService<*>,
75 artifactPrefix: String,
78 withContext(Dispatchers.IO) {
80 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
82 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
83 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
85 templateResolutionRepository.findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
90 ) ?: throw EmptyResultDataAccessException(1)
93 suspend fun findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersion(
94 bluePrintRuntimeService: BluePrintRuntimeService<*>,
96 ): Map<String, List<String>> =
97 withContext(Dispatchers.IO) {
99 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
101 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
102 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
104 val resultList = templateResolutionRepository.findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersionAndOccurrence(
108 ) ?: throw EmptyResultDataAccessException(1)
110 var resultMap: MutableMap<String, MutableList<String>> = mutableMapOf()
112 resultList.forEach { it ->
113 if (!resultMap.containsKey(it.getArtifactName())) {
114 resultMap[it.getArtifactName()] = mutableListOf(it.getResolutionKey())
116 resultMap[it.getArtifactName()]!!.add(it.getResolutionKey())
120 .mapValues { it.value.toList() }
124 suspend fun findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
125 blueprintName: String,
126 blueprintVersion: String,
127 artifactPrefix: String,
129 resourceType: String,
132 withContext(Dispatchers.IO) {
134 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
145 properties: Map<String, Any>,
147 bluePrintRuntimeService: BluePrintRuntimeService<*>,
148 artifactPrefix: String
149 ): TemplateResolution = withContext(Dispatchers.IO) {
151 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
153 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
154 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
155 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
156 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
157 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
158 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
172 blueprintName: String,
173 blueprintVersion: String,
174 artifactPrefix: String,
177 resolutionKey: String = "",
178 resourceId: String = "",
179 resourceType: String = ""
180 ): TemplateResolution =
181 withContext(Dispatchers.IO) {
183 val resourceResolutionResult = TemplateResolution()
184 resourceResolutionResult.id = UUID.randomUUID().toString()
185 resourceResolutionResult.artifactName = artifactPrefix
186 resourceResolutionResult.blueprintVersion = blueprintVersion
187 resourceResolutionResult.blueprintName = blueprintName
188 resourceResolutionResult.resolutionKey = resolutionKey
189 resourceResolutionResult.resourceId = resourceId
190 resourceResolutionResult.resourceType = resourceType
191 resourceResolutionResult.result = template
192 resourceResolutionResult.occurrence = occurrence
194 // Overwrite template resolution-key of resourceId/resourceType already existant
195 if (resolutionKey.isNotEmpty()) {
196 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
197 resolutionKey, blueprintName, blueprintVersion, artifactPrefix, occurrence
200 "Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
201 "artifactName=($artifactPrefix) and resolutionKey=($resolutionKey)"
203 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
211 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
212 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
213 resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix, occurrence
216 "Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
217 "artifactName=($artifactPrefix), resourceId=($resourceId) and resourceType=($resourceType)"
219 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
231 "Writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
232 " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence"
234 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
235 } catch (ex: DataIntegrityViolationException) {
237 "Error writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
238 " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence error: {}",
241 throw BluePrintException("Failed to store resource api result.", ex)
245 * This returns the templates of first N 'occurrences'.
247 * @param blueprintName
248 * @param blueprintVersion
249 * @param artifactPrefix
250 * @param resolutionKey
253 suspend fun findFirstNOccurrences(
254 blueprintName: String,
255 blueprintVersion: String,
256 artifactPrefix: String,
257 resolutionKey: String,
259 ): Map<Int, List<TemplateResolution>> = withContext(Dispatchers.IO) {
261 templateResolutionRepository.findFirstNOccurrences(
267 ).groupBy(TemplateResolution::occurrence).toSortedMap(reverseOrder())
271 * This returns the templates of last N 'occurrences'.
273 * @param blueprintName
274 * @param blueprintVersion
275 * @param artifactPrefix
276 * @param resolutionKey
279 suspend fun findLastNOccurrences(
280 blueprintName: String,
281 blueprintVersion: String,
282 artifactPrefix: String,
283 resolutionKey: String,
285 ): Map<Int, List<TemplateResolution>> = withContext(Dispatchers.IO) {
287 templateResolutionRepository.findLastNOccurrences(
293 ).groupBy(TemplateResolution::occurrence).toSortedMap(reverseOrder())
297 * This returns the templates with 'occurrence' value between begin and end.
299 * @param blueprintName
300 * @param blueprintVersion
301 * @param artifactPrefix
302 * @param resolutionKey
306 suspend fun findOccurrencesWithinRange(
307 blueprintName: String,
308 blueprintVersion: String,
309 artifactPrefix: String,
310 resolutionKey: String,
313 ): Map<Int, List<TemplateResolution>> = withContext(Dispatchers.IO) {
315 templateResolutionRepository.findOccurrencesWithinRange(
322 ).groupBy(TemplateResolution::occurrence).toSortedMap(reverseOrder())
325 suspend fun readWithResolutionKey(
326 blueprintName: String,
327 blueprintVersion: String,
328 artifactPrefix: String,
329 resolutionKey: String
330 ): List<TemplateResolution> = withContext(Dispatchers.IO) {
332 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(