Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 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.net.group; |
| 17 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 19 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 22 | import org.onosproject.core.GroupId; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * ONOS implementation of default group that is stored in the system. |
| 27 | */ |
| 28 | public class DefaultGroup extends DefaultGroupDescription |
| 29 | implements Group, StoredGroupEntry { |
| 30 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 31 | private GroupState state; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 32 | private boolean isGroupStateAddedFirstTime; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 33 | private long life; |
| 34 | private long packets; |
| 35 | private long bytes; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 36 | private long referenceCount; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 37 | private GroupId id; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 38 | private int age; |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 39 | private int failedRetryCount; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Ray Milkey | 69f0317 | 2015-06-10 13:14:15 -0700 | [diff] [blame] | 42 | * 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; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 53 | age = 0; |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 54 | failedRetryCount = 0; |
Ray Milkey | 69f0317 | 2015-06-10 13:14:15 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 58 | * 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 Milkey | 69f0317 | 2015-06-10 13:14:15 -0700 | [diff] [blame] | 65 | initialize(id); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 66 | } |
| 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 Milkey | 69f0317 | 2015-06-10 13:14:15 -0700 | [diff] [blame] | 82 | initialize(id); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 83 | } |
| 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 | |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public int age() { |
| 137 | return age; |
| 138 | } |
| 139 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 140 | @Override |
| 141 | public int failedRetryCount() { |
| 142 | return failedRetryCount; |
| 143 | } |
| 144 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 145 | /** |
| 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 Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 185 | @Override |
| 186 | public void setReferenceCount(long referenceCount) { |
| 187 | this.referenceCount = referenceCount; |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 188 | if (referenceCount == 0) { |
| 189 | age++; |
| 190 | } else { |
| 191 | age = 0; |
| 192 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public long referenceCount() { |
| 197 | return referenceCount; |
| 198 | } |
| 199 | |
Saurav Das | 137f27f | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 200 | @Override |
| 201 | public void incrFailedRetryCount() { |
| 202 | failedRetryCount++; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void setFailedRetryCount(int failedRetryCount) { |
| 207 | this.failedRetryCount = failedRetryCount; |
| 208 | } |
| 209 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 210 | /* |
| 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 Yuta | ca9cc8e | 2015-10-29 23:26:51 -0700 | [diff] [blame] | 218 | return Objects.hash(super.hashCode(), id); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 219 | } |
| 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 239 | |
| 240 | @Override |
| 241 | public String toString() { |
| 242 | return toStringHelper(this) |
| 243 | .add("description", super.toString()) |
| 244 | .add("groupid", id) |
| 245 | .add("state", state) |
alshabib | b028599 | 2016-03-28 23:30:37 -0700 | [diff] [blame] | 246 | .add("age", age) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 247 | .toString(); |
| 248 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 249 | |
| 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 Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 259 | } |