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