aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/enhancer/ResourceDefinitionEnhancerService.kt
blob: 08a41f7bed737426512f779545436a3401144177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.enhancer

import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service.ResourceDefinitionRepoService
import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.BlueprintEnhancerUtils
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTypeEnhancerService
import org.onap.ccsdk.cds.controllerblueprints.core.logger
import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintRuntimeService
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils
import org.springframework.stereotype.Service

interface ResourceDefinitionEnhancerService {

    @Throws(BlueprintException::class)
    fun enhance(
        bluePrintTypeEnhancerService: BlueprintTypeEnhancerService,
        bluePrintRuntimeService: BlueprintRuntimeService<*>
    ): List<ResourceDefinition>
}

@Service
class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
    ResourceDefinitionEnhancerService {

    private val log = logger(ResourceDefinitionEnhancerService::class)

    companion object {

        const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
    }

    // Enhance the Resource Definition
    // 1. Get the Resource Mapping files from all NodeTemplates.
    // 2. Get all the Unique Resource assignments from all mapping files
    // 3. Collect the Resource Definition for Resource Assignment names from database.
    // 4. Create the Resource Definition under blueprint base path.
    override fun enhance(
        bluePrintTypeEnhancerService: BlueprintTypeEnhancerService,
        bluePrintRuntimeService: BlueprintRuntimeService<*>
    ): List<ResourceDefinition> {

        var resourceDefinitions: List<ResourceDefinition> = mutableListOf()

        val blueprintContext = bluePrintRuntimeService.bluePrintContext()

        val mappingFiles = getAllResourceMappingFiles(blueprintContext)
        log.info("resources assignment files ($mappingFiles)")
        if (mappingFiles != null) {
            resourceDefinitions = getResourceDefinition(blueprintContext, mappingFiles)
            // Enriching Resource Definition Sources
            enrichResourceDefinitionSources(bluePrintRuntimeService.bluePrintContext(), resourceDefinitions)
        }
        return resourceDefinitions
    }

    // Get all the Mapping files from all node templates.
    private fun getAllResourceMappingFiles(blueprintContext: BlueprintContext): List<String>? {

        return blueprintContext.nodeTemplates()?.mapNotNull { nodeTemplateMap ->

            // Return only Mapping Artifact File Names
            nodeTemplateMap.value.artifacts?.filter { artifactDefinitionMap ->
                artifactDefinitionMap.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
            }?.mapNotNull { artifactDefinitionMap ->
                artifactDefinitionMap.value.file
            }
        }?.flatten()?.distinct()
    }

    // Convert file content to ResourceAssignments asynchronously
    private fun getResourceDefinition(blueprintContext: BlueprintContext, files: List<String>) = runBlocking {
        val blueprintBasePath = blueprintContext.rootPath
        val deferredResourceAssignments = mutableListOf<Deferred<List<ResourceAssignment>>>()
        for (file in files) {
            log.info("processing file ($file)")
            deferredResourceAssignments += async {
                ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file")
            }
        }

        val resourceAssignments = mutableListOf<ResourceAssignment>()
        for (deferredResourceAssignment in deferredResourceAssignments) {
            resourceAssignments.addAll(deferredResourceAssignment.await())
        }

        val distinctResourceAssignments = resourceAssignments.distinctBy { it.name }
        generateResourceDictionary(blueprintBasePath, distinctResourceAssignments)
        // log.info("distinct Resource assignment ($distinctResourceAssignments)")
    }

    // Read the Resource Definitions from the Database and write to type file.
    private fun generateResourceDictionary(blueprintBasePath: String, resourceAssignments: List<ResourceAssignment>):
        List<ResourceDefinition> {
            val resourceKeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct().sorted()
            log.info("distinct resource keys ($resourceKeys)")

            // TODO("Optimise DB single Query to multiple Query")
            return resourceKeys.map { resourceKey ->
                getResourceDefinition(resourceKey)
            }
        }

    private fun enrichResourceDefinitionSources(
        bluePrintContext: BlueprintContext,
        resourceDefinitions: List<ResourceDefinition>
    ) {
        val sources = resourceDefinitions
            .map { it.sources }
            .map {
                it.values
                    .map { nodeTemplate ->
                        nodeTemplate.type
                    }
            }
            .flatten().distinct()
        log.info("Enriching Resource Definition sources Node Template: $sources")
        sources.forEach {
            BlueprintEnhancerUtils.populateNodeType(bluePrintContext, resourceDefinitionRepoService, it)
        }
    }

    // Get the Resource Definition from Database
    private fun getResourceDefinition(name: String): ResourceDefinition {
        return resourceDefinitionRepoService.getResourceDefinition(name)
    }
}