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 | /** |
| 95 | * Constructor of DefaultWorkflowContext. |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 96 | * @param builder default workflow context builder |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 97 | */ |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 98 | protected DefaultWorkflowContext(Builder builder) { |
| 99 | super(builder.data); |
| 100 | this.workflowId = builder.workflowId; |
| 101 | this.workplaceName = builder.workplaceName; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 102 | this.state = WorkflowState.IDLE; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 103 | this.current = ProgramCounter.INIT_PC; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 104 | } |
| 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 |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 147 | public ProgramCounter current() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 148 | return this.current; |
| 149 | } |
| 150 | |
| 151 | @Override |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 152 | public void setCurrent(ProgramCounter pc) { |
| 153 | this.current = pc; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 154 | } |
| 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; |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 175 | 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, |
jaegonkim | ce75d3c | 2020-03-22 00:44:29 +0900 | [diff] [blame] | 183 | WorkExecutor eventGenerator, long timeoutMs) { |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 184 | this.completionEventType = eventType; |
| 185 | this.completionEventHintSet = new HashSet<>(); |
| 186 | this.completionEventHintSet.addAll(eventHint); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 187 | 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 |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 202 | public Set<String> completionEventHints() { |
| 203 | return completionEventHintSet; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 204 | } |
| 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 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 246 | 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 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 256 | @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 | } |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 267 | |
| 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 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 338 | } |