a85d71f31a7bc38250f6bb6acae160a429d37852
[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
44         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
45     }
46
47     private val log = logger(BlueprintArtifactDefinitionEnhancerImpl::class)
48
49     lateinit var bluePrintRuntimeService: BlueprintRuntimeService<*>
50     lateinit var bluePrintContext: BlueprintContext
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 }