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