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 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame^] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.workflow.api.Workflow; |
| 23 | import org.onosproject.workflow.api.WorkflowStore; |
| 24 | |
| 25 | import java.util.Objects; |
| 26 | import java.net.URI; |
| 27 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame^] | 28 | @Service |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 29 | @Command(scope = "onos", name = "workflowstore", description = "workflow store cli") |
| 30 | public class WorkFlowStoreCommand extends AbstractShellCommand { |
| 31 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame^] | 32 | @Argument(index = 0, name = "cmd", description = "command(rm)", required = false) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 33 | private String cmd = null; |
| 34 | |
| 35 | @Argument (index = 1, name = "id", description = "workflow id(URI)", required = false) |
| 36 | private String id = null; |
| 37 | |
| 38 | @Override |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame^] | 39 | protected void doExecute() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 40 | |
| 41 | if (Objects.isNull(cmd)) { |
| 42 | printAllWorkflow(); |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | if (Objects.isNull(id)) { |
| 47 | print("invalid id"); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | switch (cmd) { |
| 52 | case "rm": |
| 53 | rmWorkflow(id); |
| 54 | break; |
| 55 | default: |
| 56 | print("Unsupported cmd: " + cmd); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | private void rmWorkflow(String id) { |
| 61 | WorkflowStore workflowStore = get(WorkflowStore.class); |
| 62 | workflowStore.unregister(URI.create(id)); |
| 63 | } |
| 64 | |
| 65 | private void printAllWorkflow() { |
| 66 | WorkflowStore workflowStore = get(WorkflowStore.class); |
| 67 | for (Workflow workflow : workflowStore.getAll()) { |
| 68 | print(getWorkflowString(workflow)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private String getWorkflowString(Workflow workflow) { |
| 73 | return workflow.toString(); |
| 74 | } |
| 75 | } |