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