blob: 977a9787f6f1e6131d95fd5bfadc4b89809461d0 [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 /**
nitinanandc8b70252019-04-17 15:35:43 +053095 * Service reference for eventMap store.
96 */
97 private transient ContextEventMapStore eventMapStore;
98
99
100 /**
jaegonkim6a7b5242018-09-12 23:09:42 +0900101 * Constructor of DefaultWorkflowContext.
jaegonkime0f45b52018-10-09 20:23:26 +0900102 * @param builder default workflow context builder
jaegonkim6a7b5242018-09-12 23:09:42 +0900103 */
jaegonkime0f45b52018-10-09 20:23:26 +0900104 protected DefaultWorkflowContext(Builder builder) {
105 super(builder.data);
106 this.workflowId = builder.workflowId;
107 this.workplaceName = builder.workplaceName;
jaegonkim6a7b5242018-09-12 23:09:42 +0900108 this.state = WorkflowState.IDLE;
jaegonkime0f45b52018-10-09 20:23:26 +0900109 this.current = ProgramCounter.INIT_PC;
jaegonkim6a7b5242018-09-12 23:09:42 +0900110 }
111
112 /**
113 * DefaultWorkflowContext name builder.
114 * @param workflowid workflow id
115 * @param workplacename workplace name
116 * @return DefaultWorkflowContext name
117 */
118 public static String nameBuilder(URI workflowid, String workplacename) {
119 return workflowid.toString() + "@" + workplacename;
120 }
121
122 @Override
123 public String name() {
124 return nameBuilder(workflowId, workplaceName);
125 }
126
127 @Override
128 public String distributor() {
129 return workplaceName();
130 }
131
132 @Override
133 public URI workflowId() {
134 return this.workflowId;
135 }
136
137 @Override
138 public String workplaceName() {
139 return workplaceName;
140 }
141
142 @Override
143 public WorkflowState state() {
144 return state;
145 }
146
147 @Override
148 public void setState(WorkflowState state) {
149 this.state = state;
150 }
151
152 @Override
jaegonkime0f45b52018-10-09 20:23:26 +0900153 public ProgramCounter current() {
jaegonkim6a7b5242018-09-12 23:09:42 +0900154 return this.current;
155 }
156
157 @Override
jaegonkime0f45b52018-10-09 20:23:26 +0900158 public void setCurrent(ProgramCounter pc) {
159 this.current = pc;
jaegonkim6a7b5242018-09-12 23:09:42 +0900160 }
161
162 @Override
163 public String cause() {
164 return this.cause;
165 }
166
167 @Override
168 public void setCause(String cause) {
169 this.cause = cause;
170 }
171
172 @Override
173 public void completed() {
174 setTriggerNext(true);
175 }
176
177 @Override
178 public void waitCompletion(Class<? extends Event> eventType, String eventHint,
179 WorkExecutor eventGenerator, long timeoutMs) {
180 this.completionEventType = eventType;
nitinanandf3f94c62019-02-08 10:36:39 +0530181 this.completionEventHintSet = new HashSet<>();
182 this.completionEventHintSet.add(eventHint);
183 this.completionEventGenerator = eventGenerator;
184 this.completionEventTimeoutMs = timeoutMs;
185 }
186
187 @Override
188 public void waitAnyCompletion(Class<? extends Event> eventType, Set<String> eventHint,
189 WorkExecutor eventGenerator, long timeoutMs) {
190 this.completionEventType = eventType;
191 this.completionEventHintSet = new HashSet<>();
192 this.completionEventHintSet.addAll(eventHint);
jaegonkim6a7b5242018-09-12 23:09:42 +0900193 this.completionEventGenerator = eventGenerator;
194 this.completionEventTimeoutMs = timeoutMs;
195 }
196
197 @Override
198 public void waitFor(long timeoutMs) {
199 this.completionEventTimeoutMs = timeoutMs;
200 }
201
202 @Override
203 public Class<? extends Event> completionEventType() {
204 return completionEventType;
205 }
206
207 @Override
nitinanandf3f94c62019-02-08 10:36:39 +0530208 public Set<String> completionEventHints() {
209 return completionEventHintSet;
jaegonkim6a7b5242018-09-12 23:09:42 +0900210 }
211
212 @Override
213 public WorkExecutor completionEventGenerator() {
214 return completionEventGenerator;
215 }
216
217 @Override
218 public long completionEventTimeout() {
219 return completionEventTimeoutMs;
220 }
221
222 @Override
nitinanandc8b70252019-04-17 15:35:43 +0530223 public void registerTriggerEvent(Class<? extends Event> event, Set<String> eventHintSet) throws WorkflowException {
224 eventMapStore.registerTriggerFlag(event.getName(), eventHintSet, this.name());
225 }
226
227 @Override
jaegonkim6a7b5242018-09-12 23:09:42 +0900228 public void setWorkflowExecutionService(WorkflowExecutionService workflowExecutionService) {
229 this.workflowExecutionService = workflowExecutionService;
230 }
231
232 @Override
233 public WorkflowExecutionService workflowService() {
234 return workflowExecutionService;
235 }
236
237 @Override
238 public void setWorkflowStore(WorkflowStore workflowStore) {
239 this.workflowStore = workflowStore;
240 }
241
242 @Override
243 public WorkflowStore workflowStore() {
244 return workflowStore;
245 }
246
247 @Override
248 public void setWorkplaceStore(WorkplaceStore workplaceStore) {
249 this.workplaceStore = workplaceStore;
250 }
251
252 @Override
253 public WorkplaceStore workplaceStore() {
254 return workplaceStore;
255 }
256
nitinanandc8b70252019-04-17 15:35:43 +0530257
258 @Override
259 public void setEventMapStore(ContextEventMapStore contextEventMapStore) {
260 this.eventMapStore = contextEventMapStore;
261 }
262
263 @Override
264 public ContextEventMapStore eventMapStore() {
265 return eventMapStore;
266 }
267
jaegonkime0f45b52018-10-09 20:23:26 +0900268 public <T> T getService(Class<T> serviceClass) throws WorkflowException {
269 T service;
270 try {
271 service = DefaultServiceDirectory.getService(serviceClass);
272 } catch (ServiceNotFoundException e) {
273 throw new WorkflowException(e);
274 }
275 return service;
276 }
277
jaegonkim6a7b5242018-09-12 23:09:42 +0900278 @Override
279 public String toString() {
280 return MoreObjects.toStringHelper(getClass())
281 .add("name", name())
282 .add("triggernext", triggerNext())
283 .add("data", data())
284 .add("current", current)
285 .add("state", state())
286 .add("cause", cause())
287 .toString();
288 }
jaegonkime0f45b52018-10-09 20:23:26 +0900289
290 /**
291 * Gets builder instance.
292 * @return builder instance
293 */
294 public static final Builder builder() {
295 return new Builder();
296 }
297
298 /**
299 * Builder for default workflow context.
300 */
301 public static class Builder {
302
303 /**
304 * ID of workflow.
305 */
306 private URI workflowId;
307
308 /**
309 * Workplace name of the workflow.
310 */
311 private String workplaceName;
312
313 /**
314 * Data model tree.
315 */
316 private DataModelTree data;
317
318 /**
319 * Sets workflow id.
320 * @param workflowId workflow id
321 * @return builder
322 */
323 public Builder workflowId(URI workflowId) {
324 this.workflowId = workflowId;
325 return this;
326 }
327
328 /**
329 * Sets workplace name.
330 * @param workplaceName workplace name
331 * @return builder
332 */
333 public Builder workplaceName(String workplaceName) {
334 this.workplaceName = workplaceName;
335 return this;
336 }
337
338 /**
339 * Sets data model tree.
340 * @param data data model tree
341 * @return builder
342 */
343 public Builder data(DataModelTree data) {
344 this.data = data;
345 return this;
346 }
347
348 /**
349 * Builds default workflow context.
350 * @return instance of default workflow context
351 * @throws WorkflowException workflow exception
352 */
353 public DefaultWorkflowContext build() throws WorkflowException {
354 check(data != null, "Invalid data model tree");
355 check(workflowId != null, "Invalid workflowId");
356 check(workplaceName != null, "Invalid workplaceName");
357 return new DefaultWorkflowContext(this);
358 }
359 }
jaegonkim6a7b5242018-09-12 23:09:42 +0900360}