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 findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
74 blueprintName: String,
75 blueprintVersion: String,
76 artifactPrefix: String,
81 withContext(Dispatchers.IO) {
83 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
94 properties: Map<String, Any>,
96 bluePrintRuntimeService: BluePrintRuntimeService<*>,
97 artifactPrefix: String
98 ): TemplateResolution = withContext(Dispatchers.IO) {
100 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
102 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
103 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
104 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
105 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
106 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
107 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
121 blueprintName: String,
122 blueprintVersion: String,
123 artifactPrefix: String,
126 resolutionKey: String = "",
127 resourceId: String = "",
128 resourceType: String = ""
129 ): TemplateResolution =
130 withContext(Dispatchers.IO) {
132 val resourceResolutionResult = TemplateResolution()
133 resourceResolutionResult.id = UUID.randomUUID().toString()
134 resourceResolutionResult.artifactName = artifactPrefix
135 resourceResolutionResult.blueprintVersion = blueprintVersion
136 resourceResolutionResult.blueprintName = blueprintName
137 resourceResolutionResult.resolutionKey = resolutionKey
138 resourceResolutionResult.resourceId = resourceId
139 resourceResolutionResult.resourceType = resourceType
140 resourceResolutionResult.result = template
141 resourceResolutionResult.occurrence = occurrence
143 // Overwrite template resolution-key of resourceId/resourceType already existant
144 if (resolutionKey.isNotEmpty()) {
145 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
146 resolutionKey, blueprintName, blueprintVersion, artifactPrefix, occurrence
149 "Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
150 "artifactName=($artifactPrefix) and resolutionKey=($resolutionKey)"
152 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
160 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
161 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
162 resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix, occurrence
165 "Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
166 "artifactName=($artifactPrefix), resourceId=($resourceId) and resourceType=($resourceType)"
168 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
180 "Writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
181 " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence"
183 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
184 } catch (ex: DataIntegrityViolationException) {
186 "Error writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
187 " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence error: {}",
190 throw BluePrintException("Failed to store resource api result.", ex)