blob: 8d13508cb9a832fe199c995e7e80e4d73765436e [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;
jaegonkime0f45b52018-10-09 20:23:26 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeydf521292018-10-04 15:13:33 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
jaegonkim6a7b5242018-09-12 23:09:42 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.workflow.api.DefaultWorkflowDescription;
25import org.onosproject.workflow.api.WorkflowException;
26import org.onosproject.workflow.api.WorkflowService;
27
28import java.util.Arrays;
29import java.util.Objects;
30
Ray Milkeydf521292018-10-04 15:13:33 -070031@Service
jaegonkim6a7b5242018-09-12 23:09:42 +090032@Command(scope = "onos", name = "workflow-test", description = "workflow test cli")
33public class WorkFlowTestCommand extends AbstractShellCommand {
34
Ray Milkeydf521292018-10-04 15:13:33 -070035 @Argument(index = 0, name = "cmd", description = "command(invoke)", required = true)
jaegonkim6a7b5242018-09-12 23:09:42 +090036 private String cmd = null;
37
jaegonkime0f45b52018-10-09 20:23:26 +090038 @Argument(index = 1, name = "number", description = "number of test", required = true)
jaegonkim6a7b5242018-09-12 23:09:42 +090039 private String number = null;
40
41 @Override
Ray Milkeydf521292018-10-04 15:13:33 -070042 protected void doExecute() {
jaegonkim6a7b5242018-09-12 23:09:42 +090043 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);
jaegonkime0f45b52018-10-09 20:23:26 +090078
79 ObjectNode dataModel = JsonNodeFactory.instance.objectNode();
80 dataModel.put("count", 0);
81
jaegonkim6a7b5242018-09-12 23:09:42 +090082 try {
jaegonkime0f45b52018-10-09 20:23:26 +090083 DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder()
84 .workplaceName(workplaceName)
85 .id(workflowId)
86 .data(dataModel)
87 .build();
jaegonkim6a7b5242018-09-12 23:09:42 +090088 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);
jaegonkime0f45b52018-10-09 20:23:26 +0900102 invoke("sample.workflow-1", wpName);
103 invoke("sample.workflow-2", wpName);
jaegonkim6a7b5242018-09-12 23:09:42 +0900104 }
105 }
106}