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