e9679eeba8023ba90b0114cf5dacb7c90479ce69
[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.dao.EmptyResultDataAccessException
29 import org.springframework.stereotype.Service
30 import java.util.*
31
32 @Service
33 class ResourceResolutionDBService(private val resourceResolutionRepository: ResourceResolutionRepository) {
34
35     private val log = LoggerFactory.getLogger(ResourceResolutionDBService::class.toString())
36
37     suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
38         bluePrintRuntimeService: BluePrintRuntimeService<*>, key: String,
39         occurrence: Int, artifactPrefix: String): List<ResourceResolution> {
40         return try {
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             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
47                 blueprintName,
48                 blueprintVersion,
49                 artifactPrefix,
50                 key,
51                 occurrence)
52         } catch (e: EmptyResultDataAccessException) {
53             emptyList()
54         }
55     }
56
57     suspend fun findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
58         bluePrintRuntimeService: BluePrintRuntimeService<*>, resourceId: String,
59         resourceType: String, occurrence: Int,
60         artifactPrefix: String): List<ResourceResolution> {
61         return try {
62
63             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
64
65             val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
66             val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
67
68             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
69                 blueprintName,
70                 blueprintVersion,
71                 artifactPrefix,
72                 resourceId,
73                 resourceType,
74                 occurrence)
75         } catch (e: EmptyResultDataAccessException) {
76             emptyList()
77         }
78     }
79
80     suspend fun readValue(blueprintName: String,
81                           blueprintVersion: String,
82                           artifactPrefix: String,
83                           resolutionKey: String,
84                           name: String): ResourceResolution = withContext(Dispatchers.IO) {
85
86         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
87             resolutionKey,
88             blueprintName,
89             blueprintVersion,
90             artifactPrefix,
91             name)
92     }
93
94     suspend fun readWithResolutionKey(blueprintName: String,
95                                       blueprintVersion: String,
96                                       artifactPrefix: String,
97                                       resolutionKey: String): List<ResourceResolution> = withContext(Dispatchers.IO) {
98
99         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
100             resolutionKey,
101             blueprintName,
102             blueprintVersion,
103             artifactPrefix)
104     }
105
106     suspend fun readWithResourceIdAndResourceType(blueprintName: String,
107                                                   blueprintVersion: String,
108                                                   resourceId: String,
109                                                   resourceType: String): List<ResourceResolution> =
110         withContext(Dispatchers.IO) {
111
112             resourceResolutionRepository.findByBlueprintNameAndBlueprintVersionAndResourceIdAndResourceType(
113                 blueprintName,
114                 blueprintVersion,
115                 resourceId,
116                 resourceType)
117         }
118
119     suspend fun write(properties: Map<String, Any>,
120                       bluePrintRuntimeService: BluePrintRuntimeService<*>,
121                       artifactPrefix: String,
122                       resourceAssignment: ResourceAssignment): ResourceResolution = withContext(Dispatchers.IO) {
123
124         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
125
126         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]!!
127         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]!!
128
129         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY] as String
130         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
131         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
132         val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
133
134         write(blueprintName,
135             blueprintVersion,
136             resolutionKey,
137             resourceId,
138             resourceType,
139             artifactPrefix,
140             resourceAssignment,
141             occurrence)
142     }
143
144     suspend fun write(blueprintName: String,
145                       blueprintVersion: String,
146                       resolutionKey: String,
147                       resourceId: String,
148                       resourceType: String,
149                       artifactPrefix: String,
150                       resourceAssignment: ResourceAssignment,
151                       occurrence: Int = 0): ResourceResolution = withContext(Dispatchers.IO) {
152
153         val resourceResolution = ResourceResolution()
154         resourceResolution.id = UUID.randomUUID().toString()
155         resourceResolution.artifactName = artifactPrefix
156         resourceResolution.occurrence = occurrence
157         resourceResolution.blueprintVersion = blueprintVersion
158         resourceResolution.blueprintName = blueprintName
159         resourceResolution.resolutionKey = resolutionKey
160         resourceResolution.resourceType = resourceType
161         resourceResolution.resourceId = resourceId
162         if (BluePrintConstants.STATUS_SUCCESS == resourceAssignment.status) {
163             resourceResolution.value = JacksonUtils.getValue(resourceAssignment.property?.value!!).toString()
164         } else {
165             resourceResolution.value = ""
166         }
167         resourceResolution.name = resourceAssignment.name
168         resourceResolution.dictionaryName = resourceAssignment.dictionaryName
169         resourceResolution.dictionaryVersion = resourceAssignment.version
170         resourceResolution.dictionarySource = resourceAssignment.dictionarySource
171         resourceResolution.status = resourceAssignment.status
172
173         try {
174             resourceResolutionRepository.saveAndFlush(resourceResolution)
175         } catch (ex: Exception) {
176             throw BluePrintException("Failed to store resource resolution result.", ex)
177         }
178     }
179 }