2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 @file:Suppress("unused")
19 package org.onap.ccsdk.cds.controllerblueprints.core.service
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
48 * @author Brinda Santh
50 class BluePrintContext(val serviceTemplate: ServiceTemplate) {
52 private val log = LoggerFactory.getLogger(this::class.toString())
55 * Blueprint CBA extracted file location
59 * Root Definition file path
61 var entryDefinition = ""
63 /** Other definitions along with model, It may Resource Definition, Resource Assignments, Configurations etc..*/
64 var otherDefinitions: MutableMap<String, Any> = hashMapOf()
66 fun <T> otherDefinition(key: String) = otherDefinitions[key] as T
68 fun checkOtherDefinition(key: String) = otherDefinitions.containsKey(key)
70 fun imports(): List<ImportDefinition>? = serviceTemplate.imports
72 fun dslDefinitions() = serviceTemplate.dslDefinitions
74 val metadata: MutableMap<String, String>? = serviceTemplate.metadata
76 fun dataTypes(): MutableMap<String, DataType>? = serviceTemplate.dataTypes
78 fun inputs(): MutableMap<String, PropertyDefinition>? = serviceTemplate.topologyTemplate?.inputs
80 fun blueprintJson(pretty: Boolean = false): String = print("json", pretty)
82 private fun print(type: String? = "json", pretty: Boolean = false): String {
83 return JacksonUtils.getJson(serviceTemplate, pretty)
86 fun name(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_NAME)
87 ?: throw BluePrintException("could't get template name from meta data")
89 fun version(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_VERSION)
90 ?: throw BluePrintException("could't get template version from meta data")
92 fun author(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_AUTHOR)
93 ?: throw BluePrintException("could't get template author from meta data")
96 fun workflows(): MutableMap<String, Workflow>? = serviceTemplate.topologyTemplate?.workflows
98 fun workflowByName(workFlowName: String): Workflow = workflows()?.get(workFlowName)
99 ?: throw BluePrintException("could't get workflow($workFlowName)")
101 fun workflowInputs(workFlowName: String) = workflowByName(workFlowName).inputs
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)")
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)")
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)
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) ")
125 fun dslPropertiesByName(name: String): JsonNode = dslDefinitions()?.get(name)
126 ?: throw BluePrintException("couldn't get policy type for the dsl($name)")
129 fun dataTypeByName(name: String): DataType? = dataTypes()?.get(name)
132 fun artifactTypes(): MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
135 fun policyTypes(): MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
137 fun policyTypeByName(policyName: String) = policyTypes()?.get(policyName)
138 ?: throw BluePrintException("could't get policy type for the name($policyName)")
140 fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
141 return policyTypes()?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
144 fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
145 return policyTypes()?.filterValues { it.targets.contains(target) }?.toMutableMap()
148 fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
149 return policyTypesDerivedFrom(derivedFrom)?.filterValues {
150 it.targets.contains(target)
155 fun nodeTypes(): MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
157 fun nodeTypeByName(name: String): NodeType =
158 nodeTypes()?.get(name)
159 ?: throw BluePrintException("could't get node type for the name($name)")
161 fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
162 return nodeTypes()?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
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)")
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)")
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")
180 fun nodeTypeInterfaceOperationInputs(
181 nodeTypeName: String,
182 interfaceName: String,
183 operationName: String
184 ): MutableMap<String, PropertyDefinition>? {
185 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
188 fun nodeTypeInterfaceOperationOutputs(
189 nodeTypeName: String,
190 interfaceName: String,
191 operationName: String
192 ): MutableMap<String, PropertyDefinition>? {
193 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
196 // Node Template Methods
197 fun nodeTemplates(): MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
199 fun nodeTemplateByName(name: String): NodeTemplate =
200 nodeTemplates()?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
202 fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
203 return nodeTemplates()?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
206 fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
207 val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
208 return nodeTypeByName(nodeTemplateType)
211 fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
212 return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
215 fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
216 return nodeTemplateByName(nodeTemplateName).artifacts
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)")
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)")
229 fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
230 return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
231 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
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")
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")
244 fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
246 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
249 fun nodeTemplateInterfaceOperationInputs(nodeTemplateName: String, interfaceName: String, operationName: String): MutableMap<String, JsonNode>? {
250 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
253 fun nodeTemplateInterfaceOperationOutputs(nodeTemplateName: String, interfaceName: String, operationName: String): MutableMap<String, JsonNode>? {
254 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
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)")
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)")
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)")
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)")
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)
283 fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
284 return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
289 fun nodeTypeChained(nodeTypeName: String): NodeType {
290 return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
293 fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
294 return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)