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; |
| 19 | import org.onosproject.event.Event; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | |
| 23 | /** |
| 24 | * Default implementation of WorkflowContext. |
| 25 | */ |
| 26 | public class DefaultWorkflowContext extends WorkflowContext { |
| 27 | |
| 28 | /** |
| 29 | * ID of workflow. |
| 30 | */ |
| 31 | private URI workflowId; |
| 32 | |
| 33 | /** |
| 34 | * Workplace name of the workflow. |
| 35 | */ |
| 36 | private String workplaceName; |
| 37 | |
| 38 | /** |
| 39 | * State of workflow. |
| 40 | */ |
| 41 | private WorkflowState state; |
| 42 | |
| 43 | /** |
| 44 | * Current worklet of the workflow. |
| 45 | */ |
| 46 | private String current; |
| 47 | |
| 48 | /** |
| 49 | * Cause of workflow exception. |
| 50 | */ |
| 51 | private String cause; |
| 52 | |
| 53 | /** |
| 54 | * Completion event type. |
| 55 | */ |
| 56 | private transient Class<? extends Event> completionEventType; |
| 57 | |
| 58 | /** |
| 59 | * Completion event hint. |
| 60 | */ |
| 61 | private transient String completionEventHint; |
| 62 | |
| 63 | /** |
| 64 | * Completion event generator method reference. |
| 65 | */ |
| 66 | private transient WorkExecutor completionEventGenerator; |
| 67 | |
| 68 | /** |
| 69 | * Completion event timeout milliseconds. |
| 70 | */ |
| 71 | private transient long completionEventTimeoutMs; |
| 72 | |
| 73 | /** |
| 74 | * Service reference for workflow service. |
| 75 | */ |
| 76 | private transient WorkflowExecutionService workflowExecutionService; |
| 77 | |
| 78 | /** |
| 79 | * Service reference for workflow store. |
| 80 | */ |
| 81 | private transient WorkflowStore workflowStore; |
| 82 | |
| 83 | /** |
| 84 | * Service reference for workplace store. |
| 85 | */ |
| 86 | private transient WorkplaceStore workplaceStore; |
| 87 | |
| 88 | /** |
| 89 | * Constructor of DefaultWorkflowContext. |
| 90 | * @param workflowId ID of workflow |
| 91 | * @param workplaceName name of workplace |
| 92 | * @param data data model tree |
| 93 | */ |
| 94 | public DefaultWorkflowContext(URI workflowId, String workplaceName, DataModelTree data) { |
| 95 | super(data); |
| 96 | this.workflowId = workflowId; |
| 97 | this.workplaceName = workplaceName; |
| 98 | this.state = WorkflowState.IDLE; |
| 99 | this.current = Worklet.Common.INIT.name(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * DefaultWorkflowContext name builder. |
| 104 | * @param workflowid workflow id |
| 105 | * @param workplacename workplace name |
| 106 | * @return DefaultWorkflowContext name |
| 107 | */ |
| 108 | public static String nameBuilder(URI workflowid, String workplacename) { |
| 109 | return workflowid.toString() + "@" + workplacename; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public String name() { |
| 114 | return nameBuilder(workflowId, workplaceName); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String distributor() { |
| 119 | return workplaceName(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public URI workflowId() { |
| 124 | return this.workflowId; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String workplaceName() { |
| 129 | return workplaceName; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public WorkflowState state() { |
| 134 | return state; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void setState(WorkflowState state) { |
| 139 | this.state = state; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public String current() { |
| 144 | return this.current; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void setCurrent(Worklet worklet) { |
| 149 | this.current = worklet.tag(); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public String cause() { |
| 154 | return this.cause; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void setCause(String cause) { |
| 159 | this.cause = cause; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void completed() { |
| 164 | setTriggerNext(true); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void waitCompletion(Class<? extends Event> eventType, String eventHint, |
| 169 | WorkExecutor eventGenerator, long timeoutMs) { |
| 170 | this.completionEventType = eventType; |
| 171 | this.completionEventHint = eventHint; |
| 172 | this.completionEventGenerator = eventGenerator; |
| 173 | this.completionEventTimeoutMs = timeoutMs; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void waitFor(long timeoutMs) { |
| 178 | this.completionEventTimeoutMs = timeoutMs; |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Class<? extends Event> completionEventType() { |
| 183 | return completionEventType; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public String completionEventHint() { |
| 188 | return completionEventHint; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public WorkExecutor completionEventGenerator() { |
| 193 | return completionEventGenerator; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public long completionEventTimeout() { |
| 198 | return completionEventTimeoutMs; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void setWorkflowExecutionService(WorkflowExecutionService workflowExecutionService) { |
| 203 | this.workflowExecutionService = workflowExecutionService; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public WorkflowExecutionService workflowService() { |
| 208 | return workflowExecutionService; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void setWorkflowStore(WorkflowStore workflowStore) { |
| 213 | this.workflowStore = workflowStore; |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public WorkflowStore workflowStore() { |
| 218 | return workflowStore; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void setWorkplaceStore(WorkplaceStore workplaceStore) { |
| 223 | this.workplaceStore = workplaceStore; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public WorkplaceStore workplaceStore() { |
| 228 | return workplaceStore; |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public String toString() { |
| 233 | return MoreObjects.toStringHelper(getClass()) |
| 234 | .add("name", name()) |
| 235 | .add("triggernext", triggerNext()) |
| 236 | .add("data", data()) |
| 237 | .add("current", current) |
| 238 | .add("state", state()) |
| 239 | .add("cause", cause()) |
| 240 | .toString(); |
| 241 | } |
| 242 | } |