blob: 14ceef6f3426267c2478b0d3f2dcb98be4a5b252 [file] [log] [blame]
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080039
40 /**
Ray Milkey69f03172015-06-10 13:14:15 -070041 * Initializes default values.
42 *
43 * @param newId group id for new group
44 */
45 private void initialize(GroupId newId) {
46 id = newId;
47 state = GroupState.PENDING_ADD;
48 life = 0;
49 packets = 0;
50 bytes = 0;
51 referenceCount = 0;
alshabibb0285992016-03-28 23:30:37 -070052 age = 0;
Ray Milkey69f03172015-06-10 13:14:15 -070053 }
54
55 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080056 * Default group object constructor with the parameters.
57 *
58 * @param id group identifier
59 * @param groupDesc group description parameters
60 */
61 public DefaultGroup(GroupId id, GroupDescription groupDesc) {
62 super(groupDesc);
Ray Milkey69f03172015-06-10 13:14:15 -070063 initialize(id);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080064 }
65
66 /**
67 * Default group object constructor with the available information
68 * from data plane.
69 *
70 * @param id group identifier
71 * @param deviceId device identifier
72 * @param type type of the group
73 * @param buckets immutable list of group bucket
74 */
75 public DefaultGroup(GroupId id,
76 DeviceId deviceId,
77 GroupDescription.Type type,
78 GroupBuckets buckets) {
79 super(deviceId, type, buckets);
Ray Milkey69f03172015-06-10 13:14:15 -070080 initialize(id);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080081 }
82
83 /**
84 * Returns group identifier associated with a group object.
85 *
86 * @return GroupId Group Identifier
87 */
88 @Override
89 public GroupId id() {
90 return this.id;
91 }
92
93 /**
94 * Returns current state of a group object.
95 *
96 * @return GroupState Group State
97 */
98 @Override
99 public GroupState state() {
100 return this.state;
101 }
102
103 /**
104 * Returns the number of milliseconds this group has been alive.
105 *
106 * @return number of millis
107 */
108 @Override
109 public long life() {
110 return this.life;
111 }
112
113 /**
114 * Returns the number of packets processed by this group.
115 *
116 * @return number of packets
117 */
118 @Override
119 public long packets() {
120 return this.packets;
121 }
122
123 /**
124 * Returns the number of bytes processed by this group.
125 *
126 * @return number of bytes
127 */
128 @Override
129 public long bytes() {
130 return this.bytes;
131 }
132
alshabibb0285992016-03-28 23:30:37 -0700133 @Override
134 public int age() {
135 return age;
136 }
137
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800138 /**
139 * Sets the new state for this entry.
140 *
141 * @param newState new group entry state.
142 */
143 @Override
144 public void setState(Group.GroupState newState) {
145 this.state = newState;
146 }
147
148 /**
149 * Sets how long this entry has been entered in the system.
150 *
151 * @param life epoch time
152 */
153 @Override
154 public void setLife(long life) {
155 this.life = life;
156 }
157
158 /**
159 * Sets number of packets processed by this group entry.
160 *
161 * @param packets a long value
162 */
163 @Override
164 public void setPackets(long packets) {
165 this.packets = packets;
166 }
167
168 /**
169 * Sets number of bytes processed by this group entry.
170 *
171 * @param bytes a long value
172 */
173 @Override
174 public void setBytes(long bytes) {
175 this.bytes = bytes;
176 }
177
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800178 @Override
179 public void setReferenceCount(long referenceCount) {
180 this.referenceCount = referenceCount;
alshabibb0285992016-03-28 23:30:37 -0700181 if (referenceCount == 0) {
182 age++;
183 } else {
184 age = 0;
185 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800186 }
187
188 @Override
189 public long referenceCount() {
190 return referenceCount;
191 }
192
193 /*
194 * The deviceId, type and buckets are used for hash.
195 *
196 * (non-Javadoc)
197 * @see java.lang.Object#equals(java.lang.Object)
198 */
199 @Override
200 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700201 return Objects.hash(super.hashCode(), id);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800202 }
203
204 /*
205 * The deviceId, groupId, type and buckets should be same.
206 *
207 * (non-Javadoc)
208 * @see java.lang.Object#equals(java.lang.Object)
209 */
210 @Override
211 public boolean equals(Object obj) {
212 if (this == obj) {
213 return true;
214 }
215 if (obj instanceof DefaultGroup) {
216 DefaultGroup that = (DefaultGroup) obj;
217 return super.equals(obj) &&
218 Objects.equals(id, that.id);
219 }
220 return false;
221 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800222
223 @Override
224 public String toString() {
225 return toStringHelper(this)
226 .add("description", super.toString())
227 .add("groupid", id)
228 .add("state", state)
alshabibb0285992016-03-28 23:30:37 -0700229 .add("age", age)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800230 .toString();
231 }
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700232
233 @Override
234 public void setIsGroupStateAddedFirstTime(boolean isGroupStateAddedFirstTime) {
235 this.isGroupStateAddedFirstTime = isGroupStateAddedFirstTime;
236 }
237
238 @Override
239 public boolean isGroupStateAddedFirstTime() {
240 return isGroupStateAddedFirstTime;
241 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800242}