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 | |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 20 | import java.util.HashSet; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 21 | import java.util.Objects; |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 22 | import java.util.Set; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 23 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 24 | import static org.onosproject.workflow.api.CheckCondition.check; |
| 25 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 26 | /** |
| 27 | * Class for event timeout task. |
| 28 | */ |
| 29 | public final class EventTimeoutTask extends HandlerTask { |
| 30 | |
| 31 | /** |
| 32 | * Event type (Class name of event). |
| 33 | */ |
| 34 | private final String eventType; |
| 35 | |
| 36 | /** |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 37 | * Set of Event hint value for finding target event. |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 38 | */ |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 39 | private final Set<String> eventHintSet = new HashSet<>(); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Constructor of EventTimeoutTask. |
| 43 | * @param builder builder of EventTimeoutTask |
| 44 | */ |
| 45 | private EventTimeoutTask(Builder builder) { |
| 46 | super(builder); |
| 47 | this.eventType = builder.eventType; |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 48 | this.eventHintSet.addAll(builder.eventHintSet); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Gets event type (Class name of event). |
| 53 | * @return event type |
| 54 | */ |
| 55 | public String eventType() { |
| 56 | return eventType; |
| 57 | } |
| 58 | |
| 59 | /** |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 60 | * Gets set of event hint value for finding target event. |
| 61 | * @return event hint set |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 62 | */ |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 63 | public Set<String> eventHintSet() { |
| 64 | return eventHintSet; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public int hashCode() { |
| 69 | return Objects.hash(this.toString()); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean equals(Object obj) { |
| 74 | if (obj == this) { |
| 75 | return true; |
| 76 | } |
| 77 | if (!(obj instanceof EventTask)) { |
| 78 | return false; |
| 79 | } |
| 80 | return Objects.equals(this.eventType(), ((EventTask) obj).eventType()) |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 81 | && Objects.equals(this.eventHintSet(), ((EventTask) obj).eventHint()); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public String toString() { |
| 86 | return MoreObjects.toStringHelper(getClass()) |
| 87 | .add("context", context()) |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 88 | .add("programCounter", programCounter()) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 89 | .add("eventType", eventType()) |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 90 | .add("eventHint", eventHintSet()) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 91 | .toString(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets a instance of builder. |
| 96 | * @return instance of builder |
| 97 | */ |
| 98 | public static Builder builder() { |
| 99 | return new Builder(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Builder of EventTimeoutTask. |
| 104 | */ |
| 105 | public static class Builder extends HandlerTask.Builder { |
| 106 | /** |
| 107 | * Event type (Class name of event). |
| 108 | */ |
| 109 | private String eventType; |
| 110 | |
| 111 | /** |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 112 | * Set of Event hint value for finding target event. |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 113 | */ |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 114 | private Set<String> eventHintSet; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Sets Event type (Class name of event). |
| 118 | * @param eventType event type |
| 119 | * @return builder of EventTimeoutTask |
| 120 | */ |
| 121 | public Builder eventType(String eventType) { |
| 122 | this.eventType = eventType; |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Sets event hint string for finding target event. |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 128 | * @param eventHintSet Set of event hint string |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 129 | * @return builder of EventTimeoutTask |
| 130 | */ |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 131 | public Builder eventHintSet(Set<String> eventHintSet) { |
| 132 | this.eventHintSet = eventHintSet; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 133 | return this; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Builder context(WorkflowContext context) { |
| 138 | super.context(context); |
| 139 | return this; |
| 140 | } |
| 141 | |
| 142 | @Override |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 143 | public Builder programCounter(ProgramCounter programCounter) { |
| 144 | super.programCounter(programCounter); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Builds EventTimeoutTask. |
| 150 | * @return instance of EventTimeoutTask |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 151 | * @throws WorkflowException workflow exception |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 152 | */ |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 153 | public EventTimeoutTask build() throws WorkflowException { |
| 154 | check(eventType != null, "eventType is invalid"); |
nitinanand | f3f94c6 | 2019-02-08 10:36:39 +0530 | [diff] [blame] | 155 | check(eventHintSet != null, "eventHintSet is invalid"); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 156 | return new EventTimeoutTask(this); |
| 157 | } |
| 158 | } |
| 159 | } |