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