b4c658f92ba40c60da44d99db2fe7008d31ef012
[ccsdk/cds.git] /
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.controllerblueprints.core.BluePrintException
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
21 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionEnhancer
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
26 import org.onap.ccsdk.cds.controllerblueprints.core.logger
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
31 import org.onap.ccsdk.cds.controllerblueprints.service.utils.BluePrintEnhancerUtils
32 import org.springframework.stereotype.Service
33
34 @Service
35 open class BluePrintArtifactDefinitionEnhancerImpl(private val bluePrintRepoService: BluePrintRepoService,
36                                                    private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
37                                                    private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService)
38     : BluePrintArtifactDefinitionEnhancer {
39
40     companion object {
41         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
42     }
43
44
45     private val log = logger(BluePrintArtifactDefinitionEnhancerImpl::class)
46
47     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
48     lateinit var bluePrintContext: BluePrintContext
49
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
92 }