ebb34ba4aa958c71afff307db9e7bff0763f20ba
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
18
19 import kotlinx.coroutines.Dispatchers
20 import kotlinx.coroutines.withContext
21 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
25 import org.springframework.dao.DataIntegrityViolationException
26 import org.springframework.stereotype.Service
27 import java.util.*
28
29 @Service
30 class ResourceResolutionResultService(private val resourceResolutionRepository: ResourceResolutionResultRepository) {
31
32     suspend fun read(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactPrefix: String,
33                      resolutionKey: String): String = withContext(Dispatchers.IO) {
34
35         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
36
37         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
38         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
39
40         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
41                 resolutionKey,
42                 blueprintName,
43                 blueprintVersion,
44                 artifactPrefix).result!!
45     }
46
47     suspend fun write(properties: Map<String, Any>, result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
48                       artifactPrefix: String) = withContext(Dispatchers.IO) {
49
50         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
51
52         val resourceResolutionResult = ResourceResolutionResult()
53         resourceResolutionResult.id = UUID.randomUUID().toString()
54         resourceResolutionResult.artifactName = artifactPrefix
55         resourceResolutionResult.blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
56         resourceResolutionResult.blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
57         resourceResolutionResult.resolutionKey =
58                 properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
59         resourceResolutionResult.result = result
60
61         try {
62             resourceResolutionRepository.saveAndFlush(resourceResolutionResult)
63         } catch (ex: DataIntegrityViolationException) {
64             throw BluePrintException("Failed to store resource resolution result.", ex)
65         }
66     }
67
68     suspend fun readByKey(resolutionResultId: String): String = withContext(Dispatchers.IO) {
69
70         resourceResolutionRepository.getOne(resolutionResultId).result!!
71     }
72
73     suspend fun deleteByKey(resolutionResultId: String): Unit = withContext(Dispatchers.IO) {
74
75         val row = resourceResolutionRepository.getOne(resolutionResultId)
76         resourceResolutionRepository.delete(row)
77         resourceResolutionRepository.flush()
78     }
79 }