jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.workflow.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
jaegonkim | fbf6143 | 2019-03-16 09:37:53 +0900 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.workflow.api.DefaultWorkflowDescription; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 25 | import org.onosproject.workflow.api.WorkflowContext; |
| 26 | import org.onosproject.workflow.api.WorkflowExecutionService; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 27 | import org.onosproject.workflow.api.WorkflowService; |
| 28 | import org.onosproject.workflow.api.WorkflowException; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 29 | import org.onosproject.workflow.api.WorkplaceStore; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 30 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 31 | import java.util.Objects; |
| 32 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 33 | @Service |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 34 | @Command(scope = "onos", name = "workflow", description = "workflow cli") |
| 35 | public class WorkFlowCommand extends AbstractShellCommand { |
| 36 | |
jaegonkim | fbf6143 | 2019-03-16 09:37:53 +0900 | [diff] [blame] | 37 | 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) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 46 | private String cmd = null; |
| 47 | |
jaegonkim | fbf6143 | 2019-03-16 09:37:53 +0900 | [diff] [blame] | 48 | @Argument(index = 1, name = "name", |
| 49 | description = "workflow context name(workflow@workplace)", |
| 50 | required = true) |
| 51 | @Completion(WorkFlowCtxtNameCompleter.class) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 52 | private String name = null; |
| 53 | |
| 54 | @Override |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 55 | protected void doExecute() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 56 | if (Objects.isNull(cmd)) { |
| 57 | error("invalid cmd parameter"); |
| 58 | return; |
| 59 | } |
| 60 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 61 | 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 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 75 | switch (cmd) { |
jaegonkim | fbf6143 | 2019-03-16 09:37:53 +0900 | [diff] [blame] | 76 | case INVOKE: |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 77 | invoke(workflowId, workplace); |
| 78 | break; |
jaegonkim | fbf6143 | 2019-03-16 09:37:53 +0900 | [diff] [blame] | 79 | case EVAL: |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 80 | eval(name); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 81 | 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); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 95 | try { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 96 | DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder() |
| 97 | .workplaceName(workplaceName) |
| 98 | .id(workflowId) |
| 99 | .data(JsonNodeFactory.instance.objectNode()) |
| 100 | .build(); |
| 101 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 102 | service.invokeWorkflow(wfDesc); |
| 103 | } catch (WorkflowException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 104 | error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 105 | } |
| 106 | } |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 107 | |
| 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 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 124 | } |