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 checkNodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition? {
235 return nodeTemplateArtifacts(nodeTemplateName)?.get(artifactName)
238 fun nodeTemplateArtifact(nodeTemplateName: String, artifactName: String): ArtifactDefinition {
239 return checkNodeTemplateArtifact(nodeTemplateName, artifactName)
240 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s ArtifactDefinition($artifactName)")
243 fun nodeTemplateArtifactForArtifactType(nodeTemplateName: String, artifactType: String): ArtifactDefinition {
244 return nodeTemplateArtifacts(nodeTemplateName)?.filter { it.value.type == artifactType }?.map { it.value }?.get(0)
245 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s Artifact Type($artifactType)")
248 fun nodeTemplateFirstInterface(nodeTemplateName: String): InterfaceAssignment {
249 return nodeTemplateByName(nodeTemplateName).interfaces?.values?.first()
250 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment")
253 fun nodeTemplateFirstInterfaceName(nodeTemplateName: String): String {
254 return nodeTemplateByName(nodeTemplateName).interfaces?.keys?.first()
255 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment name")
258 fun nodeTemplateFirstInterfaceFirstOperationName(nodeTemplateName: String): String {
259 return nodeTemplateFirstInterface(nodeTemplateName).operations?.keys?.first()
260 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first InterfaceAssignment's first OperationAssignment name")
263 fun nodeTemplateOperationImplementation(nodeTemplateName: String, interfaceName: String, operationName: String):
265 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).implementation
268 fun nodeTemplateInterfaceOperationInputs(
269 nodeTemplateName: String,
270 interfaceName: String,
271 operationName: String
272 ): MutableMap<String, JsonNode>? {
273 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).inputs
276 fun nodeTemplateInterfaceOperationOutputs(
277 nodeTemplateName: String,
278 interfaceName: String,
279 operationName: String
280 ): MutableMap<String, JsonNode>? {
281 return nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName).outputs
284 fun nodeTemplateInterface(nodeTemplateName: String, interfaceName: String): InterfaceAssignment {
285 return nodeTemplateByName(nodeTemplateName).interfaces?.get(interfaceName)
286 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName)")
289 fun nodeTemplateInterfaceOperation(
290 nodeTemplateName: String,
291 interfaceName: String,
292 operationName: String
293 ): OperationAssignment {
294 return nodeTemplateInterface(nodeTemplateName, interfaceName).operations?.get(operationName)
295 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s InterfaceAssignment($interfaceName) OperationAssignment($operationName)")
298 fun nodeTemplateCapability(nodeTemplateName: String, capabilityName: String): CapabilityAssignment {
299 return nodeTemplateByName(nodeTemplateName).capabilities?.get(capabilityName)
300 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s CapabilityAssignment($capabilityName)")
303 fun nodeTemplateRequirement(nodeTemplateName: String, requirementName: String): RequirementAssignment {
304 return nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)
305 ?: throw BluePrintException("could't get NodeTemplate($nodeTemplateName)'s first RequirementAssignment($requirementName)")
308 fun nodeTemplateRequirementNode(nodeTemplateName: String, requirementName: String): NodeTemplate {
309 val filteredNodeTemplateName: String =
310 nodeTemplateByName(nodeTemplateName).requirements?.get(requirementName)?.node
311 ?: throw BluePrintException("could't NodeTemplate for NodeTemplate's($nodeTemplateName) requirement's ($requirementName) ")
312 return nodeTemplateByName(filteredNodeTemplateName)
315 fun nodeTemplateCapabilityProperty(nodeTemplateName: String, capabilityName: String, propertyName: String): Any? {
316 return nodeTemplateCapability(nodeTemplateName, capabilityName).properties?.get(propertyName)
319 // Relationship Template Methods
320 fun relationshipTemplates(): MutableMap<String, RelationshipTemplate>? =
321 serviceTemplate.topologyTemplate?.relationshipTemplates
323 fun relationshipTemplateByName(name: String): RelationshipTemplate = relationshipTemplates()?.get(name)
324 ?: throw BluePrintException("could't get relationship template for the name($name)")
326 fun relationshipTemplateProperty(relationshipTemplateName: String, propertyName: String): Any? {
327 return nodeTemplateByName(relationshipTemplateName).properties?.get(propertyName)
330 fun relationshipTemplateForRelationshipType(name: String): MutableMap<String, RelationshipTemplate>? {
331 return relationshipTemplates()?.filterValues { relationshipTemplate -> relationshipTemplate.type == name }
335 fun relationshipTemplateRelationshipType(relationshipName: String): RelationshipType {
336 val relationshipTemplateType: String = relationshipTemplateByName(relationshipName).type
337 return relationshipTypeByName(relationshipTemplateType)
342 fun nodeTypeChained(nodeTypeName: String): NodeType {
343 return BluePrintChainedService(this).nodeTypeChained(nodeTypeName)
346 fun nodeTypeChainedProperties(nodeTypeName: String): MutableMap<String, PropertyDefinition>? {
347 return BluePrintChainedService(this).nodeTypeChainedProperties(nodeTypeName)