Merge "Add missing implementation and Operation Type."
[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 = 1): 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 = 1): 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         write(blueprintName,
97             blueprintVersion,
98             artifactPrefix,
99             result,
100             occurrence,
101             resolutionKey,
102             resourceId,
103             resourceType)
104     }
105
106     suspend fun write(blueprintName: String, blueprintVersion: String, artifactPrefix: String,
107                       template: String, occurrence: Int = 1, resolutionKey: String = "", resourceId: String = "",
108                       resourceType: String = ""): TemplateResolution =
109         withContext(Dispatchers.IO) {
110
111             val resourceResolutionResult = TemplateResolution()
112             resourceResolutionResult.id = UUID.randomUUID().toString()
113             resourceResolutionResult.artifactName = artifactPrefix
114             resourceResolutionResult.blueprintVersion = blueprintVersion
115             resourceResolutionResult.blueprintName = blueprintName
116             resourceResolutionResult.resolutionKey = resolutionKey
117             resourceResolutionResult.resourceId = resourceId
118             resourceResolutionResult.resourceType = resourceType
119             resourceResolutionResult.result = template
120             resourceResolutionResult.occurrence = occurrence
121
122             // Overwrite template resolution-key of resourceId/resourceType already existant
123             if (resolutionKey.isNotEmpty()) {
124                 templateResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
125                     resolutionKey, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
126                     log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
127                             "artifactName=($artifactPrefix) and resolutionKey=($resolutionKey)")
128                     templateResolutionRepository.deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
129                         resolutionKey,
130                         blueprintName,
131                         blueprintVersion,
132                         artifactPrefix,
133                         occurrence)
134                 }
135             } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
136                 templateResolutionRepository.findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
137                     resourceId, resourceType, blueprintName, blueprintVersion, artifactPrefix, occurrence)?.let {
138                     log.info("Overwriting template resolution for blueprintName=($blueprintVersion), blueprintVersion=($blueprintName), " +
139                             "artifactName=($artifactPrefix), resourceId=($resourceId) and resourceType=($resourceType)")
140                     templateResolutionRepository.deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
141                         resourceId,
142                         resourceType,
143                         blueprintName,
144                         blueprintVersion,
145                         artifactPrefix,
146                         occurrence)
147                 }
148             }
149             try {
150                 log.info("Writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
151                     " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence")
152                 templateResolutionRepository.saveAndFlush(resourceResolutionResult)
153             } catch (ex: DataIntegrityViolationException) {
154                 log.error("Error writing out template_resolution result: bpName: $blueprintName bpVer $blueprintVersion resKey:$resolutionKey" +
155                     " (resourceId: $resourceId resourceType: $resourceType) occurrence:$occurrence error: {}", ex.message)
156                 throw BluePrintException("Failed to store resource api result.", ex)
157             }
158         }
159 }