2 * Copyright © 2019 IBM.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.NullNode
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.bpClone
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
26 * Generate Service Template for the simplified DSL.
27 * @author Brinda Santh
29 class BluePrintServiceTemplateGenerator(private val dslBluePrint: DSLBluePrint) {
31 private var serviceTemplate = ServiceTemplate()
33 private val nodeTypes: MutableMap<String, NodeType> = hashMapOf()
34 private val artifactTypes: MutableMap<String, ArtifactType> = hashMapOf()
35 private val dataTypes: MutableMap<String, DataType> = hashMapOf()
37 fun serviceTemplate(): ServiceTemplate {
38 serviceTemplate.metadata = dslBluePrint.metadata
39 serviceTemplate.dslDefinitions = dslBluePrint.properties
41 dataTypes.putAll(dslBluePrint.dataTypes)
42 artifactTypes.putAll(dslBluePrint.artifactTypes)
44 serviceTemplate.dataTypes = dataTypes
45 serviceTemplate.artifactTypes = artifactTypes
46 serviceTemplate.nodeTypes = nodeTypes
48 serviceTemplate.topologyTemplate = populateTopologyTemplate()
50 return serviceTemplate
53 private fun populateTopologyTemplate(): TopologyTemplate {
54 val topologyTemplate = TopologyTemplate()
55 topologyTemplate.nodeTemplates = populateNodeTemplates()
56 topologyTemplate.workflows = populateWorkflow()
57 return topologyTemplate
60 private fun populateNodeTemplates(): MutableMap<String, NodeTemplate> {
62 val nodeTemplates: MutableMap<String, NodeTemplate> = hashMapOf()
64 // For New or Dynamic Components
65 val components = dslBluePrint.components
66 components.forEach { (dslCompName, dslComp) ->
67 val nodeTemplate = NodeTemplate()
68 nodeTemplate.type = dslComp.type
69 nodeTemplate.properties = propertyAssignments(dslComp.properties)
70 nodeTemplate.artifacts = dslComp.artifacts
71 nodeTemplate.interfaces = populateInterfaceAssignments(dslComp)
72 nodeTemplates[dslCompName] = nodeTemplate
75 nodeTypes[dslComp.type] = populateNodeType(dslComp)
78 // For Registry Components
79 val registryComponents = dslBluePrint.registryComponents
80 registryComponents.forEach { (dslCompName, dslComp) ->
81 val nodeTemplate = NodeTemplate()
82 nodeTemplate.type = dslComp.type
83 nodeTemplate.properties = dslComp.properties
84 nodeTemplate.artifacts = dslComp.artifacts
85 nodeTemplate.interfaces = populateInterfaceAssignments(dslComp)
86 nodeTemplates[dslCompName] = nodeTemplate
92 private fun populateWorkflow(): MutableMap<String, Workflow>? {
93 var workflows: MutableMap<String, Workflow>? = null
94 if (dslBluePrint.workflows.isNotEmpty()) {
95 workflows = hashMapOf()
97 dslBluePrint.workflows.forEach { (dslWorkflowName, dslWorkflow) ->
98 val workflow = Workflow()
99 workflow.description = dslWorkflow.description
100 workflow.steps = dslWorkflow.steps
101 workflow.inputs = dslWorkflow.inputs
102 workflow.outputs = dslWorkflow.outputs
103 workflows[dslWorkflowName] = workflow
109 private fun populateNodeType(dslComponent: DSLComponent): NodeType {
110 val nodeType = NodeType()
111 nodeType.derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT
112 nodeType.version = dslComponent.version
113 nodeType.description = dslComponent.description
114 nodeType.interfaces = populateInterfaceDefinitions(dslComponent, nodeType)
118 private fun populateInterfaceDefinitions(dslComponent: DSLComponent, nodeType: NodeType): MutableMap<String, InterfaceDefinition> {
120 // Populate Node Type Attribute
121 nodeType.attributes = attributeDefinitions(dslComponent.attributes)
123 val operationDefinition = OperationDefinition()
124 operationDefinition.inputs = propertyDefinitions(dslComponent.inputs)
125 operationDefinition.outputs = propertyDefinitions(dslComponent.outputs)
127 val operations: MutableMap<String, OperationDefinition> = hashMapOf()
128 operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationDefinition
130 val interfaceDefinition = InterfaceDefinition()
131 interfaceDefinition.operations = operations
133 val interfaces: MutableMap<String, InterfaceDefinition> = hashMapOf()
134 interfaces[BluePrintConstants.DEFAULT_STEP_INTERFACE] = interfaceDefinition
138 private fun populateInterfaceAssignments(dslComponent: DSLRegistryComponent): MutableMap<String, InterfaceAssignment> {
139 val operationAssignment = OperationAssignment()
140 operationAssignment.implementation = dslComponent.implementation
141 operationAssignment.inputs = dslComponent.inputs
142 operationAssignment.outputs = dslComponent.outputs
144 val operations: MutableMap<String, OperationAssignment> = hashMapOf()
145 operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationAssignment
147 val interfaceAssignment = InterfaceAssignment()
148 interfaceAssignment.operations = operations
150 val interfaces: MutableMap<String, InterfaceAssignment> = hashMapOf()
151 interfaces[dslComponent.interfaceName] = interfaceAssignment
155 private fun populateInterfaceAssignments(dslComponent: DSLComponent): MutableMap<String, InterfaceAssignment> {
156 val operationAssignment = OperationAssignment()
157 operationAssignment.implementation = dslComponent.implementation
158 operationAssignment.inputs = propertyAssignments(dslComponent.inputs)
159 operationAssignment.outputs = propertyAssignments(dslComponent.outputs)
161 val operations: MutableMap<String, OperationAssignment> = hashMapOf()
162 operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationAssignment
164 val interfaceAssignment = InterfaceAssignment()
165 interfaceAssignment.operations = operations
167 val interfaces: MutableMap<String, InterfaceAssignment> = hashMapOf()
168 interfaces[BluePrintConstants.DEFAULT_STEP_INTERFACE] = interfaceAssignment
172 private fun propertyDefinitions(propertyDefinitions: Map<String, PropertyDefinition>?): MutableMap<String, PropertyDefinition>? {
173 val definitions: MutableMap<String, PropertyDefinition>? = propertyDefinitions?.bpClone()?.toMutableMap()
175 definitions?.forEach { (_, prop) ->
181 private fun attributeDefinitions(attributeDefinitions: Map<String, AttributeDefinition>?): MutableMap<String, AttributeDefinition>? {
182 val definitions: MutableMap<String, AttributeDefinition>? = attributeDefinitions?.bpClone()?.toMutableMap()
184 definitions?.forEach { (_, prop) ->
190 private fun propertyAssignments(propertyDefinitions: Map<String, PropertyDefinition>?): MutableMap<String, JsonNode>? {
191 var assignments: MutableMap<String, JsonNode>? = null
192 if (propertyDefinitions != null) {
193 assignments = hashMapOf()
194 propertyDefinitions.forEach { (propertyName, property) ->
195 assignments[propertyName] = property.value ?: NullNode.instance