Search in sources :

Example 1 with JsonDataModel

use of org.onosproject.workflow.api.JsonDataModel in project onos by opennetworkinglab.

the class WorkflowManager method checkWorkflowDataModelSchema.

/**
 * Checks the schema of workflow data.
 *
 * @param workflow workflow
 * @param worklowDescJson jsonNode
 * @throws WorkflowException workflow exception
 */
private void checkWorkflowDataModelSchema(Workflow workflow, JsonNode worklowDescJson) throws WorkflowException {
    List<String> errors = new ArrayList<>();
    JsonNode dataNode = worklowDescJson.get("data");
    if (Objects.isNull(dataNode) || dataNode instanceof MissingNode) {
        errors.add("workflow description json does not have 'data'");
        throw new WorkflowDataModelException(workflow.id(), worklowDescJson, errors);
    }
    for (ProgramCounter pc : workflow.getProgram()) {
        Worklet worklet = workflow.getWorkletInstance(pc);
        if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) {
            continue;
        }
        Class cls = worklet.getClass();
        for (Field field : cls.getDeclaredFields()) {
            if (field.isSynthetic()) {
                continue;
            }
            for (Annotation annotation : field.getAnnotations()) {
                if (!(annotation instanceof JsonDataModel)) {
                    continue;
                }
                JsonDataModel jsonDataModel = (JsonDataModel) annotation;
                Matcher matcher = Pattern.compile("(\\w+)").matcher(jsonDataModel.path());
                if (!matcher.find()) {
                    throw new WorkflowException("Invalid Json Data Model Path(" + jsonDataModel.path() + ") in " + worklet.tag());
                }
                String path = matcher.group(1);
                Optional<String> optError = getJsonNodeDataError(dataNode, worklet, field, path, jsonDataModel.optional());
                if (optError.isPresent()) {
                    errors.add(optError.get());
                }
            }
        }
    }
    if (!errors.isEmpty()) {
        throw new WorkflowDataModelException(workflow.id(), worklowDescJson, errors);
    }
}
Also used : Matcher(java.util.regex.Matcher) WorkflowException(org.onosproject.workflow.api.WorkflowException) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) MissingNode(com.fasterxml.jackson.databind.node.MissingNode) Worklet(org.onosproject.workflow.api.Worklet) WorkflowDataModelException(org.onosproject.workflow.api.WorkflowDataModelException) Annotation(java.lang.annotation.Annotation) Field(java.lang.reflect.Field) JsonDataModel(org.onosproject.workflow.api.JsonDataModel) ProgramCounter(org.onosproject.workflow.api.ProgramCounter)

Example 2 with JsonDataModel

use of org.onosproject.workflow.api.JsonDataModel in project onos by opennetworkinglab.

the class WorkflowManager method checkWorkflow.

/**
 * Checks the validity of workflow definition.
 * @param workflow workflow to be checked
 * @throws WorkflowException workflow exception
 */
private void checkWorkflow(Workflow workflow) throws WorkflowException {
    Map<String, WorkletDataModelFieldDesc> descMap = new HashMap<>();
    List<String> errors = new ArrayList<>();
    for (ProgramCounter pc : workflow.getProgram()) {
        Worklet worklet = workflow.getWorkletInstance(pc);
        if (Worklet.Common.COMPLETED.equals(worklet) || Worklet.Common.INIT.equals(worklet)) {
            continue;
        }
        Class cls = worklet.getClass();
        for (Field field : cls.getDeclaredFields()) {
            if (field.isSynthetic()) {
                continue;
            }
            for (Annotation annotation : field.getAnnotations()) {
                if (!(annotation instanceof JsonDataModel)) {
                    continue;
                }
                JsonDataModel jsonDataModel = (JsonDataModel) annotation;
                Matcher matcher = Pattern.compile("(\\w+)").matcher(jsonDataModel.path());
                if (!matcher.find()) {
                    throw new WorkflowException("Invalid Json Data Model Path(" + jsonDataModel.path() + ") in " + worklet.tag());
                }
                String path = matcher.group(1);
                WorkletDataModelFieldDesc desc = new WorkletDataModelFieldDesc(pc.workletType(), path, field.getType(), jsonDataModel.optional());
                WorkletDataModelFieldDesc existing = descMap.get(path);
                if (Objects.isNull(existing)) {
                    descMap.put(path, desc);
                } else {
                    if (!desc.hasSameAttributes(existing)) {
                        errors.add("" + desc + " is conflicted with " + existing + " in workflow " + workflow.id());
                    }
                }
            }
        }
    }
    if (!errors.isEmpty()) {
        throw new WorkflowDefinitionException(workflow.id(), errors);
    }
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) WorkflowException(org.onosproject.workflow.api.WorkflowException) ArrayList(java.util.ArrayList) Worklet(org.onosproject.workflow.api.Worklet) Annotation(java.lang.annotation.Annotation) WorkflowDefinitionException(org.onosproject.workflow.api.WorkflowDefinitionException) Field(java.lang.reflect.Field) JsonDataModel(org.onosproject.workflow.api.JsonDataModel) ProgramCounter(org.onosproject.workflow.api.ProgramCounter)

Aggregations

Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 JsonDataModel (org.onosproject.workflow.api.JsonDataModel)2 ProgramCounter (org.onosproject.workflow.api.ProgramCounter)2 WorkflowException (org.onosproject.workflow.api.WorkflowException)2 Worklet (org.onosproject.workflow.api.Worklet)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 MissingNode (com.fasterxml.jackson.databind.node.MissingNode)1 HashMap (java.util.HashMap)1 WorkflowDataModelException (org.onosproject.workflow.api.WorkflowDataModelException)1 WorkflowDefinitionException (org.onosproject.workflow.api.WorkflowDefinitionException)1