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