blob: fadc3a2d9e93eb024d5d8265125096849531f9b5 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
alshabibca5706c2014-10-04 20:29:41 -070017
18import static com.google.common.base.MoreObjects.toStringHelper;
Thiago Santos877914d2016-07-20 18:29:29 -030019import static java.util.concurrent.TimeUnit.NANOSECONDS;
20import static java.util.concurrent.TimeUnit.SECONDS;
alshabibca5706c2014-10-04 20:29:41 -070021import static org.slf4j.LoggerFactory.getLogger;
22
alshabibca5706c2014-10-04 20:29:41 -070023import org.slf4j.Logger;
24
Thiago Santos877914d2016-07-20 18:29:29 -030025import java.util.concurrent.TimeUnit;
26
Yuta HIGUCHIf6f50a62014-10-19 15:58:49 -070027public class DefaultFlowEntry extends DefaultFlowRule
Madan Jampani54d34992015-03-06 17:27:52 -080028 implements StoredFlowEntry {
alshabibca5706c2014-10-04 20:29:41 -070029
Yuta HIGUCHI3498aab2014-10-17 21:05:40 -070030 private static final Logger log = getLogger(DefaultFlowEntry.class);
alshabibca5706c2014-10-04 20:29:41 -070031
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090032 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 Santos877914d2016-07-20 18:29:29 -030036 /* Stored in nanoseconds (allows for 292 years) */
alshabibca5706c2014-10-04 20:29:41 -070037 private long life;
Thiago Santos877914d2016-07-20 18:29:29 -030038
alshabibca5706c2014-10-04 20:29:41 -070039 private long packets;
40 private long bytes;
41 private FlowEntryState state;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090042 private FlowLiveType liveType;
alshabibca5706c2014-10-04 20:29:41 -070043
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090044 private long lastSeen = DEFAULT_LAST_SEEN;
alshabibca5706c2014-10-04 20:29:41 -070045
alshabib193525b2014-10-08 18:58:03 -070046 private final int errType;
47
48 private final int errCode;
49
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090050 /**
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 */
alshabibca5706c2014-10-04 20:29:41 -070061 public DefaultFlowEntry(FlowRule rule, FlowEntryState state,
Thiago Santos877914d2016-07-20 18:29:29 -030062 long life, TimeUnit lifeTimeUnit, long packets, long bytes) {
alshabibca5706c2014-10-04 20:29:41 -070063 super(rule);
64 this.state = state;
Thiago Santos877914d2016-07-20 18:29:29 -030065 this.life = lifeTimeUnit.toNanos(life);
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090066 this.liveType = FlowLiveType.UNKNOWN;
alshabibca5706c2014-10-04 20:29:41 -070067 this.packets = packets;
68 this.bytes = bytes;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090069 this.errCode = DEFAULT_ERR_CODE;
70 this.errType = DEFAULT_ERR_TYPE;
alshabibca5706c2014-10-04 20:29:41 -070071 this.lastSeen = System.currentTimeMillis();
72 }
73
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090074 /**
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 Santos877914d2016-07-20 18:29:29 -0300111 public DefaultFlowEntry(FlowRule rule, FlowEntryState state,
112 long lifeSecs, long packets, long bytes) {
113 this(rule, state, lifeSecs, SECONDS, packets, bytes);
114 }
115
alshabibca5706c2014-10-04 20:29:41 -0700116 public DefaultFlowEntry(FlowRule rule) {
Thiago Santos877914d2016-07-20 18:29:29 -0300117 this(rule, FlowEntryState.PENDING_ADD, 0, 0, 0);
alshabibca5706c2014-10-04 20:29:41 -0700118 }
119
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900120 /**
Yuta HIGUCHI44f18e92017-03-02 22:05:41 -0800121 * 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 Yoonb1b823f2016-05-16 18:55:39 +0900132 * 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 */
alshabib193525b2014-10-08 18:58:03 -0700139 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 Milkey930fc662014-11-11 16:07:45 -0800144 this.lastSeen = System.currentTimeMillis();
alshabib193525b2014-10-08 18:58:03 -0700145 }
146
alshabibca5706c2014-10-04 20:29:41 -0700147 @Override
148 public long life() {
Thiago Santos877914d2016-07-20 18:29:29 -0300149 return life(SECONDS);
150 }
151
152 @Override
153 public long life(TimeUnit timeUnit) {
154 return timeUnit.convert(life, NANOSECONDS);
alshabibca5706c2014-10-04 20:29:41 -0700155 }
156
157 @Override
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900158 public FlowLiveType liveType() {
159 return liveType;
160 }
161
162 @Override
alshabibca5706c2014-10-04 20:29:41 -0700163 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 Santos877914d2016-07-20 18:29:29 -0300194 setLife(life, SECONDS);
195 }
196
197 @Override
198 public void setLife(long life, TimeUnit timeUnit) {
199 this.life = timeUnit.toNanos(life);
alshabibca5706c2014-10-04 20:29:41 -0700200 }
201
202 @Override
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900203 public void setLiveType(FlowLiveType liveType) {
204 this.liveType = liveType;
205 }
206
207 @Override
alshabibca5706c2014-10-04 20:29:41 -0700208 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
alshabib193525b2014-10-08 18:58:03 -0700218 public int errType() {
219 return this.errType;
220 }
221
222 @Override
223 public int errCode() {
224 return this.errCode;
225 }
226
227 @Override
alshabibca5706c2014-10-04 20:29:41 -0700228 public String toString() {
229 return toStringHelper(this)
230 .add("rule", super.toString())
231 .add("state", state)
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900232 .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)
alshabibca5706c2014-10-04 20:29:41 -0700239 .toString();
240 }
alshabibca5706c2014-10-04 20:29:41 -0700241}