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