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<*>, key: String,
38 occurrence: Int, artifactPrefix: String): List<ResourceResolution> {
40 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
42 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
43 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
45 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
51 } catch (e: EmptyResultDataAccessException) {
56 suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
57 bluePrintRuntimeService: BluePrintRuntimeService<*>, resourceId: String,
58 resourceType: String, occurrence: Int,
59 artifactPrefix: String): List<ResourceResolution> {
62 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
64 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
65 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
67 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
74 } catch (e: EmptyResultDataAccessException) {
79 suspend fun readValue(blueprintName: String,
80 blueprintVersion: String,
81 artifactPrefix: String,
82 resolutionKey: String,
83 name: String): ResourceResolution = withContext(Dispatchers.IO) {
85 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
93 suspend fun readWithResolutionKey(blueprintName: String,
94 blueprintVersion: String,
95 artifactPrefix: String,
96 resolutionKey: String): List<ResourceResolution> = withContext(Dispatchers.IO) {
98 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
105 suspend fun readWithResourceIdAndResourceType(blueprintName: String,
106 blueprintVersion: String,
108 resourceType: String): List<ResourceResolution> =
109 withContext(Dispatchers.IO) {
111 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
118 suspend fun write(properties: Map<String, Any>,
119 bluePrintRuntimeService: BluePrintRuntimeService<*>,
120 artifactPrefix: String,
121 resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
123 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
125 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
126 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
128 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
129 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
130 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
131 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
143 suspend fun write(blueprintName: String,
144 blueprintVersion: String,
145 resolutionKey: String,
147 resourceType: String,
148 artifactPrefix: String,
149 resourceAssignment: ResourceAssignment,
150 occurrence: Int = 0): ResourceResolution = withContext(Dispatchers.IO) {
152 val resourceResolution = ResourceResolution()
153 resourceResolution.id = UUID.randomUUID().toString()
154 resourceResolution.artifactName = artifactPrefix
155 resourceResolution.occurrence = occurrence
156 resourceResolution.blueprintVersion = blueprintVersion
157 resourceResolution.blueprintName = blueprintName
158 resourceResolution.resolutionKey = resolutionKey
159 resourceResolution.resourceType = resourceType
160 resourceResolution.resourceId = resourceId
161 if (BluePrintConstants.STATUS_SUCCESS == resourceAssignment.status) {
162 resourceResolution.value = JacksonUtils.getValue(resourceAssignment.property?.value!!).toString()
164 resourceResolution.value = ""
166 resourceResolution.name = resourceAssignment.name
167 resourceResolution.dictionaryName = resourceAssignment.dictionaryName
168 resourceResolution.dictionaryVersion = resourceAssignment.version
169 resourceResolution.dictionarySource = resourceAssignment.dictionarySource
170 resourceResolution.status = resourceAssignment.status
173 resourceResolutionRepository.saveAndFlush(resourceResolution)
174 } catch (ex: Exception) {
175 throw BluePrintException("Failed to store resource resolution result.", ex)