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; |
| 24 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 25 | import static org.onosproject.workflow.api.CheckCondition.check; |
| 26 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 27 | /** |
| 28 | * Default implementation of WorkflowContext. |
| 29 | */ |
| 30 | public class DefaultWorkflowContext extends WorkflowContext { |
| 31 | |
| 32 | /** |
| 33 | * ID of workflow. |
| 34 | */ |
| 35 | private URI workflowId; |
| 36 | |
| 37 | /** |
| 38 | * Workplace name of the workflow. |
| 39 | */ |
| 40 | private String workplaceName; |
| 41 | |
| 42 | /** |
| 43 | * State of workflow. |
| 44 | */ |
| 45 | private WorkflowState state; |
| 46 | |
| 47 | /** |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 48 | * Current program counter of the workflow. |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 49 | */ |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 50 | private ProgramCounter current; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Cause of workflow exception. |
| 54 | */ |
| 55 | private String cause; |
| 56 | |
| 57 | /** |
| 58 | * Completion event type. |
| 59 | */ |
| 60 | private transient Class<? extends Event> completionEventType; |
| 61 | |
| 62 | /** |
| 63 | * Completion event hint. |
| 64 | */ |
| 65 | private transient String completionEventHint; |
| 66 | |
| 67 | /** |
| 68 | * Completion event generator method reference. |
| 69 | */ |
| 70 | private transient WorkExecutor completionEventGenerator; |
| 71 | |
| 72 | /** |
| 73 | * Completion event timeout milliseconds. |
| 74 | */ |
| 75 | private transient long completionEventTimeoutMs; |
| 76 | |
| 77 | /** |
| 78 | * Service reference for workflow service. |
| 79 | */ |
| 80 | private transient WorkflowExecutionService workflowExecutionService; |
| 81 | |
| 82 | /** |
| 83 | * Service reference for workflow store. |
| 84 | */ |
| 85 | private transient WorkflowStore workflowStore; |
| 86 | |
| 87 | /** |
| 88 | * Service reference for workplace store. |
| 89 | */ |
| 90 | private transient WorkplaceStore workplaceStore; |
| 91 | |
| 92 | /** |
| 93 | * Constructor of DefaultWorkflowContext. |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 94 | * @param builder default workflow context builder |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 95 | */ |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 96 | protected DefaultWorkflowContext(Builder builder) { |
| 97 | super(builder.data); |
| 98 | this.workflowId = builder.workflowId; |
| 99 | this.workplaceName = builder.workplaceName; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 100 | this.state = WorkflowState.IDLE; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 101 | this.current = ProgramCounter.INIT_PC; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * DefaultWorkflowContext name builder. |
| 106 | * @param workflowid workflow id |
| 107 | * @param workplacename workplace name |
| 108 | * @return DefaultWorkflowContext name |
| 109 | */ |
| 110 | public static String nameBuilder(URI workflowid, String workplacename) { |
| 111 | return workflowid.toString() + "@" + workplacename; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String name() { |
| 116 | return nameBuilder(workflowId, workplaceName); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String distributor() { |
| 121 | return workplaceName(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public URI workflowId() { |
| 126 | return this.workflowId; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String workplaceName() { |
| 131 | return workplaceName; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public WorkflowState state() { |
| 136 | return state; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void setState(WorkflowState state) { |
| 141 | this.state = state; |
| 142 | } |
| 143 | |
| 144 | @Override |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 145 | public ProgramCounter current() { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 146 | return this.current; |
| 147 | } |
| 148 | |
| 149 | @Override |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 150 | public void setCurrent(ProgramCounter pc) { |
| 151 | this.current = pc; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public String cause() { |
| 156 | return this.cause; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void setCause(String cause) { |
| 161 | this.cause = cause; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void completed() { |
| 166 | setTriggerNext(true); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void waitCompletion(Class<? extends Event> eventType, String eventHint, |
| 171 | WorkExecutor eventGenerator, long timeoutMs) { |
| 172 | this.completionEventType = eventType; |
| 173 | this.completionEventHint = eventHint; |
| 174 | this.completionEventGenerator = eventGenerator; |
| 175 | this.completionEventTimeoutMs = timeoutMs; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void waitFor(long timeoutMs) { |
| 180 | this.completionEventTimeoutMs = timeoutMs; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public Class<? extends Event> completionEventType() { |
| 185 | return completionEventType; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public String completionEventHint() { |
| 190 | return completionEventHint; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public WorkExecutor completionEventGenerator() { |
| 195 | return completionEventGenerator; |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public long completionEventTimeout() { |
| 200 | return completionEventTimeoutMs; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void setWorkflowExecutionService(WorkflowExecutionService workflowExecutionService) { |
| 205 | this.workflowExecutionService = workflowExecutionService; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public WorkflowExecutionService workflowService() { |
| 210 | return workflowExecutionService; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void setWorkflowStore(WorkflowStore workflowStore) { |
| 215 | this.workflowStore = workflowStore; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public WorkflowStore workflowStore() { |
| 220 | return workflowStore; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void setWorkplaceStore(WorkplaceStore workplaceStore) { |
| 225 | this.workplaceStore = workplaceStore; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public WorkplaceStore workplaceStore() { |
| 230 | return workplaceStore; |
| 231 | } |
| 232 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 233 | public <T> T getService(Class<T> serviceClass) throws WorkflowException { |
| 234 | T service; |
| 235 | try { |
| 236 | service = DefaultServiceDirectory.getService(serviceClass); |
| 237 | } catch (ServiceNotFoundException e) { |
| 238 | throw new WorkflowException(e); |
| 239 | } |
| 240 | return service; |
| 241 | } |
| 242 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 243 | @Override |
| 244 | public String toString() { |
| 245 | return MoreObjects.toStringHelper(getClass()) |
| 246 | .add("name", name()) |
| 247 | .add("triggernext", triggerNext()) |
| 248 | .add("data", data()) |
| 249 | .add("current", current) |
| 250 | .add("state", state()) |
| 251 | .add("cause", cause()) |
| 252 | .toString(); |
| 253 | } |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame^] | 254 | |
| 255 | /** |
| 256 | * Gets builder instance. |
| 257 | * @return builder instance |
| 258 | */ |
| 259 | public static final Builder builder() { |
| 260 | return new Builder(); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Builder for default workflow context. |
| 265 | */ |
| 266 | public static class Builder { |
| 267 | |
| 268 | /** |
| 269 | * ID of workflow. |
| 270 | */ |
| 271 | private URI workflowId; |
| 272 | |
| 273 | /** |
| 274 | * Workplace name of the workflow. |
| 275 | */ |
| 276 | private String workplaceName; |
| 277 | |
| 278 | /** |
| 279 | * Data model tree. |
| 280 | */ |
| 281 | private DataModelTree data; |
| 282 | |
| 283 | /** |
| 284 | * Sets workflow id. |
| 285 | * @param workflowId workflow id |
| 286 | * @return builder |
| 287 | */ |
| 288 | public Builder workflowId(URI workflowId) { |
| 289 | this.workflowId = workflowId; |
| 290 | return this; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Sets workplace name. |
| 295 | * @param workplaceName workplace name |
| 296 | * @return builder |
| 297 | */ |
| 298 | public Builder workplaceName(String workplaceName) { |
| 299 | this.workplaceName = workplaceName; |
| 300 | return this; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Sets data model tree. |
| 305 | * @param data data model tree |
| 306 | * @return builder |
| 307 | */ |
| 308 | public Builder data(DataModelTree data) { |
| 309 | this.data = data; |
| 310 | return this; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Builds default workflow context. |
| 315 | * @return instance of default workflow context |
| 316 | * @throws WorkflowException workflow exception |
| 317 | */ |
| 318 | public DefaultWorkflowContext build() throws WorkflowException { |
| 319 | check(data != null, "Invalid data model tree"); |
| 320 | check(workflowId != null, "Invalid workflowId"); |
| 321 | check(workplaceName != null, "Invalid workplaceName"); |
| 322 | return new DefaultWorkflowContext(this); |
| 323 | } |
| 324 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 325 | } |