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

io.cdap.plugin.common.batch.action.ConditionConfig Maven / Gradle / Ivy

There is a newer version: 2.12.3
Show newest version
/*
 * Copyright © 2016-2019 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.plugin.common.batch.action;

import com.google.common.base.Joiner;
import io.cdap.cdap.api.annotation.Description;
import io.cdap.cdap.api.plugin.PluginConfig;
import io.cdap.cdap.etl.api.FailureCollector;
import io.cdap.cdap.etl.api.batch.BatchActionContext;

import javax.annotation.Nullable;

/**
 * Base plugin config for post actions that contain a setting for when the action should run.
 */
public class ConditionConfig extends PluginConfig {
  protected static final String NAME_RUN_CONDITION = "runCondition";

  @Nullable
  @Description("When to run the action. Must be 'completion', 'success', or 'failure'. Defaults to 'completion'. " +
    "If set to 'completion', the action will be executed regardless of whether the pipeline run succeeded or failed." +
    "If set to 'success', the action will only be executed if the pipeline run succeeded. " +
    "If set to 'failure', the action will only be executed if the pipeline run failed.")
  public String runCondition;

  public ConditionConfig() {
    this.runCondition = Condition.COMPLETION.name();
  }

  public ConditionConfig(String runCondition) {
    this.runCondition = runCondition;
  }

  /**
   * Validates condition config.
   *
   * Deprecated since 2.3.0. Use {@link ConditionConfig#validate(FailureCollector)} instead.
   */
  @Deprecated
  public void validate() {
    try {
      Condition.valueOf(runCondition.toUpperCase());
    } catch (IllegalArgumentException e) {
      throw new IllegalArgumentException(String.format(
        "Invalid runCondition value '%s'.  Must be one of %s.",
        runCondition, Joiner.on(',').join(Condition.values())));
    }
  }

  @SuppressWarnings("ConstantConditions")
  public void validate(FailureCollector collector) {
    try {
      Condition.valueOf(runCondition.toUpperCase());
    } catch (IllegalArgumentException e) {
      collector.addFailure(String.format(
        "Invalid runCondition value '%s'.  Must be one of %s.",
        runCondition, Joiner.on(',').join(Condition.values())), "").withConfigProperty(NAME_RUN_CONDITION);
    }
  }

  @SuppressWarnings("ConstantConditions")
  public boolean shouldRun(BatchActionContext actionContext) {
    Condition condition = Condition.valueOf(runCondition.toUpperCase());
    switch (condition) {
      case COMPLETION:
        return true;
      case SUCCESS:
        return actionContext.isSuccessful();
      case FAILURE:
        return !actionContext.isSuccessful();
      default:
        throw new IllegalStateException("Unknown value for runCondition: " + runCondition);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy