blob: b3c34ec506c9439c76c840e4fa3a13f29fac7f5c [file] [log] [blame]
jaegonkim6a7b5242018-09-12 23:09:42 +09001/*
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 */
16package org.onosproject.workflow.cli;
17
18import com.fasterxml.jackson.databind.node.JsonNodeFactory;
Ray Milkeydf521292018-10-04 15:13:33 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
jaegonkim6a7b5242018-09-12 23:09:42 +090022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.workflow.api.DefaultWorkflowDescription;
24import org.onosproject.workflow.api.WorkflowException;
25import org.onosproject.workflow.api.WorkflowService;
26
27import java.util.Arrays;
28import java.util.Objects;
29
Ray Milkeydf521292018-10-04 15:13:33 -070030@Service
jaegonkim6a7b5242018-09-12 23:09:42 +090031@Command(scope = "onos", name = "workflow-test", description = "workflow test cli")
32public class WorkFlowTestCommand extends AbstractShellCommand {
33
Ray Milkeydf521292018-10-04 15:13:33 -070034 @Argument(index = 0, name = "cmd", description = "command(invoke)", required = true)
jaegonkim6a7b5242018-09-12 23:09:42 +090035 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 Milkeydf521292018-10-04 15:13:33 -070041 protected void doExecute() {
jaegonkim6a7b5242018-09-12 23:09:42 +090042 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}