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.blueprintsprocessor.designer.api.enhancer
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
35 open class BlueprintArtifactDefinitionEnhancerImpl(
36 private val bluePrintRepoService: BlueprintRepoService,
37 private val bluePrintTypeEnhancerService: BlueprintTypeEnhancerService,
38 private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService
40 BlueprintArtifactDefinitionEnhancer {
44 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
47 private val log = logger(BlueprintArtifactDefinitionEnhancerImpl::class)
49 lateinit var bluePrintRuntimeService: BlueprintRuntimeService<*>
50 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())