Share RR context within node template
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / db / TemplateResolutionService.kt
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
17
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
26 import java.util.*
27
28 @Service
29 class TemplateResolutionService(private val templateResolutionRepository: TemplateResolutionRepository) {
30
31     suspend fun read(bluePrintRuntimeService: BluePrintRuntimeService<*>,
32                      artifactPrefix: String,
33                      resolutionKey: String): String = withContext(Dispatchers.IO) {
34
35         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
36
37         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
38         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
39
40         read(blueprintName, blueprintVersion, artifactPrefix, resolutionKey)
41     }
42
43     suspend fun read(blueprintName: String,
44                      blueprintVersion: String,
45                      artifactPrefix: String,
46                      resolutionKey: String): String = withContext(Dispatchers.IO) {
47
48         templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
49             resolutionKey,
50             blueprintName,
51             blueprintVersion,
52             artifactPrefix).result!!
53     }
54
55     suspend fun write(properties: Map<String, Any>,
56                       result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
57                       artifactPrefix: String): TemplateResolution = withContext(Dispatchers.IO) {
58
59         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
60
61         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
62         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
63         val resolutionKey =
64             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
65
66         write(blueprintName, blueprintVersion, resolutionKey, artifactPrefix, result)
67     }
68
69     suspend fun write(blueprintName: String,
70                       blueprintVersion: String,
71                       resolutionKey: String,
72                       artifactPrefix: String,
73                       template: String): TemplateResolution = withContext(Dispatchers.IO) {
74
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
82
83         try {
84             templateResolutionRepository.saveAndFlush(resourceResolutionResult)
85         } catch (ex: DataIntegrityViolationException) {
86             throw BluePrintException("Failed to store resource api result.", ex)
87         }
88     }
89 }