blob: 1b4ba8a0757d58be3b6cadd54946dc04920e5605 [file] [log] [blame]
jaegonkim6a7b5242018-09-12 23:09:42 +09001/*
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 */
16package org.onosproject.workflow.cli;
17
Ray Milkeydf521292018-10-04 15:13:33 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Option;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
jaegonkim6a7b5242018-09-12 23:09:42 +090022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.workflow.api.WorkflowContext;
24import org.onosproject.workflow.api.WorkflowException;
25import org.onosproject.workflow.api.WorkflowService;
26import org.onosproject.workflow.api.Workplace;
27import org.onosproject.workflow.api.DefaultWorkplaceDescription;
28import org.onosproject.workflow.api.WorkplaceStore;
29
30import java.util.Arrays;
31import java.util.Objects;
32
Ray Milkeydf521292018-10-04 15:13:33 -070033@Service
jaegonkim6a7b5242018-09-12 23:09:42 +090034@Command(scope = "onos", name = "workplace",
35 description = "workplace cli")
36public class WorkplaceStoreCommand extends AbstractShellCommand {
37
Ray Milkeydf521292018-10-04 15:13:33 -070038 @Argument(index = 0, name = "cmd", description = "command(add/rm/clear/print)", required = false)
jaegonkim6a7b5242018-09-12 23:09:42 +090039 private String cmd = null;
40
jaegonkime0f45b52018-10-09 20:23:26 +090041 @Argument(index = 1, name = "name", description = "workspace name", required = false)
jaegonkim6a7b5242018-09-12 23:09:42 +090042 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 Milkeydf521292018-10-04 15:13:33 -070053 protected void doExecute() {
jaegonkim6a7b5242018-09-12 23:09:42 +090054
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);
jaegonkim6a7b5242018-09-12 23:09:42 +0900100 try {
jaegonkime0f45b52018-10-09 20:23:26 +0900101 DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder()
102 .name(name)
103 .build();
jaegonkim6a7b5242018-09-12 23:09:42 +0900104 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);
jaegonkim6a7b5242018-09-12 23:09:42 +0900128 try {
jaegonkime0f45b52018-10-09 20:23:26 +0900129 DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder()
130 .name(name)
131 .build();
jaegonkim6a7b5242018-09-12 23:09:42 +0900132 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}