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 workflowSteps(workFlowName: String) =
107 workflowByName(workFlowName).steps ?: throw BluePrintException("could't get steps for workflow($workFlowName)")
109 fun workflowStepByName(workFlowName: String, stepName: String): Step {
110 return workflowSteps(workFlowName)[stepName]
111 ?: throw BluePrintException("could't get step($stepName) for workflow($workFlowName)")
114 fun workflowStepNodeTemplate(workFlowName: String, stepName: String): String {
115 return workflowStepByName(workFlowName, stepName).target
116 ?: throw BluePrintException("could't get node template name for workflow($workFlowName)'s step($stepName)")
119 fun workflowFirstStepNodeTemplate(workFlowName: String): String {
120 val firstStepName = workflowSteps(workFlowName).keys.ifEmpty {
121 throw BluePrintException("could't get first step for workflow($workFlowName)")
123 return workflowStepNodeTemplate(workFlowName, firstStepName)
126 fun workflowStepFirstCallOperation(workFlowName: String, stepName: String): String {
127 return workflowStepByName(
130 ).activities?.filter { it.callOperation != null }?.single()?.callOperation
131 ?: throw BluePrintException("couldn't get first callOperation for WorkFlow($workFlowName) ")
135 fun dslPropertiesByName(name: String): JsonNode = dslDefinitions()?.get(name)
136 ?: throw BluePrintException("couldn't get policy type for the dsl($name)")
139 fun dataTypeByName(name: String): DataType? = dataTypes()?.get(name)
142 fun artifactTypes(): MutableMap<String, ArtifactType>? = serviceTemplate.artifactTypes
145 fun policyTypes(): MutableMap<String, PolicyType>? = serviceTemplate.policyTypes
147 fun policyTypeByName(policyName: String) = policyTypes()?.get(policyName)
148 ?: throw BluePrintException("could't get policy type for the name($policyName)")
150 fun policyTypesDerivedFrom(name: String): MutableMap<String, PolicyType>? {
151 return policyTypes()?.filterValues { policyType -> policyType.derivedFrom == name }?.toMutableMap()
154 fun policyTypesTarget(target: String): MutableMap<String, PolicyType>? {
155 return policyTypes()?.filterValues { it.targets.contains(target) }?.toMutableMap()
158 fun policyTypesTargetNDerivedFrom(target: String, derivedFrom: String): MutableMap<String, PolicyType>? {
159 return policyTypesDerivedFrom(derivedFrom)?.filterValues {
160 it.targets.contains(target)
165 fun nodeTypes(): MutableMap<String, NodeType>? = serviceTemplate.nodeTypes
167 fun nodeTypeByName(name: String): NodeType =
168 nodeTypes()?.get(name)
169 ?: throw BluePrintException("could't get node type for the name($name)")
171 fun nodeTypeDerivedFrom(name: String): MutableMap<String, NodeType>? {
172 return nodeTypes()?.filterValues { nodeType -> nodeType.derivedFrom == name }?.toMutableMap()
175 fun nodeTypeInterface(nodeTypeName: String, interfaceName: String): InterfaceDefinition {
176 return nodeTypeByName(nodeTypeName).interfaces?.get(interfaceName)
177 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName)")
180 fun nodeTypeInterfaceOperation(
181 nodeTypeName: String,
182 interfaceName: String,
183 operationName: String
184 ): OperationDefinition {
185 return nodeTypeInterface(nodeTypeName, interfaceName).operations?.get(operationName)
186 ?: throw BluePrintException("could't get node type($nodeTypeName)'s interface definition($interfaceName) operation definition($operationName)")
189 fun interfaceNameForNodeType(nodeTypeName: String): String {
190 return nodeTypeByName(nodeTypeName).interfaces?.keys?.first()
191 ?: throw BluePrintException("could't get NodeType($nodeTypeName)'s first InterfaceDefinition name")
194 fun nodeTypeInterfaceOperationInputs(
195 nodeTypeName: String,
196 interfaceName: String,
197 operationName: String
198 ): MutableMap<String, PropertyDefinition>? {
199 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).inputs
202 fun nodeTypeInterfaceOperationOutputs(
203 nodeTypeName: String,
204 interfaceName: String,
205 operationName: String
206 ): MutableMap<String, PropertyDefinition>? {
207 return nodeTypeInterfaceOperation(nodeTypeName, interfaceName, operationName).outputs
210 // Relationship Type Methods
211 fun relationshipTypes(): MutableMap<String, RelationshipType>? = serviceTemplate.relationshipTypes
213 fun relationshipTypeByName(name: String): RelationshipType = relationshipTypes()?.get(name)
214 ?: throw BluePrintException("could't get relationship type for the name($name)")
216 // Node Template Methods
217 fun nodeTemplates(): MutableMap<String, NodeTemplate>? = serviceTemplate.topologyTemplate?.nodeTemplates
219 fun nodeTemplateByName(name: String): NodeTemplate =
220 nodeTemplates()?.get(name) ?: throw BluePrintException("could't get node template for the name($name)")
222 fun nodeTemplateForNodeType(name: String): MutableMap<String, NodeTemplate>? {
223 return nodeTemplates()?.filterValues { nodeTemplate -> nodeTemplate.type == name }?.toMutableMap()
226 fun nodeTemplateNodeType(nodeTemplateName: String): NodeType {
227 val nodeTemplateType: String = nodeTemplateByName(nodeTemplateName).type
228 return nodeTypeByName(nodeTemplateType)
231 fun nodeTemplateProperty(nodeTemplateName: String, propertyName: String): Any? {
232 return nodeTemplateByName(nodeTemplateName).properties?.get(propertyName)
235 fun nodeTemplateArtifacts(nodeTemplateName: String): MutableMap<String, ArtifactDefinition>? {
236 return nodeTemplateByName(nodeTemplateName).artifacts
239 fun checkNodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition? {
240 return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
243 fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
244 return checkNodeTemplateArtifact(nodeTemplateName, artifactName)
245 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
248 fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
249 return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(0)
250 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
253 fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
254 return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
255 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
258 fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
259 return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
260 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
263 fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
264 return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
265 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
268 fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
270 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
273 fun nodeTemplateInterfaceOperationInputs(
274 nodeTemplateName: String,
275 interfaceName: String,
276 operationName: String
277 ): MutableMap<String, JsonNode>? {
278 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
281 fun nodeTemplateInterfaceOperationOutputs(
282 nodeTemplateName: String,
283 interfaceName: String,
284 operationName: String
285 ): MutableMap<String, JsonNode>? {
286 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
289 fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
290 return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
291 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
294 fun nodeTemplateInterfaceOperation(
295 nodeTemplateName: String,
296 interfaceName: String,
297 operationName: String
298 ): OperationAssignment {
299 return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
300 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
303 fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
304 return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
305 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
308 fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
309 return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
310 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
313 fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
314 val filteredNodeTemplateName: String =
315 nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
316 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
317 return nodeTemplateByName(filteredNodeTemplateName)
320 fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
321 return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
324 // Relationship Template Methods
325 fun relationshipTemplates(): MutableMap<String, RelationshipTemplate>? =
326 serviceTemplate.topologyTemplate?.relationshipTemplates
328 fun relationshipTemplateByName(name: String): RelationshipTemplate = relationshipTemplates()?.get(name)
329 ?: throw BluePrintException("could't get relationship template for the name($name)")
331 fun relationshipTemplateProperty(relationshipTemplateName: String, propertyName: String): Any? {
332 return nodeTemplateByName(relationshipTemplateName).properties?.get(propertyName)
335 fun relationshipTemplateForRelationshipType(name: String): MutableMap<String, RelationshipTemplate>? {
336 return relationshipTemplates()?.filterValues { relationshipTemplate -> relationshipTemplate.type == name }
340 fun relationshipTemplateRelationshipType(relationshipName: String): RelationshipType {
341 val relationshipTemplateType: String = relationshipTemplateByName(relationshipName).type
342 return relationshipTypeByName(relationshipTemplateType)
347 fun nodeTypeChained(nodeTypeName: String): NodeType {
348 return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
351 fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
352 return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)