f954df7ac7ae36520f3e90dd500923606ce3a4c9
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / enhancer / BluePrintArtifactDefinitionEnhancerImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.enhancer
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.BluePrintEnhancerUtils
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionEnhancer
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
32 import org.springframework.stereotype.Service
33
34 @Service
35 open class BluePrintArtifactDefinitionEnhancerImpl(
36     private val bluePrintRepoService: BluePrintRepoService,
37     private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
38     private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService
39 ) :
40     BluePrintArtifactDefinitionEnhancer {
41
42     companion object {
43         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
44     }
45
46     private val log = logger(BluePrintArtifactDefinitionEnhancerImpl::class)
47
48     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
49     lateinit var bluePrintContext: BluePrintContext
50
51     override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, artifactDefinition: ArtifactDefinition) {
52         log.info("enhancing ArtifactDefinition($name)")
53
54         this.bluePrintRuntimeService = bluePrintRuntimeService
55         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
56
57         val artifactTypeName = artifactDefinition.type
58             ?: throw BluePrintException("artifact type is missing for ArtifactDefinition($name)")
59
60         // Populate Artifact Type
61         BluePrintEnhancerUtils.populateArtifactType(bluePrintContext, bluePrintRepoService, artifactTypeName)
62
63         when (artifactTypeName) {
64             ARTIFACT_TYPE_MAPPING_SOURCE -> {
65                 enhanceMappingType(name, artifactDefinition)
66             }
67         }
68     }
69
70     // Enhance Resource Mapping
71     open fun enhanceMappingType(name: String, artifactDefinition: ArtifactDefinition) {
72
73         val artifactFilePath = "${bluePrintContext.rootPath}/${artifactDefinition.file}"
74
75         val alreadyEnhancedKey = "enhanced-${artifactDefinition.file}"
76         val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
77
78         log.info("enhancing resource mapping file(${artifactDefinition.file}) already enhanced($alreadyEnhanced)")
79
80         if (!alreadyEnhanced) {
81             val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(artifactFilePath, ResourceAssignment::class.java)
82                     as? MutableList<ResourceAssignment>
83                 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($artifactFilePath)")
84
85             // Call Resource Assignment Enhancer
86             resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
87
88             bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
89         }
90     }
91 }