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,
38 resolutionKey: String): String =
39 withContext(Dispatchers.IO) {
41 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
43 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
44 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
46 findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(blueprintName,
52 suspend fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(blueprintName: String,
53 blueprintVersion: String,
54 artifactPrefix: String,
55 resolutionKey: String,
56 occurrence: Int = 1): String =
57 withContext(Dispatchers.IO) {
59 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
64 occurrence)?.result ?: throw EmptyResultDataAccessException(1)
67 suspend fun findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(blueprintName: String,
68 blueprintVersion: String,
69 artifactPrefix: String,
72 occurrence: Int = 1): String =
73 withContext(Dispatchers.IO) {
75 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
84 suspend fun write(properties: Map<String, Any>,
85 result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
86 artifactPrefix: String): TemplateResolution = withContext(Dispatchers.IO) {
88 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
90 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
91 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
92 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
93 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
94 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
95 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
106 suspend fun write(blueprintName: String, blueprintVersion: String, artifactPrefix: String,
107 template: String, occurrence: Int = 1, resolutionKey: String = "", resourceId: String = "",
108 resourceType: String = ""): TemplateResolution =
109 withContext(Dispatchers.IO) {
111 val resourceResolutionResult = TemplateResolution()
112 resourceResolutionResult.id = UUID.randomUUID().toString()
113 resourceResolutionResult.artifactName = artifactPrefix
114 resourceResolutionResult.blueprintVersion = blueprintVersion
115 resourceResolutionResult.blueprintName = blueprintName
116 resourceResolutionResult.resolutionKey = resolutionKey
117 resourceResolutionResult.resourceId = resourceId
118 resourceResolutionResult.resourceType = resourceType
119 resourceResolutionResult.result = template
120 resourceResolutionResult.occurrence = occurrence
122 // Overwrite template resolution-key of resourceId/resourceType already existant
123 if (resolutionKey.isNotEmpty()) {
124 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
125 resolutionKey, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
126 log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
127 "artifactName=($artifactPrefix) and resolutionKey=($resolutionKey)")
128 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
135 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
136 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
137 resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
138 log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
139 "artifactName=($artifactPrefix), resourceId=($resourceId) and resourceType=($resourceType)")
140 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
150 log.info("Writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
151 " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence")
152 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
153 } catch (ex: DataIntegrityViolationException) {
154 log.error("Error writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
155 " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence error: {}", ex.message)
156 throw BluePrintException("Failed to store resource api result.", ex)