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; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.workflow.api.DefaultWorkflowDescription; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 24 | import org.onosproject.workflow.api.WorkflowContext; |
| 25 | import org.onosproject.workflow.api.WorkflowExecutionService; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 26 | import org.onosproject.workflow.api.WorkflowService; |
| 27 | import org.onosproject.workflow.api.WorkflowException; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 28 | import org.onosproject.workflow.api.WorkplaceStore; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 29 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 30 | import java.util.Objects; |
| 31 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 32 | @Service |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 33 | @Command(scope = "onos", name = "workflow", description = "workflow cli") |
| 34 | public class WorkFlowCommand extends AbstractShellCommand { |
| 35 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 36 | @Argument(index = 0, name = "cmd", description = "command(invoke|eval)", required = true) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 37 | private String cmd = null; |
| 38 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 39 | @Argument(index = 1, name = "name", description = "workflow context name(workflow@workplace)", required = true) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 40 | private String name = null; |
| 41 | |
| 42 | @Override |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 43 | protected void doExecute() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 44 | if (Objects.isNull(cmd)) { |
| 45 | error("invalid cmd parameter"); |
| 46 | return; |
| 47 | } |
| 48 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 49 | 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 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 63 | switch (cmd) { |
| 64 | case "invoke": |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 65 | invoke(workflowId, workplace); |
| 66 | break; |
| 67 | case "eval": |
| 68 | eval(name); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 69 | 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); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 83 | try { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 84 | DefaultWorkflowDescription wfDesc = DefaultWorkflowDescription.builder() |
| 85 | .workplaceName(workplaceName) |
| 86 | .id(workflowId) |
| 87 | .data(JsonNodeFactory.instance.objectNode()) |
| 88 | .build(); |
| 89 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 90 | service.invokeWorkflow(wfDesc); |
| 91 | } catch (WorkflowException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 92 | error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 93 | } |
| 94 | } |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 95 | |
| 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 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 112 | } |