All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cdap.cdap.internal.app.workflow.DefaultWorkflowForkConfigurer Maven / Gradle / Ivy

There is a newer version: 6.10.1
Show newest version
/*
 * Copyright © 2015 Cask Data, Inc.
 *
 * 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 io.cdap.cdap.internal.app.workflow;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import io.cdap.cdap.api.Predicate;
import io.cdap.cdap.api.customaction.CustomAction;
import io.cdap.cdap.api.feature.FeatureFlagsProvider;
import io.cdap.cdap.api.schedule.SchedulableProgramType;
import io.cdap.cdap.api.workflow.Condition;
import io.cdap.cdap.api.workflow.ConditionSpecification;
import io.cdap.cdap.api.workflow.WorkflowConditionConfigurer;
import io.cdap.cdap.api.workflow.WorkflowConditionNode;
import io.cdap.cdap.api.workflow.WorkflowContext;
import io.cdap.cdap.api.workflow.WorkflowForkConfigurer;
import io.cdap.cdap.api.workflow.WorkflowForkNode;
import io.cdap.cdap.api.workflow.WorkflowNode;
import io.cdap.cdap.common.id.Id;
import io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentRuntimeInfo;
import io.cdap.cdap.internal.app.runtime.artifact.PluginFinder;
import io.cdap.cdap.internal.app.runtime.plugin.PluginInstantiator;
import io.cdap.cdap.internal.app.workflow.condition.DefaultConditionConfigurer;
import io.cdap.cdap.internal.workflow.condition.DefaultConditionSpecification;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import javax.annotation.Nullable;

/**
 * Default implementation of the {@link WorkflowForkConfigurer}
 * @param 
 */
public class DefaultWorkflowForkConfigurer
  implements WorkflowForkConfigurer, WorkflowForkJoiner, WorkflowConditionAdder {

  private final T parentForkConfigurer;
  private final List> branches = Lists.newArrayList();
  private final Id.Namespace deployNamespace;
  private final Id.Artifact artifactId;
  private final PluginFinder pluginFinder;
  private final PluginInstantiator pluginInstantiator;
  private final AppDeploymentRuntimeInfo runtimeInfo;
  private final FeatureFlagsProvider featureFlagsProvider;

  private List currentBranch;

  public DefaultWorkflowForkConfigurer(T parentForkConfigurer, Id.Namespace deployNamespace, Id.Artifact artifactId,
                                       PluginFinder pluginFinder, PluginInstantiator pluginInstantiator,
                                       @Nullable AppDeploymentRuntimeInfo runtimeInfo,
                                       FeatureFlagsProvider featureFlagsProvider) {
    this.parentForkConfigurer = parentForkConfigurer;
    currentBranch = Lists.newArrayList();
    this.deployNamespace = deployNamespace;
    this.artifactId = artifactId;
    this.pluginFinder = pluginFinder;
    this.pluginInstantiator = pluginInstantiator;
    this.runtimeInfo = runtimeInfo;
    this.featureFlagsProvider = featureFlagsProvider;
  }

  @Override
  public WorkflowForkConfigurer addMapReduce(String mapReduce) {
    currentBranch.add(WorkflowNodeCreator.createWorkflowActionNode(mapReduce, SchedulableProgramType.MAPREDUCE));
    return this;
  }

  @Override
  public WorkflowForkConfigurer addSpark(String spark) {
    currentBranch.add(WorkflowNodeCreator.createWorkflowActionNode(spark, SchedulableProgramType.SPARK));
    return this;
  }

  @Override
  public WorkflowForkConfigurer addAction(CustomAction action) {
    currentBranch.add(WorkflowNodeCreator.createWorkflowCustomActionNode(action, deployNamespace, artifactId,
                                                                         pluginFinder, pluginInstantiator,
                                                                         runtimeInfo, featureFlagsProvider));
    return this;
  }

  @Override
  @SuppressWarnings("unchecked")
  public WorkflowForkConfigurer> fork() {
    return new DefaultWorkflowForkConfigurer<>(this, deployNamespace, artifactId, pluginFinder,
                                               pluginInstantiator, runtimeInfo, featureFlagsProvider);
  }

  @Override
  public WorkflowConditionConfigurer> condition(
    Predicate predicate) {
    return new DefaultWorkflowConditionConfigurer<>(predicate, this, deployNamespace, artifactId, pluginFinder,
                                                    pluginInstantiator, runtimeInfo, featureFlagsProvider);
  }

  @Override
  public WorkflowConditionConfigurer> condition(Condition condition) {
    return new DefaultWorkflowConditionConfigurer<>(condition, this, deployNamespace, artifactId, pluginFinder,
                                                    pluginInstantiator, runtimeInfo, featureFlagsProvider);
  }

  @Override
  public WorkflowForkConfigurer also() {
    branches.add(currentBranch);
    currentBranch = Lists.newArrayList();
    return this;
  }

  @Override
  @SuppressWarnings("unchecked")
  public T join() {
    branches.add(currentBranch);
    parentForkConfigurer.addWorkflowForkNode(branches);
    return parentForkConfigurer;
  }

  @Override
  public void addWorkflowForkNode(List> branches) {
    currentBranch.add(new WorkflowForkNode(null, branches));
  }

  @Override
  public void addWorkflowConditionNode(Predicate predicate, List ifBranch,
                                       List elseBranch) {
    ConditionSpecification spec = new DefaultConditionSpecification(predicate.getClass().getName(),
                                                                    predicate.getClass().getSimpleName(), "",
                                                                    new HashMap(),
                                                                    new HashSet());
    currentBranch.add(new WorkflowConditionNode(spec.getName(), spec, ifBranch, elseBranch));
  }

  @Override
  public void addWorkflowConditionNode(Condition condition, List ifBranch,
                                       List elseBranch) {
    Preconditions.checkArgument(condition != null, "Condition is null.");
    ConditionSpecification spec = DefaultConditionConfigurer.configureCondition(condition, deployNamespace,
        artifactId, pluginFinder, pluginInstantiator, runtimeInfo, featureFlagsProvider);
    currentBranch.add(new WorkflowConditionNode(spec.getName(), spec, ifBranch, elseBranch));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy