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