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> = withContext(Dispatchers.IO) {
67 resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
74 suspend fun write(properties: Map<String, Any>,
75 bluePrintRuntimeService: BluePrintRuntimeService<*>,
76 artifactPrefix: String,
77 resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
79 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
81 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
82 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
84 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
86 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE].toString()
88 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID].toString()
99 suspend fun write(blueprintName: String,
100 blueprintVersion: String,
101 resolutionKey: String,
103 resourceType: String,
104 artifactPrefix: String,
105 resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
107 val resourceResolution = ResourceResolution()
108 resourceResolution.id = UUID.randomUUID().toString()
109 resourceResolution.artifactName = artifactPrefix
110 resourceResolution.blueprintVersion = blueprintVersion
111 resourceResolution.blueprintName = blueprintName
112 resourceResolution.resolutionKey = resolutionKey
113 resourceResolution.resourceType = resourceType
114 resourceResolution.resourceId = resourceId
115 resourceResolution.value = JacksonUtils.getValue(resourceAssignment.property?.value!!).toString()
116 resourceResolution.name = resourceAssignment.name
117 resourceResolution.dictionaryName = resourceAssignment.dictionaryName
118 resourceResolution.dictionaryVersion = resourceAssignment.version
119 resourceResolution.dictionarySource = resourceAssignment.dictionarySource
120 resourceResolution.status = resourceAssignment.status
123 resourceResolutionRepository.saveAndFlush(resourceResolution)
124 } catch (ex: Exception) {
125 throw BluePrintException("Failed to store resource resolution result.", ex)