blob: 06d36ce7e1a1d624891a86e59be5339cf108e031 [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;
jaegonkimfe1bc062019-03-09 20:47:18 +090020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeydf521292018-10-04 15:13:33 -070021import org.apache.karaf.shell.api.action.Option;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
jaegonkim6a7b5242018-09-12 23:09:42 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.workflow.api.WorkflowContext;
25import org.onosproject.workflow.api.WorkflowException;
26import org.onosproject.workflow.api.WorkflowService;
27import org.onosproject.workflow.api.Workplace;
28import org.onosproject.workflow.api.DefaultWorkplaceDescription;
29import org.onosproject.workflow.api.WorkplaceStore;
30
31import java.util.Arrays;
32import java.util.Objects;
33
Ray Milkeydf521292018-10-04 15:13:33 -070034@Service
jaegonkim6a7b5242018-09-12 23:09:42 +090035@Command(scope = "onos", name = "workplace",
36 description = "workplace cli")
37public class WorkplaceStoreCommand extends AbstractShellCommand {
38
jaegonkimfe1bc062019-03-09 20:47:18 +090039 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)
jaegonkim6a7b5242018-09-12 23:09:42 +090048 private String cmd = null;
49
jaegonkimfe1bc062019-03-09 20:47:18 +090050 @Argument(index = 1, name = "name",
51 description = "workplace name",
52 required = false)
53 @Completion(WorkplaceNameCompleter.class)
jaegonkim6a7b5242018-09-12 23:09:42 +090054 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 Milkeydf521292018-10-04 15:13:33 -070065 protected void doExecute() {
jaegonkim6a7b5242018-09-12 23:09:42 +090066
67 if (Objects.isNull(cmd)) {
68 printAllWorkplace();
69 return;
70 }
71
72 switch (cmd) {
jaegonkimfe1bc062019-03-09 20:47:18 +090073 case ADD:
jaegonkim6a7b5242018-09-12 23:09:42 +090074 if (Objects.isNull(name)) {
75 error("invalid name");
76 return;
77 }
78 addEmptyWorkplace(name);
79 return;
80
jaegonkimfe1bc062019-03-09 20:47:18 +090081 case RM:
jaegonkim6a7b5242018-09-12 23:09:42 +090082 if (Objects.isNull(name)) {
83 print("invalid name");
84 return;
85 }
86 rmWorkplace(name);
87 break;
88
jaegonkimfe1bc062019-03-09 20:47:18 +090089 case CLEAR:
jaegonkim6a7b5242018-09-12 23:09:42 +090090 clearWorkplace();
91 break;
92
jaegonkimfe1bc062019-03-09 20:47:18 +090093 case PRINT:
jaegonkim6a7b5242018-09-12 23:09:42 +090094 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);
jaegonkim6a7b5242018-09-12 23:09:42 +0900112 try {
jaegonkime0f45b52018-10-09 20:23:26 +0900113 DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder()
114 .name(name)
115 .build();
jaegonkim6a7b5242018-09-12 23:09:42 +0900116 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);
jaegonkim6a7b5242018-09-12 23:09:42 +0900140 try {
jaegonkime0f45b52018-10-09 20:23:26 +0900141 DefaultWorkplaceDescription wpDesc = DefaultWorkplaceDescription.builder()
142 .name(name)
143 .build();
jaegonkim6a7b5242018-09-12 23:09:42 +0900144 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);
jaegonkimfe1bc062019-03-09 20:47:18 +0900157 if (Objects.isNull(workplace)) {
158 print("Not existing workplace " + name);
159 return;
160 }
jaegonkim6a7b5242018-09-12 23:09:42 +0900161 print(getWorkplaceString(workplace));
jaegonkim86561532019-05-18 08:56:22 +0900162 printWorkplaceContexts(workplaceStore, workplace.name());
jaegonkim6a7b5242018-09-12 23:09:42 +0900163 }
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}