blob: 943ad8419b34423c884dea6c503d8e0dd853fc8c [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 /**
121 * Creates a flow entry of flow table specified with the flow rule, state,
122 * live type and statistic information.
123 *
124 * @param rule the flow rule
125 * @param errType the error type
126 * @param errCode the error code
127 */
alshabib193525b2014-10-08 18:58:03 -0700128 public DefaultFlowEntry(FlowRule rule, int errType, int errCode) {
129 super(rule);
130 this.state = FlowEntryState.FAILED;
131 this.errType = errType;
132 this.errCode = errCode;
Ray Milkey930fc662014-11-11 16:07:45 -0800133 this.lastSeen = System.currentTimeMillis();
alshabib193525b2014-10-08 18:58:03 -0700134 }
135
alshabibca5706c2014-10-04 20:29:41 -0700136 @Override
137 public long life() {
Thiago Santos877914d2016-07-20 18:29:29 -0300138 return life(SECONDS);
139 }
140
141 @Override
142 public long life(TimeUnit timeUnit) {
143 return timeUnit.convert(life, NANOSECONDS);
alshabibca5706c2014-10-04 20:29:41 -0700144 }
145
146 @Override
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900147 public FlowLiveType liveType() {
148 return liveType;
149 }
150
151 @Override
alshabibca5706c2014-10-04 20:29:41 -0700152 public long packets() {
153 return packets;
154 }
155
156 @Override
157 public long bytes() {
158 return bytes;
159 }
160
161 @Override
162 public FlowEntryState state() {
163 return this.state;
164 }
165
166 @Override
167 public long lastSeen() {
168 return lastSeen;
169 }
170
171 @Override
172 public void setLastSeen() {
173 this.lastSeen = System.currentTimeMillis();
174 }
175
176 @Override
177 public void setState(FlowEntryState newState) {
178 this.state = newState;
179 }
180
181 @Override
182 public void setLife(long life) {
Thiago Santos877914d2016-07-20 18:29:29 -0300183 setLife(life, SECONDS);
184 }
185
186 @Override
187 public void setLife(long life, TimeUnit timeUnit) {
188 this.life = timeUnit.toNanos(life);
alshabibca5706c2014-10-04 20:29:41 -0700189 }
190
191 @Override
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900192 public void setLiveType(FlowLiveType liveType) {
193 this.liveType = liveType;
194 }
195
196 @Override
alshabibca5706c2014-10-04 20:29:41 -0700197 public void setPackets(long packets) {
198 this.packets = packets;
199 }
200
201 @Override
202 public void setBytes(long bytes) {
203 this.bytes = bytes;
204 }
205
206 @Override
alshabib193525b2014-10-08 18:58:03 -0700207 public int errType() {
208 return this.errType;
209 }
210
211 @Override
212 public int errCode() {
213 return this.errCode;
214 }
215
216 @Override
alshabibca5706c2014-10-04 20:29:41 -0700217 public String toString() {
218 return toStringHelper(this)
219 .add("rule", super.toString())
220 .add("state", state)
Sangsik Yoonb1b823f2016-05-16 18:55:39 +0900221 .add("life", life)
222 .add("liveType", liveType)
223 .add("packets", packets)
224 .add("bytes", bytes)
225 .add("errCode", errCode)
226 .add("errType", errType)
227 .add("lastSeen", lastSeen)
alshabibca5706c2014-10-04 20:29:41 -0700228 .toString();
229 }
alshabibca5706c2014-10-04 20:29:41 -0700230}