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 / ResourceResolutionDBService.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.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
25 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
26 import org.slf4j.LoggerFactory
27 import org.springframework.dao.EmptyResultDataAccessException
28 import org.springframework.stereotype.Service
29 import java.util.UUID
30
31 @Service
32 class ResourceResolutionDBService(private val resourceResolutionRepository: ResourceResolutionRepository) {
33
34     private val log = LoggerFactory.getLogger(ResourceResolutionDBService::class.toString())
35
36     suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
37         bluePrintRuntimeService: BlueprintRuntimeService<*>,
38         key: String,
39         occurrence: Int,
40         artifactPrefix: String
41     ): List<ResourceResolution> {
42         return try {
43             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
44
45             val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
46             val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
47
48             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
49                 blueprintName,
50                 blueprintVersion,
51                 artifactPrefix,
52                 key,
53                 occurrence
54             )
55         } catch (e: EmptyResultDataAccessException) {
56             emptyList()
57         }
58     }
59
60     suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
61         bluePrintRuntimeService: BlueprintRuntimeService<*>,
62         resourceId: String,
63         resourceType: String,
64         occurrence: Int,
65         artifactPrefix: String
66     ): List<ResourceResolution> {
67         return try {
68
69             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
70
71             val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
72             val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
73
74             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
75                 blueprintName,
76                 blueprintVersion,
77                 artifactPrefix,
78                 resourceId,
79                 resourceType,
80                 occurrence
81             )
82         } catch (e: EmptyResultDataAccessException) {
83             emptyList()
84         }
85     }
86
87     suspend fun readValue(
88         blueprintName: String,
89         blueprintVersion: String,
90         artifactPrefix: String,
91         resolutionKey: String,
92         name: String
93     ): ResourceResolution = withContext(Dispatchers.IO) {
94
95         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
96             resolutionKey,
97             blueprintName,
98             blueprintVersion,
99             artifactPrefix,
100             name
101         )
102     }
103
104     suspend fun readWithResolutionKey(
105         blueprintName: String,
106         blueprintVersion: String,
107         artifactPrefix: String,
108         resolutionKey: String
109     ): List<ResourceResolution> = withContext(Dispatchers.IO) {
110
111         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
112             resolutionKey,
113             blueprintName,
114             blueprintVersion,
115             artifactPrefix
116         )
117     }
118
119     suspend fun readWithResourceIdAndResourceType(
120         blueprintName: String,
121         blueprintVersion: String,
122         resourceId: String,
123         resourceType: String
124     ): List<ResourceResolution> =
125         withContext(Dispatchers.IO) {
126
127             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
128                 blueprintName,
129                 blueprintVersion,
130                 resourceId,
131                 resourceType
132             )
133         }
134
135     suspend fun write(
136         properties: Map<String, Any>,
137         bluePrintRuntimeService: BlueprintRuntimeService<*>,
138         artifactPrefix: String,
139         resourceAssignment: ResourceAssignment
140     ): ResourceResolution = withContext(Dispatchers.IO) {
141
142         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
143
144         val blueprintVersion = metadata[BlueprintConstants.METADATA_TEMPLATE_VERSION]!!
145         val blueprintName = metadata[BlueprintConstants.METADATA_TEMPLATE_NAME]!!
146
147         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
148         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
149         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
150         val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
151
152         write(
153             blueprintName,
154             blueprintVersion,
155             resolutionKey,
156             resourceId,
157             resourceType,
158             artifactPrefix,
159             resourceAssignment,
160             occurrence
161         )
162     }
163
164     suspend fun write(
165         blueprintName: String,
166         blueprintVersion: String,
167         resolutionKey: String,
168         resourceId: String,
169         resourceType: String,
170         artifactPrefix: String,
171         resourceAssignment: ResourceAssignment,
172         occurrence: Int = 0
173     ): ResourceResolution = withContext(Dispatchers.IO) {
174
175         val resourceResolution = ResourceResolution()
176         resourceResolution.id = UUID.randomUUID().toString()
177         resourceResolution.artifactName = artifactPrefix
178         resourceResolution.occurrence = occurrence
179         resourceResolution.blueprintVersion = blueprintVersion
180         resourceResolution.blueprintName = blueprintName
181         resourceResolution.resolutionKey = resolutionKey
182         resourceResolution.resourceType = resourceType
183         resourceResolution.resourceId = resourceId
184         resourceResolution.value = resourceAssignment.property?.value?.let {
185             if (BlueprintConstants.STATUS_SUCCESS == resourceAssignment.status)
186                 JacksonUtils.getValue(it).toString()
187             else ""
188         } ?: ""
189         resourceResolution.name = resourceAssignment.name
190         resourceResolution.dictionaryName = resourceAssignment.dictionaryName
191         resourceResolution.dictionaryVersion = resourceAssignment.version
192         resourceResolution.dictionarySource = resourceAssignment.dictionarySource
193         resourceResolution.status = resourceAssignment.status ?: BlueprintConstants.STATUS_FAILURE
194
195         try {
196             resourceResolutionRepository.saveAndFlush(resourceResolution)
197         } catch (ex: Exception) {
198             throw BlueprintException("Failed to store resource resolution result.", ex)
199         }
200     }
201
202     /**
203      * This is a deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey method to delete resources
204      * associated to a specific resolution-key
205      *
206      * @param blueprintName name of the CBA
207      * @param blueprintVersion version of the CBA
208      * @param artifactName name of the artifact
209      * @param resolutionKey value of the resolution-key
210      */
211     suspend fun deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
212         blueprintName: String,
213         blueprintVersion: String,
214         artifactName: String,
215         resolutionKey: String
216     ) {
217         resourceResolutionRepository.deleteByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKey(
218             blueprintName,
219             blueprintVersion,
220             artifactName,
221             resolutionKey
222         )
223     }
224 }