|
| 1 | +/* |
| 2 | + * Copyright 2024 Collate |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package org.openmetadata.service.governance.workflows.elements.nodes.automatedTask; |
| 15 | + |
| 16 | +import static org.openmetadata.service.governance.workflows.Workflow.ENTITY_LIST_VARIABLE; |
| 17 | +import static org.openmetadata.service.governance.workflows.Workflow.GLOBAL_NAMESPACE; |
| 18 | +import static org.openmetadata.service.governance.workflows.Workflow.getFlowableElementId; |
| 19 | + |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import org.flowable.bpmn.model.BoundaryEvent; |
| 23 | +import org.flowable.bpmn.model.BpmnModel; |
| 24 | +import org.flowable.bpmn.model.EndEvent; |
| 25 | +import org.flowable.bpmn.model.FieldExtension; |
| 26 | +import org.flowable.bpmn.model.Process; |
| 27 | +import org.flowable.bpmn.model.SequenceFlow; |
| 28 | +import org.flowable.bpmn.model.ServiceTask; |
| 29 | +import org.flowable.bpmn.model.StartEvent; |
| 30 | +import org.flowable.bpmn.model.SubProcess; |
| 31 | +import org.openmetadata.schema.governance.workflows.WorkflowConfiguration; |
| 32 | +import org.openmetadata.schema.governance.workflows.elements.nodes.automatedTask.SinkTaskDefinition; |
| 33 | +import org.openmetadata.schema.utils.JsonUtils; |
| 34 | +import org.openmetadata.service.governance.workflows.elements.NodeInterface; |
| 35 | +import org.openmetadata.service.governance.workflows.elements.nodes.automatedTask.sink.SinkTaskDelegate; |
| 36 | +import org.openmetadata.service.governance.workflows.flowable.builders.EndEventBuilder; |
| 37 | +import org.openmetadata.service.governance.workflows.flowable.builders.FieldExtensionBuilder; |
| 38 | +import org.openmetadata.service.governance.workflows.flowable.builders.ServiceTaskBuilder; |
| 39 | +import org.openmetadata.service.governance.workflows.flowable.builders.StartEventBuilder; |
| 40 | +import org.openmetadata.service.governance.workflows.flowable.builders.SubProcessBuilder; |
| 41 | + |
| 42 | +/** |
| 43 | + * Workflow node that pushes entity data to external sink destinations. |
| 44 | + * |
| 45 | + * <p>This node integrates with the sink provider registry to support multiple sink types such as |
| 46 | + * Git repositories, webhooks, and HTTP endpoints. |
| 47 | + */ |
| 48 | +public class SinkTask implements NodeInterface { |
| 49 | + private final SubProcess subProcess; |
| 50 | + private final BoundaryEvent runtimeExceptionBoundaryEvent; |
| 51 | + |
| 52 | + public SinkTask(SinkTaskDefinition nodeDefinition, WorkflowConfiguration config) { |
| 53 | + String subProcessId = nodeDefinition.getName(); |
| 54 | + |
| 55 | + SubProcess subProcess = |
| 56 | + new SubProcessBuilder().id(subProcessId).setAsync(true).exclusive(true).build(); |
| 57 | + |
| 58 | + StartEvent startEvent = |
| 59 | + new StartEventBuilder().id(getFlowableElementId(subProcessId, "startEvent")).build(); |
| 60 | + |
| 61 | + ServiceTask sinkTask = getSinkServiceTask(subProcessId, nodeDefinition); |
| 62 | + |
| 63 | + EndEvent endEvent = |
| 64 | + new EndEventBuilder().id(getFlowableElementId(subProcessId, "endEvent")).build(); |
| 65 | + |
| 66 | + subProcess.addFlowElement(startEvent); |
| 67 | + subProcess.addFlowElement(sinkTask); |
| 68 | + subProcess.addFlowElement(endEvent); |
| 69 | + |
| 70 | + subProcess.addFlowElement(new SequenceFlow(startEvent.getId(), sinkTask.getId())); |
| 71 | + subProcess.addFlowElement(new SequenceFlow(sinkTask.getId(), endEvent.getId())); |
| 72 | + |
| 73 | + if (config.getStoreStageStatus()) { |
| 74 | + attachWorkflowInstanceStageListeners(subProcess); |
| 75 | + } |
| 76 | + |
| 77 | + this.runtimeExceptionBoundaryEvent = |
| 78 | + getRuntimeExceptionBoundaryEvent(subProcess, config.getStoreStageStatus()); |
| 79 | + this.subProcess = subProcess; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public BoundaryEvent getRuntimeExceptionBoundaryEvent() { |
| 84 | + return runtimeExceptionBoundaryEvent; |
| 85 | + } |
| 86 | + |
| 87 | + private ServiceTask getSinkServiceTask(String subProcessId, SinkTaskDefinition nodeDefinition) { |
| 88 | + var taskConfig = nodeDefinition.getConfig(); |
| 89 | + |
| 90 | + FieldExtension sinkTypeExpr = |
| 91 | + new FieldExtensionBuilder() |
| 92 | + .fieldName("sinkTypeExpr") |
| 93 | + .fieldValue(taskConfig.getSinkType() != null ? taskConfig.getSinkType().value() : "") |
| 94 | + .build(); |
| 95 | + |
| 96 | + FieldExtension sinkConfigExpr = |
| 97 | + new FieldExtensionBuilder() |
| 98 | + .fieldName("sinkConfigExpr") |
| 99 | + .fieldValue( |
| 100 | + taskConfig.getSinkConfig() != null |
| 101 | + ? JsonUtils.pojoToJson(taskConfig.getSinkConfig()) |
| 102 | + : "{}") |
| 103 | + .build(); |
| 104 | + |
| 105 | + FieldExtension syncModeExpr = |
| 106 | + new FieldExtensionBuilder() |
| 107 | + .fieldName("syncModeExpr") |
| 108 | + .fieldValue( |
| 109 | + taskConfig.getSyncMode() != null ? taskConfig.getSyncMode().value() : "overwrite") |
| 110 | + .build(); |
| 111 | + |
| 112 | + FieldExtension outputFormatExpr = |
| 113 | + new FieldExtensionBuilder() |
| 114 | + .fieldName("outputFormatExpr") |
| 115 | + .fieldValue( |
| 116 | + taskConfig.getOutputFormat() != null |
| 117 | + ? taskConfig.getOutputFormat().value() |
| 118 | + : "yaml") |
| 119 | + .build(); |
| 120 | + |
| 121 | + FieldExtension hierarchyConfigExpr = |
| 122 | + new FieldExtensionBuilder() |
| 123 | + .fieldName("hierarchyConfigExpr") |
| 124 | + .fieldValue( |
| 125 | + taskConfig.getHierarchyConfig() != null |
| 126 | + ? JsonUtils.pojoToJson(taskConfig.getHierarchyConfig()) |
| 127 | + : "{}") |
| 128 | + .build(); |
| 129 | + |
| 130 | + FieldExtension entityFilterExpr = |
| 131 | + new FieldExtensionBuilder() |
| 132 | + .fieldName("entityFilterExpr") |
| 133 | + .fieldValue( |
| 134 | + taskConfig.getEntityFilter() != null |
| 135 | + ? JsonUtils.pojoToJson(taskConfig.getEntityFilter()) |
| 136 | + : "{}") |
| 137 | + .build(); |
| 138 | + |
| 139 | + FieldExtension batchModeExpr = |
| 140 | + new FieldExtensionBuilder() |
| 141 | + .fieldName("batchModeExpr") |
| 142 | + .fieldValue( |
| 143 | + String.valueOf( |
| 144 | + taskConfig.getBatchMode() != null ? taskConfig.getBatchMode() : true)) |
| 145 | + .build(); |
| 146 | + |
| 147 | + FieldExtension timeoutSecondsExpr = |
| 148 | + new FieldExtensionBuilder() |
| 149 | + .fieldName("timeoutSecondsExpr") |
| 150 | + .fieldValue( |
| 151 | + String.valueOf( |
| 152 | + taskConfig.getTimeoutSeconds() != null ? taskConfig.getTimeoutSeconds() : 300)) |
| 153 | + .build(); |
| 154 | + |
| 155 | + // Build input namespace map, ensuring entityList is mapped to global namespace for batch mode |
| 156 | + Map<String, String> inputNamespaceMap = new HashMap<>(); |
| 157 | + if (nodeDefinition.getInputNamespaceMap() != null) { |
| 158 | + @SuppressWarnings("unchecked") |
| 159 | + Map<String, String> converted = |
| 160 | + JsonUtils.convertValue(nodeDefinition.getInputNamespaceMap(), Map.class); |
| 161 | + if (converted != null) { |
| 162 | + inputNamespaceMap.putAll(converted); |
| 163 | + } |
| 164 | + } |
| 165 | + // Always include entityList mapping for batch sink support |
| 166 | + inputNamespaceMap.putIfAbsent(ENTITY_LIST_VARIABLE, GLOBAL_NAMESPACE); |
| 167 | + |
| 168 | + FieldExtension inputNamespaceMapExpr = |
| 169 | + new FieldExtensionBuilder() |
| 170 | + .fieldName("inputNamespaceMapExpr") |
| 171 | + .fieldValue(JsonUtils.pojoToJson(inputNamespaceMap)) |
| 172 | + .build(); |
| 173 | + |
| 174 | + return new ServiceTaskBuilder() |
| 175 | + .id(getFlowableElementId(subProcessId, "executeSink")) |
| 176 | + .implementation(SinkTaskDelegate.class.getName()) |
| 177 | + .addFieldExtension(sinkTypeExpr) |
| 178 | + .addFieldExtension(sinkConfigExpr) |
| 179 | + .addFieldExtension(syncModeExpr) |
| 180 | + .addFieldExtension(outputFormatExpr) |
| 181 | + .addFieldExtension(hierarchyConfigExpr) |
| 182 | + .addFieldExtension(entityFilterExpr) |
| 183 | + .addFieldExtension(batchModeExpr) |
| 184 | + .addFieldExtension(timeoutSecondsExpr) |
| 185 | + .addFieldExtension(inputNamespaceMapExpr) |
| 186 | + .setAsync(true) |
| 187 | + .exclusive(true) |
| 188 | + .build(); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void addToWorkflow(BpmnModel model, Process process) { |
| 193 | + process.addFlowElement(subProcess); |
| 194 | + process.addFlowElement(runtimeExceptionBoundaryEvent); |
| 195 | + } |
| 196 | +} |
0 commit comments