Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / service / 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.apps.controllerblueprints.service.enhancer
18
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactDefinition
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionEnhancer
26 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
32 import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils
33 import org.springframework.stereotype.Service
34
35 @Service
36 open class BluePrintArtifactDefinitionEnhancerImpl(private val bluePrintRepoService: BluePrintRepoService,
37                                                    private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
38                                                    private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService)
39     : BluePrintArtifactDefinitionEnhancer {
40
41     companion object {
42         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
43     }
44
45
46     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintArtifactDefinitionEnhancerImpl::class.toString())
47
48     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
49     lateinit var bluePrintContext: BluePrintContext
50
51
52     override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, artifactDefinition: ArtifactDefinition) {
53         log.info("enhancing ArtifactDefinition($name)")
54
55         this.bluePrintRuntimeService = bluePrintRuntimeService
56         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
57
58         val artifactTypeName = artifactDefinition.type
59                 ?: throw BluePrintException("artifact type is missing for ArtifactDefinition($name)")
60
61         // Populate Artifact Type
62         BluePrintEnhancerUtils.populateArtifactType(bluePrintContext, bluePrintRepoService, artifactTypeName)
63
64         when (artifactTypeName) {
65             ARTIFACT_TYPE_MAPPING_SOURCE -> {
66                 enhanceMappingType(name, artifactDefinition)
67             }
68         }
69     }
70
71     // Enhance Resource Mapping
72     open fun enhanceMappingType(name: String, artifactDefinition: ArtifactDefinition) {
73
74         val artifactFilePath = "${bluePrintContext.rootPath}/${artifactDefinition.file}"
75
76         val alreadyEnhancedKey = "enhanced-${artifactDefinition.file}"
77         val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
78
79         log.info("enhancing resource mapping file(${artifactDefinition.file}) already enhanced($alreadyEnhanced)")
80
81         if (!alreadyEnhanced) {
82             val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(artifactFilePath, ResourceAssignment::class.java)
83                     as? MutableList<ResourceAssignment>
84                     ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($artifactFilePath)")
85
86             // Call Resource Assignment Enhancer
87             resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
88
89             bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
90         }
91     }
92
93 }