Search in sources :

Example 1 with NodeValue

use of co.cask.cdap.api.workflow.NodeValue in project cdap by caskdata.

the class WorkflowHttpHandler method getWorkflowToken.

@GET
@Path("/apps/{app-id}/workflows/{workflow-id}/runs/{run-id}/token")
public void getWorkflowToken(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("workflow-id") String workflowId, @PathParam("run-id") String runId, @QueryParam("scope") @DefaultValue("user") String scope, @QueryParam("key") @DefaultValue("") String key) throws NotFoundException {
    WorkflowToken workflowToken = getWorkflowToken(namespaceId, appId, workflowId, runId);
    WorkflowToken.Scope tokenScope = WorkflowToken.Scope.valueOf(scope.toUpperCase());
    WorkflowTokenDetail workflowTokenDetail = WorkflowTokenDetail.of(workflowToken.getAll(tokenScope));
    Type workflowTokenDetailType = new TypeToken<WorkflowTokenDetail>() {
    }.getType();
    if (key.isEmpty()) {
        responder.sendJson(HttpResponseStatus.OK, workflowTokenDetail, workflowTokenDetailType, GSON);
        return;
    }
    List<NodeValue> nodeValueEntries = workflowToken.getAll(key, tokenScope);
    if (nodeValueEntries.isEmpty()) {
        throw new NotFoundException(key);
    }
    responder.sendJson(HttpResponseStatus.OK, WorkflowTokenDetail.of(ImmutableMap.of(key, nodeValueEntries)), workflowTokenDetailType, GSON);
}
Also used : NodeValue(co.cask.cdap.api.workflow.NodeValue) ProgramType(co.cask.cdap.proto.ProgramType) Type(java.lang.reflect.Type) SchedulableProgramType(co.cask.cdap.api.schedule.SchedulableProgramType) WorkflowToken(co.cask.cdap.api.workflow.WorkflowToken) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) InstanceNotFoundException(co.cask.cdap.api.dataset.InstanceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) WorkflowTokenDetail(co.cask.cdap.proto.WorkflowTokenDetail) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with NodeValue

use of co.cask.cdap.api.workflow.NodeValue in project cdap by caskdata.

the class BasicWorkflowToken method put.

private void put(String key, Value value, Scope scope) {
    if (!putAllowed) {
        String msg = String.format("Failed to put key '%s' from node '%s' in the WorkflowToken. Put operation is not " + "allowed from the Mapper and Reducer classes and from Spark executor.", key, nodeName);
        throw new UnsupportedOperationException(msg);
    }
    Preconditions.checkNotNull(key, "Null key cannot be added in the WorkflowToken.");
    Preconditions.checkNotNull(value, String.format("Null value provided for the key '%s'.", key));
    Preconditions.checkNotNull(value.toString(), String.format("Null value provided for the key '%s'.", key));
    Preconditions.checkState(nodeName != null, "nodeName cannot be null.");
    List<NodeValue> nodeValueList = tokenValueMap.get(scope).get(key);
    if (nodeValueList == null) {
        nodeValueList = Lists.newArrayList();
        tokenValueMap.get(scope).put(key, nodeValueList);
    }
    NodeValue nodeValueToAddUpdate = new NodeValue(nodeName, value);
    // In that case replace that entry with the new one
    for (int i = 0; i < nodeValueList.size(); i++) {
        NodeValue existingNodeValue = nodeValueList.get(i);
        if (existingNodeValue.getNodeName().equals(nodeName)) {
            addOrUpdate(key, nodeValueToAddUpdate, nodeValueList, i);
            return;
        }
    }
    addOrUpdate(key, nodeValueToAddUpdate, nodeValueList, -1);
}
Also used : NodeValue(co.cask.cdap.api.workflow.NodeValue)

Example 3 with NodeValue

use of co.cask.cdap.api.workflow.NodeValue in project cdap by caskdata.

the class WorkflowTokenDetail method of.

/**
   * Constructs a {@link WorkflowTokenDetail} by flattening the {@link Value} from the output of
   * {@link WorkflowToken#getAll(WorkflowToken.Scope)}.
   *
   * @param data the workflow token data to flatten
   * @return {@link WorkflowTokenDetail} from the specified workflow token data
   */
public static WorkflowTokenDetail of(Map<String, List<NodeValue>> data) {
    Map<String, List<NodeValueDetail>> converted = new HashMap<>();
    for (Map.Entry<String, List<NodeValue>> entry : data.entrySet()) {
        List<NodeValueDetail> convertedList = new ArrayList<>();
        for (NodeValue nodeValue : entry.getValue()) {
            convertedList.add(new NodeValueDetail(nodeValue.getNodeName(), nodeValue.getValue().toString()));
        }
        converted.put(entry.getKey(), convertedList);
    }
    return new WorkflowTokenDetail(converted);
}
Also used : NodeValue(co.cask.cdap.api.workflow.NodeValue) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

NodeValue (co.cask.cdap.api.workflow.NodeValue)3 InstanceNotFoundException (co.cask.cdap.api.dataset.InstanceNotFoundException)1 SchedulableProgramType (co.cask.cdap.api.schedule.SchedulableProgramType)1 WorkflowToken (co.cask.cdap.api.workflow.WorkflowToken)1 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)1 ProgramType (co.cask.cdap.proto.ProgramType)1 WorkflowTokenDetail (co.cask.cdap.proto.WorkflowTokenDetail)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1