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: ResourceResolutionRepository) {
32 suspend fun write(properties: Map<String, Any>, result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
33 artifactPrefix: String) = withContext(Dispatchers.IO) {
35 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
37 val resourceResolutionResult = ResourceResolutionResult()
38 resourceResolutionResult.id = UUID.randomUUID().toString()
39 resourceResolutionResult.artifactName = artifactPrefix
40 resourceResolutionResult.blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
41 resourceResolutionResult.blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
42 resourceResolutionResult.resolutionKey =
43 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
44 resourceResolutionResult.result = result
47 resourceResolutionRepository.saveAndFlush(resourceResolutionResult)
48 } catch (ex: DataIntegrityViolationException) {
49 throw BluePrintException("Failed to store resource resolution result.", ex)
53 suspend fun read(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactPrefix: String,
54 resolutionKey: String): String = withContext(Dispatchers.IO) {
56 val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
58 val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
59 val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
61 resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
65 artifactPrefix).result!!