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.springframework.dao.DataIntegrityViolationException
25 import org.springframework.stereotype.Service
29 class TemplateResolutionService(private val templateResolutionRepository: TemplateResolutionRepository) {
31 suspend fun read(bluePrintRuntimeService: BluePrintRuntimeService<*>,
32 artifactPrefix: String,
33 resolutionKey: String): String = withContext(Dispatchers.IO) {
35 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
37 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
38 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
40 read(blueprintName, blueprintVersion, artifactPrefix, resolutionKey)
43 suspend fun read(blueprintName: String,
44 blueprintVersion: String,
45 artifactPrefix: String,
46 resolutionKey: String): String = withContext(Dispatchers.IO) {
48 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
52 artifactPrefix).result!!
55 suspend fun write(properties: Map<String, Any>,
56 result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
57 artifactPrefix: String): TemplateResolution = withContext(Dispatchers.IO) {
59 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
61 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
62 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
64 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
66 write(blueprintName, blueprintVersion, resolutionKey, artifactPrefix, result)
69 suspend fun write(blueprintName: String,
70 blueprintVersion: String,
71 resolutionKey: String,
72 artifactPrefix: String,
73 template: String): TemplateResolution = withContext(Dispatchers.IO) {
75 val resourceResolutionResult = TemplateResolution()
76 resourceResolutionResult.id = UUID.randomUUID().toString()
77 resourceResolutionResult.artifactName = artifactPrefix
78 resourceResolutionResult.blueprintVersion = blueprintVersion
79 resourceResolutionResult.blueprintName = blueprintName
80 resourceResolutionResult.resolutionKey = resolutionKey
81 resourceResolutionResult.result = template
84 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
85 } catch (ex: DataIntegrityViolationException) {
86 throw BluePrintException("Failed to store resource api result.", ex)