2 * Copyright (C) 2019 Bell Canada.
3 * Modifications Copyright © 2019 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
19 import kotlinx.coroutines.Dispatchers
20 import kotlinx.coroutines.withContext
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
25 import org.springframework.dao.DataIntegrityViolationException
26 import org.springframework.stereotype.Service
30 class ResourceResolutionResultService(private val resourceResolutionRepository: ResourceResolutionResultRepository) {
32 suspend fun read(bluePrintRuntimeService: BluePrintRuntimeService<*>, 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 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
44 artifactPrefix).result!!
47 suspend fun write(properties: Map<String, Any>, result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
48 artifactPrefix: String) = withContext(Dispatchers.IO) {
50 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
52 val resourceResolutionResult = ResourceResolutionResult()
53 resourceResolutionResult.id = UUID.randomUUID().toString()
54 resourceResolutionResult.artifactName = artifactPrefix
55 resourceResolutionResult.blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
56 resourceResolutionResult.blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
57 resourceResolutionResult.resolutionKey =
58 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
59 resourceResolutionResult.result = result
62 resourceResolutionRepository.saveAndFlush(resourceResolutionResult)
63 } catch (ex: DataIntegrityViolationException) {
64 throw BluePrintException("Failed to store resource resolution result.", ex)
68 suspend fun readByKey(resolutionResultId: String): String = withContext(Dispatchers.IO) {
70 resourceResolutionRepository.getOne(resolutionResultId).result!!
73 suspend fun deleteByKey(resolutionResultId: String): Unit = withContext(Dispatchers.IO) {
75 val row = resourceResolutionRepository.getOne(resolutionResultId)
76 resourceResolutionRepository.delete(row)
77 resourceResolutionRepository.flush()