Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / functions / resource / resolution / db / ResourceResolutionResultService.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.apps.blueprintsprocessor.functions.resource.resolution.db
17
18 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants
19 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
22 import org.springframework.dao.DataIntegrityViolationException
23 import org.springframework.stereotype.Service
24 import java.util.*
25
26 @Service
27 class ResourceResolutionResultService(private val resourceResolutionRepository: ResourceResolutionRepository) {
28
29     fun write(properties: Map<String, Any>, result: String, bluePrintRuntimeService: BluePrintRuntimeService<*>,
30               artifactPrefix: String) {
31
32         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
33
34         val resourceResolutionResult = ResourceResolutionResult()
35         resourceResolutionResult.id = UUID.randomUUID().toString()
36         resourceResolutionResult.artifactName = artifactPrefix
37         resourceResolutionResult.blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
38         resourceResolutionResult.blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
39         resourceResolutionResult.resolutionKey =
40             properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_KEY].toString()
41         resourceResolutionResult.result = result
42
43         try {
44             resourceResolutionRepository.saveAndFlush(resourceResolutionResult)
45         } catch (ex: DataIntegrityViolationException) {
46             throw BluePrintException("Failed to store resource resolution result.", ex)
47         }
48     }
49
50     fun read(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactPrefix: String,
51              resolutionKey: String): String {
52
53         val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
54
55         val blueprintVersion = metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION]
56         val blueprintName = metadata[BluePrintConstants.METADATA_TEMPLATE_NAME]
57
58         return resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
59             resolutionKey,
60             blueprintName,
61             blueprintVersion,
62             artifactPrefix).result!!
63     }
64 }