blob: 1962e0a6ffb73453579d2be9d37ce1be9cb778c2 [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;
Jayasree Ghosh2d459852016-07-02 19:06:52 +053020import static com.google.common.base.Preconditions.checkArgument;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080021
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080022import java.util.Objects;
23
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080024import org.onosproject.core.ApplicationId;
25import org.onosproject.net.DeviceId;
26
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080027/**
28 * Default implementation of group description interface.
29 */
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080030public class DefaultGroupDescription implements GroupDescription {
31 private final GroupDescription.Type type;
32 private final GroupBuckets buckets;
33 private final GroupKey appCookie;
34 private final ApplicationId appId;
35 private final DeviceId deviceId;
Saurav Das100e3b82015-04-30 11:12:10 -070036 private final Integer givenGroupId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080037
38 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080039 * Constructor to be used by north bound applications.
40 * NOTE: The caller of this subsystem MUST ensure the appCookie
Saurav Das100e3b82015-04-30 11:12:10 -070041 * provided in this API is immutable.
42 * NOTE: The caller may choose to pass in 'null' for the groupId. This is
43 * the typical case, where the caller allows the group subsystem to choose
44 * the groupId in a globally unique way. If the caller passes in the groupId,
45 * the caller MUST ensure that the id is globally unique (not just unique
46 * per device).
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080047 *
48 * @param deviceId device identifier
49 * @param type type of the group
50 * @param buckets immutable list of group bucket
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070051 * @param appCookie immutable application cookie of type DefaultGroupKey
52 * to be associated with the group
Thomas Vachuskae1e8d662015-05-07 17:11:23 -070053 * @param groupId group identifier
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080054 * @param appId application id
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080055 */
56 public DefaultGroupDescription(DeviceId deviceId,
57 GroupDescription.Type type,
58 GroupBuckets buckets,
59 GroupKey appCookie,
Saurav Das100e3b82015-04-30 11:12:10 -070060 Integer groupId,
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080061 ApplicationId appId) {
62 this.type = checkNotNull(type);
63 this.deviceId = checkNotNull(deviceId);
64 this.buckets = checkNotNull(buckets);
Jayasree Ghosh2d459852016-07-02 19:06:52 +053065 if (this.type == GroupDescription.Type.INDIRECT) {
66 checkArgument(buckets.buckets().size() == 1, "Indirect group " +
67 "should have only one action bucket");
68 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080069 this.appCookie = appCookie;
Saurav Das100e3b82015-04-30 11:12:10 -070070 this.givenGroupId = groupId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080071 this.appId = appId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080072 }
73
74 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080075 * Constructor to be used by group subsystem internal components.
76 * Creates group description object from another object of same type.
77 *
78 * @param groupDesc group description object
79 *
80 */
81 public DefaultGroupDescription(GroupDescription groupDesc) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080082 this.type = groupDesc.type();
83 this.deviceId = groupDesc.deviceId();
84 this.buckets = groupDesc.buckets();
85 this.appCookie = groupDesc.appCookie();
86 this.appId = groupDesc.appId();
Saurav Das100e3b82015-04-30 11:12:10 -070087 this.givenGroupId = groupDesc.givenGroupId();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080088 }
89
90 /**
91 * Constructor to be used by group subsystem internal components.
92 * Creates group description object from the information retrieved
93 * from data plane.
94 *
95 * @param deviceId device identifier
96 * @param type type of the group
97 * @param buckets immutable list of group bucket
98 *
99 */
100 public DefaultGroupDescription(DeviceId deviceId,
101 GroupDescription.Type type,
102 GroupBuckets buckets) {
Saurav Das100e3b82015-04-30 11:12:10 -0700103 this(deviceId, type, buckets, null, null, null);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800104 }
105
106 /**
107 * Returns type of a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800108 *
109 * @return GroupType group type
110 */
111 @Override
112 public GroupDescription.Type type() {
113 return this.type;
114 }
115
116 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800117 * Returns device identifier on which this group object is created.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800118 *
119 * @return DeviceId device identifier
120 */
121 @Override
122 public DeviceId deviceId() {
123 return this.deviceId;
124 }
125
126 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800127 * Returns application identifier that has created this group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800128 *
129 * @return ApplicationId application identifier
130 */
131 @Override
132 public ApplicationId appId() {
133 return this.appId;
134 }
135
136 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800137 * Returns application cookie associated with a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800138 *
139 * @return GroupKey application cookie
140 */
141 @Override
142 public GroupKey appCookie() {
143 return this.appCookie;
144 }
145
146 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800147 * Returns group buckets of a group.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800148 *
149 * @return GroupBuckets immutable list of group bucket
150 */
151 @Override
152 public GroupBuckets buckets() {
153 return this.buckets;
154 }
155
Saurav Das100e3b82015-04-30 11:12:10 -0700156 /**
157 * Returns groupId passed in by application.
158 *
159 * @return Integer group Id passed in by caller. May be null if caller passed
160 * in null during GroupDescription creation.
161 */
162 @Override
163 public Integer givenGroupId() {
164 return this.givenGroupId;
165 }
166
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800167 @Override
168 /*
169 * The deviceId, type and buckets are used for hash.
170 *
171 * (non-Javadoc)
172 * @see java.lang.Object#equals(java.lang.Object)
173 */
174 public int hashCode() {
175 return Objects.hash(deviceId, type, buckets);
176 }
177
178 @Override
179 /*
180 * The deviceId, type and buckets should be same.
181 *
182 * (non-Javadoc)
183 * @see java.lang.Object#equals(java.lang.Object)
184 */
185 public boolean equals(Object obj) {
186 if (this == obj) {
187 return true;
188 }
Ray Milkey02ce2742015-05-26 14:44:10 -0700189 if (obj instanceof DefaultGroupDescription) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800190 DefaultGroupDescription that = (DefaultGroupDescription) obj;
191 return Objects.equals(deviceId, that.deviceId) &&
192 Objects.equals(type, that.type) &&
193 Objects.equals(buckets, that.buckets);
194
195 }
196 return false;
197 }
198
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800199 @Override
200 public String toString() {
201 return toStringHelper(this)
202 .add("deviceId", deviceId)
203 .add("type", type)
204 .add("buckets", buckets)
205 .add("appId", appId)
Saurav Das100e3b82015-04-30 11:12:10 -0700206 .add("givenGroupId", givenGroupId)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800207 .toString();
208 }
Ray Milkey02ce2742015-05-26 14:44:10 -0700209}