7c097020287304da213fe0b8feb8b5a44ebb81e7
[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 checkNodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition? {
235         return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
236     }
237
238     fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
239         return checkNodeTemplateArtifact(nodeTemplateName, artifactName)
240             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
241     }
242
243     fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
244         return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(0)
245             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
246     }
247
248     fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
249         return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
250             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
251     }
252
253     fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
254         return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
255             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
256     }
257
258     fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
259         return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
260             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
261     }
262
263     fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
264         Implementation? {
265         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
266     }
267
268     fun nodeTemplateInterfaceOperationInputs(
269         nodeTemplateName: String,
270         interfaceName: String,
271         operationName: String
272     ): MutableMap<String, JsonNode>? {
273         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
274     }
275
276     fun nodeTemplateInterfaceOperationOutputs(
277         nodeTemplateName: String,
278         interfaceName: String,
279         operationName: String
280     ): MutableMap<String, JsonNode>? {
281         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
282     }
283
284     fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
285         return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
286             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
287     }
288
289     fun nodeTemplateInterfaceOperation(
290         nodeTemplateName: String,
291         interfaceName: String,
292         operationName: String
293     ): OperationAssignment {
294         return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
295             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
296     }
297
298     fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
299         return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
300             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
301     }
302
303     fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
304         return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
305             ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
306     }
307
308     fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
309         val filteredNodeTemplateName: String =
310             nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
311                 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
312         return nodeTemplateByName(filteredNodeTemplateName)
313     }
314
315     fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
316         return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
317     }
318
319     // Relationship Template Methods
320     fun relationshipTemplates(): MutableMap<String, RelationshipTemplate>? =
321         serviceTemplate.topologyTemplate?.relationshipTemplates
322
323     fun relationshipTemplateByName(name: String): RelationshipTemplate = relationshipTemplates()?.get(name)
324         ?: throw BluePrintException("could't get relationship template for the name($name)")
325
326     fun relationshipTemplateProperty(relationshipTemplateName: String, propertyName: String): Any? {
327         return nodeTemplateByName(relationshipTemplateName).properties?.get(propertyName)
328     }
329
330     fun relationshipTemplateForRelationshipType(name: String): MutableMap<String, RelationshipTemplate>? {
331         return relationshipTemplates()?.filterValues { relationshipTemplate -> relationshipTemplate.type == name }
332             ?.toMutableMap()
333     }
334
335     fun relationshipTemplateRelationshipType(relationshipName: String): RelationshipType {
336         val relationshipTemplateType: String = relationshipTemplateByName(relationshipName).type
337         return relationshipTypeByName(relationshipTemplateType)
338     }
339
340     // Chained Functions
341
342     fun nodeTypeChained(nodeTypeName: String): NodeType {
343         return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
344     }
345
346     fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
347         return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)
348     }
349 }