blob: 3f96ba9fc1f7dd0b8f015d9b229fc0a75fba72d4 [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.WorkflowService;
25import org.onosproject.workflow.api.WorkflowException;
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", description = "workflow cli")
32public class WorkFlowCommand 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 = "id", description = "workflow id(URI)", required = true)
38 private String id = null;
39
40 @Argument (index = 2, name = "name", description = "workplace name", required = true)
41 private String name = null;
42
43 @Override
Ray Milkeydf521292018-10-04 15:13:33 -070044 protected void doExecute() {
jaegonkim6a7b5242018-09-12 23:09:42 +090045 if (Objects.isNull(cmd)) {
46 error("invalid cmd parameter");
47 return;
48 }
49
50 switch (cmd) {
51 case "invoke":
52 if (Objects.isNull(id)) {
53 error("invalid workflow id parameter");
54 return;
55 }
56
57 if (Objects.isNull(name)) {
58 error("invalid workplace name parameter");
59 return;
60 }
61
62 invoke(id, name);
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}