aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/enhancer/ResourceAssignmentEnhancerService.kt
blob: 4d1995d53425f58c2ff986a88ccf23a25c08eb87 (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
/*
 *  Copyright © 2017-2018 AT&T Intellectual Property.
 *
 *  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 org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service.ResourceDefinitionRepoService
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes
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.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.ResourceDictionaryConstants
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
import org.springframework.beans.factory.config.ConfigurableBeanFactory
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Service

/**
 * ResourceAssignmentEnhancerService.
 *
 * @author Brinda Santh
 */
interface ResourceAssignmentEnhancerService {

    @Throws(BlueprintException::class)
    fun enhanceBlueprint(
        bluePrintTypeEnhancerService: BlueprintTypeEnhancerService,
        bluePrintRuntimeService: BlueprintRuntimeService<*>,
        resourceAssignments: List<ResourceAssignment>
    )
}

/**
 * ResourceAssignmentEnhancerDefaultService.
 *
 * @author Brinda Santh
 */
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
open class ResourceAssignmentEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
    ResourceAssignmentEnhancerService {

    private val log = logger(ResourceAssignmentEnhancerServiceImpl::class)

    /**
     * Get the defined source instance from the ResourceAssignment,
     * then get the NodeType of the Sources assigned
     */
    override fun enhanceBlueprint(
        bluePrintTypeEnhancerService: BlueprintTypeEnhancerService,
        bluePrintRuntimeService: BlueprintRuntimeService<*>,
        resourceAssignments: List<ResourceAssignment>
    ) {

        val uniqueSourceNodeTypeNames = hashSetOf<String>()

        // Iterate the Resource Assignment and
        resourceAssignments.map { resourceAssignment ->
            val dictionaryName = resourceAssignment.dictionaryName!!
            val dictionarySource = resourceAssignment.dictionarySource!!
            log.debug("Enriching assignment name(${resourceAssignment.name}), dictionary name($dictionaryName), source($dictionarySource)")
            val sourceNodeTypeName = ResourceSourceMappingFactory.getRegisterSourceMapping(dictionarySource)

            // Add Unique Node Types
            uniqueSourceNodeTypeNames.add(sourceNodeTypeName)

            // TODO("Candidate for Optimisation")
            if (checkResourceDefinitionNeeded(resourceAssignment)) {

                bluePrintTypeEnhancerService.enhancePropertyDefinition(
                    bluePrintRuntimeService, resourceAssignment.name,
                    resourceAssignment.property!!
                )

                // Get the Resource Definition from Repo
                val resourceDefinition: ResourceDefinition = getResourceDefinition(dictionaryName)

                val sourceNodeTemplate = resourceDefinition.sources[dictionarySource]
                    ?: throw BlueprintException("failed to get assigned dictionarySource($dictionarySource) from resourceDefinition($dictionaryName)")

                // Enrich as NodeTemplate
                bluePrintTypeEnhancerService.enhanceNodeTemplate(bluePrintRuntimeService, dictionarySource, sourceNodeTemplate)
            }
        }
        // Enrich the ResourceSource NodeTypes
        uniqueSourceNodeTypeNames.map { nodeTypeName ->
            val nodeType = resourceDefinitionRepoService.getNodeType(nodeTypeName)
            bluePrintTypeEnhancerService.enhanceNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)
        }
    }

    private fun checkResourceDefinitionNeeded(resourceAssignment: ResourceAssignment): Boolean {
        return !(
            (
                resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_INPUT) ||
                    resourceAssignment.dictionarySource.equals(ResourceDictionaryConstants.SOURCE_DEFAULT)
                ) &&
                BlueprintTypes.validPrimitiveOrCollectionPrimitive(resourceAssignment.property!!)
            )
    }

    private fun getResourceDefinition(name: String): ResourceDefinition {
        return resourceDefinitionRepoService.getResourceDefinition(name)
    }
}