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
61 * Root Definition file path
63 var entryDefinition = ""
65 /** Other definitions along with model, It may Resource Definition, Resource Assignments, Configurations etc..*/
66 var otherDefinitions: MutableMap<String, Any> = hashMapOf()
68 fun <T> otherDefinition(key: String) = otherDefinitions[key] as T
70 fun checkOtherDefinition(key: String) = otherDefinitions.containsKey(key)
72 fun imports(): List<ImportDefinition>? = serviceTemplate.imports
74 fun dslDefinitions() = serviceTemplate.dslDefinitions
76 val metadata: MutableMap<String, String>? = serviceTemplate.metadata
78 fun dataTypes(): MutableMap<String, DataType>? = serviceTemplate.dataTypes
80 fun inputs(): MutableMap<String, PropertyDefinition>? = serviceTemplate.topologyTemplate?.inputs
82 fun blueprintJson(pretty: Boolean = false): String = print("json", pretty)
84 private fun print(type: String? = "json", pretty: Boolean = false): String {
85 return JacksonUtils.getJson(serviceTemplate, pretty)
88 fun name(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_NAME)
89 ?: throw BluePrintException("could't get template name from meta data")
91 fun version(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_VERSION)
92 ?: throw BluePrintException("could't get template version from meta data")
94 fun author(): String = metadata?.get(BluePrintConstants.METADATA_TEMPLATE_AUTHOR)
95 ?: throw BluePrintException("could't get template author from meta data")
98 fun workflows(): MutableMap<String, Workflow>? = serviceTemplate.topologyTemplate?.workflows
100 fun workflowByName(workFlowName: String): Workflow = workflows()?.get(workFlowName)
101 ?: throw BluePrintException("could't get workflow($workFlowName)")
103 fun workflowInputs(workFlowName: String) = workflowByName(workFlowName).inputs
105 fun workflowStepByName(workFlowName: String, stepName: String): Step {
106 return workflowByName(workFlowName).steps?.get(stepName)
107 ?: throw BluePrintException("could't get step($stepName) for workflow($workFlowName)")
110 fun workflowStepNodeTemplate(workFlowName: String, stepName: String): String {
111 return workflowStepByName(workFlowName, stepName).target
112 ?: throw BluePrintException("could't get node template name for workflow($workFlowName)'s step($stepName)")
115 fun workflowFirstStepNodeTemplate(workFlowName: String): String {
116 val firstStepName = workflowByName(workFlowName).steps?.keys?.first()
117 ?: throw BluePrintException("could't get first step for workflow($workFlowName)")
118 return workflowStepNodeTemplate(workFlowName, firstStepName)
121 fun workflowStepFirstCallOperation(workFlowName: String, stepName: String): String {
122 return workflowStepByName(
125 ).activities?.filter { it.callOperation != null }?.single()?.callOperation
126 ?: throw BluePrintException("couldn't get first callOperation for WorkFlow($workFlowName) ")
130 fun dslPropertiesByName(name: String): JsonNode = dslDefinitions()?.get(name)
131 ?: throw BluePrintException("couldn't get policy type for the dsl($name)")
134 fun dataTypeByName(name: String): DataType? = dataTypes()?.get(name)
137 fun artifactTypes(): MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
140 fun policyTypes(): MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
142 fun policyTypeByName(policyName: String) = policyTypes()?.get(policyName)
143 ?: throw BluePrintException("could't get policy type for the name($policyName)")
145 fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
146 return policyTypes()?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
149 fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
150 return policyTypes()?.filterValues { it.targets.contains(target) }?.toMutableMap()
153 fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
154 return policyTypesDerivedFrom(derivedFrom)?.filterValues {
155 it.targets.contains(target)
160 fun nodeTypes(): MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
162 fun nodeTypeByName(name: String): NodeType =
163 nodeTypes()?.get(name)
164 ?: throw BluePrintException("could't get node type for the name($name)")
166 fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
167 return nodeTypes()?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
170 fun nodeTypeInterface(nodeTypeName: String, interfaceName: String): InterfaceDefinition {
171 return nodeTypeByName(nodeTypeName).interfaces?.get(interfaceName)
172 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName)")
175 fun nodeTypeInterfaceOperation(
176 nodeTypeName: String,
177 interfaceName: String,
178 operationName: String
179 ): OperationDefinition {
180 return nodeTypeInterface(nodeTypeName, interfaceName).operations?.get(operationName)
181 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName) operation definition($operationName)")
184 fun interfaceNameForNodeType(nodeTypeName: String): String {
185 return nodeTypeByName(nodeTypeName).interfaces?.keys?.first()
186 ?: throw BluePrintException("could't get NodeType($nodeTypeName)'s first InterfaceDefinition name")
189 fun nodeTypeInterfaceOperationInputs(
190 nodeTypeName: String,
191 interfaceName: String,
192 operationName: String
193 ): MutableMap<String, PropertyDefinition>? {
194 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
197 fun nodeTypeInterfaceOperationOutputs(
198 nodeTypeName: String,
199 interfaceName: String,
200 operationName: String
201 ): MutableMap<String, PropertyDefinition>? {
202 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
205 // Relationship Type Methods
206 fun relationshipTypes(): MutableMap<String, RelationshipType>? = serviceTemplate.relationshipTypes
208 fun relationshipTypeByName(name: String): RelationshipType = relationshipTypes()?.get(name)
209 ?: throw BluePrintException("could't get relationship type for the name($name)")
211 // Node Template Methods
212 fun nodeTemplates(): MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
214 fun nodeTemplateByName(name: String): NodeTemplate =
215 nodeTemplates()?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
217 fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
218 return nodeTemplates()?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
221 fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
222 val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
223 return nodeTypeByName(nodeTemplateType)
226 fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
227 return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
230 fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
231 return nodeTemplateByName(nodeTemplateName).artifacts
234 fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
235 return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
236 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
239 fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
240 return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(
243 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
246 fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
247 return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
248 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
251 fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
252 return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
253 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
256 fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
257 return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
258 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
261 fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
263 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
266 fun nodeTemplateInterfaceOperationInputs(
267 nodeTemplateName: String,
268 interfaceName: String,
269 operationName: String
270 ): MutableMap<String, JsonNode>? {
271 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
274 fun nodeTemplateInterfaceOperationOutputs(
275 nodeTemplateName: String,
276 interfaceName: String,
277 operationName: String
278 ): MutableMap<String, JsonNode>? {
279 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
282 fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
283 return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
284 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
287 fun nodeTemplateInterfaceOperation(
288 nodeTemplateName: String,
289 interfaceName: String,
290 operationName: String
291 ): OperationAssignment {
292 return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
293 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
296 fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
297 return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
298 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
301 fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
302 return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
303 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
306 fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
307 val filteredNodeTemplateName: String =
308 nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
309 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
310 return nodeTemplateByName(filteredNodeTemplateName)
313 fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
314 return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
317 // Relationship Template Methods
318 fun relationshipTemplates(): MutableMap<String, RelationshipTemplate>? =
319 serviceTemplate.topologyTemplate?.relationshipTemplates
321 fun relationshipTemplateByName(name: String): RelationshipTemplate = relationshipTemplates()?.get(name)
322 ?: throw BluePrintException("could't get relationship template for the name($name)")
324 fun relationshipTemplateProperty(relationshipTemplateName: String, propertyName: String): Any? {
325 return nodeTemplateByName(relationshipTemplateName).properties?.get(propertyName)
328 fun relationshipTemplateForRelationshipType(name: String): MutableMap<String, RelationshipTemplate>? {
329 return relationshipTemplates()?.filterValues { relationshipTemplate -> relationshipTemplate.type == name }
333 fun relationshipTemplateRelationshipType(relationshipName: String): RelationshipType {
334 val relationshipTemplateType: String = relationshipTemplateByName(relationshipName).type
335 return relationshipTypeByName(relationshipTemplateType)
340 fun nodeTypeChained(nodeTypeName: String): NodeType {
341 return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
344 fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
345 return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)