Improve bulk processing
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceResolutionService.kt
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *  Modifications Copyright © 2018-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
18 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
19
20 import kotlinx.coroutines.async
21 import kotlinx.coroutines.awaitAll
22 import kotlinx.coroutines.coroutineScope
23 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionResultService
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmptyOrThrow
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
34 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
35 import org.slf4j.LoggerFactory
36 import org.springframework.context.ApplicationContext
37 import org.springframework.stereotype.Service
38 import java.io.File
39
40 interface ResourceResolutionService {
41
42     fun registeredResourceSources(): List<String>
43
44     suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>, artifactTemplate: String,
45                                     resolutionKey: String): String
46
47     suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
48                                  artifactNames: List<String>, properties: Map<String, Any>): MutableMap<String, String>
49
50     suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
51                                  artifactPrefix: String, properties: Map<String, Any>): String
52
53     suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
54                                  artifactMapping: String, artifactTemplate: String?): String
55
56     suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
57                                            resourceDictionaries: MutableMap<String, ResourceDefinition>,
58                                            resourceAssignments: MutableList<ResourceAssignment>,
59                                            identifierName: String)
60 }
61
62 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
63 open class ResourceResolutionServiceImpl(private var applicationContext: ApplicationContext,
64                                          private var resolutionResultService: ResourceResolutionResultService) :
65         ResourceResolutionService {
66
67     private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java)
68
69     override fun registeredResourceSources(): List<String> {
70         return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java)
71                 .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
72                 .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) }
73     }
74
75     override suspend fun resolveFromDatabase(bluePrintRuntimeService: BluePrintRuntimeService<*>,
76                                              artifactTemplate: String,
77                                              resolutionKey: String): String {
78         return resolutionResultService.read(bluePrintRuntimeService, artifactTemplate, resolutionKey)
79     }
80
81     override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
82                                           artifactNames: List<String>,
83                                           properties: Map<String, Any>): MutableMap<String, String> {
84
85         val resolvedParams: MutableMap<String, String> = hashMapOf()
86         artifactNames.forEach { artifactName ->
87             val resolvedContent = resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactName, properties)
88             resolvedParams[artifactName] = resolvedContent
89         }
90         return resolvedParams
91     }
92
93     override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
94                                           artifactPrefix: String, properties: Map<String, Any>): String {
95
96         // Velocity Artifact Definition Name
97         val artifactTemplate = "$artifactPrefix-template"
98         // Resource Assignment Artifact Definition Name
99         val artifactMapping = "$artifactPrefix-mapping"
100
101         val result = resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactMapping, artifactTemplate)
102
103         if (properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)
104                 && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean) {
105             resolutionResultService.write(properties, result, bluePrintRuntimeService, artifactPrefix)
106             log.info("resolution saved into database successfully : ($properties)")
107         }
108
109         return result
110     }
111
112
113     override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
114                                           artifactMapping: String, artifactTemplate: String?): String {
115
116         val resolvedContent: String
117         log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
118
119         val identifierName = artifactTemplate ?: "no-template"
120
121         val resourceAssignmentContent =
122                 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
123
124         val resourceAssignments: MutableList<ResourceAssignment> =
125                 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
126                         as? MutableList<ResourceAssignment>
127                         ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
128
129         // Get the Resource Dictionary Name
130         val dictionaryFile = bluePrintRuntimeService.bluePrintContext().rootPath.plus(File.separator)
131                 .plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR).plus(File.separator)
132                 .plus(ResourceResolutionConstants.FILE_NAME_RESOURCE_DEFINITION_TYPES)
133
134         val resourceDictionaries: MutableMap<String, ResourceDefinition> =
135                 JacksonUtils.getMapFromFile(dictionaryFile, ResourceDefinition::class.java)
136
137         // Resolve resources
138         resolveResourceAssignments(bluePrintRuntimeService, resourceDictionaries, resourceAssignments, identifierName)
139
140         val resolvedParamJsonContent =
141                 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
142
143         // Check Template is there
144         if (artifactTemplate != null) {
145             val templateContent =
146                     bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactTemplate)
147             resolvedContent = BluePrintTemplateService.generateContent(templateContent, resolvedParamJsonContent)
148         } else {
149             resolvedContent = resolvedParamJsonContent
150         }
151
152         return resolvedContent
153     }
154
155     /**
156      * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
157      * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
158      * request.
159      */
160     override suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
161                                                     resourceDictionaries: MutableMap<String, ResourceDefinition>,
162                                                     resourceAssignments: MutableList<ResourceAssignment>,
163                                                     identifierName: String) {
164
165         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
166         val resourceAssignmentRuntimeService =
167                 ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, identifierName)
168
169         coroutineScope {
170             bulkSequenced.forEach { batchResourceAssignments ->
171                 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
172                 val deferred = batchResourceAssignments.filter { it.name != "*" && it.name != "start" }
173                         .map { resourceAssignment ->
174                             async {
175                                 val dictionaryName = resourceAssignment.dictionaryName
176                                 val dictionarySource = resourceAssignment.dictionarySource
177                                 /**
178                                  * Get the Processor name
179                                  */
180                                 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDictionaries)
181
182                                 val resourceAssignmentProcessor =
183                                         applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
184                                                 ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " +
185                                                         "for resource assignment(${resourceAssignment.name})")
186                                 try {
187                                     // Set BluePrint Runtime Service
188                                     resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
189                                     // Set Resource Dictionaries
190                                     resourceAssignmentProcessor.resourceDictionaries = resourceDictionaries
191                                     // Invoke Apply Method
192                                     resourceAssignmentProcessor.applyNB(resourceAssignment)
193                                     // Set errors from RA
194                                     blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
195                                 } catch (e: RuntimeException) {
196                                     throw BluePrintProcessorException(e)
197                                 }
198                             }
199                         }
200                 log.debug("Resolving (${deferred.size})resources parallel.")
201                 deferred.awaitAll()
202             }
203         }
204
205     }
206
207
208     /**
209      * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
210      *  derive the default input processor.
211      */
212     private fun processorName(dictionaryName: String, dictionarySource: String,
213                               resourceDictionaries: MutableMap<String, ResourceDefinition>): String {
214         val processorName: String = when (dictionarySource) {
215             "input" -> {
216                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
217             }
218             "default" -> {
219                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
220             }
221             else -> {
222                 val resourceDefinition = resourceDictionaries[dictionaryName]
223                         ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
224
225                 val resourceSource = resourceDefinition.sources[dictionarySource]
226                         ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
227
228                 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
229             }
230         }
231         checkNotEmptyOrThrow(processorName,
232                 "couldn't get processor name for resource dictionary definition($dictionaryName) source" +
233                         "($dictionarySource)")
234
235         return processorName
236
237     }
238 }