blob: 19856b37dbfd4f751d0033871d3ce35bb99aa7ab [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;
jaegonkimfbf61432019-03-16 09:37:53 +090021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeydf521292018-10-04 15:13:33 -070022import 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;
jaegonkime0f45b52018-10-09 20:23:26 +090025import org.onosproject.workflow.api.WorkflowContext;
26import org.onosproject.workflow.api.WorkflowExecutionService;
jaegonkim6a7b5242018-09-12 23:09:42 +090027import org.onosproject.workflow.api.WorkflowService;
28import org.onosproject.workflow.api.WorkflowException;
jaegonkime0f45b52018-10-09 20:23:26 +090029import org.onosproject.workflow.api.WorkplaceStore;
jaegonkim6a7b5242018-09-12 23:09:42 +090030
jaegonkim6a7b5242018-09-12 23:09:42 +090031import java.util.Objects;
32
Ray Milkeydf521292018-10-04 15:13:33 -070033@Service
jaegonkim6a7b5242018-09-12 23:09:42 +090034@Command(scope = "onos", name = "workflow", description = "workflow cli")
35public class WorkFlowCommand extends AbstractShellCommand {
36
jaegonkimfbf61432019-03-16 09:37:53 +090037 static final String INVOKE = "invoke";
38 static final String EVAL = "eval";
39
40
41
42 @Argument(index = 0, name = "cmd",
43 description = "command(" + INVOKE + "|" + EVAL + "eval)",
44 required = true)
45 @Completion(WorkFlowCompleter.class)
jaegonkim6a7b5242018-09-12 23:09:42 +090046 private String cmd = null;
47
jaegonkimfbf61432019-03-16 09:37:53 +090048 @Argument(index = 1, name = "name",
49 description = "workflow context name(workflow@workplace)",
50 required = true)
51 @Completion(WorkFlowCtxtNameCompleter.class)
jaegonkim6a7b5242018-09-12 23:09:42 +090052 private String name = null;
53
54 @Override
Ray Milkeydf521292018-10-04 15:13:33 -070055 protected void doExecute() {
jaegonkim6a7b5242018-09-12 23:09:42 +090056 if (Objects.isNull(cmd)) {
57 error("invalid cmd parameter");
58 return;
59 }
60
jaegonkime0f45b52018-10-09 20:23:26 +090061 if (Objects.isNull(name)) {
62 error("invalid workflow context name");
63 return;
64 }
65
66 String[] tokens = name.split("@");
67 if (tokens != null && tokens.length != 2) {
68 error("invalid workflow context name(workflow@workplace)");
69 return;
70 }
71
72 String workflowId = tokens[0];
73 String workplace = tokens[1];
74
jaegonkim6a7b5242018-09-12 23:09:42 +090075 switch (cmd) {
jaegonkimfbf61432019-03-16 09:37:53 +090076 case INVOKE:
jaegonkime0f45b52018-10-09 20:23:26 +090077 invoke(workflowId, workplace);
78 break;
jaegonkimfbf61432019-03-16 09:37:53 +090079 case EVAL:
jaegonkime0f45b52018-10-09 20:23:26 +090080 eval(name);
jaegonkim6a7b5242018-09-12 23:09:42 +090081 break;
82 default:
83 print("Unsupported cmd: " + cmd);
84 }
85 }
86
87 /**
88 * Invokes workflow.
89 * @param workflowId workflow id
90 * @param workplaceName workplace name
91 */
92 private void invoke(String workflowId, String workplaceName) {
93
94 WorkflowService service = get(WorkflowService.class);
jaegonkim6a7b5242018-09-12 23:09:42 +090095 try {
jaegonkime0f45b52018-10-09 20:23:26 +090096 DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder()
97 .workplaceName(workplaceName)
98 .id(workflowId)
99 .data(JsonNodeFactory.instance.objectNode())
100 .build();
101
jaegonkim6a7b5242018-09-12 23:09:42 +0900102 service.invokeWorkflow(wfDesc);
103 } catch (WorkflowException e) {
jaegonkime0f45b52018-10-09 20:23:26 +0900104 error("Exception: ", e);
jaegonkim6a7b5242018-09-12 23:09:42 +0900105 }
106 }
jaegonkime0f45b52018-10-09 20:23:26 +0900107
108 /**
109 * Evaluates workflow context.
110 * @param workflowContextName workflow context name
111 */
112 private void eval(String workflowContextName) {
113 WorkplaceStore storService = get(WorkplaceStore.class);
114 WorkflowExecutionService execService = get(WorkflowExecutionService.class);
115
116 WorkflowContext context = storService.getContext(workflowContextName);
117 if (context == null) {
118 error("failed to find workflow context {}", workflowContextName);
119 return;
120 }
121 execService.eval(workflowContextName);
122 }
123
jaegonkim6a7b5242018-09-12 23:09:42 +0900124}