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(
119 suspend fun readWithResourceIdAndResourceType(
120 blueprintName: String,
121 blueprintVersion: String,
124 ): List<ResourceResolution> =
125 withContext(Dispatchers.IO) {
127 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
136 properties: Map<String, Any>,
137 bluePrintRuntimeService: BluePrintRuntimeService<*>,
138 artifactPrefix: String,
139 resourceAssignment: ResourceAssignment
140 ): ResourceResolution = withContext(Dispatchers.IO) {
142 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
144 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
145 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
147 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
148 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
149 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
150 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
165 blueprintName: String,
166 blueprintVersion: String,
167 resolutionKey: String,
169 resourceType: String,
170 artifactPrefix: String,
171 resourceAssignment: ResourceAssignment,
173 ): ResourceResolution = withContext(Dispatchers.IO) {
175 val resourceResolution = ResourceResolution()
176 resourceResolution.id = UUID.randomUUID().toString()
177 resourceResolution.artifactName = artifactPrefix
178 resourceResolution.occurrence = occurrence
179 resourceResolution.blueprintVersion = blueprintVersion
180 resourceResolution.blueprintName = blueprintName
181 resourceResolution.resolutionKey = resolutionKey
182 resourceResolution.resourceType = resourceType
183 resourceResolution.resourceId = resourceId
184 resourceResolution.value = resourceAssignment.property?.value?.let {
185 if (BluePrintConstants.STATUS_SUCCESS == resourceAssignment.status)
186 JacksonUtils.getValue(it).toString()
189 resourceResolution.name = resourceAssignment.name
190 resourceResolution.dictionaryName = resourceAssignment.dictionaryName
191 resourceResolution.dictionaryVersion = resourceAssignment.version
192 resourceResolution.dictionarySource = resourceAssignment.dictionarySource
193 resourceResolution.status = resourceAssignment.status ?: BluePrintConstants.STATUS_FAILURE
196 resourceResolutionRepository.saveAndFlush(resourceResolution)
197 } catch (ex: Exception) {
198 throw BluePrintException("Failed to store resource resolution result.", ex)
203 * This is a deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey method to delete resources
204 * associated to a specific resolution-key
206 * @param blueprintName name of the CBA
207 * @param blueprintVersion version of the CBA
208 * @param artifactName name of the artifact
209 * @param resolutionKey value of the resolution-key
211 suspend fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
212 blueprintName: String,
213 blueprintVersion: String,
214 artifactName: String,
215 resolutionKey: String
217 resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(