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

azkaban.flow.Flow Maven / Gradle / Ivy

/*
 * Copyright 2012 LinkedIn Corp.
 *
 * 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 azkaban.flow;

import azkaban.Constants;
import azkaban.executor.mail.DefaultMailCreator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Flow {

  private final String id;
  private final HashMap nodes = new HashMap<>();
  private final HashMap edges = new HashMap<>();
  private final HashMap> outEdges =
      new HashMap<>();
  private final HashMap> inEdges = new HashMap<>();
  private final HashMap flowProps =
      new HashMap<>();
  private int projectId;
  private ArrayList startNodes = null;
  private ArrayList endNodes = null;
  private int numLevels = -1;
  private List failureEmail = new ArrayList<>();
  private List successEmail = new ArrayList<>();
  private String mailCreator = DefaultMailCreator.DEFAULT_MAIL_CREATOR;
  private ArrayList errors;
  private int version = -1;
  private Map metadata = new HashMap<>();

  private boolean isLayedOut = false;
  private boolean isEmbeddedFlow = false;
  private double azkabanFlowVersion = Constants.DEFAULT_AZKABAN_FLOW_VERSION;
  private String condition = null;

  public Flow(final String id) {
    this.id = id;
  }

  public static Flow flowFromObject(final Object object) {
    final Map flowObject = (Map) object;

    final String id = (String) flowObject.get("id");
    final Boolean layedout = (Boolean) flowObject.get("layedout");
    final Boolean isEmbeddedFlow = (Boolean) flowObject.get("embeddedFlow");
    final Double azkabanFlowVersion = (Double) flowObject.get("azkabanFlowVersion");
    final String condition = (String) flowObject.get("condition");

    final Flow flow = new Flow(id);
    if (layedout != null) {
      flow.setLayedOut(layedout);
    }

    if (isEmbeddedFlow != null) {
      flow.setEmbeddedFlow(isEmbeddedFlow);
    }

    if (azkabanFlowVersion != null) {
      flow.setAzkabanFlowVersion(azkabanFlowVersion);
    }

    if (condition != null) {
      flow.setCondition(condition);
    }

    final int projId = (Integer) flowObject.get("project.id");
    flow.setProjectId(projId);

    final int version = (Integer) flowObject.get("version");
    flow.setVersion(version);

    // Loading projects
    final List propertiesList = (List) flowObject.get("props");
    final Map properties =
        loadPropertiesFromObject(propertiesList);
    flow.addAllFlowProperties(properties.values());

    // Loading nodes
    final List nodeList = (List) flowObject.get("nodes");
    final Map nodes = loadNodesFromObjects(nodeList);
    flow.addAllNodes(nodes.values());

    // Loading edges
    final List edgeList = (List) flowObject.get("edges");
    final List edges = loadEdgeFromObjects(edgeList, nodes);
    flow.addAllEdges(edges);

    final Map metadata =
        (Map) flowObject.get("metadata");

    if (metadata != null) {
      flow.setMetadata(metadata);
    }

    flow.failureEmail = (List) flowObject.get("failure.email");
    flow.successEmail = (List) flowObject.get("success.email");
    if (flowObject.containsKey("mailCreator")) {
      flow.mailCreator = flowObject.get("mailCreator").toString();
    }
    return flow;
  }

  private static Map loadNodesFromObjects(final List nodeList) {
    final Map nodeMap = new HashMap<>();

    for (final Object obj : nodeList) {
      final Node node = Node.fromObject(obj);
      nodeMap.put(node.getId(), node);
    }

    return nodeMap;
  }

  private static List loadEdgeFromObjects(final List edgeList,
      final Map nodes) {
    final List edgeResult = new ArrayList<>();

    for (final Object obj : edgeList) {
      final Edge edge = Edge.fromObject(obj);
      edgeResult.add(edge);
    }

    return edgeResult;
  }

  private static Map loadPropertiesFromObject(
      final List propertyObjectList) {
    final Map properties = new HashMap<>();

    for (final Object propObj : propertyObjectList) {
      final FlowProps prop = FlowProps.fromObject(propObj);
      properties.put(prop.getSource(), prop);
    }

    return properties;
  }

  public int getVersion() {
    return this.version;
  }

  public void setVersion(final int version) {
    this.version = version;
  }

  public void initialize() {
    if (this.startNodes == null) {
      this.startNodes = new ArrayList<>();
      this.endNodes = new ArrayList<>();
      for (final Node node : this.nodes.values()) {
        // If it doesn't have any incoming edges, its a start node
        if (!this.inEdges.containsKey(node.getId())) {
          this.startNodes.add(node);
        }

        // If it doesn't contain any outgoing edges, its an end node.
        if (!this.outEdges.containsKey(node.getId())) {
          this.endNodes.add(node);
        }
      }

      setLevelsAndEdgeNodes(new HashSet<>(this.startNodes), 0);
    }
  }

  private void setLevelsAndEdgeNodes(final Set levelNodes, final int level) {
    final Set nextLevelNodes = new HashSet<>();

    for (final Node node : levelNodes) {
      node.setLevel(level);

      final Set edges = this.outEdges.get(node.getId());
      if (edges != null) {
        edges.forEach(edge -> {
          edge.setSource(node);
          edge.setTarget(this.nodes.get(edge.getTargetId()));

          nextLevelNodes.add(edge.getTarget());
        });
      }
    }

    this.numLevels = level;

    if (!nextLevelNodes.isEmpty()) {
      setLevelsAndEdgeNodes(nextLevelNodes, level + 1);
    }
  }

  public Node getNode(final String nodeId) {
    return this.nodes.get(nodeId);
  }

  public List getSuccessEmails() {
    return this.successEmail;
  }

  public String getMailCreator() {
    return this.mailCreator;
  }

  public void setMailCreator(final String mailCreator) {
    this.mailCreator = mailCreator;
  }

  public List getFailureEmails() {
    return this.failureEmail;
  }

  public void addSuccessEmails(final Collection emails) {
    this.successEmail.addAll(emails);
  }

  public void addFailureEmails(final Collection emails) {
    this.failureEmail.addAll(emails);
  }

  public int getNumLevels() {
    return this.numLevels;
  }

  public List getStartNodes() {
    return this.startNodes;
  }

  public List getEndNodes() {
    return this.endNodes;
  }

  public Set getInEdges(final String id) {
    return this.inEdges.get(id);
  }

  public Set getOutEdges(final String id) {
    return this.outEdges.get(id);
  }

  public void addAllNodes(final Collection nodes) {
    for (final Node node : nodes) {
      addNode(node);
    }
  }

  public void addNode(final Node node) {
    this.nodes.put(node.getId(), node);
  }

  public void addAllFlowProperties(final Collection props) {
    for (final FlowProps prop : props) {
      this.flowProps.put(prop.getSource(), prop);
    }
  }

  public String getId() {
    return this.id;
  }

  public void addError(final String error) {
    if (this.errors == null) {
      this.errors = new ArrayList<>();
    }

    this.errors.add(error);
  }

  public List getErrors() {
    return this.errors;
  }

  public boolean hasErrors() {
    return this.errors != null && !this.errors.isEmpty();
  }

  public Collection getNodes() {
    return this.nodes.values();
  }

  public Collection getEdges() {
    return this.edges.values();
  }

  public void addAllEdges(final Collection edges) {
    for (final Edge edge : edges) {
      addEdge(edge);
    }
  }

  public void addEdge(final Edge edge) {
    final String source = edge.getSourceId();
    final String target = edge.getTargetId();

    if (edge.hasError()) {
      addError("Error on " + edge.getId() + ". " + edge.getError());
    }

    final Set sourceSet = getEdgeSet(this.outEdges, source);
    sourceSet.add(edge);

    final Set targetSet = getEdgeSet(this.inEdges, target);
    targetSet.add(edge);

    this.edges.put(edge.getId(), edge);
  }

  private Set getEdgeSet(final HashMap> map, final String id) {
    Set edges = map.get(id);
    if (edges == null) {
      edges = new HashSet<>();
      map.put(id, edges);
    }

    return edges;
  }

  public Map toObject() {
    final HashMap flowObj = new HashMap<>();
    flowObj.put("type", "flow");
    flowObj.put("id", getId());
    flowObj.put("project.id", this.projectId);
    flowObj.put("version", this.version);
    flowObj.put("props", objectizeProperties());
    flowObj.put("nodes", objectizeNodes());
    flowObj.put("edges", objectizeEdges());
    flowObj.put("failure.email", this.failureEmail);
    flowObj.put("success.email", this.successEmail);
    flowObj.put("mailCreator", this.mailCreator);
    flowObj.put("layedout", this.isLayedOut);
    flowObj.put("embeddedFlow", this.isEmbeddedFlow);
    flowObj.put("azkabanFlowVersion", this.azkabanFlowVersion);
    flowObj.put("condition", this.condition);

    if (this.errors != null) {
      flowObj.put("errors", this.errors);
    }

    if (this.metadata != null) {
      flowObj.put("metadata", this.metadata);
    }

    return flowObj;
  }

  private List objectizeProperties() {
    final ArrayList result = new ArrayList<>();
    for (final FlowProps props : this.flowProps.values()) {
      final Object objProps = props.toObject();
      result.add(objProps);
    }

    return result;
  }

  private List objectizeNodes() {
    final ArrayList result = new ArrayList<>();
    for (final Node node : getNodes()) {
      final Object nodeObj = node.toObject();
      result.add(nodeObj);
    }

    return result;
  }

  private List objectizeEdges() {
    final ArrayList result = new ArrayList<>();
    for (final Edge edge : getEdges()) {
      final Object edgeObj = edge.toObject();
      result.add(edgeObj);
    }

    return result;
  }

  public boolean isLayedOut() {
    return this.isLayedOut;
  }

  public void setLayedOut(final boolean layedOut) {
    this.isLayedOut = layedOut;
  }

  public boolean isEmbeddedFlow() {
    return this.isEmbeddedFlow;
  }

  public void setEmbeddedFlow(final boolean embeddedFlow) {
    this.isEmbeddedFlow = embeddedFlow;
  }

  public double getAzkabanFlowVersion() {
    return this.azkabanFlowVersion;
  }

  public void setAzkabanFlowVersion(final double azkabanFlowVersion) {
    this.azkabanFlowVersion = azkabanFlowVersion;
  }

  public String getCondition() {
    return this.condition;
  }

  public void setCondition(final String condition) {
    this.condition = condition;
  }

  public Map getMetadata() {
    if (this.metadata == null) {
      this.metadata = new HashMap<>();
    }
    return this.metadata;
  }

  public void setMetadata(final Map metadata) {
    this.metadata = metadata;
  }

  public Map getNodeMap() {
    return this.nodes;
  }

  public Map> getOutEdgeMap() {
    return this.outEdges;
  }

  public Map> getInEdgeMap() {
    return this.inEdges;
  }

  public FlowProps getFlowProps(final String propSource) {
    return this.flowProps.get(propSource);
  }

  public Map getAllFlowProps() {
    return this.flowProps;
  }

  public int getProjectId() {
    return this.projectId;
  }

  public void setProjectId(final int projectId) {
    this.projectId = projectId;
  }

}