c6ffde74247255c0efad965a4207906be458faeb
[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> =
66         withContext(Dispatchers.IO) {
67
68             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
69                 blueprintName,
70                 blueprintVersion,
71                 resourceId,
72                 resourceType)
73         }
74
75     suspend fun write(properties: Map<String, Any>,
76                       bluePrintRuntimeService: BluePrintRuntimeService<*>,
77                       artifactPrefix: String,
78                       resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
79
80         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
81
82         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
83         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
84         val resolutionKey =
85             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
86         val resourceType =
87             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE].toString()
88         val resourceId =
89             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID].toString()
90
91         write(blueprintName,
92             blueprintVersion,
93             resolutionKey,
94             resourceId,
95             resourceType,
96             artifactPrefix,
97             resourceAssignment)
98     }
99
100     suspend fun write(blueprintName: String,
101                       blueprintVersion: String,
102                       resolutionKey: String,
103                       resourceId: String,
104                       resourceType: String,
105                       artifactPrefix: String,
106                       resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
107
108         val resourceResolution = ResourceResolution()
109         resourceResolution.id = UUID.randomUUID().toString()
110         resourceResolution.artifactName = artifactPrefix
111         resourceResolution.blueprintVersion = blueprintVersion
112         resourceResolution.blueprintName = blueprintName
113         resourceResolution.resolutionKey = resolutionKey
114         resourceResolution.resourceType = resourceType
115         resourceResolution.resourceId = resourceId
116         if (BluePrintConstants.STATUS_FAILURE == resourceAssignment.status) {
117             resourceResolution.value = ""
118         } else {
119             resourceResolution.value = JacksonUtils.getValue(resourceAssignment.property?.value!!).toString()
120         }
121         resourceResolution.name = resourceAssignment.name
122         resourceResolution.dictionaryName = resourceAssignment.dictionaryName
123         resourceResolution.dictionaryVersion = resourceAssignment.version
124         resourceResolution.dictionarySource = resourceAssignment.dictionarySource
125         resourceResolution.status = resourceAssignment.status
126
127         try {
128             resourceResolutionRepository.saveAndFlush(resourceResolution)
129         } catch (ex: Exception) {
130             throw BluePrintException("Failed to store resource resolution result.", ex)
131         }
132     }
133 }