Revert "Renaming Files having BluePrint to have Blueprint"
[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 workflowSteps(workFlowName: String) =
107         workflowByName(workFlowName).steps ?: throw BluePrintException("could't get steps for workflow($workFlowName)")
108
109     fun workflowStepByName(workFlowName: String, stepName: String): Step {
110         return workflowSteps(workFlowName)[stepName]
111             ?: throw BluePrintException("could't get step($stepName) for workflow($workFlowName)")
112     }
113
114     fun workflowStepNodeTemplate(workFlowName: String, stepName: String): String {
115         return workflowStepByName(workFlowName, stepName).target
116             ?: throw BluePrintException("could't get node template name for workflow($workFlowName)'s step($stepName)")
117     }
118
119     fun workflowFirstStepNodeTemplate(workFlowName: String): String {
120         val firstStepName = workflowSteps(workFlowName).keys.ifEmpty {
121             throw BluePrintException("could't get first step for workflow($workFlowName)")
122         }.first()
123         return workflowStepNodeTemplate(workFlowName, firstStepName)
124     }
125
126     fun workflowStepFirstCallOperation(workFlowName: String, stepName: String): String {
127         return workflowStepByName(
128             workFlowName,
129             stepName
130         ).activities?.filter { it.callOperation != null }?.single()?.callOperation
131             ?: throw BluePrintException("couldn't get first callOperation for WorkFlow($workFlowName) ")
132     }
133
134     // DSL
135     fun dslPropertiesByName(name: String): JsonNode = dslDefinitions()?.get(name)
136         ?: throw BluePrintException("couldn't get policy type for the dsl($name)")
137
138     // Data Type
139     fun dataTypeByName(name: String): DataType? = dataTypes()?.get(name)
140
141     // Artifact Type
142     fun artifactTypes(): MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
143
144     // Policy Types
145     fun policyTypes(): MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
146
147     fun policyTypeByName(policyName: String) = policyTypes()?.get(policyName)
148         ?: throw BluePrintException("could't get policy type for the name($policyName)")
149
150     fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
151         return policyTypes()?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
152     }
153
154     fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
155         return policyTypes()?.filterValues { it.targets.contains(target) }?.toMutableMap()
156     }
157
158     fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
159         return policyTypesDerivedFrom(derivedFrom)?.filterValues {
160             it.targets.contains(target)
161         }?.toMutableMap()
162     }
163
164     // Node Type Methods
165     fun nodeTypes(): MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
166
167     fun nodeTypeByName(name: String): NodeType =
168         nodeTypes()?.get(name)
169             ?: throw BluePrintException("could't get node type for the name($name)")
170
171     fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
172         return nodeTypes()?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
173     }
174
175     fun nodeTypeInterface(nodeTypeName: String, interfaceName: String): InterfaceDefinition {
176         return nodeTypeByName(nodeTypeName).interfaces?.get(interfaceName)
177             ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName)")
178     }
179
180     fun nodeTypeInterfaceOperation(
181         nodeTypeName: String,
182         interfaceName: String,
183         operationName: String
184     ): OperationDefinition {
185         return nodeTypeInterface(nodeTypeName, interfaceName).operations?.get(operationName)
186             ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName) operation definition($operationName)")
187     }
188
189     fun interfaceNameForNodeType(nodeTypeName: String): String {
190         return nodeTypeByName(nodeTypeName).interfaces?.keys?.first()
191             ?: throw BluePrintException("could't get NodeType($nodeTypeName)'s first InterfaceDefinition name")
192     }
193
194     fun nodeTypeInterfaceOperationInputs(
195         nodeTypeName: String,
196         interfaceName: String,
197         operationName: String
198     ): MutableMap<String, PropertyDefinition>? {
199         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
200     }
201
202     fun nodeTypeInterfaceOperationOutputs(
203         nodeTypeName: String,
204         interfaceName: String,
205         operationName: String
206     ): MutableMap<String, PropertyDefinition>? {
207         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
208     }
209
210     // Relationship Type Methods
211     fun relationshipTypes(): MutableMap<String, RelationshipType>? = serviceTemplate.relationshipTypes
212
213     fun relationshipTypeByName(name: String): RelationshipType = relationshipTypes()?.get(name)
214         ?: throw BluePrintException("could't get relationship type for the name($name)")
215
216     // Node Template Methods
217     fun nodeTemplates(): MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
218
219     fun nodeTemplateByName(name: String): NodeTemplate =
220         nodeTemplates()?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
221
222     fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
223         return nodeTemplates()?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
224     }
225
226     fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
227         val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
228         return nodeTypeByName(nodeTemplateType)
229     }
230
231     fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
232         return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
233     }
234
235     fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
236         return nodeTemplateByName(nodeTemplateName).artifacts
237     }
238
239     fun checkNodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition? {
240         return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
241     }
242
243     fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
244         return checkNodeTemplateArtifact(nodeTemplateName, artifactName)
245             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
246     }
247
248     fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
249         return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(0)
250             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
251     }
252
253     fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
254         return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
255             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
256     }
257
258     fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
259         return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
260             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
261     }
262
263     fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
264         return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
265             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
266     }
267
268     fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
269         Implementation? {
270             return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
271         }
272
273     fun nodeTemplateInterfaceOperationInputs(
274         nodeTemplateName: String,
275         interfaceName: String,
276         operationName: String
277     ): MutableMap<String, JsonNode>? {
278         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
279     }
280
281     fun nodeTemplateInterfaceOperationOutputs(
282         nodeTemplateName: String,
283         interfaceName: String,
284         operationName: String
285     ): MutableMap<String, JsonNode>? {
286         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
287     }
288
289     fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
290         return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
291             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
292     }
293
294     fun nodeTemplateInterfaceOperation(
295         nodeTemplateName: String,
296         interfaceName: String,
297         operationName: String
298     ): OperationAssignment {
299         return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
300             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
301     }
302
303     fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
304         return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
305             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
306     }
307
308     fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
309         return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
310             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
311     }
312
313     fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
314         val filteredNodeTemplateName: String =
315             nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
316                 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
317         return nodeTemplateByName(filteredNodeTemplateName)
318     }
319
320     fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
321         return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
322     }
323
324     // Relationship Template Methods
325     fun relationshipTemplates(): MutableMap<String, RelationshipTemplate>? =
326         serviceTemplate.topologyTemplate?.relationshipTemplates
327
328     fun relationshipTemplateByName(name: String): RelationshipTemplate = relationshipTemplates()?.get(name)
329         ?: throw BluePrintException("could't get relationship template for the name($name)")
330
331     fun relationshipTemplateProperty(relationshipTemplateName: String, propertyName: String): Any? {
332         return nodeTemplateByName(relationshipTemplateName).properties?.get(propertyName)
333     }
334
335     fun relationshipTemplateForRelationshipType(name: String): MutableMap<String, RelationshipTemplate>? {
336         return relationshipTemplates()?.filterValues { relationshipTemplate -> relationshipTemplate.type == name }
337             ?.toMutableMap()
338     }
339
340     fun relationshipTemplateRelationshipType(relationshipName: String): RelationshipType {
341         val relationshipTemplateType: String = relationshipTemplateByName(relationshipName).type
342         return relationshipTypeByName(relationshipTemplateType)
343     }
344
345     // Chained Functions
346
347     fun nodeTypeChained(nodeTypeName: String): NodeType {
348         return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
349     }
350
351     fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
352         return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)
353     }
354 }