81239be303524f5f86c50984ddd7f450dcbb3fe7
[ccsdk/cds.git] /
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.DataIntegrityViolationException
28 import org.springframework.stereotype.Service
29 import java.util.*
30
31 @Service
32 class ResourceResolutionDBService(private val resourceResolutionRepository: ResourceResolutionRepository) {
33
34     private val log = LoggerFactory.getLogger(ResourceResolutionDBService::class.toString())
35
36     suspend fun readValue(blueprintName: String,
37                           blueprintVersion: String,
38                           artifactPrefix: String,
39                           resolutionKey: String,
40                           name: String): ResourceResolution = withContext(Dispatchers.IO) {
41
42         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
43             resolutionKey,
44             blueprintName,
45             blueprintVersion,
46             artifactPrefix,
47             name)
48     }
49
50     suspend fun readWithResolutionKey(blueprintName: String,
51                                       blueprintVersion: String,
52                                       artifactPrefix: String,
53                                       resolutionKey: String): List<ResourceResolution> = withContext(Dispatchers.IO) {
54
55         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
56             resolutionKey,
57             blueprintName,
58             blueprintVersion,
59             artifactPrefix)
60     }
61
62     suspend fun readWithResourceIdAndResourceType(blueprintName: String,
63                                                   blueprintVersion: String,
64                                                   resourceId: String,
65                                                   resourceType: String): List<ResourceResolution> = withContext(Dispatchers.IO) {
66
67         resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
68             blueprintName,
69             blueprintVersion,
70             resourceId,
71             resourceType)
72     }
73
74     suspend fun write(properties: Map<String, Any>,
75                       bluePrintRuntimeService: BluePrintRuntimeService<*>,
76                       artifactPrefix: String,
77                       resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
78
79         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
80
81         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
82         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
83         val resolutionKey =
84             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
85         val resourceType =
86             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE].toString()
87         val resourceId =
88             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID].toString()
89
90         write(blueprintName,
91             blueprintVersion,
92             resolutionKey,
93             resourceId,
94             resourceType,
95             artifactPrefix,
96             resourceAssignment)
97     }
98
99     suspend fun write(blueprintName: String,
100                       blueprintVersion: String,
101                       resolutionKey: String,
102                       resourceId: String,
103                       resourceType: String,
104                       artifactPrefix: String,
105                       resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
106
107         val resourceResolution = ResourceResolution()
108         resourceResolution.id = UUID.randomUUID().toString()
109         resourceResolution.artifactName = artifactPrefix
110         resourceResolution.blueprintVersion = blueprintVersion
111         resourceResolution.blueprintName = blueprintName
112         resourceResolution.resolutionKey = resolutionKey
113         resourceResolution.resourceType = resourceType
114         resourceResolution.resourceId = resourceId
115         resourceResolution.value = JacksonUtils.getValue(resourceAssignment.property?.value!!).toString()
116         resourceResolution.name = resourceAssignment.name
117         resourceResolution.dictionaryName = resourceAssignment.dictionaryName
118         resourceResolution.dictionaryVersion = resourceAssignment.version
119         resourceResolution.dictionarySource = resourceAssignment.dictionarySource
120         resourceResolution.status = resourceAssignment.status
121
122         try {
123             resourceResolutionRepository.saveAndFlush(resourceResolution)
124         } catch (ex: Exception) {
125             throw BluePrintException("Failed to store resource resolution result.", ex)
126         }
127     }
128 }