Search in sources :

Example 6 with WorkflowException

use of org.onosproject.workflow.api.WorkflowException 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 7 with WorkflowException

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

the class OfOverlayWorkflowRegister method registerWorkflows.

/**
 * Registers workflows.
 */
private void registerWorkflows() {
    try {
        // registering class-loader
        workflowStore.registerLocal(this.getClass().getClassLoader());
        // registering new workflow definition
        URI uri = URI.create("of-overlay.workflow-nova");
        Workflow workflow = ImmutableListWorkflow.builder().id(uri).chain(Ovs.CreateOvsdbDevice.class.getName()).chain(Ovs.UpdateOvsVersion.class.getName()).chain(Ovs.UpdateBridgeId.class.getName()).chain(DefaultWorkletDescription.builder().name(Ovs.CreateBridge.class.getName()).staticDataModel(BRIDGE_NAME, "br-int").build()).chain(DefaultWorkletDescription.builder().name(Ovs.CreateBridge.class.getName()).staticDataModel(BRIDGE_NAME, "br-phy").build()).chain(Ovs.CreateOverlayBridgeVxlanPort.class.getName()).chain(Ovs.AddPhysicalPortsOnUnderlayBridge.class.getName()).chain(Ovs.ConfigureUnderlayBridgeLocalIp.class.getName()).build();
        workflowStore.register(workflow);
        // registering new workflow definition based on multi-event handling
        uri = URI.create("of-overlay.workflow-nova-multiEvent-test");
        workflow = ImmutableListWorkflow.builder().id(uri).chain(Ovs.CreateOvsdbDevice.class.getName()).chain(Ovs.UpdateOvsVersion.class.getName()).chain(Ovs.UpdateBridgeId.class.getName()).chain(Ovs.CreateOverlayBridgeMultiEvent.class.getName()).chain(Ovs.UpdateBridgeId.class.getName()).chain(DefaultWorkletDescription.builder().name(Ovs.CreateBridge.class.getName()).staticDataModel(BRIDGE_NAME, "br-phy").build()).chain(Ovs.CreateOverlayBridgeVxlanPort.class.getName()).chain(Ovs.AddPhysicalPortsOnUnderlayBridge.class.getName()).chain(Ovs.ConfigureUnderlayBridgeLocalIp.class.getName()).build();
        workflowStore.register(workflow);
        uri = URI.create("of-overlay.clean-workflow-nova");
        workflow = ImmutableListWorkflow.builder().id(uri).chain(Ovs.DeleteOverlayBridgeConfig.class.getName()).chain(Ovs.RemoveOverlayBridgeOfDevice.class.getName()).chain(Ovs.DeleteUnderlayBridgeConfig.class.getName()).chain(Ovs.RemoveUnderlayBridgeOfDevice.class.getName()).chain(Ovs.RemoveOvsdbDevice.class.getName()).build();
        workflowStore.register(workflow);
        uri = URI.create("of-overlay.clean-workflow-nova-waitAll-Bridge-Del");
        workflow = ImmutableListWorkflow.builder().id(uri).chain(Ovs.DeleteOverlayBridgeConfig.class.getName()).chain(Ovs.DeleteUnderlayBridgeConfig.class.getName()).chain(Ovs.RemoveBridgeOfDevice.class.getName()).chain(Ovs.RemoveOvsdbDevice.class.getName()).build();
        workflowStore.register(workflow);
        uri = URI.create("of-overlay.workflow-ovs-leaf");
        workflow = ImmutableListWorkflow.builder().id(uri).chain(Ovs.CreateOvsdbDevice.class.getName()).chain(Ovs.UpdateOvsVersion.class.getName()).chain(Ovs.UpdateBridgeId.class.getName()).chain(DefaultWorkletDescription.builder().name(Ovs.CreateBridge.class.getName()).staticDataModel(BRIDGE_NAME, "br-phy").build()).chain(Ovs.AddPhysicalPortsOnUnderlayBridge.class.getName()).build();
        workflowStore.register(workflow);
        uri = URI.create("of-overlay.workflow-ovs-spine");
        workflow = ImmutableListWorkflow.builder().id(uri).chain(Ovs.CreateOvsdbDevice.class.getName()).chain(Ovs.UpdateOvsVersion.class.getName()).chain(Ovs.UpdateBridgeId.class.getName()).chain(DefaultWorkletDescription.builder().name(Ovs.CreateBridge.class.getName()).staticDataModel(BRIDGE_NAME, "br-phy").build()).chain(Ovs.AddPhysicalPortsOnUnderlayBridge.class.getName()).build();
        workflowStore.register(workflow);
        deviceService.addListener(event -> {
            // trigger EventTask for DeviceEvent
            eventMapTriggerExecutor.submit(() -> workflowExecutionService.eventMapTrigger(event, // event hint supplier
            (ev) -> {
                if (ev == null || ev.subject() == null) {
                    return null;
                }
                String hint = event.subject().id().toString();
                log.debug("hint: {}", hint);
                return hint;
            }));
        });
    } catch (WorkflowException e) {
        e.printStackTrace();
    }
}
Also used : Logger(org.slf4j.Logger) WorkflowException(org.onosproject.workflow.api.WorkflowException) Deactivate(org.osgi.service.component.annotations.Deactivate) DeviceService(org.onosproject.net.device.DeviceService) LoggerFactory(org.slf4j.LoggerFactory) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) WorkflowExecutionService(org.onosproject.workflow.api.WorkflowExecutionService) WorkflowStore(org.onosproject.workflow.api.WorkflowStore) Workflow(org.onosproject.workflow.api.Workflow) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) Component(org.osgi.service.component.annotations.Component) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) DefaultWorkletDescription(org.onosproject.workflow.api.DefaultWorkletDescription) WorkplaceStore(org.onosproject.workflow.api.WorkplaceStore) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Activate(org.osgi.service.component.annotations.Activate) URI(java.net.URI) Reference(org.osgi.service.component.annotations.Reference) ImmutableListWorkflow(org.onosproject.workflow.api.ImmutableListWorkflow) WorkflowException(org.onosproject.workflow.api.WorkflowException) Workflow(org.onosproject.workflow.api.Workflow) ImmutableListWorkflow(org.onosproject.workflow.api.ImmutableListWorkflow) URI(java.net.URI)

Example 8 with WorkflowException

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

the class WorkflowManager method getJsonNodeDataError.

private Optional<String> getJsonNodeDataError(JsonNode dataNode, Worklet worklet, Field field, String path, boolean isOptional) throws WorkflowException {
    // Checking the existence of path in dataNode
    JsonNode pathNode = dataNode.get(path);
    if (Objects.isNull(pathNode) || pathNode instanceof MissingNode) {
        if (isOptional) {
            return Optional.empty();
        } else {
            return Optional.of("data doesn't have '" + path + "' in worklet<" + worklet.tag() + ">");
        }
    }
    // Checking the type of path
    JsonNodeType type = pathNode.getNodeType();
    if (Objects.isNull(type)) {
        throw new WorkflowException("Invalid type for " + pathNode);
    }
    switch(type) {
        case NUMBER:
            if (!(field.getType().isAssignableFrom(Integer.class))) {
                return Optional.of("'" + path + "<NUMBER>' cannot be assigned to " + field.getName() + "<" + field.getType() + "> in worklet<" + worklet.tag() + ">");
            }
            break;
        case STRING:
            if (!(field.getType().isAssignableFrom(String.class))) {
                return Optional.of("'" + path + "<STRING>' cannot be assigned to " + field.getName() + "<" + field.getType() + "> in worklet<" + worklet.tag() + ">");
            }
            break;
        case BOOLEAN:
            if (!(field.getType().isAssignableFrom(Boolean.class))) {
                return Optional.of("'" + path + "<BOOLEAN>' cannot be assigned to " + field.getName() + "<" + field.getType() + "> in worklet<" + worklet.tag() + ">");
            }
            break;
        case OBJECT:
            if (!(field.getType().isAssignableFrom(JsonNode.class))) {
                return Optional.of("'" + path + "<OBJECT>' cannot be assigned to " + field.getName() + "<" + field.getType() + "> in worklet<" + worklet.tag() + ">");
            }
            break;
        case ARRAY:
            if (!(field.getType().isAssignableFrom(ArrayNode.class))) {
                return Optional.of("'" + path + "<ARRAY>' cannot be assigned to " + field.getName() + "<" + field.getType() + "> in worklet<" + worklet.tag() + ">");
            }
            break;
        default:
            return Optional.of("'" + path + "<" + type + ">' is not supported");
    }
    return Optional.empty();
}
Also used : JsonNodeType(com.fasterxml.jackson.databind.node.JsonNodeType) WorkflowException(org.onosproject.workflow.api.WorkflowException) JsonNode(com.fasterxml.jackson.databind.JsonNode) MissingNode(com.fasterxml.jackson.databind.node.MissingNode)

Example 9 with WorkflowException

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

the class NetconfAccessInfo method valueOf.

/**
 * Builds NetconfAccessInfo from json.
 * @param root json root node for NetconfAccessinfo
 * @return NETCONF access information
 * @throws WorkflowException workflow exception
 */
public static NetconfAccessInfo valueOf(JsonNode root) throws WorkflowException {
    JsonNode node = root.at(ptr(REMOTE_IP));
    if (node == null || !(node instanceof TextNode)) {
        throw new WorkflowException("invalid remoteIp for " + root);
    }
    IpAddress remoteIp = IpAddress.valueOf(node.asText());
    node = root.at(ptr(PORT));
    if (node == null || !(node instanceof NumericNode)) {
        throw new WorkflowException("invalid port for " + root);
    }
    TpPort tpPort = TpPort.tpPort(node.asInt());
    node = root.at(ptr(USER));
    if (node == null || !(node instanceof TextNode)) {
        throw new WorkflowException("invalid user for " + root);
    }
    String strUser = node.asText();
    node = root.at(ptr(PASSWORD));
    if (node == null || !(node instanceof TextNode)) {
        throw new WorkflowException("invalid password for " + root);
    }
    String strPassword = node.asText();
    return new NetconfAccessInfo(remoteIp, tpPort, strUser, strPassword);
}
Also used : WorkflowException(org.onosproject.workflow.api.WorkflowException) JsonNode(com.fasterxml.jackson.databind.JsonNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) IpAddress(org.onlab.packet.IpAddress) TpPort(org.onlab.packet.TpPort) NumericNode(com.fasterxml.jackson.databind.node.NumericNode)

Example 10 with WorkflowException

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

the class WorkFlowTestCommand method defineInvalidWorkflow.

private void defineInvalidWorkflow() {
    WorkflowService service = get(WorkflowService.class);
    try {
        URI uri = URI.create("sample.workflow-invalid-datamodel-type");
        Workflow workflow = ImmutableListWorkflow.builder().id(uri).chain(SampleWorkflow.SampleWorklet5.class.getName()).chain(SampleWorkflow.SampleWorklet6.class.getName()).build();
        service.register(workflow);
    } catch (WorkflowException e) {
        error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace()));
    }
}
Also used : WorkflowService(org.onosproject.workflow.api.WorkflowService) WorkflowException(org.onosproject.workflow.api.WorkflowException) SampleWorkflow(org.onosproject.workflow.impl.example.SampleWorkflow) Workflow(org.onosproject.workflow.api.Workflow) SampleWorkflow(org.onosproject.workflow.impl.example.SampleWorkflow) ImmutableListWorkflow(org.onosproject.workflow.api.ImmutableListWorkflow) URI(java.net.URI)

Aggregations

WorkflowException (org.onosproject.workflow.api.WorkflowException)24 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 Workflow (org.onosproject.workflow.api.Workflow)9 WorkflowContext (org.onosproject.workflow.api.WorkflowContext)7 Worklet (org.onosproject.workflow.api.Worklet)7 ProgramCounter (org.onosproject.workflow.api.ProgramCounter)5 SystemWorkflowContext (org.onosproject.workflow.api.SystemWorkflowContext)5 WorkflowService (org.onosproject.workflow.api.WorkflowService)5 StorageException (org.onosproject.store.service.StorageException)4 NumericNode (com.fasterxml.jackson.databind.node.NumericNode)3 TextNode (com.fasterxml.jackson.databind.node.TextNode)3 ArrayList (java.util.ArrayList)3 IpAddress (org.onlab.packet.IpAddress)3 TpPort (org.onlab.packet.TpPort)3 WorkletDescription (org.onosproject.workflow.api.WorkletDescription)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 MissingNode (com.fasterxml.jackson.databind.node.MissingNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Annotation (java.lang.annotation.Annotation)2 Field (java.lang.reflect.Field)2