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.apache.karaf.shell.commands.Option; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.workflow.api.WorkflowContext; |
| 23 | import org.onosproject.workflow.api.WorkflowException; |
| 24 | import org.onosproject.workflow.api.WorkflowService; |
| 25 | import org.onosproject.workflow.api.Workplace; |
| 26 | import org.onosproject.workflow.api.DefaultWorkplaceDescription; |
| 27 | import org.onosproject.workflow.api.WorkplaceStore; |
| 28 | |
| 29 | import java.util.Arrays; |
| 30 | import java.util.Objects; |
| 31 | |
| 32 | @Command(scope = "onos", name = "workplace", |
| 33 | description = "workplace cli") |
| 34 | public class WorkplaceStoreCommand extends AbstractShellCommand { |
| 35 | |
| 36 | @Argument (index = 0, name = "cmd", description = "command(add/rm/clear/print)", required = false) |
| 37 | private String cmd = null; |
| 38 | |
| 39 | @Argument (index = 1, name = "name", description = "workspace name", required = false) |
| 40 | private String name = null; |
| 41 | |
| 42 | @Option(name = "-f", aliases = "--filter", description = "including filter", |
| 43 | required = false, multiValued = false) |
| 44 | private String inFilter = null; |
| 45 | |
| 46 | @Option(name = "-e", aliases = "--excludefilter", description = "excluding filter", |
| 47 | required = false, multiValued = false) |
| 48 | private String exFilter = null; |
| 49 | |
| 50 | @Override |
| 51 | protected void execute() { |
| 52 | |
| 53 | if (Objects.isNull(cmd)) { |
| 54 | printAllWorkplace(); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | switch (cmd) { |
| 59 | case "add": |
| 60 | if (Objects.isNull(name)) { |
| 61 | error("invalid name"); |
| 62 | return; |
| 63 | } |
| 64 | addEmptyWorkplace(name); |
| 65 | return; |
| 66 | |
| 67 | case "rm": |
| 68 | if (Objects.isNull(name)) { |
| 69 | print("invalid name"); |
| 70 | return; |
| 71 | } |
| 72 | rmWorkplace(name); |
| 73 | break; |
| 74 | |
| 75 | case "clear": |
| 76 | clearWorkplace(); |
| 77 | break; |
| 78 | |
| 79 | case "print": |
| 80 | if (Objects.isNull(name)) { |
| 81 | print("invalid name"); |
| 82 | return; |
| 83 | } |
| 84 | printWorkplace(name); |
| 85 | break; |
| 86 | |
| 87 | default: |
| 88 | print("Unsupported cmd: " + cmd); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Adds empty workplace. |
| 94 | * @param name workplace name |
| 95 | */ |
| 96 | private void addEmptyWorkplace(String name) { |
| 97 | WorkflowService service = get(WorkflowService.class); |
| 98 | DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder() |
| 99 | .name(name) |
| 100 | .build(); |
| 101 | try { |
| 102 | service.createWorkplace(wpDesc); |
| 103 | } catch (WorkflowException e) { |
| 104 | error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace())); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Clears all workplaces. |
| 110 | */ |
| 111 | private void clearWorkplace() { |
| 112 | WorkflowService service = get(WorkflowService.class); |
| 113 | try { |
| 114 | service.clearWorkplace(); |
| 115 | } catch (WorkflowException e) { |
| 116 | error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace())); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Removes workplace. |
| 122 | * @param name workplace name to remove |
| 123 | */ |
| 124 | private void rmWorkplace(String name) { |
| 125 | WorkflowService service = get(WorkflowService.class); |
| 126 | DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder() |
| 127 | .name(name) |
| 128 | .build(); |
| 129 | try { |
| 130 | service.removeWorkplace(wpDesc); |
| 131 | } catch (WorkflowException e) { |
| 132 | error(e.getMessage() + ", trace: " + Arrays.asList(e.getStackTrace())); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Prints workplace. |
| 138 | * @param name workplace name |
| 139 | */ |
| 140 | private void printWorkplace(String name) { |
| 141 | WorkplaceStore workplaceStore = get(WorkplaceStore.class); |
| 142 | Workplace workplace = workplaceStore.getWorkplace(name); |
| 143 | print(getWorkplaceString(workplace)); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Prints all workplaces. |
| 148 | */ |
| 149 | private void printAllWorkplace() { |
| 150 | WorkplaceStore workplaceStore = get(WorkplaceStore.class); |
| 151 | for (Workplace workplace : workplaceStore.getWorkplaces()) { |
| 152 | print(getWorkplaceString(workplace)); |
| 153 | printWorkplaceContexts(workplaceStore, workplace.name()); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Prints contexts of workplace. |
| 159 | * @param workplaceStore workplace store service |
| 160 | * @param workplaceName workplace name |
| 161 | */ |
| 162 | private void printWorkplaceContexts(WorkplaceStore workplaceStore, String workplaceName) { |
| 163 | for (WorkflowContext context : workplaceStore.getWorkplaceContexts(workplaceName)) { |
| 164 | String str = context.toString(); |
| 165 | if (Objects.nonNull(inFilter)) { |
| 166 | if (str.indexOf(inFilter) != -1) { |
| 167 | if (Objects.nonNull(exFilter)) { |
| 168 | if (str.indexOf(exFilter) == -1) { |
| 169 | print(" - " + context); |
| 170 | } |
| 171 | } else { |
| 172 | print(" - " + context); |
| 173 | } |
| 174 | } |
| 175 | } else { |
| 176 | if (Objects.nonNull(exFilter)) { |
| 177 | if (str.indexOf(exFilter) == -1) { |
| 178 | print(" - " + context); |
| 179 | } |
| 180 | } else { |
| 181 | print(" - " + context); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Gets workplace string. |
| 189 | * @param workplace workplace |
| 190 | * @return workplace string |
| 191 | */ |
| 192 | private String getWorkplaceString(Workplace workplace) { |
| 193 | return workplace.toString(); |
| 194 | } |
| 195 | } |