Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintContext.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 @file:Suppress("unused")
18
19 package org.onap.ccsdk.cds.controllerblueprints.core.service
20
21 import com.att.eelf.configuration.EELFLogger
22 import com.att.eelf.configuration.EELFManager
23 import com.fasterxml.jackson.databind.JsonNode
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28
29 /**
30  *
31  *
32  * @author Brinda Santh
33  */
34 class BluePrintContext(val serviceTemplate: ServiceTemplate) {
35
36     private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
37
38     /**
39      * Blueprint CBA extracted file location
40      */
41     var rootPath = "."
42     /**
43      * Root Definition file path
44      */
45     var entryDefinition = ""
46
47     fun imports(): List<ImportDefinition>? = serviceTemplate.imports
48
49     fun dslDefinitions() = serviceTemplate.dslDefinitions
50
51     val metadata: MutableMap<String, String>? = serviceTemplate.metadata
52
53     fun dataTypes(): MutableMap<String, DataType>? = serviceTemplate.dataTypes
54
55     fun inputs(): MutableMap<String, PropertyDefinition>? = serviceTemplate.topologyTemplate?.inputs
56
57     fun blueprintJson(pretty: Boolean = false): String = print("json", pretty)
58
59     private fun print(type: String? = "json", pretty: Boolean = false): String {
60         return JacksonUtils.getJson(serviceTemplate, pretty)
61     }
62
63     fun name(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_NAME)
64             ?: throw BluePrintException("could't get template name from meta data")
65
66     fun version(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_VERSION)
67             ?: throw BluePrintException("could't get template version from meta data")
68
69     fun author(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_AUTHOR)
70             ?: throw BluePrintException("could't get template author from meta data")
71
72     // Workflow
73     fun workflows(): MutableMap<String, Workflow>? = serviceTemplate.topologyTemplate?.workflows
74
75     fun workflowByName(workFlowName: String): Workflow = workflows()?.get(workFlowName)
76             ?: throw BluePrintException("could't get workflow($workFlowName)")
77
78     fun workflowInputs(workFlowName: String) = workflowByName(workFlowName).inputs
79
80     fun workflowStepByName(workFlowName: String, stepName: String): Step {
81         return workflowByName(workFlowName).steps?.get(stepName)
82                 ?: throw BluePrintException("could't get step($stepName) for workflow($workFlowName)")
83     }
84
85     fun workflowStepNodeTemplate(workFlowName: String, stepName: String): String {
86         return workflowStepByName(workFlowName, stepName).target
87                 ?: throw BluePrintException("could't get node template name for workflow($workFlowName)'s step($stepName)")
88     }
89
90     fun workflowFirstStepNodeTemplate(workFlowName: String): String {
91         val firstStepName = workflowByName(workFlowName).steps?.keys?.first()
92                 ?: throw BluePrintException("could't get first step for workflow($workFlowName)")
93         return workflowStepNodeTemplate(workFlowName, firstStepName)
94     }
95
96     fun workflowStepFirstCallOperation(workFlowName: String, stepName: String): String {
97         return workflowStepByName(workFlowName, stepName).activities?.filter { it.callOperation != null }?.single()?.callOperation
98                 ?: throw BluePrintException("could't get first callOperation for WorkFlow($workFlowName) ")
99     }
100
101     // DSL
102     fun dslPropertiesByName(name: String): JsonNode = dslDefinitions()?.get(name)
103             ?: throw BluePrintException("could't get policy type for the dsl($name)")
104
105     // Data Type
106     fun dataTypeByName(name: String): DataType? = dataTypes()?.get(name)
107
108     // Artifact Type
109     fun artifactTypes(): MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
110
111     // Policy Types
112     fun policyTypes(): MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
113
114     fun policyTypeByName(policyName: String) = policyTypes()?.get(policyName)
115             ?: throw BluePrintException("could't get policy type for the name($policyName)")
116
117     fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
118         return policyTypes()?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
119     }
120
121     fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
122         return policyTypes()?.filterValues { it.targets.contains(target) }?.toMutableMap()
123     }
124
125     fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
126         return policyTypesDerivedFrom(derivedFrom)?.filterValues {
127             it.targets.contains(target)
128         }?.toMutableMap()
129     }
130
131     // Node Type Methods
132     fun nodeTypes(): MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
133
134     fun nodeTypeByName(name: String): NodeType =
135             nodeTypes()?.get(name)
136                     ?: throw BluePrintException("could't get node type for the name($name)")
137
138     fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
139         return nodeTypes()?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
140     }
141
142     fun nodeTypeInterface(nodeTypeName: String, interfaceName: String): InterfaceDefinition {
143         return nodeTypeByName(nodeTypeName).interfaces?.get(interfaceName)
144                 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName)")
145     }
146
147     fun nodeTypeInterfaceOperation(nodeTypeName: String, interfaceName: String, operationName: String): OperationDefinition {
148         return nodeTypeInterface(nodeTypeName, interfaceName).operations?.get(operationName)
149                 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName) operation definition($operationName)")
150     }
151
152     fun interfaceNameForNodeType(nodeTypeName: String): String {
153         return nodeTypeByName(nodeTypeName).interfaces?.keys?.first()
154                 ?: throw BluePrintException("could't get NodeType($nodeTypeName)'s first InterfaceDefinition name")
155     }
156
157     fun nodeTypeInterfaceOperationInputs(nodeTypeName: String, interfaceName: String, operationName: String): MutableMap<String, PropertyDefinition>? {
158         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
159     }
160
161     fun nodeTypeInterfaceOperationOutputs(nodeTypeName: String, interfaceName: String, operationName: String): MutableMap<String, PropertyDefinition>? {
162         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
163     }
164
165     // Node Template Methods
166     fun nodeTemplates(): MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
167
168     fun nodeTemplateByName(name: String): NodeTemplate =
169             nodeTemplates()?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
170
171     fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
172         return nodeTemplates()?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
173     }
174
175     fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
176         val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
177         return nodeTypeByName(nodeTemplateType)
178     }
179
180     fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
181         return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
182     }
183
184     fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
185         return nodeTemplateByName(nodeTemplateName).artifacts
186     }
187
188     fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
189         return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
190                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
191     }
192
193     fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
194         return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(0)
195                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
196     }
197
198     fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
199         return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
200                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
201     }
202
203     fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
204         return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
205                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
206     }
207
208     fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
209         return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
210                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
211     }
212
213     fun nodeTemplateInterfaceOperationInputs(nodeTemplateName: String, interfaceName: String, operationName: String): MutableMap<String, JsonNode>? {
214         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
215     }
216
217     fun nodeTemplateInterfaceOperationOutputs(nodeTemplateName: String, interfaceName: String, operationName: String): MutableMap<String, JsonNode>? {
218         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
219     }
220
221     fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
222         return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
223                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
224     }
225
226     fun nodeTemplateInterfaceOperation(nodeTemplateName: String, interfaceName: String, operationName: String): OperationAssignment {
227         return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
228                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
229     }
230
231     fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
232         return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
233                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
234     }
235
236     fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
237         return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
238                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
239     }
240
241     fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
242         val filteredNodeTemplateName: String = nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
243                 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
244         return nodeTemplateByName(filteredNodeTemplateName)
245     }
246
247     fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
248         return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
249     }
250
251     // Chained Functions
252
253     fun nodeTypeChained(nodeTypeName: String): NodeType {
254         return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
255     }
256
257     fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
258         return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)
259     }
260
261 }