use of co.cask.cdap.etl.api.condition.Condition in project cdap by caskdata.
the class PipelineCondition method apply.
@Override
public boolean apply(@Nullable WorkflowContext input) {
if (input == null) {
// should not happen
throw new IllegalStateException("WorkflowContext for the Condition cannot be null.");
}
Map<String, String> properties = input.getConditionSpecification().getProperties();
BatchPhaseSpec phaseSpec = GSON.fromJson(properties.get(Constants.PIPELINEID), BatchPhaseSpec.class);
PipelinePhase phase = phaseSpec.getPhase();
StageSpec stageSpec = phase.iterator().next();
PluginContext pluginContext = new PipelinePluginContext(input, metrics, phaseSpec.isStageLoggingEnabled(), phaseSpec.isProcessTimingEnabled());
MacroEvaluator macroEvaluator = new DefaultMacroEvaluator(new BasicArguments(input.getToken(), input.getRuntimeArguments()), input.getLogicalStartTime(), input, input.getNamespace());
try {
Condition condition = pluginContext.newPluginInstance(stageSpec.getName(), macroEvaluator);
PipelineRuntime pipelineRuntime = new PipelineRuntime(input, metrics);
ConditionContext conditionContext = new BasicConditionContext(input, pipelineRuntime, stageSpec);
boolean result = condition.apply(conditionContext);
WorkflowToken token = input.getToken();
if (token == null) {
throw new IllegalStateException("WorkflowToken cannot be null when Condition is executed through Workflow.");
}
for (Map.Entry<String, String> entry : pipelineRuntime.getArguments().getAddedArguments().entrySet()) {
token.put(entry.getKey(), entry.getValue());
}
return result;
} catch (Exception e) {
String msg = String.format("Error executing condition '%s' in the pipeline.", stageSpec.getName());
throw new RuntimeException(msg, e);
}
}
Aggregations