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