cbc68bbc9acf0e21661fed34742bae46b88ed14f
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  * Modifications Copyright © 2019 IBM.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
18
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
27 import java.util.*
28
29 @Service
30 class ResourceResolutionResultService(private val resourceResolutionRepository: ResourceResolutionRepository) {
31
32     suspend fun write(properties: Map<String, Any>, result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
33                       artifactPrefix: String) = withContext(Dispatchers.IO) {
34
35         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
36
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
45
46         try {
47             resourceResolutionRepository.saveAndFlush(resourceResolutionResult)
48         } catch (ex: DataIntegrityViolationException) {
49             throw BluePrintException("Failed to store resource resolution result.", ex)
50         }
51     }
52
53     suspend fun read(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactPrefix: String,
54                      resolutionKey: String): String = withContext(Dispatchers.IO) {
55
56         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
57
58         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
59         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
60
61         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
62                 resolutionKey,
63                 blueprintName,
64                 blueprintVersion,
65                 artifactPrefix).result!!
66     }
67 }