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; |
| 24 | import org.onosproject.workflow.api.WorkflowService; |
| 25 | import org.onosproject.workflow.api.WorkflowException; |
| 26 | |
| 27 | import java.util.Arrays; |
| 28 | import java.util.Objects; |
| 29 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 30 | @Service |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 31 | @Command(scope = "onos", name = "workflow", description = "workflow cli") |
| 32 | public class WorkFlowCommand extends AbstractShellCommand { |
| 33 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 34 | @Argument(index = 0, name = "cmd", description = "command(invoke)", required = true) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 35 | 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 Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 44 | protected void doExecute() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 45 | 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 | } |