Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.flow; |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 19 | import static java.util.concurrent.TimeUnit.NANOSECONDS; |
| 20 | import static java.util.concurrent.TimeUnit.SECONDS; |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 25 | import java.util.concurrent.TimeUnit; |
| 26 | |
Yuta HIGUCHI | f6f50a6 | 2014-10-19 15:58:49 -0700 | [diff] [blame] | 27 | public class DefaultFlowEntry extends DefaultFlowRule |
Madan Jampani | 54d3499 | 2015-03-06 17:27:52 -0800 | [diff] [blame] | 28 | implements StoredFlowEntry { |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 29 | |
Yuta HIGUCHI | 3498aab | 2014-10-17 21:05:40 -0700 | [diff] [blame] | 30 | private static final Logger log = getLogger(DefaultFlowEntry.class); |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 31 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 32 | private static final long DEFAULT_LAST_SEEN = -1; |
| 33 | private static final int DEFAULT_ERR_CODE = -1; |
| 34 | private static final int DEFAULT_ERR_TYPE = -1; |
| 35 | |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 36 | /* Stored in nanoseconds (allows for 292 years) */ |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 37 | private long life; |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 38 | |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 39 | private long packets; |
| 40 | private long bytes; |
| 41 | private FlowEntryState state; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 42 | private FlowLiveType liveType; |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 43 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 44 | private long lastSeen = DEFAULT_LAST_SEEN; |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 45 | |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 46 | private final int errType; |
| 47 | |
| 48 | private final int errCode; |
| 49 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 50 | /** |
| 51 | * Creates a flow entry of flow table specified with the flow rule, state |
| 52 | * and statistic information. |
| 53 | * |
| 54 | * @param rule the flow rule |
| 55 | * @param state the flow state |
| 56 | * @param life the duration second of flow |
| 57 | * @param lifeTimeUnit life time unit |
| 58 | * @param packets the number of packets of this flow |
| 59 | * @param bytes the the number of bytes of this flow |
| 60 | */ |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 61 | public DefaultFlowEntry(FlowRule rule, FlowEntryState state, |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 62 | long life, TimeUnit lifeTimeUnit, long packets, long bytes) { |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 63 | super(rule); |
| 64 | this.state = state; |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 65 | this.life = lifeTimeUnit.toNanos(life); |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 66 | this.liveType = FlowLiveType.UNKNOWN; |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 67 | this.packets = packets; |
| 68 | this.bytes = bytes; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 69 | this.errCode = DEFAULT_ERR_CODE; |
| 70 | this.errType = DEFAULT_ERR_TYPE; |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 71 | this.lastSeen = System.currentTimeMillis(); |
| 72 | } |
| 73 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 74 | /** |
| 75 | * Creates a flow entry of flow table specified with the flow rule, state |
| 76 | * and statistic information. |
| 77 | * |
| 78 | * @param rule the flow rule |
| 79 | * @param state the flow state |
| 80 | * @param life the duration second of flow |
| 81 | * @param lifeTimeUnit life time unit |
| 82 | * @param liveType the flow live type, i.e., IMMEDIATE, SHORT, MID, LONG |
| 83 | * @param packets the number of packets of this flow |
| 84 | * @param bytes the the number of bytes of this flow |
| 85 | */ |
| 86 | public DefaultFlowEntry(FlowRule rule, FlowEntryState state, |
| 87 | long life, TimeUnit lifeTimeUnit, FlowLiveType liveType, |
| 88 | long packets, long bytes) { |
| 89 | this(rule, state, life, lifeTimeUnit, packets, bytes); |
| 90 | this.liveType = liveType; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Creates a flow entry of flow table specified with the flow rule, state, |
| 95 | * live type and statistic information. |
| 96 | * |
| 97 | * @param rule the flow rule |
| 98 | * @param state the flow state |
| 99 | * @param lifeSecs the duration second of flow |
| 100 | * @param liveType the flow live type, i.e., IMMEDIATE, SHORT, MID, LONG |
| 101 | * @param packets the number of packets of this flow |
| 102 | * @param bytes the the number of bytes of this flow |
| 103 | */ |
| 104 | public DefaultFlowEntry(FlowRule rule, FlowEntryState state, |
| 105 | long lifeSecs, FlowLiveType liveType, |
| 106 | long packets, long bytes) { |
| 107 | this(rule, state, lifeSecs, SECONDS, packets, bytes); |
| 108 | this.liveType = liveType; |
| 109 | } |
| 110 | |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 111 | public DefaultFlowEntry(FlowRule rule, FlowEntryState state, |
| 112 | long lifeSecs, long packets, long bytes) { |
| 113 | this(rule, state, lifeSecs, SECONDS, packets, bytes); |
| 114 | } |
| 115 | |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 116 | public DefaultFlowEntry(FlowRule rule) { |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 117 | this(rule, FlowEntryState.PENDING_ADD, 0, 0, 0); |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 120 | /** |
Yuta HIGUCHI | 44f18e9 | 2017-03-02 22:05:41 -0800 | [diff] [blame] | 121 | * Creates a flow entry based on specified flow rule and state. |
| 122 | * |
| 123 | * @param rule to use as base |
| 124 | * @param state of the flow entry |
| 125 | */ |
| 126 | public DefaultFlowEntry(FlowRule rule, FlowEntryState state) { |
| 127 | this(rule, state, 0, 0, 0); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 132 | * Creates a flow entry of flow table specified with the flow rule, state, |
| 133 | * live type and statistic information. |
| 134 | * |
| 135 | * @param rule the flow rule |
| 136 | * @param errType the error type |
| 137 | * @param errCode the error code |
| 138 | */ |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 139 | public DefaultFlowEntry(FlowRule rule, int errType, int errCode) { |
| 140 | super(rule); |
| 141 | this.state = FlowEntryState.FAILED; |
| 142 | this.errType = errType; |
| 143 | this.errCode = errCode; |
Ray Milkey | 930fc66 | 2014-11-11 16:07:45 -0800 | [diff] [blame] | 144 | this.lastSeen = System.currentTimeMillis(); |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 145 | } |
| 146 | |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 147 | @Override |
| 148 | public long life() { |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 149 | return life(SECONDS); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public long life(TimeUnit timeUnit) { |
| 154 | return timeUnit.convert(life, NANOSECONDS); |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 158 | public FlowLiveType liveType() { |
| 159 | return liveType; |
| 160 | } |
| 161 | |
| 162 | @Override |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 163 | public long packets() { |
| 164 | return packets; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public long bytes() { |
| 169 | return bytes; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public FlowEntryState state() { |
| 174 | return this.state; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public long lastSeen() { |
| 179 | return lastSeen; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void setLastSeen() { |
| 184 | this.lastSeen = System.currentTimeMillis(); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void setState(FlowEntryState newState) { |
| 189 | this.state = newState; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void setLife(long life) { |
Thiago Santos | 877914d | 2016-07-20 18:29:29 -0300 | [diff] [blame] | 194 | setLife(life, SECONDS); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void setLife(long life, TimeUnit timeUnit) { |
| 199 | this.life = timeUnit.toNanos(life); |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 203 | public void setLiveType(FlowLiveType liveType) { |
| 204 | this.liveType = liveType; |
| 205 | } |
| 206 | |
| 207 | @Override |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 208 | public void setPackets(long packets) { |
| 209 | this.packets = packets; |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void setBytes(long bytes) { |
| 214 | this.bytes = bytes; |
| 215 | } |
| 216 | |
| 217 | @Override |
alshabib | 193525b | 2014-10-08 18:58:03 -0700 | [diff] [blame] | 218 | public int errType() { |
| 219 | return this.errType; |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public int errCode() { |
| 224 | return this.errCode; |
| 225 | } |
| 226 | |
| 227 | @Override |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 228 | public String toString() { |
| 229 | return toStringHelper(this) |
| 230 | .add("rule", super.toString()) |
| 231 | .add("state", state) |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 232 | .add("life", life) |
| 233 | .add("liveType", liveType) |
| 234 | .add("packets", packets) |
| 235 | .add("bytes", bytes) |
| 236 | .add("errCode", errCode) |
| 237 | .add("errType", errType) |
| 238 | .add("lastSeen", lastSeen) |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 239 | .toString(); |
| 240 | } |
alshabib | ca5706c | 2014-10-04 20:29:41 -0700 | [diff] [blame] | 241 | } |