a0bf054b9a6626ecd20463a1226ae87a89bc9d06
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 @file:Suppress("unused")
17
18 package org.onap.ccsdk.apps.controllerblueprints.core.service
19
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import com.fasterxml.jackson.databind.JsonNode
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
27
28 /**
29  *
30  *
31  * @author Brinda Santh
32  */
33 class BluePrintContext(val serviceTemplate: ServiceTemplate) {
34
35     private val log: EELFLogger = EELFManager.getInstance().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     val imports: List<ImportDefinition>? = serviceTemplate.imports
47
48     val metadata: MutableMap<String, String>? = serviceTemplate.metadata
49
50     val dataTypes: MutableMap<String, DataType>? = serviceTemplate.dataTypes
51
52     val inputs: MutableMap<String, PropertyDefinition>? = serviceTemplate.topologyTemplate?.inputs
53
54     fun blueprintJson(pretty: Boolean = false): String = print("json", pretty)
55
56     private fun print(type: String? = "json", pretty: Boolean = false): String {
57         return JacksonUtils.getJson(serviceTemplate, pretty)
58     }
59
60     fun name(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_NAME)
61             ?: throw BluePrintException("could't get template name from meta data")
62
63     fun version(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_VERSION)
64             ?: throw BluePrintException("could't get template version from meta data")
65
66     fun author(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_AUTHOR)
67             ?: throw BluePrintException("could't get template author from meta data")
68
69     // Workflow
70     val workflows: MutableMap<String, Workflow>? = serviceTemplate.topologyTemplate?.workflows
71
72     fun workflowByName(workFlowName: String): Workflow = workflows?.get(workFlowName)
73             ?: throw BluePrintException("could't get workflow($workFlowName)")
74
75     fun workflowInputs(workFlowName: String) = workflowByName(workFlowName).inputs
76
77     fun workflowStepByName(workFlowName: String, stepName: String): Step {
78         return workflowByName(workFlowName).steps?.get(stepName)
79                 ?: throw BluePrintException("could't get step($stepName) for workflow($workFlowName)")
80     }
81
82     fun workflowStepNodeTemplate(workFlowName: String, stepName: String): String {
83         return workflowStepByName(workFlowName, stepName).target
84                 ?: throw BluePrintException("could't get node template name for workflow($workFlowName)'s step($stepName)")
85     }
86
87     fun workflowFirstStepNodeTemplate(workFlowName: String): String {
88         val firstStepName = workflowByName(workFlowName).steps?.keys?.first()
89                 ?: throw BluePrintException("could't get first step for workflow($workFlowName)")
90         return workflowStepNodeTemplate(workFlowName, firstStepName)
91     }
92
93     fun workflowStepFirstCallOperation(workFlowName: String, stepName: String): String {
94         return workflowStepByName(workFlowName, stepName).activities?.filter { it.callOperation != null }?.single()?.callOperation
95                 ?: throw BluePrintException("could't get first callOperation for WorkFlow($workFlowName) ")
96     }
97
98     // Data Type
99     fun dataTypeByName(name: String): DataType? = dataTypes?.get(name)
100
101     // Artifact Type
102     val artifactTypes: MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
103
104     // Policy Types
105     val policyTypes: MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
106
107     fun policyTypeByName(policyName: String) = policyTypes?.get(policyName)
108             ?: throw BluePrintException("could't get policy type for the name($policyName)")
109
110     fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
111         return policyTypes?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
112     }
113
114     fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
115         return policyTypes?.filterValues { it.targets.contains(target) }?.toMutableMap()
116     }
117
118     fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
119         return policyTypesDerivedFrom(derivedFrom)?.filterValues {
120             it.targets.contains(target)
121         }?.toMutableMap()
122     }
123
124     // Node Type Methods
125     val nodeTypes: MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
126
127     fun nodeTypeByName(name: String): NodeType =
128             nodeTypes?.get(name)
129                     ?: throw BluePrintException("could't get node type for the name($name)")
130
131     fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
132         return nodeTypes?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
133     }
134
135     fun nodeTypeInterface(nodeTypeName: String, interfaceName: String): InterfaceDefinition {
136         return nodeTypeByName(nodeTypeName).interfaces?.get(interfaceName)
137                 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName)")
138     }
139
140     fun nodeTypeInterfaceOperation(nodeTypeName: String, interfaceName: String, operationName: String): OperationDefinition {
141         return nodeTypeInterface(nodeTypeName, interfaceName).operations?.get(operationName)
142                 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName) operation definition($operationName)")
143     }
144
145     fun interfaceNameForNodeType(nodeTypeName: String): String {
146         return nodeTypeByName(nodeTypeName).interfaces?.keys?.first()
147                 ?: throw BluePrintException("could't get NodeType($nodeTypeName)'s first InterfaceDefinition name")
148     }
149
150     fun nodeTypeInterfaceOperationInputs(nodeTypeName: String, interfaceName: String, operationName: String): MutableMap<String, PropertyDefinition>? {
151         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
152     }
153
154     fun nodeTypeInterfaceOperationOutputs(nodeTypeName: String, interfaceName: String, operationName: String): MutableMap<String, PropertyDefinition>? {
155         return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
156     }
157
158     // Node Template Methods
159     val nodeTemplates: MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
160
161     fun nodeTemplateByName(name: String): NodeTemplate =
162             nodeTemplates?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
163
164     fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
165         return nodeTemplates?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
166     }
167
168     fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
169         val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
170         return nodeTypeByName(nodeTemplateType)
171     }
172
173     fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
174         return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
175     }
176
177     fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
178         return nodeTemplateByName(nodeTemplateName).artifacts
179     }
180
181     fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
182         return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
183                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
184     }
185
186     fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
187         return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(0)
188                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
189     }
190
191     fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
192         return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
193                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
194     }
195
196     fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
197         return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
198                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
199     }
200
201     fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
202         return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
203                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
204     }
205
206     fun nodeTemplateInterfaceOperationInputs(nodeTemplateName: String, interfaceName: String, operationName: String): MutableMap<String, JsonNode>? {
207         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
208     }
209
210     fun nodeTemplateInterfaceOperationOutputs(nodeTemplateName: String, interfaceName: String, operationName: String): MutableMap<String, JsonNode>? {
211         return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
212     }
213
214     fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
215         return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
216                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
217     }
218
219     fun nodeTemplateInterfaceOperation(nodeTemplateName: String, interfaceName: String, operationName: String): OperationAssignment {
220         return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
221                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
222     }
223
224     fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
225         return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
226                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
227     }
228
229     fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
230         return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
231                 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
232     }
233
234     fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
235         val filteredNodeTemplateName: String = nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
236                 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
237         return nodeTemplateByName(filteredNodeTemplateName)
238     }
239
240     fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
241         return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
242     }
243
244     // Chained Functions
245
246     fun nodeTypeChained(nodeTypeName: String): NodeType {
247         return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
248     }
249
250     fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
251         return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)
252     }
253
254 }