a112b6e6c4b4ef51af49faa353db0a349cda4a1b
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / 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.fasterxml.jackson.databind.JsonNode
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.CapabilityAssignment
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.ImportDefinition
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.InterfaceAssignment
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.InterfaceDefinition
32 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
33 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
34 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
35 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationDefinition
36 import org.onap.ccsdk.cds.controllerblueprints.core.data.PolicyType
37 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
38 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipTemplate
39 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
40 import org.onap.ccsdk.cds.controllerblueprints.core.data.RequirementAssignment
41 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
42 import org.onap.ccsdk.cds.controllerblueprints.core.data.Step
43 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
44 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
45 import org.slf4j.LoggerFactory
46
47 /**
48  *
49  *
50  * @author Brinda Santh
51  */
52 class BluePrintContext(val serviceTemplate: ServiceTemplate) {
53
54     private val log = LoggerFactory.getLogger(this::class.toString())
55
56     /**
57      * Blueprint CBA extracted file location
58      */
59     var rootPath = "."
60     /**
61      * Root Definition file path
62      */
63     var entryDefinition = ""
64
65     /** Other definitions along with model, It may Resource Definition, Resource Assignments, Configurations etc..*/
66     var otherDefinitions: MutableMap<String, Any> = hashMapOf()
67
68     fun <T> otherDefinition(key: String) = otherDefinitions[key] as T
69
70     fun checkOtherDefinition(key: String) = otherDefinitions.containsKey(key)
71
72     fun imports(): List<ImportDefinition>? = serviceTemplate.imports
73
74     fun dslDefinitions() = serviceTemplate.dslDefinitions
75
76     val metadata: MutableMap<String, String>? = serviceTemplate.metadata
77
78     fun dataTypes(): MutableMap<String, DataType>? = serviceTemplate.dataTypes
79
80     fun inputs(): MutableMap<String, PropertyDefinition>? = serviceTemplate.topologyTemplate?.inputs
81
82     fun blueprintJson(pretty: Boolean = false): String = print("json", pretty)
83
84     private fun print(type: String? = "json", pretty: Boolean = false): String {
85         return JacksonUtils.getJson(serviceTemplate, pretty)
86     }
87
88     fun name(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_NAME)
89         ?: throw BluePrintException("could't get template name from meta data")
90
91     fun version(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_VERSION)
92         ?: throw BluePrintException("could't get template version from meta data")
93
94     fun author(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_AUTHOR)
95         ?: throw BluePrintException("could't get template author from meta data")
96
97     // Workflow
98     fun workflows(): MutableMap<String, Workflow>? = serviceTemplate.topologyTemplate?.workflows
99
100     fun workflowByName(workFlowName: String): Workflow = workflows()?.get(workFlowName)
101         ?: throw BluePrintException("could't get workflow($workFlowName)")
102
103     fun workflowInputs(workFlowName: String) = workflowByName(workFlowName).inputs
104
105     fun workflowStepByName(workFlowName: String, stepName: String): Step {
106         return workflowByName(workFlowName).steps?.get(stepName)
107             ?: throw BluePrintException("could't get step($stepName) for workflow($workFlowName)")
108     }
109
110     fun workflowStepNodeTemplate(workFlowName: String, stepName: String): String {
111         return workflowStepByName(workFlowName, stepName).target
112             ?: throw BluePrintException("could't get node template name for workflow($workFlowName)'s step($stepName)")
113     }
114
115     fun workflowFirstStepNodeTemplate(workFlowName: String): String {
116         val firstStepName = workflowByName(workFlowName).steps?.keys?.first()
117             ?: throw BluePrintException("could't get first step for workflow($workFlowName)")
118         return workflowStepNodeTemplate(workFlowName, firstStepName)
119     }
120
121     fun workflowStepFirstCallOperation(workFlowName: String, stepName: String): String {
122         return workflowStepByName(
123             workFlowName,
124             stepName
125         ).activities?.filter { it.callOperation != null }?.single()?.callOperation
126             ?: throw BluePrintException("couldn't get first callOperation for WorkFlow($workFlowName) ")
127     }
128
129     // DSL
130     fun dslPropertiesByName(name: String): JsonNode = dslDefinitions()?.get(name)
131         ?: throw BluePrintException("couldn't get policy type for the dsl($name)")
132
133     // Data Type
134     fun dataTypeByName(name: String): DataType? = dataTypes()?.get(name)
135
136     // Artifact Type
137     fun artifactTypes(): MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
138
139     // Policy Types
140     fun policyTypes(): MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
141
142     fun policyTypeByName(policyName: String) = policyTypes()?.get(policyName)
143         ?: throw BluePrintException("could't get policy type for the name($policyName)")
144
145     fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
146         return policyTypes()?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
147     }
148
149     fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
150         return policyTypes()?.filterValues { it.targets.contains(target) }?.toMutableMap()
151     }
152
153     fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
154         return policyTypesDerivedFrom(derivedFrom)?.filterValues {
155             it.targets.contains(target)
156         }?.toMutableMap()
157     }
158
159     // Node Type Methods
160     fun nodeTypes(): MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
161
162     fun nodeTypeByName(name: String): NodeType =
163         nodeTypes()?.get(name)
164             ?: throw BluePrintException("could't get node type for the name($name)")
165
166     fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
167         return nodeTypes()?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
168     }
169
170     fun nodeTypeInterface(nodeTypeName: String, interfaceName: String): InterfaceDefinition {
171         return nodeTypeByName(nodeTypeName).interfaces?.get(interfaceName)
172             ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName)")
173     }
174
175     fun nodeTypeInterfaceOperation(
176         nodeTypeName: String,
177         interfaceName: String,
178         operationName: String
179     ): OperationDefinition {
180         return nodeTypeInterface(nodeTypeName, interfaceName).operations?.get(operationName)
181             ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName) operation definition($operationName)")
182     }
183
184     fun interfaceNameForNodeType(nodeTypeName: String): String {
185         return nodeTypeByName(nodeTypeName).interfaces?.keys?.first()
186             ?: throw BluePrintException("could't get NodeType($nodeTypeName)'s first InterfaceDefinition name")
187     }
188
189     fun nodeTypeInterfaceOperationInputs(
190         nodeTypeName: String,
191         interfaceName: String,
192         operationName: String
193     ): MutableMap<String, PropertyDefinition>? {
194         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
195     }
196
197     fun nodeTypeInterfaceOperationOutputs(
198         nodeTypeName: String,
199         interfaceName: String,
200         operationName: String
201     ): MutableMap<String, PropertyDefinition>? {
202         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
203     }
204
205     // Relationship Type Methods
206     fun relationshipTypes(): MutableMap<String, RelationshipType>? = serviceTemplate.relationshipTypes
207
208     fun relationshipTypeByName(name: String): RelationshipType = relationshipTypes()?.get(name)
209         ?: throw BluePrintException("could't get relationship type for the name($name)")
210
211     // Node Template Methods
212     fun nodeTemplates(): MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
213
214     fun nodeTemplateByName(name: String): NodeTemplate =
215         nodeTemplates()?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
216
217     fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
218         return nodeTemplates()?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
219     }
220
221     fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
222         val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
223         return nodeTypeByName(nodeTemplateType)
224     }
225
226     fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
227         return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
228     }
229
230     fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
231         return nodeTemplateByName(nodeTemplateName).artifacts
232     }
233
234     fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
235         return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
236             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
237     }
238
239     fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
240         return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(
241             0
242         )
243             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
244     }
245
246     fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
247         return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
248             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
249     }
250
251     fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
252         return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
253             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
254     }
255
256     fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
257         return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
258             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
259     }
260
261     fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
262         Implementation? {
263         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
264     }
265
266     fun nodeTemplateInterfaceOperationInputs(
267         nodeTemplateName: String,
268         interfaceName: String,
269         operationName: String
270     ): MutableMap<String, JsonNode>? {
271         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
272     }
273
274     fun nodeTemplateInterfaceOperationOutputs(
275         nodeTemplateName: String,
276         interfaceName: String,
277         operationName: String
278     ): MutableMap<String, JsonNode>? {
279         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
280     }
281
282     fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
283         return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
284             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
285     }
286
287     fun nodeTemplateInterfaceOperation(
288         nodeTemplateName: String,
289         interfaceName: String,
290         operationName: String
291     ): OperationAssignment {
292         return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
293             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
294     }
295
296     fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
297         return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
298             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
299     }
300
301     fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
302         return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
303             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
304     }
305
306     fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
307         val filteredNodeTemplateName: String =
308             nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
309                 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
310         return nodeTemplateByName(filteredNodeTemplateName)
311     }
312
313     fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
314         return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
315     }
316
317     // Relationship Template Methods
318     fun relationshipTemplates(): MutableMap<String, RelationshipTemplate>? =
319         serviceTemplate.topologyTemplate?.relationshipTemplates
320
321     fun relationshipTemplateByName(name: String): RelationshipTemplate = relationshipTemplates()?.get(name)
322         ?: throw BluePrintException("could't get relationship template for the name($name)")
323
324     fun relationshipTemplateProperty(relationshipTemplateName: String, propertyName: String): Any? {
325         return nodeTemplateByName(relationshipTemplateName).properties?.get(propertyName)
326     }
327
328     fun relationshipTemplateForRelationshipType(name: String): MutableMap<String, RelationshipTemplate>? {
329         return relationshipTemplates()?.filterValues { relationshipTemplate -> relationshipTemplate.type == name }
330             ?.toMutableMap()
331     }
332
333     fun relationshipTemplateRelationshipType(relationshipName: String): RelationshipType {
334         val relationshipTemplateType: String = relationshipTemplateByName(relationshipName).type
335         return relationshipTypeByName(relationshipTemplateType)
336     }
337
338     // Chained Functions
339
340     fun nodeTypeChained(nodeTypeName: String): NodeType {
341         return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
342     }
343
344     fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
345         return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)
346     }
347 }