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