Merge "Created media folders for ResourceDictionary"
[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.*
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): String =
39         withContext(Dispatchers.IO) {
40
41             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
42
43             val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
44             val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
45
46             findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(blueprintName,
47                 blueprintVersion,
48                 artifactPrefix,
49                 resolutionKey)
50         }
51
52     suspend fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(blueprintName: String,
53                                                                                       blueprintVersion: String,
54                                                                                       artifactPrefix: String,
55                                                                                       resolutionKey: String,
56                                                                                       occurrence: Int = 0): String =
57         withContext(Dispatchers.IO) {
58
59             templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
60                 resolutionKey,
61                 blueprintName,
62                 blueprintVersion,
63                 artifactPrefix,
64                 occurrence)?.result ?: throw EmptyResultDataAccessException(1)
65         }
66
67     suspend fun findByResoureIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactName(blueprintName: String,
68                                                                                                  blueprintVersion: String,
69                                                                                                  artifactPrefix: String,
70                                                                                                  resourceId: String,
71                                                                                                  resourceType: String,
72                                                                                                  occurrence: Int = 0): String =
73         withContext(Dispatchers.IO) {
74
75             templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
76                 resourceId,
77                 resourceType,
78                 blueprintName,
79                 blueprintVersion,
80                 artifactPrefix,
81                 occurrence)?.result!!
82         }
83
84     suspend fun write(properties: Map<String, Any>,
85                       result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
86                       artifactPrefix: String): TemplateResolution = withContext(Dispatchers.IO) {
87
88         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
89
90         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
91         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
92         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
93         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
94         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
95         val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
96
97         write(blueprintName,
98             blueprintVersion,
99             artifactPrefix,
100             result,
101             occurrence,
102             resolutionKey,
103             resourceId,
104             resourceType)
105     }
106
107     suspend fun write(blueprintName: String, blueprintVersion: String, artifactPrefix: String,
108                       template: String, occurrence: Int = 0, resolutionKey: String = "", resourceId: String = "",
109                       resourceType: String = ""): TemplateResolution =
110         withContext(Dispatchers.IO) {
111
112             val resourceResolutionResult = TemplateResolution()
113             resourceResolutionResult.id = UUID.randomUUID().toString()
114             resourceResolutionResult.artifactName = artifactPrefix
115             resourceResolutionResult.blueprintVersion = blueprintVersion
116             resourceResolutionResult.blueprintName = blueprintName
117             resourceResolutionResult.resolutionKey = resolutionKey
118             resourceResolutionResult.resourceId = resourceId
119             resourceResolutionResult.resourceType = resourceType
120             resourceResolutionResult.result = template
121             resourceResolutionResult.occurrence = occurrence
122
123             // Overwrite template resolution-key of resourceId/resourceType already existant
124             if (resolutionKey.isNotEmpty()) {
125                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
126                     resolutionKey, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
127                     log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
128                             "artifactName=($artifactPrefix) and resolutionKey=($resolutionKey)")
129                     templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
130                         resolutionKey,
131                         blueprintName,
132                         blueprintVersion,
133                         artifactPrefix,
134                         occurrence)
135                 }
136             } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
137                 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
138                     resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
139                     log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
140                             "artifactName=($artifactPrefix), resourceId=($resourceId) and resourceType=($resourceType)")
141                     templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
142                         resourceId,
143                         resourceType,
144                         blueprintName,
145                         blueprintVersion,
146                         artifactPrefix,
147                         occurrence)
148                 }
149             }
150             try {
151                 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
152             } catch (ex: DataIntegrityViolationException) {
153                 throw BluePrintException("Failed to store resource api result.", ex)
154             }
155         }
156 }