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.DataIntegrityViolationException
28 import org.springframework.dao.EmptyResultDataAccessException
29 import org.springframework.stereotype.Service
33 class ResourceResolutionDBService(private val resourceResolutionRepository: ResourceResolutionRepository) {
35 private val log = LoggerFactory.getLogger(ResourceResolutionDBService::class.toString())
37 suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
38 bluePrintRuntimeService: BluePrintRuntimeService<*>, key: String,
39 occurrence: Int, artifactPrefix: String): List<ResourceResolution> {
41 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
43 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
44 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
46 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
52 } catch (e: EmptyResultDataAccessException) {
57 suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
58 bluePrintRuntimeService: BluePrintRuntimeService<*>, resourceId: String,
59 resourceType: String, occurrence: Int,
60 artifactPrefix: String): List<ResourceResolution> {
63 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
65 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
66 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
68 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
75 } catch (e: EmptyResultDataAccessException) {
80 suspend fun readValue(blueprintName: String,
81 blueprintVersion: String,
82 artifactPrefix: String,
83 resolutionKey: String,
84 name: String): ResourceResolution = withContext(Dispatchers.IO) {
86 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
94 suspend fun readWithResolutionKey(blueprintName: String,
95 blueprintVersion: String,
96 artifactPrefix: String,
97 resolutionKey: String): List<ResourceResolution> = withContext(Dispatchers.IO) {
99 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
106 suspend fun readWithResourceIdAndResourceType(blueprintName: String,
107 blueprintVersion: String,
109 resourceType: String): List<ResourceResolution> =
110 withContext(Dispatchers.IO) {
112 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
119 suspend fun write(properties: Map<String, Any>,
120 bluePrintRuntimeService: BluePrintRuntimeService<*>,
121 artifactPrefix: String,
122 resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
124 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
126 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
127 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
129 val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] as String
130 val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
131 val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
132 val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
144 suspend fun write(blueprintName: String,
145 blueprintVersion: String,
146 resolutionKey: String,
148 resourceType: String,
149 artifactPrefix: String,
150 resourceAssignment: ResourceAssignment,
151 occurrence: Int = 0): ResourceResolution = withContext(Dispatchers.IO) {
153 val resourceResolution = ResourceResolution()
154 resourceResolution.id = UUID.randomUUID().toString()
155 resourceResolution.artifactName = artifactPrefix
156 resourceResolution.occurrence = occurrence
157 resourceResolution.blueprintVersion = blueprintVersion
158 resourceResolution.blueprintName = blueprintName
159 resourceResolution.resolutionKey = resolutionKey
160 resourceResolution.resourceType = resourceType
161 resourceResolution.resourceId = resourceId
162 if (BluePrintConstants.STATUS_SUCCESS == resourceAssignment.status) {
163 resourceResolution.value = JacksonUtils.getValue(resourceAssignment.property?.value!!).toString()
165 resourceResolution.value = ""
167 resourceResolution.name = resourceAssignment.name
168 resourceResolution.dictionaryName = resourceAssignment.dictionaryName
169 resourceResolution.dictionaryVersion = resourceAssignment.version
170 resourceResolution.dictionarySource = resourceAssignment.dictionarySource
171 resourceResolution.status = resourceAssignment.status
174 resourceResolutionRepository.saveAndFlush(resourceResolution)
175 } catch (ex: Exception) {
176 throw BluePrintException("Failed to store resource resolution result.", ex)