blob: 119d535c26afbc8b74549324cb54ef82f377183e [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08003 *
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
Thomas Vachuskae1e8d662015-05-07 17:11:23 -070052 * @param groupId group identifier
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080053 * @param appId application id
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080054 */
55 public DefaultGroupDescription(DeviceId deviceId,
56 GroupDescription.Type type,
57 GroupBuckets buckets,
58 GroupKey appCookie,
Saurav Das100e3b82015-04-30 11:12:10 -070059 Integer groupId,
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080060 ApplicationId appId) {
61 this.type = checkNotNull(type);
62 this.deviceId = checkNotNull(deviceId);
63 this.buckets = checkNotNull(buckets);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080064 this.appCookie = appCookie;
Saurav Das100e3b82015-04-30 11:12:10 -070065 this.givenGroupId = groupId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080066 this.appId = appId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080067 }
68
69 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080070 * Constructor to be used by group subsystem internal components.
71 * Creates group description object from another object of same type.
72 *
73 * @param groupDesc group description object
74 *
75 */
76 public DefaultGroupDescription(GroupDescription groupDesc) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080077 this.type = groupDesc.type();
78 this.deviceId = groupDesc.deviceId();
79 this.buckets = groupDesc.buckets();
80 this.appCookie = groupDesc.appCookie();
81 this.appId = groupDesc.appId();
Saurav Das100e3b82015-04-30 11:12:10 -070082 this.givenGroupId = groupDesc.givenGroupId();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080083 }
84
85 /**
86 * Constructor to be used by group subsystem internal components.
87 * Creates group description object from the information retrieved
88 * from data plane.
89 *
90 * @param deviceId device identifier
91 * @param type type of the group
92 * @param buckets immutable list of group bucket
93 *
94 */
95 public DefaultGroupDescription(DeviceId deviceId,
96 GroupDescription.Type type,
97 GroupBuckets buckets) {
Saurav Das100e3b82015-04-30 11:12:10 -070098 this(deviceId, type, buckets, null, null, null);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080099 }
100
101 /**
102 * Returns type of a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800103 *
104 * @return GroupType group type
105 */
106 @Override
107 public GroupDescription.Type type() {
108 return this.type;
109 }
110
111 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800112 * Returns device identifier on which this group object is created.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800113 *
114 * @return DeviceId device identifier
115 */
116 @Override
117 public DeviceId deviceId() {
118 return this.deviceId;
119 }
120
121 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800122 * Returns application identifier that has created this group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800123 *
124 * @return ApplicationId application identifier
125 */
126 @Override
127 public ApplicationId appId() {
128 return this.appId;
129 }
130
131 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800132 * Returns application cookie associated with a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800133 *
134 * @return GroupKey application cookie
135 */
136 @Override
137 public GroupKey appCookie() {
138 return this.appCookie;
139 }
140
141 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800142 * Returns group buckets of a group.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800143 *
144 * @return GroupBuckets immutable list of group bucket
145 */
146 @Override
147 public GroupBuckets buckets() {
148 return this.buckets;
149 }
150
Saurav Das100e3b82015-04-30 11:12:10 -0700151 /**
152 * Returns groupId passed in by application.
153 *
154 * @return Integer group Id passed in by caller. May be null if caller passed
155 * in null during GroupDescription creation.
156 */
157 @Override
158 public Integer givenGroupId() {
159 return this.givenGroupId;
160 }
161
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800162 @Override
163 /*
164 * The deviceId, type and buckets are used for hash.
165 *
166 * (non-Javadoc)
167 * @see java.lang.Object#equals(java.lang.Object)
168 */
169 public int hashCode() {
170 return Objects.hash(deviceId, type, buckets);
171 }
172
173 @Override
174 /*
175 * The deviceId, type and buckets should be same.
176 *
177 * (non-Javadoc)
178 * @see java.lang.Object#equals(java.lang.Object)
179 */
180 public boolean equals(Object obj) {
181 if (this == obj) {
182 return true;
183 }
Ray Milkey02ce2742015-05-26 14:44:10 -0700184 if (obj instanceof DefaultGroupDescription) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800185 DefaultGroupDescription that = (DefaultGroupDescription) obj;
186 return Objects.equals(deviceId, that.deviceId) &&
187 Objects.equals(type, that.type) &&
188 Objects.equals(buckets, that.buckets);
189
190 }
191 return false;
192 }
193
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800194 @Override
195 public String toString() {
196 return toStringHelper(this)
197 .add("deviceId", deviceId)
198 .add("type", type)
199 .add("buckets", buckets)
200 .add("appId", appId)
Saurav Das100e3b82015-04-30 11:12:10 -0700201 .add("givenGroupId", givenGroupId)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800202 .toString();
203 }
Ray Milkey02ce2742015-05-26 14:44:10 -0700204}