2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.service.enhancer
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionEnhancer
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
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.onap.ccsdk.cds.controllerblueprints.service.utils.BluePrintEnhancerUtils
33 import org.springframework.stereotype.Service
36 open class BluePrintArtifactDefinitionEnhancerImpl(private val bluePrintRepoService: BluePrintRepoService,
37 private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
38 private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService)
39 : BluePrintArtifactDefinitionEnhancer {
42 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
46 private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintArtifactDefinitionEnhancerImpl::class.toString())
48 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
49 lateinit var bluePrintContext: BluePrintContext
52 override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, artifactDefinition: ArtifactDefinition) {
53 log.info("enhancing ArtifactDefinition($name)")
55 this.bluePrintRuntimeService = bluePrintRuntimeService
56 this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
58 val artifactTypeName = artifactDefinition.type
59 ?: throw BluePrintException("artifact type is missing for ArtifactDefinition($name)")
61 // Populate Artifact Type
62 BluePrintEnhancerUtils.populateArtifactType(bluePrintContext, bluePrintRepoService, artifactTypeName)
64 when (artifactTypeName) {
65 ARTIFACT_TYPE_MAPPING_SOURCE -> {
66 enhanceMappingType(name, artifactDefinition)
71 // Enhance Resource Mapping
72 open fun enhanceMappingType(name: String, artifactDefinition: ArtifactDefinition) {
74 val artifactFilePath = "${bluePrintContext.rootPath}/${artifactDefinition.file}"
76 val alreadyEnhancedKey = "enhanced-${artifactDefinition.file}"
77 val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
79 log.info("enhancing resource mapping file(${artifactDefinition.file}) already enhanced($alreadyEnhanced)")
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)")
86 // Call Resource Assignment Enhancer
87 resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
89 bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())