Refactor Netconf script component parent.
[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, Bell Canada
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 com.fasterxml.jackson.databind.JsonNode
21 import kotlinx.coroutines.async
22 import kotlinx.coroutines.awaitAll
23 import kotlinx.coroutines.coroutineScope
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
29 import org.onap.ccsdk.cds.controllerblueprints.core.*
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
34 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
35 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
36 import org.slf4j.LoggerFactory
37 import org.springframework.context.ApplicationContext
38 import org.springframework.stereotype.Service
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, JsonNode>
49
50     suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
51                                  artifactPrefix: String, properties: Map<String, Any>): String
52
53     suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
54                                            resourceDefinitions: MutableMap<String, ResourceDefinition>,
55                                            resourceAssignments: MutableList<ResourceAssignment>,
56                                            artifactPrefix: String,
57                                            properties: Map<String, Any>)
58 }
59
60 @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
61 open class ResourceResolutionServiceImpl(private var applicationContext: ApplicationContext,
62                                          private var templateResolutionDBService: TemplateResolutionService,
63                                          private var blueprintTemplateService: BluePrintTemplateService,
64                                          private var resourceResolutionDBService: ResourceResolutionDBService) :
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 templateResolutionDBService.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
79                 bluePrintRuntimeService,
80                 artifactTemplate,
81                 resolutionKey)
82     }
83
84     override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
85                                           artifactNames: List<String>,
86                                           properties: Map<String, Any>): MutableMap<String, JsonNode> {
87
88         val resourceAssignmentRuntimeService =
89             ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString())
90
91         val resolvedParams: MutableMap<String, JsonNode> = hashMapOf()
92         artifactNames.forEach { artifactName ->
93             val resolvedContent = resolveResources(resourceAssignmentRuntimeService, nodeTemplateName,
94                 artifactName, properties)
95
96             resolvedParams[artifactName] = resolvedContent.asJsonType()
97         }
98         return resolvedParams
99     }
100
101
102     override suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String,
103                                           artifactPrefix: String, properties: Map<String, Any>): String {
104
105         // Velocity Artifact Definition Name
106         val artifactTemplate = "$artifactPrefix-template"
107         // Resource Assignment Artifact Definition Name
108         val artifactMapping = "$artifactPrefix-mapping"
109
110         val resolvedContent: String
111         log.info("Resolving resource for template artifact($artifactTemplate) with resource assignment artifact($artifactMapping)")
112
113         val resourceAssignmentContent =
114                 bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping)
115
116         val resourceAssignments: MutableList<ResourceAssignment> =
117                 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java)
118                         as? MutableList<ResourceAssignment>
119                         ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions")
120
121         if (isToStore(properties)) {
122             val existingResourceResolution = isNewResolution(bluePrintRuntimeService, properties, artifactPrefix)
123             if (existingResourceResolution.isNotEmpty()) {
124                 updateResourceAssignmentWithExisting(existingResourceResolution, resourceAssignments)
125             }
126         }
127
128         // Get the Resource Dictionary Name
129         val resourceDefinitions: MutableMap<String, ResourceDefinition> = ResourceAssignmentUtils
130                 .resourceDefinitions(bluePrintRuntimeService.bluePrintContext().rootPath)
131
132         // Resolve resources
133         resolveResourceAssignments(bluePrintRuntimeService,
134                 resourceDefinitions,
135                 resourceAssignments,
136                 artifactPrefix,
137                 properties)
138
139         val resolvedParamJsonContent =
140                 ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList())
141
142         resolvedContent = blueprintTemplateService.generateContent(bluePrintRuntimeService, nodeTemplateName,
143                 artifactTemplate, resolvedParamJsonContent)
144
145         if (isToStore(properties)) {
146             templateResolutionDBService.write(properties, resolvedContent, bluePrintRuntimeService, artifactPrefix)
147             log.info("Template resolution saved into database successfully : ($properties)")
148         }
149
150         return resolvedContent
151     }
152
153     /**
154      * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the
155      * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the
156      * request.
157      */
158     override suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>,
159                                                     resourceDefinitions: MutableMap<String, ResourceDefinition>,
160                                                     resourceAssignments: MutableList<ResourceAssignment>,
161                                                     artifactPrefix: String,
162                                                     properties: Map<String, Any>) {
163
164         val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments)
165
166         // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService
167         val resourceAssignmentRuntimeService = if (!(blueprintRuntimeService is ResourceAssignmentRuntimeService)) {
168             ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix)
169         } else {
170             blueprintRuntimeService
171         }
172
173
174         coroutineScope {
175             bulkSequenced.forEach { batchResourceAssignments ->
176                 // Execute Non Dependent Assignments in parallel ( ie asynchronously )
177                 val deferred = batchResourceAssignments
178                         .filter { it.name != "*" && it.name != "start" }
179                         .filter { it.status != BluePrintConstants.STATUS_SUCCESS }
180                         .map { resourceAssignment ->
181                             async {
182                                 val dictionaryName = resourceAssignment.dictionaryName
183                                 val dictionarySource = resourceAssignment.dictionarySource
184
185                                 val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDefinitions)
186
187                                 val resourceAssignmentProcessor =
188                                         applicationContext.getBean(processorName) as? ResourceAssignmentProcessor
189                                                 ?: throw BluePrintProcessorException("failed to get resource processor ($processorName) " +
190                                                         "for resource assignment(${resourceAssignment.name})")
191                                 try {
192                                     // Set BluePrint Runtime Service
193                                     resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService
194                                     // Set Resource Dictionaries
195                                     resourceAssignmentProcessor.resourceDictionaries = resourceDefinitions
196                                     // Invoke Apply Method
197                                     resourceAssignmentProcessor.applyNB(resourceAssignment)
198
199                                     if (isToStore(properties)) {
200                                         resourceResolutionDBService.write(properties,
201                                                 blueprintRuntimeService,
202                                                 artifactPrefix,
203                                                 resourceAssignment)
204                                         log.info("Resource resolution saved into database successfully : ($resourceAssignment)")
205                                     }
206
207                                     // Set errors from RA
208                                     blueprintRuntimeService.setBluePrintError(resourceAssignmentRuntimeService.getBluePrintError())
209                                 } catch (e: RuntimeException) {
210                                     log.error("Fail in processing ${resourceAssignment.name}", e)
211                                     throw BluePrintProcessorException(e)
212                                 }
213                             }
214                         }
215                 log.debug("Resolving (${deferred.size})resources parallel.")
216                 deferred.awaitAll()
217             }
218         }
219
220     }
221
222     /**
223      * If the Source instance is "input", then it is not mandatory to have source Resource Definition, So it can
224      *  derive the default input processor.
225      */
226     private fun processorName(dictionaryName: String, dictionarySource: String,
227                               resourceDefinitions: MutableMap<String, ResourceDefinition>): String {
228         val processorName: String = when (dictionarySource) {
229             "input" -> {
230                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-input"
231             }
232             "default" -> {
233                 "${ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-default"
234             }
235             else -> {
236                 val resourceDefinition = resourceDefinitions[dictionaryName]
237                         ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName")
238
239                 val resourceSource = resourceDefinition.sources[dictionarySource]
240                         ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)")
241
242                 ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR.plus(resourceSource.type)
243             }
244         }
245         checkNotEmpty(processorName) {
246             "couldn't get processor name for resource dictionary definition($dictionaryName) source($dictionarySource)"
247         }
248
249         return processorName
250
251     }
252
253     // Check whether to store or not the resolution of resource and template
254     private fun isToStore(properties: Map<String, Any>): Boolean {
255         return properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)
256                 && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean
257     }
258
259     // Check whether resolution already exist in the database for the specified resolution-key or resourceId/resourceType
260     private suspend fun isNewResolution(bluePrintRuntimeService: BluePrintRuntimeService<*>,
261                                         properties: Map<String, Any>,
262                                         artifactPrefix: String): List<ResourceResolution> {
263         val occurrence = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] as Int
264         val resolutionKey = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] as String
265         val resourceId = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] as String
266         val resourceType = properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] as String
267
268         if (resolutionKey.isNotEmpty()) {
269             val existingResourceAssignments =
270                     resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResolutionKeyAndOccurrence(
271                             bluePrintRuntimeService,
272                             resolutionKey,
273                             occurrence,
274                             artifactPrefix)
275             if (existingResourceAssignments.isNotEmpty()) {
276                 log.info("Resolution with resolutionKey=($resolutionKey) already exist - will resolve all resources not already resolved.",
277                         resolutionKey)
278             }
279             return existingResourceAssignments
280         } else if (resourceId.isNotEmpty() && resourceType.isNotEmpty()) {
281             val existingResourceAssignments =
282                     resourceResolutionDBService.findByBlueprintNameAndBlueprintVersionAndArtifactNameAndResourceIdAndResourceTypeAndOccurrence(
283                             bluePrintRuntimeService,
284                             resourceId,
285                             resourceType,
286
287                             occurrence,
288                             artifactPrefix)
289             if (existingResourceAssignments.isNotEmpty()) {
290                 log.info("Resolution with resourceId=($resourceId) and resourceType=($resourceType) already exist - will resolve " +
291                         "all resources not already resolved.")
292             }
293             return existingResourceAssignments
294         }
295         return emptyList()
296     }
297
298     // Update the resource assignment list with the status of the resource that have already been resolved
299     private fun updateResourceAssignmentWithExisting(resourceResolutionList: List<ResourceResolution>,
300                                                      resourceAssignmentList: MutableList<ResourceAssignment>) {
301         resourceResolutionList.forEach { resourceResolution ->
302             if (resourceResolution.status == BluePrintConstants.STATUS_SUCCESS) {
303                 resourceAssignmentList.forEach {
304                     if (compareOne(resourceResolution, it)) {
305                         log.info("Resource ({}) already resolve: value=({})", it.name, resourceResolution.value)
306                         it.property!!.value = resourceResolution.value!!.asJsonPrimitive()
307                         it.status = resourceResolution.status
308                     }
309                 }
310             }
311         }
312     }
313
314     // Comparision between what we have in the database vs what we have to assign.
315     private fun compareOne(resourceResolution: ResourceResolution, resourceAssignment: ResourceAssignment): Boolean {
316         return (resourceResolution.name == resourceAssignment.name
317                 && resourceResolution.dictionaryName == resourceAssignment.dictionaryName
318                 && resourceResolution.dictionarySource == resourceAssignment.dictionarySource
319                 && resourceResolution.dictionaryVersion == resourceAssignment.version)
320     }
321
322 }