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 | |
| 19 | import org.onosproject.event.Event; |
| 20 | |
| 21 | /** |
| 22 | * An interface representing worklet. A workflow is composed of worklets. |
| 23 | */ |
| 24 | public interface Worklet { |
| 25 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 26 | int MAX_WORKS = 10000; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Returns tag name of worklet. class name is usually used. |
| 30 | * @return tag name |
| 31 | */ |
| 32 | String tag(); |
| 33 | |
| 34 | /** |
| 35 | * Processes tasks of the worklet under the workflow context. |
| 36 | * @param context workflow context |
| 37 | * @throws WorkflowException workflow exception |
| 38 | */ |
| 39 | void process(WorkflowContext context) throws WorkflowException; |
| 40 | |
| 41 | /** |
| 42 | * Checks whether is this worklet next worklet to be done under the workflow context. |
| 43 | * @param context workflow context |
| 44 | * @return true means this worklet is the next worklet to be processed |
| 45 | * @throws WorkflowException workflow exception |
| 46 | */ |
| 47 | boolean isNext(WorkflowContext context) throws WorkflowException; |
| 48 | |
| 49 | /** |
| 50 | * Checks whether is this worklet completed or not. 'isCompleted' checking is triggered by an event task. |
| 51 | * @param context workflow context |
| 52 | * @param event an event triggering this 'isCompleted' checking |
| 53 | * @return completed or not |
| 54 | * @throws WorkflowException workflow exception |
| 55 | */ |
| 56 | boolean isCompleted(WorkflowContext context, Event event) throws WorkflowException; |
| 57 | |
| 58 | /** |
| 59 | * Completion event timeout handler. |
| 60 | * @param context workflow context |
| 61 | * @throws WorkflowException workflow exception |
| 62 | */ |
| 63 | void timeout(WorkflowContext context) throws WorkflowException; |
| 64 | |
| 65 | /** |
| 66 | * Common worklet enum. |
| 67 | */ |
| 68 | enum Common implements Worklet { |
| 69 | |
| 70 | /** |
| 71 | * Init worklet. |
| 72 | */ |
| 73 | INIT { |
| 74 | @Override |
| 75 | public String tag() { |
| 76 | return INIT.name(); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void process(WorkflowContext context) throws WorkflowException { |
| 81 | throw new WorkflowException("(" + tag() + ").process should not be called"); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 86 | throw new WorkflowException("(" + tag() + ").isNext should not be called"); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException { |
| 91 | throw new WorkflowException("(" + tag() + ").isCompleted should not be called"); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void timeout(WorkflowContext context) throws WorkflowException { |
| 96 | throw new WorkflowException("(" + tag() + ").timeout should not be called"); |
| 97 | } |
| 98 | }, |
| 99 | |
| 100 | /** |
| 101 | * Completed worklet. |
| 102 | */ |
| 103 | COMPLETED { |
| 104 | @Override |
| 105 | public String tag() { |
| 106 | return COMPLETED.name(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void process(WorkflowContext context) throws WorkflowException { |
| 111 | throw new WorkflowException("(" + tag() + ").process should not be called"); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 116 | throw new WorkflowException("(" + tag() + ").isNext should not be called"); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException { |
| 121 | throw new WorkflowException("(" + tag() + ").isCompleted should not be called"); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void timeout(WorkflowContext context) throws WorkflowException { |
| 126 | throw new WorkflowException("(" + tag() + ").timeout should not be called"); |
| 127 | } |
| 128 | }, |
| 129 | |
| 130 | /** |
| 131 | * Interrupted worklet. |
| 132 | */ |
| 133 | INTERRUPTED { |
| 134 | @Override |
| 135 | public String tag() { |
| 136 | return INTERRUPTED.name(); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void process(WorkflowContext context) throws WorkflowException { |
| 141 | throw new WorkflowException("(" + tag() + ").process should not be called"); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean isNext(WorkflowContext context) throws WorkflowException { |
| 146 | throw new WorkflowException("(" + tag() + ").isNext should not be called"); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean isCompleted(WorkflowContext context, Event event)throws WorkflowException { |
| 151 | throw new WorkflowException("(" + tag() + ").isCompleted should not be called"); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void timeout(WorkflowContext context) throws WorkflowException { |
| 156 | throw new WorkflowException("(" + tag() + ").timeout should not be called"); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |