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; |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.workflow.api.DefaultWorkflowDescription; |
| 24 | import org.onosproject.workflow.api.WorkflowException; |
| 25 | import org.onosproject.workflow.api.WorkflowService; |
| 26 | |
| 27 | import java.util.Arrays; |
| 28 | import java.util.Objects; |
| 29 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 30 | @Service |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 31 | @Command(scope = "onos", name = "workflow-test", description = "workflow test cli") |
| 32 | public class WorkFlowTestCommand extends AbstractShellCommand { |
| 33 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 34 | @Argument(index = 0, name = "cmd", description = "command(invoke)", required = true) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 35 | private String cmd = null; |
| 36 | |
| 37 | @Argument (index = 1, name = "number", description = "number of test", required = true) |
| 38 | private String number = null; |
| 39 | |
| 40 | @Override |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 41 | protected void doExecute() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 42 | if (Objects.isNull(cmd)) { |
| 43 | error("invalid cmd parameter"); |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | switch (cmd) { |
| 48 | case "invoke": |
| 49 | if (Objects.isNull(number)) { |
| 50 | error("invalid number of test"); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | int num; |
| 55 | try { |
| 56 | num = Integer.parseInt(number); |
| 57 | } catch (Exception e) { |
| 58 | error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace())); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | test(num); |
| 63 | break; |
| 64 | default: |
| 65 | print("Unsupported cmd: " + cmd); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Invokes workflow. |
| 71 | * @param workflowId workflow id |
| 72 | * @param workplaceName workplace name |
| 73 | */ |
| 74 | private void invoke(String workflowId, String workplaceName) { |
| 75 | |
| 76 | WorkflowService service = get(WorkflowService.class); |
| 77 | DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder() |
| 78 | .workplaceName(workplaceName) |
| 79 | .id(workflowId) |
| 80 | .data(JsonNodeFactory.instance.objectNode()) |
| 81 | .build(); |
| 82 | try { |
| 83 | service.invokeWorkflow(wfDesc); |
| 84 | } catch (WorkflowException e) { |
| 85 | error(e.getMessage() + "trace: " + Arrays.asList(e.getStackTrace())); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Workflow invoke test. |
| 91 | * @param num the number of workflow to test |
| 92 | */ |
| 93 | private void test(int num) { |
| 94 | for (int i = 0; i <= num; i++) { |
| 95 | String wpName = "test-" + i; |
| 96 | invoke("sample.workflow-0", wpName); |
| 97 | } |
| 98 | } |
| 99 | } |