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.stereotype.Service
32 class ResourceResolutionDBService(private val resourceResolutionRepository: ResourceResolutionRepository) {
34 private val log = LoggerFactory.getLogger(ResourceResolutionDBService::class.toString())
36 suspend fun readValue(blueprintName: String,
37 blueprintVersion: String,
38 artifactPrefix: String,
39 resolutionKey: String,
40 name: String): ResourceResolution = withContext(Dispatchers.IO) {
42 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
50 suspend fun readWithResolutionKey(blueprintName: String,
51 blueprintVersion: String,
52 artifactPrefix: String,
53 resolutionKey: String): List<ResourceResolution> = withContext(Dispatchers.IO) {
55 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
62 suspend fun readWithResourceIdAndResourceType(blueprintName: String,
63 blueprintVersion: String,
65 resourceType: String): List<ResourceResolution> =
66 withContext(Dispatchers.IO) {
68 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
75 suspend fun write(properties: Map<String, Any>,
76 bluePrintRuntimeService: BluePrintRuntimeService<*>,
77 artifactPrefix: String,
78 resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
80 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
82 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
83 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
85 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
87 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE].toString()
89 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID].toString()
100 suspend fun write(blueprintName: String,
101 blueprintVersion: String,
102 resolutionKey: String,
104 resourceType: String,
105 artifactPrefix: String,
106 resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
108 val resourceResolution = ResourceResolution()
109 resourceResolution.id = UUID.randomUUID().toString()
110 resourceResolution.artifactName = artifactPrefix
111 resourceResolution.blueprintVersion = blueprintVersion
112 resourceResolution.blueprintName = blueprintName
113 resourceResolution.resolutionKey = resolutionKey
114 resourceResolution.resourceType = resourceType
115 resourceResolution.resourceId = resourceId
116 if (BluePrintConstants.STATUS_FAILURE == resourceAssignment.status) {
117 resourceResolution.value = ""
119 resourceResolution.value = JacksonUtils.getValue(resourceAssignment.property?.value!!).toString()
121 resourceResolution.name = resourceAssignment.name
122 resourceResolution.dictionaryName = resourceAssignment.dictionaryName
123 resourceResolution.dictionaryVersion = resourceAssignment.version
124 resourceResolution.dictionarySource = resourceAssignment.dictionarySource
125 resourceResolution.status = resourceAssignment.status
128 resourceResolutionRepository.saveAndFlush(resourceResolution)
129 } catch (ex: Exception) {
130 throw BluePrintException("Failed to store resource resolution result.", ex)