blob: 97f8aedfefb6843b8d7b32e0a0001d60660ae603 [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 /**
Ray Milkey69f03172015-06-10 13:14:15 -070040 * 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 Vavilapalli0599d512015-01-30 12:57:56 -080054 * 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 Milkey69f03172015-06-10 13:14:15 -070061 initialize(id);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080062 }
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 Milkey69f03172015-06-10 13:14:15 -070078 initialize(id);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080079 }
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 Vavilapalli45c27c82015-01-30 12:57:56 -0800171 @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() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700189 return Objects.hash(super.hashCode(), id);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800190 }
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 Vavilapallied12ae52015-02-09 14:43:19 -0800210
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 Vavilapalli717361f2015-03-16 12:06:04 -0700219
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 Vavilapalli0599d512015-01-30 12:57:56 -0800229}