blob: 5ea2d2ec2e610d74d7ca6bb95010fd2525bc14d9 [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -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 Vavilapalli56db94f2015-01-22 22:30:17 -080019import static com.google.common.base.Preconditions.checkNotNull;
20
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080021import java.util.Objects;
22
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.net.DeviceId;
25
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080026/**
27 * Default implementation of group description interface.
28 */
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080029public class DefaultGroupDescription implements GroupDescription {
30 private final GroupDescription.Type type;
31 private final GroupBuckets buckets;
32 private final GroupKey appCookie;
33 private final ApplicationId appId;
34 private final DeviceId deviceId;
Saurav Das100e3b82015-04-30 11:12:10 -070035 private final Integer givenGroupId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080036
37 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080038 * Constructor to be used by north bound applications.
39 * NOTE: The caller of this subsystem MUST ensure the appCookie
Saurav Das100e3b82015-04-30 11:12:10 -070040 * provided in this API is immutable.
41 * NOTE: The caller may choose to pass in 'null' for the groupId. This is
42 * the typical case, where the caller allows the group subsystem to choose
43 * the groupId in a globally unique way. If the caller passes in the groupId,
44 * the caller MUST ensure that the id is globally unique (not just unique
45 * per device).
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080046 *
47 * @param deviceId device identifier
48 * @param type type of the group
49 * @param buckets immutable list of group bucket
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070050 * @param appCookie immutable application cookie of type DefaultGroupKey
51 * to be associated with the group
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080052 * @param appId application id
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080053 */
54 public DefaultGroupDescription(DeviceId deviceId,
55 GroupDescription.Type type,
56 GroupBuckets buckets,
57 GroupKey appCookie,
Saurav Das100e3b82015-04-30 11:12:10 -070058 Integer groupId,
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080059 ApplicationId appId) {
60 this.type = checkNotNull(type);
61 this.deviceId = checkNotNull(deviceId);
62 this.buckets = checkNotNull(buckets);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080063 this.appCookie = appCookie;
Saurav Das100e3b82015-04-30 11:12:10 -070064 this.givenGroupId = groupId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080065 this.appId = appId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080066 }
67
68 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080069 * Constructor to be used by group subsystem internal components.
70 * Creates group description object from another object of same type.
71 *
72 * @param groupDesc group description object
73 *
74 */
75 public DefaultGroupDescription(GroupDescription groupDesc) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080076 this.type = groupDesc.type();
77 this.deviceId = groupDesc.deviceId();
78 this.buckets = groupDesc.buckets();
79 this.appCookie = groupDesc.appCookie();
80 this.appId = groupDesc.appId();
Saurav Das100e3b82015-04-30 11:12:10 -070081 this.givenGroupId = groupDesc.givenGroupId();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080082 }
83
84 /**
85 * Constructor to be used by group subsystem internal components.
86 * Creates group description object from the information retrieved
87 * from data plane.
88 *
89 * @param deviceId device identifier
90 * @param type type of the group
91 * @param buckets immutable list of group bucket
92 *
93 */
94 public DefaultGroupDescription(DeviceId deviceId,
95 GroupDescription.Type type,
96 GroupBuckets buckets) {
Saurav Das100e3b82015-04-30 11:12:10 -070097 this(deviceId, type, buckets, null, null, null);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080098 }
99
100 /**
101 * Returns type of a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800102 *
103 * @return GroupType group type
104 */
105 @Override
106 public GroupDescription.Type type() {
107 return this.type;
108 }
109
110 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800111 * Returns device identifier on which this group object is created.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800112 *
113 * @return DeviceId device identifier
114 */
115 @Override
116 public DeviceId deviceId() {
117 return this.deviceId;
118 }
119
120 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800121 * Returns application identifier that has created this group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800122 *
123 * @return ApplicationId application identifier
124 */
125 @Override
126 public ApplicationId appId() {
127 return this.appId;
128 }
129
130 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800131 * Returns application cookie associated with a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800132 *
133 * @return GroupKey application cookie
134 */
135 @Override
136 public GroupKey appCookie() {
137 return this.appCookie;
138 }
139
140 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800141 * Returns group buckets of a group.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800142 *
143 * @return GroupBuckets immutable list of group bucket
144 */
145 @Override
146 public GroupBuckets buckets() {
147 return this.buckets;
148 }
149
Saurav Das100e3b82015-04-30 11:12:10 -0700150 /**
151 * Returns groupId passed in by application.
152 *
153 * @return Integer group Id passed in by caller. May be null if caller passed
154 * in null during GroupDescription creation.
155 */
156 @Override
157 public Integer givenGroupId() {
158 return this.givenGroupId;
159 }
160
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800161 @Override
162 /*
163 * The deviceId, type and buckets are used for hash.
164 *
165 * (non-Javadoc)
166 * @see java.lang.Object#equals(java.lang.Object)
167 */
168 public int hashCode() {
169 return Objects.hash(deviceId, type, buckets);
170 }
171
172 @Override
173 /*
174 * The deviceId, type and buckets should be same.
175 *
176 * (non-Javadoc)
177 * @see java.lang.Object#equals(java.lang.Object)
178 */
179 public boolean equals(Object obj) {
180 if (this == obj) {
181 return true;
182 }
183 if (obj instanceof DefaultGroupDescription) {
184 DefaultGroupDescription that = (DefaultGroupDescription) obj;
185 return Objects.equals(deviceId, that.deviceId) &&
186 Objects.equals(type, that.type) &&
187 Objects.equals(buckets, that.buckets);
188
189 }
190 return false;
191 }
192
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800193 @Override
194 public String toString() {
195 return toStringHelper(this)
196 .add("deviceId", deviceId)
197 .add("type", type)
198 .add("buckets", buckets)
199 .add("appId", appId)
Saurav Das100e3b82015-04-30 11:12:10 -0700200 .add("givenGroupId", givenGroupId)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800201 .toString();
202 }
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800203}