blob: d0777eced732cc71f8485bfb41930a6da83bdfeb [file] [log] [blame]
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -08003 *
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 */
16package org.onosproject.net.group;
17
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080018import static com.google.common.base.MoreObjects.toStringHelper;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080019
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080020import java.util.Objects;
21
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080022import org.onosproject.core.GroupId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080023import org.onosproject.net.DeviceId;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080024
25/**
26 * ONOS implementation of default group that is stored in the system.
27 */
28public class DefaultGroup extends DefaultGroupDescription
29 implements Group, StoredGroupEntry {
30
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080031 private GroupState state;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070032 private boolean isGroupStateAddedFirstTime;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080033 private long life;
34 private long packets;
35 private long bytes;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080036 private long referenceCount;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080037 private GroupId id;
alshabibb0285992016-03-28 23:30:37 -070038 private int age;
Saurav Das137f27f2018-06-11 17:02:31 -070039 private int failedRetryCount;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080040
41 /**
Ray Milkey69f03172015-06-10 13:14:15 -070042 * Initializes default values.
43 *
44 * @param newId group id for new group
45 */
46 private void initialize(GroupId newId) {
47 id = newId;
48 state = GroupState.PENDING_ADD;
49 life = 0;
50 packets = 0;
51 bytes = 0;
52 referenceCount = 0;
alshabibb0285992016-03-28 23:30:37 -070053 age = 0;
Saurav Das137f27f2018-06-11 17:02:31 -070054 failedRetryCount = 0;
Ray Milkey69f03172015-06-10 13:14:15 -070055 }
56
57 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080058 * Default group object constructor with the parameters.
59 *
60 * @param id group identifier
61 * @param groupDesc group description parameters
62 */
63 public DefaultGroup(GroupId id, GroupDescription groupDesc) {
64 super(groupDesc);
Ray Milkey69f03172015-06-10 13:14:15 -070065 initialize(id);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080066 }
67
68 /**
69 * Default group object constructor with the available information
70 * from data plane.
71 *
72 * @param id group identifier
73 * @param deviceId device identifier
74 * @param type type of the group
75 * @param buckets immutable list of group bucket
76 */
77 public DefaultGroup(GroupId id,
78 DeviceId deviceId,
79 GroupDescription.Type type,
80 GroupBuckets buckets) {
81 super(deviceId, type, buckets);
Ray Milkey69f03172015-06-10 13:14:15 -070082 initialize(id);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080083 }
84
85 /**
86 * Returns group identifier associated with a group object.
87 *
88 * @return GroupId Group Identifier
89 */
90 @Override
91 public GroupId id() {
92 return this.id;
93 }
94
95 /**
96 * Returns current state of a group object.
97 *
98 * @return GroupState Group State
99 */
100 @Override
101 public GroupState state() {
102 return this.state;
103 }
104
105 /**
106 * Returns the number of milliseconds this group has been alive.
107 *
108 * @return number of millis
109 */
110 @Override
111 public long life() {
112 return this.life;
113 }
114
115 /**
116 * Returns the number of packets processed by this group.
117 *
118 * @return number of packets
119 */
120 @Override
121 public long packets() {
122 return this.packets;
123 }
124
125 /**
126 * Returns the number of bytes processed by this group.
127 *
128 * @return number of bytes
129 */
130 @Override
131 public long bytes() {
132 return this.bytes;
133 }
134
alshabibb0285992016-03-28 23:30:37 -0700135 @Override
136 public int age() {
137 return age;
138 }
139
Saurav Das137f27f2018-06-11 17:02:31 -0700140 @Override
141 public int failedRetryCount() {
142 return failedRetryCount;
143 }
144
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800145 /**
146 * Sets the new state for this entry.
147 *
148 * @param newState new group entry state.
149 */
150 @Override
151 public void setState(Group.GroupState newState) {
152 this.state = newState;
153 }
154
155 /**
156 * Sets how long this entry has been entered in the system.
157 *
158 * @param life epoch time
159 */
160 @Override
161 public void setLife(long life) {
162 this.life = life;
163 }
164
165 /**
166 * Sets number of packets processed by this group entry.
167 *
168 * @param packets a long value
169 */
170 @Override
171 public void setPackets(long packets) {
172 this.packets = packets;
173 }
174
175 /**
176 * Sets number of bytes processed by this group entry.
177 *
178 * @param bytes a long value
179 */
180 @Override
181 public void setBytes(long bytes) {
182 this.bytes = bytes;
183 }
184
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800185 @Override
186 public void setReferenceCount(long referenceCount) {
187 this.referenceCount = referenceCount;
alshabibb0285992016-03-28 23:30:37 -0700188 if (referenceCount == 0) {
189 age++;
190 } else {
191 age = 0;
192 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800193 }
194
195 @Override
196 public long referenceCount() {
197 return referenceCount;
198 }
199
Saurav Das137f27f2018-06-11 17:02:31 -0700200 @Override
201 public void incrFailedRetryCount() {
202 failedRetryCount++;
203 }
204
205 @Override
206 public void setFailedRetryCount(int failedRetryCount) {
207 this.failedRetryCount = failedRetryCount;
208 }
209
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800210 /*
211 * The deviceId, type and buckets are used for hash.
212 *
213 * (non-Javadoc)
214 * @see java.lang.Object#equals(java.lang.Object)
215 */
216 @Override
217 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700218 return Objects.hash(super.hashCode(), id);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800219 }
220
221 /*
222 * The deviceId, groupId, type and buckets should be same.
223 *
224 * (non-Javadoc)
225 * @see java.lang.Object#equals(java.lang.Object)
226 */
227 @Override
228 public boolean equals(Object obj) {
229 if (this == obj) {
230 return true;
231 }
232 if (obj instanceof DefaultGroup) {
233 DefaultGroup that = (DefaultGroup) obj;
234 return super.equals(obj) &&
235 Objects.equals(id, that.id);
236 }
237 return false;
238 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800239
240 @Override
241 public String toString() {
242 return toStringHelper(this)
243 .add("description", super.toString())
244 .add("groupid", id)
245 .add("state", state)
alshabibb0285992016-03-28 23:30:37 -0700246 .add("age", age)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800247 .toString();
248 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700249
250 @Override
251 public void setIsGroupStateAddedFirstTime(boolean isGroupStateAddedFirstTime) {
252 this.isGroupStateAddedFirstTime = isGroupStateAddedFirstTime;
253 }
254
255 @Override
256 public boolean isGroupStateAddedFirstTime() {
257 return isGroupStateAddedFirstTime;
258 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800259}