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 = 0): 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 = 0): 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
107 suspend fun write(blueprintName: String, blueprintVersion: String, artifactPrefix: String,
108 template: String, occurrence: Int = 0, resolutionKey: String = "", resourceId: String = "",
109 resourceType: String = ""): TemplateResolution =
110 withContext(Dispatchers.IO) {
112 val resourceResolutionResult = TemplateResolution()
113 resourceResolutionResult.id = UUID.randomUUID().toString()
114 resourceResolutionResult.artifactName = artifactPrefix
115 resourceResolutionResult.blueprintVersion = blueprintVersion
116 resourceResolutionResult.blueprintName = blueprintName
117 resourceResolutionResult.resolutionKey = resolutionKey
118 resourceResolutionResult.resourceId = resourceId
119 resourceResolutionResult.resourceType = resourceType
120 resourceResolutionResult.result = template
121 resourceResolutionResult.occurrence = occurrence
123 // Overwrite template resolution-key of resourceId/resourceType already existant
124 if (resolutionKey.isNotEmpty()) {
125 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
126 resolutionKey, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
127 log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
128 "artifactName=($artifactPrefix) and resolutionKey=($resolutionKey)")
129 templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
136 } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
137 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
138 resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
139 log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
140 "artifactName=($artifactPrefix), resourceId=($resourceId) and resourceType=($resourceType)")
141 templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
151 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
152 } catch (ex: DataIntegrityViolationException) {
153 throw BluePrintException("Failed to store resource api result.", ex)