blob: 5c0f5de35a4e81c2836b98bd7c0bac1d6911e261 [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;
jaegonkime0f45b52018-10-09 20:23:26 +090024import org.onosproject.workflow.api.WorkflowContext;
25import org.onosproject.workflow.api.WorkflowExecutionService;
jaegonkim6a7b5242018-09-12 23:09:42 +090026import org.onosproject.workflow.api.WorkflowService;
27import org.onosproject.workflow.api.WorkflowException;
jaegonkime0f45b52018-10-09 20:23:26 +090028import org.onosproject.workflow.api.WorkplaceStore;
jaegonkim6a7b5242018-09-12 23:09:42 +090029
jaegonkim6a7b5242018-09-12 23:09:42 +090030import java.util.Objects;
31
Ray Milkeydf521292018-10-04 15:13:33 -070032@Service
jaegonkim6a7b5242018-09-12 23:09:42 +090033@Command(scope = "onos", name = "workflow", description = "workflow cli")
34public class WorkFlowCommand extends AbstractShellCommand {
35
jaegonkime0f45b52018-10-09 20:23:26 +090036 @Argument(index = 0, name = "cmd", description = "command(invoke|eval)", required = true)
jaegonkim6a7b5242018-09-12 23:09:42 +090037 private String cmd = null;
38
jaegonkime0f45b52018-10-09 20:23:26 +090039 @Argument(index = 1, name = "name", description = "workflow context name(workflow@workplace)", required = true)
jaegonkim6a7b5242018-09-12 23:09:42 +090040 private String name = null;
41
42 @Override
Ray Milkeydf521292018-10-04 15:13:33 -070043 protected void doExecute() {
jaegonkim6a7b5242018-09-12 23:09:42 +090044 if (Objects.isNull(cmd)) {
45 error("invalid cmd parameter");
46 return;
47 }
48
jaegonkime0f45b52018-10-09 20:23:26 +090049 if (Objects.isNull(name)) {
50 error("invalid workflow context name");
51 return;
52 }
53
54 String[] tokens = name.split("@");
55 if (tokens != null && tokens.length != 2) {
56 error("invalid workflow context name(workflow@workplace)");
57 return;
58 }
59
60 String workflowId = tokens[0];
61 String workplace = tokens[1];
62
jaegonkim6a7b5242018-09-12 23:09:42 +090063 switch (cmd) {
64 case "invoke":
jaegonkime0f45b52018-10-09 20:23:26 +090065 invoke(workflowId, workplace);
66 break;
67 case "eval":
68 eval(name);
jaegonkim6a7b5242018-09-12 23:09:42 +090069 break;
70 default:
71 print("Unsupported cmd: " + cmd);
72 }
73 }
74
75 /**
76 * Invokes workflow.
77 * @param workflowId workflow id
78 * @param workplaceName workplace name
79 */
80 private void invoke(String workflowId, String workplaceName) {
81
82 WorkflowService service = get(WorkflowService.class);
jaegonkim6a7b5242018-09-12 23:09:42 +090083 try {
jaegonkime0f45b52018-10-09 20:23:26 +090084 DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder()
85 .workplaceName(workplaceName)
86 .id(workflowId)
87 .data(JsonNodeFactory.instance.objectNode())
88 .build();
89
jaegonkim6a7b5242018-09-12 23:09:42 +090090 service.invokeWorkflow(wfDesc);
91 } catch (WorkflowException e) {
jaegonkime0f45b52018-10-09 20:23:26 +090092 error("Exception: ", e);
jaegonkim6a7b5242018-09-12 23:09:42 +090093 }
94 }
jaegonkime0f45b52018-10-09 20:23:26 +090095
96 /**
97 * Evaluates workflow context.
98 * @param workflowContextName workflow context name
99 */
100 private void eval(String workflowContextName) {
101 WorkplaceStore storService = get(WorkplaceStore.class);
102 WorkflowExecutionService execService = get(WorkflowExecutionService.class);
103
104 WorkflowContext context = storService.getContext(workflowContextName);
105 if (context == null) {
106 error("failed to find workflow context {}", workflowContextName);
107 return;
108 }
109 execService.eval(workflowContextName);
110 }
111
jaegonkim6a7b5242018-09-12 23:09:42 +0900112}