-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modifies use case template format and adds graph validation when prov…
…isioning (#119) * Simplifying Template format, removing operations, resources created, user outputs Signed-off-by: Joshua Palis <jpalis@amazon.com> * Initial commit, modifies use case template to seperate workflow inputs into previous_node_inputs and user_inputs, adds graph validation after topologically sorting a workflow into a list of ProcessNode Signed-off-by: Joshua Palis <jpalis@amazon.com> * Adding tests Signed-off-by: Joshua Palis <jpalis@amazon.com> * Adding validate graph test Signed-off-by: Joshua Palis <jpalis@amazon.com> * Addressing PR comments, moving sorting/validating prior to executing async, adding success test case for graph validation Signed-off-by: Joshua Palis <jpalis@amazon.com> * Adding javadocs Signed-off-by: Joshua Palis <jpalis@amazon.com> * Moving validation prior to updating workflow state to provisioning Signed-off-by: Joshua Palis <jpalis@amazon.com> * Addressing PR comments Part 1 Signed-off-by: Joshua Palis <jpalis@amazon.com> * Addressing PR comments Part 2 : Moving field names to common value class and using constants Signed-off-by: Joshua Palis <jpalis@amazon.com> * Adding definition for noop workflow step Signed-off-by: Joshua Palis <jpalis@amazon.com> * Addressing PR comments Part 3 Signed-off-by: Joshua Palis <jpalis@amazon.com> --------- Signed-off-by: Joshua Palis <jpalis@amazon.com>
- Loading branch information
Showing
28 changed files
with
672 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.model; | ||
|
||
import org.opensearch.core.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
/** | ||
* This represents the an object of workflow steps json which maps each step to expected inputs and outputs | ||
*/ | ||
public class WorkflowStepValidator { | ||
|
||
/** Inputs field name */ | ||
private static final String INPUTS_FIELD = "inputs"; | ||
/** Outputs field name */ | ||
private static final String OUTPUTS_FIELD = "outputs"; | ||
|
||
private List<String> inputs; | ||
private List<String> outputs; | ||
|
||
/** | ||
* Intantiate the object representing a Workflow Step validator | ||
* @param inputs the workflow step inputs | ||
* @param outputs the workflow step outputs | ||
*/ | ||
public WorkflowStepValidator(List<String> inputs, List<String> outputs) { | ||
this.inputs = inputs; | ||
this.outputs = outputs; | ||
} | ||
|
||
/** | ||
* Parse raw json content into a WorkflowStepValidator instance | ||
* @param parser json based content parser | ||
* @return an instance of the WorkflowStepValidator | ||
* @throws IOException if the content cannot be parsed correctly | ||
*/ | ||
public static WorkflowStepValidator parse(XContentParser parser) throws IOException { | ||
List<String> parsedInputs = new ArrayList<>(); | ||
List<String> parsedOutputs = new ArrayList<>(); | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String fieldName = parser.currentName(); | ||
parser.nextToken(); | ||
switch (fieldName) { | ||
case INPUTS_FIELD: | ||
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
parsedInputs.add(parser.text()); | ||
} | ||
break; | ||
case OUTPUTS_FIELD: | ||
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
parsedOutputs.add(parser.text()); | ||
} | ||
break; | ||
default: | ||
throw new IOException("Unable to parse field [" + fieldName + "] in a WorkflowStepValidator object."); | ||
} | ||
} | ||
return new WorkflowStepValidator(parsedInputs, parsedOutputs); | ||
} | ||
|
||
/** | ||
* Get the required inputs | ||
* @return the inputs | ||
*/ | ||
public List<String> getInputs() { | ||
return List.copyOf(inputs); | ||
} | ||
|
||
/** | ||
* Get the required outputs | ||
* @return the outputs | ||
*/ | ||
public List<String> getOutputs() { | ||
return List.copyOf(outputs); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/org/opensearch/flowframework/model/WorkflowValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
package org.opensearch.flowframework.model; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.google.common.io.Resources; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
import org.opensearch.flowframework.util.ParseUtils; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
/** | ||
* This represents the workflow steps json which maps each step to expected inputs and outputs | ||
*/ | ||
public class WorkflowValidator { | ||
|
||
private Map<String, WorkflowStepValidator> workflowStepValidators; | ||
|
||
/** | ||
* Intantiate the object representing a Workflow validator | ||
* @param workflowStepValidators a map of {@link WorkflowStepValidator} | ||
*/ | ||
public WorkflowValidator(Map<String, WorkflowStepValidator> workflowStepValidators) { | ||
this.workflowStepValidators = workflowStepValidators; | ||
} | ||
|
||
/** | ||
* Parse raw json content into a WorkflowValidator instance | ||
* @param parser json based content parser | ||
* @return an instance of the WorkflowValidator | ||
* @throws IOException if the content cannot be parsed correctly | ||
*/ | ||
public static WorkflowValidator parse(XContentParser parser) throws IOException { | ||
|
||
Map<String, WorkflowStepValidator> workflowStepValidators = new HashMap<>(); | ||
|
||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser); | ||
while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
String type = parser.currentName(); | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
workflowStepValidators.put(type, WorkflowStepValidator.parse(parser)); | ||
} | ||
return new WorkflowValidator(workflowStepValidators); | ||
} | ||
|
||
/** | ||
* Parse a workflow step JSON file into a WorkflowValidator object | ||
* | ||
* @param file the file name of the workflow step json | ||
* @return A {@link WorkflowValidator} represented by the JSON | ||
* @throws IOException on failure to read and parse the json file | ||
*/ | ||
public static WorkflowValidator parse(String file) throws IOException { | ||
URL url = WorkflowValidator.class.getClassLoader().getResource(file); | ||
String json = Resources.toString(url, Charsets.UTF_8); | ||
return parse(ParseUtils.jsonToParser(json)); | ||
} | ||
|
||
/** | ||
* Get the map of WorkflowStepValidators | ||
* @return the map of WorkflowStepValidators | ||
*/ | ||
public Map<String, WorkflowStepValidator> getWorkflowStepValidators() { | ||
return Map.copyOf(this.workflowStepValidators); | ||
} | ||
|
||
} |
Oops, something went wrong.