blob: 92486254cc9711dbd641ec2ca5560ce733250b42 [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 -080019import static org.slf4j.LoggerFactory.getLogger;
20
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080021import java.util.Objects;
22
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080023import org.onosproject.core.GroupId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080024import org.onosproject.net.DeviceId;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080025import org.slf4j.Logger;
26
27/**
28 * ONOS implementation of default group that is stored in the system.
29 */
30public class DefaultGroup extends DefaultGroupDescription
31 implements Group, StoredGroupEntry {
32
33 private final Logger log = getLogger(getClass());
34
35 private GroupState state;
36 private long life;
37 private long packets;
38 private long bytes;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080039 private long referenceCount;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080040 private GroupId id;
41
42 /**
43 * Default group object constructor with the parameters.
44 *
45 * @param id group identifier
46 * @param groupDesc group description parameters
47 */
48 public DefaultGroup(GroupId id, GroupDescription groupDesc) {
49 super(groupDesc);
50 this.id = id;
51 this.state = GroupState.PENDING_ADD;
52 this.life = 0;
53 this.packets = 0;
54 this.bytes = 0;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080055 this.referenceCount = 0;
56 }
57
58 /**
59 * Default group object constructor with the available information
60 * from data plane.
61 *
62 * @param id group identifier
63 * @param deviceId device identifier
64 * @param type type of the group
65 * @param buckets immutable list of group bucket
66 */
67 public DefaultGroup(GroupId id,
68 DeviceId deviceId,
69 GroupDescription.Type type,
70 GroupBuckets buckets) {
71 super(deviceId, type, buckets);
72 this.id = id;
73 this.state = GroupState.PENDING_ADD;
74 this.life = 0;
75 this.packets = 0;
76 this.bytes = 0;
77 this.referenceCount = 0;
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080078 }
79
80 /**
81 * Returns group identifier associated with a group object.
82 *
83 * @return GroupId Group Identifier
84 */
85 @Override
86 public GroupId id() {
87 return this.id;
88 }
89
90 /**
91 * Returns current state of a group object.
92 *
93 * @return GroupState Group State
94 */
95 @Override
96 public GroupState state() {
97 return this.state;
98 }
99
100 /**
101 * Returns the number of milliseconds this group has been alive.
102 *
103 * @return number of millis
104 */
105 @Override
106 public long life() {
107 return this.life;
108 }
109
110 /**
111 * Returns the number of packets processed by this group.
112 *
113 * @return number of packets
114 */
115 @Override
116 public long packets() {
117 return this.packets;
118 }
119
120 /**
121 * Returns the number of bytes processed by this group.
122 *
123 * @return number of bytes
124 */
125 @Override
126 public long bytes() {
127 return this.bytes;
128 }
129
130 /**
131 * Sets the new state for this entry.
132 *
133 * @param newState new group entry state.
134 */
135 @Override
136 public void setState(Group.GroupState newState) {
137 this.state = newState;
138 }
139
140 /**
141 * Sets how long this entry has been entered in the system.
142 *
143 * @param life epoch time
144 */
145 @Override
146 public void setLife(long life) {
147 this.life = life;
148 }
149
150 /**
151 * Sets number of packets processed by this group entry.
152 *
153 * @param packets a long value
154 */
155 @Override
156 public void setPackets(long packets) {
157 this.packets = packets;
158 }
159
160 /**
161 * Sets number of bytes processed by this group entry.
162 *
163 * @param bytes a long value
164 */
165 @Override
166 public void setBytes(long bytes) {
167 this.bytes = bytes;
168 }
169
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800170 @Override
171 public void setReferenceCount(long referenceCount) {
172 this.referenceCount = referenceCount;
173 }
174
175 @Override
176 public long referenceCount() {
177 return referenceCount;
178 }
179
180 /*
181 * The deviceId, type and buckets are used for hash.
182 *
183 * (non-Javadoc)
184 * @see java.lang.Object#equals(java.lang.Object)
185 */
186 @Override
187 public int hashCode() {
188 return super.hashCode() + Objects.hash(id);
189 }
190
191 /*
192 * The deviceId, groupId, type and buckets should be same.
193 *
194 * (non-Javadoc)
195 * @see java.lang.Object#equals(java.lang.Object)
196 */
197 @Override
198 public boolean equals(Object obj) {
199 if (this == obj) {
200 return true;
201 }
202 if (obj instanceof DefaultGroup) {
203 DefaultGroup that = (DefaultGroup) obj;
204 return super.equals(obj) &&
205 Objects.equals(id, that.id);
206 }
207 return false;
208 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800209
210 @Override
211 public String toString() {
212 return toStringHelper(this)
213 .add("description", super.toString())
214 .add("groupid", id)
215 .add("state", state)
216 .toString();
217 }
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800218}