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