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.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
50 * @author Brinda Santh
52 class BluePrintContext(val serviceTemplate: ServiceTemplate) {
54 private val log = LoggerFactory.getLogger(this::class.toString())
57 * Blueprint CBA extracted file location
62 * Root Definition file path
64 var entryDefinition = ""
66 /** Other definitions along with model, It may Resource Definition, Resource Assignments, Configurations etc..*/
67 var otherDefinitions: MutableMap<String, Any> = hashMapOf()
69 fun <T> otherDefinition(key: String) = otherDefinitions[key] as T
71 fun checkOtherDefinition(key: String) = otherDefinitions.containsKey(key)
73 fun imports(): List<ImportDefinition>? = serviceTemplate.imports
75 fun dslDefinitions() = serviceTemplate.dslDefinitions
77 val metadata: MutableMap<String, String>? = serviceTemplate.metadata
79 fun dataTypes(): MutableMap<String, DataType>? = serviceTemplate.dataTypes
81 fun inputs(): MutableMap<String, PropertyDefinition>? = serviceTemplate.topologyTemplate?.inputs
83 fun blueprintJson(pretty: Boolean = false): String = print("json", pretty)
85 private fun print(type: String? = "json", pretty: Boolean = false): String {
86 return JacksonUtils.getJson(serviceTemplate, pretty)
89 fun name(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_NAME)
90 ?: throw BluePrintException("could't get template name from meta data")
92 fun version(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_VERSION)
93 ?: throw BluePrintException("could't get template version from meta data")
95 fun author(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_AUTHOR)
96 ?: throw BluePrintException("could't get template author from meta data")
99 fun workflows(): MutableMap<String, Workflow>? = serviceTemplate.topologyTemplate?.workflows
101 fun workflowByName(workFlowName: String): Workflow = workflows()?.get(workFlowName)
102 ?: throw BluePrintException("could't get workflow($workFlowName)")
104 fun workflowInputs(workFlowName: String) = workflowByName(workFlowName).inputs
106 fun workflowStepByName(workFlowName: String, stepName: String): Step {
107 return workflowByName(workFlowName).steps?.get(stepName)
108 ?: throw BluePrintException("could't get step($stepName) for workflow($workFlowName)")
111 fun workflowStepNodeTemplate(workFlowName: String, stepName: String): String {
112 return workflowStepByName(workFlowName, stepName).target
113 ?: throw BluePrintException("could't get node template name for workflow($workFlowName)'s step($stepName)")
116 fun workflowFirstStepNodeTemplate(workFlowName: String): String {
117 val firstStepName = workflowByName(workFlowName).steps?.keys?.first()
118 ?: throw BluePrintException("could't get first step for workflow($workFlowName)")
119 return workflowStepNodeTemplate(workFlowName, firstStepName)
122 fun workflowStepFirstCallOperation(workFlowName: String, stepName: String): String {
123 return workflowStepByName(
126 ).activities?.filter { it.callOperation != null }?.single()?.callOperation
127 ?: throw BluePrintException("couldn't get first callOperation for WorkFlow($workFlowName) ")
131 fun dslPropertiesByName(name: String): JsonNode = dslDefinitions()?.get(name)
132 ?: throw BluePrintException("couldn't get policy type for the dsl($name)")
135 fun dataTypeByName(name: String): DataType? = dataTypes()?.get(name)
138 fun artifactTypes(): MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
141 fun policyTypes(): MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
143 fun policyTypeByName(policyName: String) = policyTypes()?.get(policyName)
144 ?: throw BluePrintException("could't get policy type for the name($policyName)")
146 fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
147 return policyTypes()?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
150 fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
151 return policyTypes()?.filterValues { it.targets.contains(target) }?.toMutableMap()
154 fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
155 return policyTypesDerivedFrom(derivedFrom)?.filterValues {
156 it.targets.contains(target)
161 fun nodeTypes(): MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
163 fun nodeTypeByName(name: String): NodeType =
164 nodeTypes()?.get(name)
165 ?: throw BluePrintException("could't get node type for the name($name)")
167 fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
168 return nodeTypes()?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
171 fun nodeTypeInterface(nodeTypeName: String, interfaceName: String): InterfaceDefinition {
172 return nodeTypeByName(nodeTypeName).interfaces?.get(interfaceName)
173 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName)")
176 fun nodeTypeInterfaceOperation(
177 nodeTypeName: String,
178 interfaceName: String,
179 operationName: String
180 ): OperationDefinition {
181 return nodeTypeInterface(nodeTypeName, interfaceName).operations?.get(operationName)
182 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName) operation definition($operationName)")
185 fun interfaceNameForNodeType(nodeTypeName: String): String {
186 return nodeTypeByName(nodeTypeName).interfaces?.keys?.first()
187 ?: throw BluePrintException("could't get NodeType($nodeTypeName)'s first InterfaceDefinition name")
190 fun nodeTypeInterfaceOperationInputs(
191 nodeTypeName: String,
192 interfaceName: String,
193 operationName: String
194 ): MutableMap<String, PropertyDefinition>? {
195 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
198 fun nodeTypeInterfaceOperationOutputs(
199 nodeTypeName: String,
200 interfaceName: String,
201 operationName: String
202 ): MutableMap<String, PropertyDefinition>? {
203 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
206 // Relationship Type Methods
207 fun relationshipTypes(): MutableMap<String, RelationshipType>? = serviceTemplate.relationshipTypes
209 fun relationshipTypeByName(name: String): RelationshipType = relationshipTypes()?.get(name)
210 ?: throw BluePrintException("could't get relationship type for the name($name)")
212 // Node Template Methods
213 fun nodeTemplates(): MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
215 fun nodeTemplateByName(name: String): NodeTemplate =
216 nodeTemplates()?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
218 fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
219 return nodeTemplates()?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
222 fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
223 val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
224 return nodeTypeByName(nodeTemplateType)
227 fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
228 return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
231 fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
232 return nodeTemplateByName(nodeTemplateName).artifacts
235 fun checkNodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition? {
236 return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
239 fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
240 return checkNodeTemplateArtifact(nodeTemplateName, artifactName)
241 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
244 fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
245 return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(0)
246 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
249 fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
250 return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
251 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
254 fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
255 return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
256 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
259 fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
260 return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
261 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
264 fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
266 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
269 fun nodeTemplateInterfaceOperationInputs(
270 nodeTemplateName: String,
271 interfaceName: String,
272 operationName: String
273 ): MutableMap<String, JsonNode>? {
274 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
277 fun nodeTemplateInterfaceOperationOutputs(
278 nodeTemplateName: String,
279 interfaceName: String,
280 operationName: String
281 ): MutableMap<String, JsonNode>? {
282 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
285 fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
286 return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
287 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
290 fun nodeTemplateInterfaceOperation(
291 nodeTemplateName: String,
292 interfaceName: String,
293 operationName: String
294 ): OperationAssignment {
295 return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
296 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
299 fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
300 return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
301 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
304 fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
305 return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
306 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
309 fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
310 val filteredNodeTemplateName: String =
311 nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
312 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
313 return nodeTemplateByName(filteredNodeTemplateName)
316 fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
317 return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
320 // Relationship Template Methods
321 fun relationshipTemplates(): MutableMap<String, RelationshipTemplate>? =
322 serviceTemplate.topologyTemplate?.relationshipTemplates
324 fun relationshipTemplateByName(name: String): RelationshipTemplate = relationshipTemplates()?.get(name)
325 ?: throw BluePrintException("could't get relationship template for the name($name)")
327 fun relationshipTemplateProperty(relationshipTemplateName: String, propertyName: String): Any? {
328 return nodeTemplateByName(relationshipTemplateName).properties?.get(propertyName)
331 fun relationshipTemplateForRelationshipType(name: String): MutableMap<String, RelationshipTemplate>? {
332 return relationshipTemplates()?.filterValues { relationshipTemplate -> relationshipTemplate.type == name }
336 fun relationshipTemplateRelationshipType(relationshipName: String): RelationshipType {
337 val relationshipTemplateType: String = relationshipTemplateByName(relationshipName).type
338 return relationshipTypeByName(relationshipTemplateType)
343 fun nodeTypeChained(nodeTypeName: String): NodeType {
344 return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
347 fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
348 return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)