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.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 19 | import org.onlab.osgi.DefaultServiceDirectory; |
| 20 | import org.onlab.osgi.ServiceNotFoundException; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 21 | import org.onosproject.event.Event; |
| 22 | |
| 23 | import java.net.URI; |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 24 | import java.util.HashSet; |
| 25 | import java.util.Set; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 26 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 27 | import static org.onosproject.workflow.api.CheckCondition.check; |
| 28 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 29 | /** |
| 30 | * Default implementation of WorkflowContext. |
| 31 | */ |
| 32 | public 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 | /** |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 50 | * Current program counter of the workflow. |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 51 | */ |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 52 | private ProgramCounter current; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 53 | |
| 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 | /** |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 65 | * Completion event hint Set. |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 66 | */ |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 67 | private transient Set<String> completionEventHintSet; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 68 | |
| 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 | /** |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame^] | 95 | * Service reference for eventMap store. |
| 96 | */ |
| 97 | private transient ContextEventMapStore eventMapStore; |
| 98 | |
| 99 | |
| 100 | /** |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 101 | * Constructor of DefaultWorkflowContext. |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 102 | * @param builder default workflow context builder |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 103 | */ |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 104 | protected DefaultWorkflowContext(Builder builder) { |
| 105 | super(builder.data); |
| 106 | this.workflowId = builder.workflowId; |
| 107 | this.workplaceName = builder.workplaceName; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 108 | this.state = WorkflowState.IDLE; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 109 | this.current = ProgramCounter.INIT_PC; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 110 | } |
| 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 |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 153 | public ProgramCounter current() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 154 | return this.current; |
| 155 | } |
| 156 | |
| 157 | @Override |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 158 | public void setCurrent(ProgramCounter pc) { |
| 159 | this.current = pc; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 160 | } |
| 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; |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 181 | 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); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 193 | 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 |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 208 | public Set<String> completionEventHints() { |
| 209 | return completionEventHintSet; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 210 | } |
| 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 |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame^] | 223 | public void registerTriggerEvent(Class<? extends Event> event, Set<String> eventHintSet) throws WorkflowException { |
| 224 | eventMapStore.registerTriggerFlag(event.getName(), eventHintSet, this.name()); |
| 225 | } |
| 226 | |
| 227 | @Override |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 228 | 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 | |
nitinanand | c8b7025 | 2019-04-17 15:35:43 +0530 | [diff] [blame^] | 257 | |
| 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 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 268 | 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 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 278 | @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 | } |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 289 | |
| 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 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 360 | } |