Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / processor-core / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / core / utils / PayloadUtils.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.core.utils
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
23 import org.onap.ccsdk.cds.controllerblueprints.core.returnNullIfMissing
24 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
25 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
26
27 object PayloadUtils {
28
29     fun prepareRequestPayloadStr(workflowName: String, jsonNode: JsonNode): String {
30         return prepareRequestPayload(workflowName, jsonNode).asJsonString(false)
31     }
32
33     fun prepareRequestPayload(workflowName: String, jsonNode: JsonNode): JsonNode {
34         val objectNode = JacksonUtils.objectMapper.createObjectNode()
35         objectNode.set<JsonNode>("$workflowName-request", jsonNode)
36         return objectNode
37     }
38
39     fun getResponseDataFromPayload(workflowName: String, responsePayload: JsonNode): JsonNode {
40         return responsePayload.get("$workflowName-response").returnNullIfMissing()
41             ?: throw BluePrintProcessorException("failed to get property($workflowName-response)")
42     }
43
44     fun prepareInputsFromWorkflowPayload(
45         bluePrintRuntimeService: BluePrintRuntimeService<*>,
46         payload: JsonNode,
47         workflowName: String
48     ) {
49         val input = payload.get("$workflowName-request")
50         bluePrintRuntimeService.assignWorkflowInputs(workflowName, input)
51     }
52
53     fun prepareDynamicInputsFromWorkflowPayload(
54         bluePrintRuntimeService: BluePrintRuntimeService<*>,
55         payload: JsonNode,
56         workflowName: String
57     ) {
58         val input = payload.get("$workflowName-request")
59         val propertyFields = input.get("$workflowName-properties")
60         prepareDynamicInputsFromComponentPayload(bluePrintRuntimeService, propertyFields)
61     }
62
63     fun prepareDynamicInputsFromComponentPayload(
64         bluePrintRuntimeService: BluePrintRuntimeService<*>,
65         payload: JsonNode
66     ) {
67         payload.fields().forEach { property ->
68             val path = StringBuilder(BluePrintConstants.PATH_INPUTS)
69                 .append(BluePrintConstants.PATH_DIVIDER).append(property.key).toString()
70             bluePrintRuntimeService.put(path, property.value)
71         }
72     }
73 }