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