jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.workflow.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.workflow.api.DefaultWorkflowDescription; |
| 23 | import org.onosproject.workflow.api.WorkflowException; |
| 24 | import org.onosproject.workflow.api.WorkflowService; |
| 25 | |
| 26 | import java.util.Arrays; |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | @Command(scope = "onos", name = "workflow-test", description = "workflow test cli") |
| 30 | public class WorkFlowTestCommand extends AbstractShellCommand { |
| 31 | |
| 32 | @Argument (index = 0, name = "cmd", description = "command(invoke)", required = true) |
| 33 | private String cmd = null; |
| 34 | |
| 35 | @Argument (index = 1, name = "number", description = "number of test", required = true) |
| 36 | private String number = null; |
| 37 | |
| 38 | @Override |
| 39 | protected void execute() { |
| 40 | if (Objects.isNull(cmd)) { |
| 41 | error("invalid cmd parameter"); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | switch (cmd) { |
| 46 | case "invoke": |
| 47 | if (Objects.isNull(number)) { |
| 48 | error("invalid number of test"); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | int num; |
| 53 | try { |
| 54 | num = Integer.parseInt(number); |
| 55 | } catch (Exception e) { |
| 56 | error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace())); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | test(num); |
| 61 | break; |
| 62 | default: |
| 63 | print("Unsupported cmd: " + cmd); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Invokes workflow. |
| 69 | * @param workflowId workflow id |
| 70 | * @param workplaceName workplace name |
| 71 | */ |
| 72 | private void invoke(String workflowId, String workplaceName) { |
| 73 | |
| 74 | WorkflowService service = get(WorkflowService.class); |
| 75 | DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder() |
| 76 | .workplaceName(workplaceName) |
| 77 | .id(workflowId) |
| 78 | .data(JsonNodeFactory.instance.objectNode()) |
| 79 | .build(); |
| 80 | try { |
| 81 | service.invokeWorkflow(wfDesc); |
| 82 | } catch (WorkflowException e) { |
| 83 | error(e.getMessage() + "trace: " + Arrays.asList(e.getStackTrace())); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Workflow invoke test. |
| 89 | * @param num the number of workflow to test |
| 90 | */ |
| 91 | private void test(int num) { |
| 92 | for (int i = 0; i <= num; i++) { |
| 93 | String wpName = "test-" + i; |
| 94 | invoke("sample.workflow-0", wpName); |
| 95 | } |
| 96 | } |
| 97 | } |