Merge "Implement Resource Resolution Services"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / functions / resource / resolution / ResourceResolutionService.kt
1 /*\r
2  *  Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *  Modifications Copyright © 2018 IBM.\r
4  *\r
5  *  Licensed under the Apache License, Version 2.0 (the "License");\r
6  *  you may not use this file except in compliance with the License.\r
7  *  You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  *  Unless required by applicable law or agreed to in writing, software\r
12  *  distributed under the License is distributed on an "AS IS" BASIS,\r
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  *  See the License for the specific language governing permissions and\r
15  *  limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution\r
19 \r
20 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolutionprocessor.ResourceAssignmentProcessor\r
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils\r
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment\r
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition\r
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils\r
28 import org.slf4j.LoggerFactory\r
29 import org.springframework.beans.factory.annotation.Autowired\r
30 import org.springframework.context.ApplicationContext\r
31 import org.springframework.stereotype.Service\r
32 import java.io.File\r
33 \r
34 /**\r
35  * ResourceResolutionService\r
36  * @author Brinda Santh\r
37  * 8/14/2018\r
38  */\r
39 \r
40 @Service\r
41 class ResourceResolutionService {\r
42 \r
43     private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)\r
44 \r
45     @Autowired\r
46     private lateinit var applicationContext: ApplicationContext\r
47 \r
48     fun registeredResourceSources(): List<String> {\r
49         return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)\r
50                 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }\r
51                 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR) }\r
52     }\r
53 \r
54 \r
55     fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,\r
56                          artifactNames: List<String>): MutableMap<String, String> {\r
57 \r
58         val resolvedParams: MutableMap<String, String> = hashMapOf()\r
59         artifactNames.forEach { artifactName ->\r
60             val resolvedContent = resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactName)\r
61             resolvedParams[artifactName] = resolvedContent\r
62         }\r
63         return resolvedParams\r
64     }\r
65 \r
66 \r
67     fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String, artifactName: String): String {\r
68 \r
69         var resolvedContent = ""\r
70         // Velocity Artifact Definition Name\r
71         val templateArtifactName = "$artifactName-template"\r
72         // Resource Assignment Artifact Definition Name\r
73         val mappingArtifactName = "$artifactName-mapping"\r
74 \r
75         log.info("Resolving resource for template artifact($templateArtifactName) with resource assignment artifact($mappingArtifactName)")\r
76 \r
77         val resourceAssignmentContent = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, mappingArtifactName)\r
78 \r
79         val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)\r
80                 as? MutableList<ResourceAssignment>\r
81                 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")\r
82 \r
83         // Get the Resource Dictionary Name\r
84         val dictionaryFile = bluePrintRuntimeService.bluePrintContext().rootPath.plus(File.separator)\r
85                 .plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR).plus(File.separator)\r
86                 .plus(ResourceResolutionConstants.FILE_NAME_RESOURCE_DICTIONARY_TYPES)\r
87 \r
88         val resourceDictionaries: MutableMap<String, ResourceDefinition> = JacksonUtils.getMapFromFile(dictionaryFile, ResourceDefinition::class.java)\r
89                 ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")\r
90 \r
91         executeProcessors(bluePrintRuntimeService, resourceDictionaries, resourceAssignments)\r
92 \r
93         // Check Template is there\r
94         val templateContent = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, mappingArtifactName)\r
95 \r
96         // TODO ("Generate Param JSON from Resource Assignment")\r
97         val resolvedParamJsonContent = "{}"\r
98 \r
99         if (templateContent.isNotEmpty()) {\r
100             // TODO ( "Mash Data and Content")\r
101             resolvedContent = "Mashed Content"\r
102 \r
103         } else {\r
104             resolvedContent = resolvedParamJsonContent\r
105         }\r
106         return resolvedContent\r
107     }\r
108 \r
109 \r
110     fun executeProcessors(bluePrintRuntimeService: BluePrintRuntimeService<*>,\r
111                           resourceDictionaries: MutableMap<String, ResourceDefinition>,\r
112                           resourceAssignments: MutableList<ResourceAssignment>) {\r
113 \r
114         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)\r
115 \r
116         bulkSequenced.map { batchResourceAssignments ->\r
117             batchResourceAssignments.filter { it.name != "*" && it.name != "start" }\r
118                     .map { resourceAssignment ->\r
119                         val dictionarySource = resourceAssignment.dictionarySource\r
120                         val processorInstanceName = ResourceResolutionConstants.PREFIX_RESOURCE_ASSIGNMENT_PROCESSOR.plus(dictionarySource)\r
121 \r
122                         val resourceAssignmentProcessor = applicationContext.getBean(processorInstanceName) as? ResourceAssignmentProcessor\r
123                                 ?: throw BluePrintProcessorException("failed to get resource processor for instance name($processorInstanceName) " +\r
124                                         "for resource assignment(${resourceAssignment.name})")\r
125                         try {\r
126                             // Set BluePrint Runtime Service\r
127                             resourceAssignmentProcessor.bluePrintRuntimeService = bluePrintRuntimeService\r
128                             // Set Resource Dictionaries\r
129                             resourceAssignmentProcessor.resourceDictionaries = resourceDictionaries\r
130                             // Invoke Apply Method\r
131                             resourceAssignmentProcessor.apply(resourceAssignment)\r
132                         } catch (e: RuntimeException) {\r
133                             resourceAssignmentProcessor.recover(e, resourceAssignment)\r
134                             throw BluePrintProcessorException(e)\r
135                         }\r
136                     }\r
137         }\r
138     }\r
139 \r
140 }\r