aboutsummaryrefslogtreecommitdiffstats
path: root/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeService.kt
blob: ce0bceeea7387f782401ceb6a0e31fb4fc6d5cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.apps.controllerblueprints.core.service


import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.NullNode
import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactDefinition
import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
import com.att.eelf.configuration.EELFLogger
import com.att.eelf.configuration.EELFManager
/**
 *
 *
 * @author Brinda Santh
 */
open class BluePrintRuntimeService(var bluePrintContext: BluePrintContext, var context: MutableMap<String, Any> = hashMapOf()) {

    private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintRuntimeService::class.toString())

    /*
        Get the Node Type Definition for the Node Template, Then iterate Node Type Properties and resolve the expressing
     */
    open fun resolveNodeTemplateProperties(nodeTemplateName: String): MutableMap<String, Any?> {
        log.info("resolveNodeTemplatePropertyValues for node template ({})", nodeTemplateName)
        val propertyAssignmentValue: MutableMap<String, Any?> = hashMapOf()

        val nodeTemplate: NodeTemplate = bluePrintContext.nodeTemplateByName(nodeTemplateName)

        val propertyAssignments: MutableMap<String, Any?> =
                nodeTemplate.properties as MutableMap<String, Any?>

        // Get the Node Type Definitions
        val nodeTypeProperties: MutableMap<String, PropertyDefinition> =
                bluePrintContext.nodeTypeChainedProperties(nodeTemplate.type)!!

        // Iterate Node Type Properties
        nodeTypeProperties.forEach { nodeTypePropertyName, nodeTypeProperty ->
            // Get the Express or Value for the Node Template
            val propertyAssignment: Any? = propertyAssignments[nodeTypePropertyName]

            var resolvedValue: JsonNode = NullNode.getInstance()
            if (propertyAssignment != null) {
                // Resolve the Expressing
                val propertyAssignmentExpression = PropertyAssignmentService(context, this)
                resolvedValue = propertyAssignmentExpression.resolveAssignmentExpression(nodeTemplateName, nodeTypePropertyName, propertyAssignment)
            } else {
                // Assign default value to the Operation
                nodeTypeProperty.defaultValue?.let { defaultValue ->
                    resolvedValue = defaultValue
                }
            }
            // Set for Return of method
            propertyAssignmentValue[nodeTypePropertyName] = resolvedValue
        }
        log.info("resolved property definition for node template ({}), values ({})", nodeTemplateName, propertyAssignmentValue)
        return propertyAssignmentValue
    }

    open fun resolveNodeTemplateInterfaceOperationInputs(nodeTemplateName: String,
                                                    interfaceName: String, operationName: String): MutableMap<String, Any?> {
        log.info("nodeTemplateInterfaceOperationInputsResolvedExpression for node template ({}),interface name ({}), " +
                "operationName({})", nodeTemplateName, interfaceName, operationName)

        val propertyAssignmentValue: MutableMap<String, Any?> = hashMapOf()

        val propertyAssignments: MutableMap<String, Any> =
                bluePrintContext.nodeTemplateInterfaceOperationInputs(nodeTemplateName, interfaceName, operationName) as? MutableMap<String, Any>
                        ?: throw BluePrintException(String.format("failed to get input definitions for node template (%s), " +
                                "interface name (%s), operationName(%s)", nodeTemplateName, interfaceName, operationName))

        val nodeTypeName = bluePrintContext.nodeTemplateByName(nodeTemplateName).type

        val nodeTypeInterfaceOperationInputs: MutableMap<String, PropertyDefinition> =
                bluePrintContext.nodeTypeInterfaceOperationInputs(nodeTypeName, interfaceName, operationName)
                        ?: throw BluePrintException(String.format("failed to get input definitions for node type (%s), " +
                                "interface name (%s), operationName(%s)", nodeTypeName, interfaceName, operationName))

        log.info("input definition for node template ({}), values ({})", nodeTemplateName, propertyAssignments)

        // Iterate Node Type Properties
        nodeTypeInterfaceOperationInputs.forEach { nodeTypePropertyName, nodeTypeProperty ->
            // Get the Express or Value for the Node Template
            val propertyAssignment: Any? = propertyAssignments[nodeTypePropertyName]

            var resolvedValue: JsonNode = NullNode.getInstance()
            if (propertyAssignment != null) {
                // Resolve the Expressing
                val propertyAssignmentExpression = PropertyAssignmentService(context, this)
                resolvedValue = propertyAssignmentExpression.resolveAssignmentExpression(nodeTemplateName, nodeTypePropertyName, propertyAssignment)
            } else {
                // Assign default value to the Operation
                nodeTypeProperty.defaultValue?.let {
                    resolvedValue = JacksonUtils.jsonNodeFromObject(nodeTypeProperty.defaultValue!!)
                }
            }
            // Set for Return of method
            propertyAssignmentValue[nodeTypePropertyName] = resolvedValue
        }
        log.info("resolved input assignments for node template ({}), values ({})", nodeTemplateName, propertyAssignmentValue)

        return propertyAssignmentValue
    }


    open fun resolveNodeTemplateInterfaceOperationOutputs(nodeTemplateName: String,
                                                     interfaceName: String, operationName: String, componentContext: MutableMap<String, Any?>) {
        log.info("nodeTemplateInterfaceOperationInputsResolvedExpression for node template ({}),interface name ({}), " +
                "operationName({})", nodeTemplateName, interfaceName, operationName)

        val nodeTypeName = bluePrintContext.nodeTemplateByName(nodeTemplateName).type

        val nodeTypeInterfaceOperationOutputs: MutableMap<String, PropertyDefinition> =
                bluePrintContext.nodeTypeInterfaceOperationOutputs(nodeTypeName, interfaceName, operationName)
                        ?: throw BluePrintException(String.format("failed to get input definitions for node type (%s), " +
                                "interface name (%s), operationName(%s)", nodeTypeName, interfaceName, operationName))

        // Iterate Node Type Properties
        nodeTypeInterfaceOperationOutputs.forEach { nodeTypePropertyName, nodeTypeProperty ->

            val operationOutputPropertyName: String = StringBuilder().append(nodeTemplateName)
                    .append(".").append(interfaceName)
                    .append(".").append(operationName)
                    .append(".").append(nodeTypePropertyName).toString()
            // Get the Value from component context
            val resolvedValue: JsonNode = componentContext[operationOutputPropertyName] as? JsonNode
                    ?: NullNode.getInstance()
            // Store  operation output values into context
            setNodeTemplateOperationPropertyValue(nodeTemplateName, interfaceName, operationName, nodeTypePropertyName, resolvedValue)
            log.debug("resolved output assignments for node template ({}), property name ({}), value ({})", nodeTemplateName, nodeTypePropertyName, resolvedValue)
        }
    }

    open fun resolveNodeTemplateArtifact(nodeTemplateName: String,
                                    artifactName: String): String {
        val nodeTemplate = bluePrintContext.nodeTemplateByName(nodeTemplateName)

        val artifactDefinition: ArtifactDefinition = nodeTemplate.artifacts?.get(artifactName)
                ?: throw BluePrintProcessorException(String.format("failed to get artifat definition {} from the node template"
                        , artifactName))
        val propertyAssignmentExpression = PropertyAssignmentService(context, this)
        return propertyAssignmentExpression.artifactContent(artifactDefinition)
    }


    open fun setInputValue(propertyName: String, propertyDefinition: PropertyDefinition, value: JsonNode) {
        val path = StringBuilder(BluePrintConstants.PATH_INPUTS)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        log.trace("setting input path ({}), values ({})", path, value)
        context[path] = value
    }

    open fun setWorkflowInputValue(workflowName: String, propertyName: String, value: JsonNode) {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_WORKFLOWS).append(BluePrintConstants.PATH_DIVIDER).append(workflowName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_INPUTS)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        context[path] = value
    }

    open fun setNodeTemplatePropertyValue(nodeTemplateName: String, propertyName: String, value: JsonNode) {

        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        context[path] = value
    }

    open fun setNodeTemplateOperationPropertyValue(nodeTemplateName: String, interfaceName: String, operationName: String, propertyName: String,
                                              value: JsonNode) {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_INTERFACES).append(BluePrintConstants.PATH_DIVIDER).append(interfaceName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_OPERATIONS).append(BluePrintConstants.PATH_DIVIDER).append(operationName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        log.trace("setting operation property path ({}), values ({})", path, value)
        context[path] = value
    }

    open fun setNodeTemplateOperationInputValue(nodeTemplateName: String, interfaceName: String, operationName: String, propertyName: String,
                                           value: JsonNode) {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_INTERFACES).append(interfaceName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_OPERATIONS).append(operationName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_INPUTS)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        context[path] = value
    }

    open fun setNodeTemplateOperationOutputValue(nodeTemplateName: String, interfaceName: String, operationName: String, propertyName: String,
                                            value: JsonNode) {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_INTERFACES).append(interfaceName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_OPERATIONS).append(operationName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_OUTPUTS)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        context[path] = value
    }


    open fun getInputValue(propertyName: String): JsonNode {
        val path = StringBuilder(BluePrintConstants.PATH_INPUTS)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        return context[path] as? JsonNode ?: NullNode.instance
    }

    open fun getNodeTemplateOperationOutputValue(nodeTemplateName: String, interfaceName: String, operationName: String, propertyName: String): JsonNode {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_INTERFACES).append(BluePrintConstants.PATH_DIVIDER).append(interfaceName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_OPERATIONS).append(BluePrintConstants.PATH_DIVIDER).append(operationName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        return context[path] as JsonNode
    }

    open fun getPropertyValue(nodeTemplateName: String, propertyName: String): JsonNode? {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        return context[path] as JsonNode
    }

    open fun getRequirementPropertyValue(nodeTemplateName: String, requirementName: String, propertyName: String): JsonNode? {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_REQUIREMENTS).append(requirementName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        return context[path] as JsonNode
    }

    open fun getCapabilityPropertyValue(nodeTemplateName: String, capabilityName: String, propertyName: String): JsonNode? {
        val path: String = StringBuilder(BluePrintConstants.PATH_NODE_TEMPLATES).append(BluePrintConstants.PATH_DIVIDER).append(nodeTemplateName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_CAPABILITIES).append(capabilityName)
                .append(BluePrintConstants.PATH_DIVIDER).append(BluePrintConstants.PATH_PROPERTIES)
                .append(BluePrintConstants.PATH_DIVIDER).append(propertyName).toString()
        return context[path] as JsonNode
    }

    open fun assignInputs(jsonNode: JsonNode) {
        log.info("assignInputs from input JSON ({})", jsonNode.toString())
        bluePrintContext.inputs?.forEach { propertyName, property ->
            val valueNode: JsonNode = jsonNode.at(BluePrintConstants.PATH_DIVIDER + propertyName)
                    ?: NullNode.getInstance()
            setInputValue(propertyName, property, valueNode)
        }
    }

    open fun assignWorkflowInputs(workflowName: String, jsonNode: JsonNode) {
        log.info("assign workflow {} input value ({})", workflowName, jsonNode.toString())
        bluePrintContext.workflowByName(workflowName)?.inputs?.forEach { propertyName, property ->
            val valueNode: JsonNode = jsonNode.at(BluePrintConstants.PATH_DIVIDER + propertyName)
                    ?: NullNode.getInstance()
            setWorkflowInputValue(workflowName, propertyName, valueNode)
        }
    }
}