Renaming Files having BluePrint to have Blueprint
[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.slf4j.LoggerFactory
25 import org.springframework.dao.DataIntegrityViolationException
26 import org.springframework.dao.EmptyResultDataAccessException
27 import org.springframework.stereotype.Service
28 import java.util.UUID
29
30 @Service
31 class TemplateResolutionService(private val templateResolutionRepository: TemplateResolutionRepository) {
32
33     private val log = LoggerFactory.getLogger(TemplateResolutionService::class.toString())
34
35     suspend fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
36         bluePrintRuntimeService: BlueprintRuntimeService<*>,
37         artifactPrefix: String,
38         resolutionKey: String
39     ): String =
40         withContext(Dispatchers.IO) {
41
42             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
43
44             val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
45             val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
46
47             findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
48                 blueprintName,
49                 blueprintVersion,
50                 artifactPrefix,
51                 resolutionKey
52             )
53         }
54
55     suspend fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
56         blueprintName: String,
57         blueprintVersion: String,
58         artifactPrefix: String,
59         resolutionKey: String,
60         occurrence: Int = 1
61     ): String =
62         withContext(Dispatchers.IO) {
63
64             templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
65                 resolutionKey,
66                 blueprintName,
67                 blueprintVersion,
68                 artifactPrefix,
69                 occurrence
70             )?.result ?: throw EmptyResultDataAccessException(1)
71         }
72
73     suspend fun findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(
74         blueprintName: String,
75         blueprintVersion: String,
76         artifactPrefix: String,
77         resourceId: String,
78         resourceType: String,
79         occurrence: Int = 1
80     ): String =
81         withContext(Dispatchers.IO) {
82
83             templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
84                 resourceId,
85                 resourceType,
86                 blueprintName,
87                 blueprintVersion,
88                 artifactPrefix,
89                 occurrence
90             )?.result!!
91         }
92
93     suspend fun write(
94         properties: Map<String, Any>,
95         result: String,
96         bluePrintRuntimeService: BlueprintRuntimeService<*>,
97         artifactPrefix: String
98     ): TemplateResolution = withContext(Dispatchers.IO) {
99
100         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
101
102         val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
103         val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
104         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
105         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
106         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
107         val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
108         write(
109             blueprintName,
110             blueprintVersion,
111             artifactPrefix,
112             result,
113             occurrence,
114             resolutionKey,
115             resourceId,
116             resourceType
117         )
118     }
119
120     suspend fun write(
121         blueprintName: String,
122         blueprintVersion: String,
123         artifactPrefix: String,
124         template: String,
125         occurrence: Int = 1,
126         resolutionKey: String = "",
127         resourceId: String = "",
128         resourceType: String = ""
129     ): TemplateResolution =
130         withContext(Dispatchers.IO) {
131
132             val resourceResolutionResult = TemplateResolution()
133             resourceResolutionResult.id = UUID.randomUUID().toString()
134             resourceResolutionResult.artifactName = artifactPrefix
135             resourceResolutionResult.blueprintVersion = blueprintVersion
136             resourceResolutionResult.blueprintName = blueprintName
137             resourceResolutionResult.resolutionKey = resolutionKey
138             resourceResolutionResult.resourceId = resourceId
139             resourceResolutionResult.resourceType = resourceType
140             resourceResolutionResult.result = template
141             resourceResolutionResult.occurrence = occurrence
142
143             // Overwrite template resolution-key of resourceId/resourceType already existant
144             if (resolutionKey.isNotEmpty()) {
145                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
146                     resolutionKey, blueprintName, blueprintVersion, artifactPrefix, occurrence
147                 )?.let {
148                     log.info(
149                         "Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
150                             "artifactName=($artifactPrefix) and resolutionKey=($resolutionKey)"
151                     )
152                     templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
153                         resolutionKey,
154                         blueprintName,
155                         blueprintVersion,
156                         artifactPrefix,
157                         occurrence
158                     )
159                 }
160             } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
161                 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
162                     resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix, occurrence
163                 )?.let {
164                     log.info(
165                         "Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
166                             "artifactName=($artifactPrefix), resourceId=($resourceId) and resourceType=($resourceType)"
167                     )
168                     templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
169                         resourceId,
170                         resourceType,
171                         blueprintName,
172                         blueprintVersion,
173                         artifactPrefix,
174                         occurrence
175                     )
176                 }
177             }
178             try {
179                 log.info(
180                     "Writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
181                         " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence"
182                 )
183                 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
184             } catch (ex: DataIntegrityViolationException) {
185                 log.error(
186                     "Error writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
187                         " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence error: {}",
188                     ex.message
189                 )
190                 throw BlueprintException("Failed to store resource api result.", ex)
191             }
192         }
193 }