cfbfab71d33190a2eb01b3f34b582e52a4084ec5
[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.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.BluePrintError
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactType
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
26 import org.onap.ccsdk.apps.controllerblueprints.core.format
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintNodeTemplateEnhancer
28 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
29 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
31 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
32 import org.springframework.beans.factory.config.ConfigurableBeanFactory
33 import org.springframework.context.annotation.Scope
34 import org.springframework.stereotype.Service
35
36 @Service
37 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
38 open class BluePrintNodeTemplateEnhancerImpl(private val bluePrintRepoService: BluePrintRepoService,
39                                              private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService)
40     : BluePrintNodeTemplateEnhancer {
41
42     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintNodeTemplateEnhancerImpl::class.toString())
43
44     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
45     lateinit var bluePrintContext: BluePrintContext
46
47
48     override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, nodeTemplate: NodeTemplate) {
49         log.info("Enhancing NodeTemplate($name)")
50         this.bluePrintRuntimeService = bluePrintRuntimeService
51         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
52
53
54         val nodeTypeName = nodeTemplate.type
55         // Get NodeType from Repo and Update Service Template
56         val nodeType = populateNodeType(nodeTypeName)
57
58         // Enrich NodeType
59         bluePrintTypeEnhancerService.enhanceNodeType(bluePrintRuntimeService, nodeTypeName, nodeType)
60
61         //Enrich Node Template Artifacts
62         enhanceNodeTemplateArtifactDefinition(name, nodeTemplate)
63     }
64
65
66     open fun populateNodeType(nodeTypeName: String): NodeType {
67
68         val nodeType = bluePrintContext.serviceTemplate.nodeTypes?.get(nodeTypeName)
69                 ?: bluePrintRepoService.getNodeType(nodeTypeName)
70                 ?: throw BluePrintException(format("Couldn't get NodeType({}) from repo.", nodeTypeName))
71         bluePrintContext.serviceTemplate.nodeTypes?.put(nodeTypeName, nodeType)
72         return nodeType
73     }
74
75     open fun enhanceNodeTemplateArtifactDefinition(nodeTemplateName: String, nodeTemplate: NodeTemplate) {
76
77         nodeTemplate.artifacts?.forEach { artifactDefinitionName, artifactDefinition ->
78             val artifactTypeName = artifactDefinition.type
79                     ?: throw BluePrintException(format("Artifact type is missing for NodeTemplate({}) artifact({})", nodeTemplateName, artifactDefinitionName))
80
81             // Populate Artifact Type
82             populateArtifactType(artifactTypeName)
83         }
84     }
85
86     open fun populateArtifactType(artifactTypeName: String): ArtifactType {
87         val artifactType = bluePrintContext.serviceTemplate.artifactTypes?.get(artifactTypeName)
88                 ?: bluePrintRepoService.getArtifactType(artifactTypeName)
89                 ?: throw BluePrintException(format("Couldn't get ArtifactType({}) from repo.", artifactTypeName))
90         bluePrintContext.serviceTemplate.artifactTypes?.put(artifactTypeName, artifactType)
91         return artifactType
92     }
93
94 }