blob: 603790add4a5e6b6ae191a60290ab4356762eebc [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.api;
17
18import com.google.common.base.MoreObjects;
jaegonkime0f45b52018-10-09 20:23:26 +090019import org.onlab.osgi.DefaultServiceDirectory;
20import org.onlab.osgi.ServiceNotFoundException;
jaegonkim6a7b5242018-09-12 23:09:42 +090021import org.onosproject.event.Event;
22
23import java.net.URI;
nitinanandf3f94c62019-02-08 10:36:39 +053024import java.util.HashSet;
25import java.util.Set;
jaegonkim6a7b5242018-09-12 23:09:42 +090026
jaegonkime0f45b52018-10-09 20:23:26 +090027import static org.onosproject.workflow.api.CheckCondition.check;
28
jaegonkim6a7b5242018-09-12 23:09:42 +090029/**
30 * Default implementation of WorkflowContext.
31 */
32public class DefaultWorkflowContext extends WorkflowContext {
33
34 /**
35 * ID of workflow.
36 */
37 private URI workflowId;
38
39 /**
40 * Workplace name of the workflow.
41 */
42 private String workplaceName;
43
44 /**
45 * State of workflow.
46 */
47 private WorkflowState state;
48
49 /**
jaegonkime0f45b52018-10-09 20:23:26 +090050 * Current program counter of the workflow.
jaegonkim6a7b5242018-09-12 23:09:42 +090051 */
jaegonkime0f45b52018-10-09 20:23:26 +090052 private ProgramCounter current;
jaegonkim6a7b5242018-09-12 23:09:42 +090053
54 /**
55 * Cause of workflow exception.
56 */
57 private String cause;
58
59 /**
60 * Completion event type.
61 */
62 private transient Class<? extends Event> completionEventType;
63
64 /**
nitinanandf3f94c62019-02-08 10:36:39 +053065 * Completion event hint Set.
jaegonkim6a7b5242018-09-12 23:09:42 +090066 */
nitinanandf3f94c62019-02-08 10:36:39 +053067 private transient Set<String> completionEventHintSet;
jaegonkim6a7b5242018-09-12 23:09:42 +090068
69 /**
70 * Completion event generator method reference.
71 */
72 private transient WorkExecutor completionEventGenerator;
73
74 /**
75 * Completion event timeout milliseconds.
76 */
77 private transient long completionEventTimeoutMs;
78
79 /**
80 * Service reference for workflow service.
81 */
82 private transient WorkflowExecutionService workflowExecutionService;
83
84 /**
85 * Service reference for workflow store.
86 */
87 private transient WorkflowStore workflowStore;
88
89 /**
90 * Service reference for workplace store.
91 */
92 private transient WorkplaceStore workplaceStore;
93
94 /**
95 * Constructor of DefaultWorkflowContext.
jaegonkime0f45b52018-10-09 20:23:26 +090096 * @param builder default workflow context builder
jaegonkim6a7b5242018-09-12 23:09:42 +090097 */
jaegonkime0f45b52018-10-09 20:23:26 +090098 protected DefaultWorkflowContext(Builder builder) {
99 super(builder.data);
100 this.workflowId = builder.workflowId;
101 this.workplaceName = builder.workplaceName;
jaegonkim6a7b5242018-09-12 23:09:42 +0900102 this.state = WorkflowState.IDLE;
jaegonkime0f45b52018-10-09 20:23:26 +0900103 this.current = ProgramCounter.INIT_PC;
jaegonkim6a7b5242018-09-12 23:09:42 +0900104 }
105
106 /**
107 * DefaultWorkflowContext name builder.
108 * @param workflowid workflow id
109 * @param workplacename workplace name
110 * @return DefaultWorkflowContext name
111 */
112 public static String nameBuilder(URI workflowid, String workplacename) {
113 return workflowid.toString() + "@" + workplacename;
114 }
115
116 @Override
117 public String name() {
118 return nameBuilder(workflowId, workplaceName);
119 }
120
121 @Override
122 public String distributor() {
123 return workplaceName();
124 }
125
126 @Override
127 public URI workflowId() {
128 return this.workflowId;
129 }
130
131 @Override
132 public String workplaceName() {
133 return workplaceName;
134 }
135
136 @Override
137 public WorkflowState state() {
138 return state;
139 }
140
141 @Override
142 public void setState(WorkflowState state) {
143 this.state = state;
144 }
145
146 @Override
jaegonkime0f45b52018-10-09 20:23:26 +0900147 public ProgramCounter current() {
jaegonkim6a7b5242018-09-12 23:09:42 +0900148 return this.current;
149 }
150
151 @Override
jaegonkime0f45b52018-10-09 20:23:26 +0900152 public void setCurrent(ProgramCounter pc) {
153 this.current = pc;
jaegonkim6a7b5242018-09-12 23:09:42 +0900154 }
155
156 @Override
157 public String cause() {
158 return this.cause;
159 }
160
161 @Override
162 public void setCause(String cause) {
163 this.cause = cause;
164 }
165
166 @Override
167 public void completed() {
168 setTriggerNext(true);
169 }
170
171 @Override
172 public void waitCompletion(Class<? extends Event> eventType, String eventHint,
173 WorkExecutor eventGenerator, long timeoutMs) {
174 this.completionEventType = eventType;
nitinanandf3f94c62019-02-08 10:36:39 +0530175 this.completionEventHintSet = new HashSet<>();
176 this.completionEventHintSet.add(eventHint);
177 this.completionEventGenerator = eventGenerator;
178 this.completionEventTimeoutMs = timeoutMs;
179 }
180
181 @Override
182 public void waitAnyCompletion(Class<? extends Event> eventType, Set<String> eventHint,
jaegonkimce75d3c2020-03-22 00:44:29 +0900183 WorkExecutor eventGenerator, long timeoutMs) {
nitinanandf3f94c62019-02-08 10:36:39 +0530184 this.completionEventType = eventType;
185 this.completionEventHintSet = new HashSet<>();
186 this.completionEventHintSet.addAll(eventHint);
jaegonkim6a7b5242018-09-12 23:09:42 +0900187 this.completionEventGenerator = eventGenerator;
188 this.completionEventTimeoutMs = timeoutMs;
189 }
190
191 @Override
192 public void waitFor(long timeoutMs) {
193 this.completionEventTimeoutMs = timeoutMs;
194 }
195
196 @Override
197 public Class<? extends Event> completionEventType() {
198 return completionEventType;
199 }
200
201 @Override
nitinanandf3f94c62019-02-08 10:36:39 +0530202 public Set<String> completionEventHints() {
203 return completionEventHintSet;
jaegonkim6a7b5242018-09-12 23:09:42 +0900204 }
205
206 @Override
207 public WorkExecutor completionEventGenerator() {
208 return completionEventGenerator;
209 }
210
211 @Override
212 public long completionEventTimeout() {
213 return completionEventTimeoutMs;
214 }
215
216 @Override
217 public void setWorkflowExecutionService(WorkflowExecutionService workflowExecutionService) {
218 this.workflowExecutionService = workflowExecutionService;
219 }
220
221 @Override
222 public WorkflowExecutionService workflowService() {
223 return workflowExecutionService;
224 }
225
226 @Override
227 public void setWorkflowStore(WorkflowStore workflowStore) {
228 this.workflowStore = workflowStore;
229 }
230
231 @Override
232 public WorkflowStore workflowStore() {
233 return workflowStore;
234 }
235
236 @Override
237 public void setWorkplaceStore(WorkplaceStore workplaceStore) {
238 this.workplaceStore = workplaceStore;
239 }
240
241 @Override
242 public WorkplaceStore workplaceStore() {
243 return workplaceStore;
244 }
245
jaegonkime0f45b52018-10-09 20:23:26 +0900246 public <T> T getService(Class<T> serviceClass) throws WorkflowException {
247 T service;
248 try {
249 service = DefaultServiceDirectory.getService(serviceClass);
250 } catch (ServiceNotFoundException e) {
251 throw new WorkflowException(e);
252 }
253 return service;
254 }
255
jaegonkim6a7b5242018-09-12 23:09:42 +0900256 @Override
257 public String toString() {
258 return MoreObjects.toStringHelper(getClass())
259 .add("name", name())
260 .add("triggernext", triggerNext())
261 .add("data", data())
262 .add("current", current)
263 .add("state", state())
264 .add("cause", cause())
265 .toString();
266 }
jaegonkime0f45b52018-10-09 20:23:26 +0900267
268 /**
269 * Gets builder instance.
270 * @return builder instance
271 */
272 public static final Builder builder() {
273 return new Builder();
274 }
275
276 /**
277 * Builder for default workflow context.
278 */
279 public static class Builder {
280
281 /**
282 * ID of workflow.
283 */
284 private URI workflowId;
285
286 /**
287 * Workplace name of the workflow.
288 */
289 private String workplaceName;
290
291 /**
292 * Data model tree.
293 */
294 private DataModelTree data;
295
296 /**
297 * Sets workflow id.
298 * @param workflowId workflow id
299 * @return builder
300 */
301 public Builder workflowId(URI workflowId) {
302 this.workflowId = workflowId;
303 return this;
304 }
305
306 /**
307 * Sets workplace name.
308 * @param workplaceName workplace name
309 * @return builder
310 */
311 public Builder workplaceName(String workplaceName) {
312 this.workplaceName = workplaceName;
313 return this;
314 }
315
316 /**
317 * Sets data model tree.
318 * @param data data model tree
319 * @return builder
320 */
321 public Builder data(DataModelTree data) {
322 this.data = data;
323 return this;
324 }
325
326 /**
327 * Builds default workflow context.
328 * @return instance of default workflow context
329 * @throws WorkflowException workflow exception
330 */
331 public DefaultWorkflowContext build() throws WorkflowException {
332 check(data != null, "Invalid data model tree");
333 check(workflowId != null, "Invalid workflowId");
334 check(workplaceName != null, "Invalid workplaceName");
335 return new DefaultWorkflowContext(this);
336 }
337 }
jaegonkim6a7b5242018-09-12 23:09:42 +0900338}