Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 38 | |
| 39 | /** |
Ray Milkey | 69f0317 | 2015-06-10 13:14:15 -0700 | [diff] [blame] | 40 | * Initializes default values. |
| 41 | * |
| 42 | * @param newId group id for new group |
| 43 | */ |
| 44 | private void initialize(GroupId newId) { |
| 45 | id = newId; |
| 46 | state = GroupState.PENDING_ADD; |
| 47 | life = 0; |
| 48 | packets = 0; |
| 49 | bytes = 0; |
| 50 | referenceCount = 0; |
| 51 | } |
| 52 | |
| 53 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 54 | * Default group object constructor with the parameters. |
| 55 | * |
| 56 | * @param id group identifier |
| 57 | * @param groupDesc group description parameters |
| 58 | */ |
| 59 | public DefaultGroup(GroupId id, GroupDescription groupDesc) { |
| 60 | super(groupDesc); |
Ray Milkey | 69f0317 | 2015-06-10 13:14:15 -0700 | [diff] [blame] | 61 | initialize(id); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Default group object constructor with the available information |
| 66 | * from data plane. |
| 67 | * |
| 68 | * @param id group identifier |
| 69 | * @param deviceId device identifier |
| 70 | * @param type type of the group |
| 71 | * @param buckets immutable list of group bucket |
| 72 | */ |
| 73 | public DefaultGroup(GroupId id, |
| 74 | DeviceId deviceId, |
| 75 | GroupDescription.Type type, |
| 76 | GroupBuckets buckets) { |
| 77 | super(deviceId, type, buckets); |
Ray Milkey | 69f0317 | 2015-06-10 13:14:15 -0700 | [diff] [blame] | 78 | initialize(id); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns group identifier associated with a group object. |
| 83 | * |
| 84 | * @return GroupId Group Identifier |
| 85 | */ |
| 86 | @Override |
| 87 | public GroupId id() { |
| 88 | return this.id; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns current state of a group object. |
| 93 | * |
| 94 | * @return GroupState Group State |
| 95 | */ |
| 96 | @Override |
| 97 | public GroupState state() { |
| 98 | return this.state; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns the number of milliseconds this group has been alive. |
| 103 | * |
| 104 | * @return number of millis |
| 105 | */ |
| 106 | @Override |
| 107 | public long life() { |
| 108 | return this.life; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns the number of packets processed by this group. |
| 113 | * |
| 114 | * @return number of packets |
| 115 | */ |
| 116 | @Override |
| 117 | public long packets() { |
| 118 | return this.packets; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Returns the number of bytes processed by this group. |
| 123 | * |
| 124 | * @return number of bytes |
| 125 | */ |
| 126 | @Override |
| 127 | public long bytes() { |
| 128 | return this.bytes; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sets the new state for this entry. |
| 133 | * |
| 134 | * @param newState new group entry state. |
| 135 | */ |
| 136 | @Override |
| 137 | public void setState(Group.GroupState newState) { |
| 138 | this.state = newState; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets how long this entry has been entered in the system. |
| 143 | * |
| 144 | * @param life epoch time |
| 145 | */ |
| 146 | @Override |
| 147 | public void setLife(long life) { |
| 148 | this.life = life; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sets number of packets processed by this group entry. |
| 153 | * |
| 154 | * @param packets a long value |
| 155 | */ |
| 156 | @Override |
| 157 | public void setPackets(long packets) { |
| 158 | this.packets = packets; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Sets number of bytes processed by this group entry. |
| 163 | * |
| 164 | * @param bytes a long value |
| 165 | */ |
| 166 | @Override |
| 167 | public void setBytes(long bytes) { |
| 168 | this.bytes = bytes; |
| 169 | } |
| 170 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 171 | @Override |
| 172 | public void setReferenceCount(long referenceCount) { |
| 173 | this.referenceCount = referenceCount; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public long referenceCount() { |
| 178 | return referenceCount; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * The deviceId, type and buckets are used for hash. |
| 183 | * |
| 184 | * (non-Javadoc) |
| 185 | * @see java.lang.Object#equals(java.lang.Object) |
| 186 | */ |
| 187 | @Override |
| 188 | public int hashCode() { |
| 189 | return super.hashCode() + Objects.hash(id); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * The deviceId, groupId, type and buckets should be same. |
| 194 | * |
| 195 | * (non-Javadoc) |
| 196 | * @see java.lang.Object#equals(java.lang.Object) |
| 197 | */ |
| 198 | @Override |
| 199 | public boolean equals(Object obj) { |
| 200 | if (this == obj) { |
| 201 | return true; |
| 202 | } |
| 203 | if (obj instanceof DefaultGroup) { |
| 204 | DefaultGroup that = (DefaultGroup) obj; |
| 205 | return super.equals(obj) && |
| 206 | Objects.equals(id, that.id); |
| 207 | } |
| 208 | return false; |
| 209 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 210 | |
| 211 | @Override |
| 212 | public String toString() { |
| 213 | return toStringHelper(this) |
| 214 | .add("description", super.toString()) |
| 215 | .add("groupid", id) |
| 216 | .add("state", state) |
| 217 | .toString(); |
| 218 | } |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 219 | |
| 220 | @Override |
| 221 | public void setIsGroupStateAddedFirstTime(boolean isGroupStateAddedFirstTime) { |
| 222 | this.isGroupStateAddedFirstTime = isGroupStateAddedFirstTime; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public boolean isGroupStateAddedFirstTime() { |
| 227 | return isGroupStateAddedFirstTime; |
| 228 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 229 | } |