blob: 4322482ac3be177754a21d17d0ba7e0a2813a7a6 [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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");
Charles Chanc88ebaa2018-11-01 20:08:47 -070068 }
69 checkArgument(buckets.buckets().stream().allMatch(b -> b.type() == type), "Inconsistent bucket type");
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080070 this.appCookie = appCookie;
Saurav Das100e3b82015-04-30 11:12:10 -070071 this.givenGroupId = groupId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080072 this.appId = appId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080073 }
74
75 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080076 * Constructor to be used by group subsystem internal components.
77 * Creates group description object from another object of same type.
78 *
79 * @param groupDesc group description object
80 *
81 */
82 public DefaultGroupDescription(GroupDescription groupDesc) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080083 this.type = groupDesc.type();
84 this.deviceId = groupDesc.deviceId();
85 this.buckets = groupDesc.buckets();
86 this.appCookie = groupDesc.appCookie();
87 this.appId = groupDesc.appId();
Saurav Das100e3b82015-04-30 11:12:10 -070088 this.givenGroupId = groupDesc.givenGroupId();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080089 }
90
91 /**
92 * Constructor to be used by group subsystem internal components.
93 * Creates group description object from the information retrieved
94 * from data plane.
95 *
96 * @param deviceId device identifier
97 * @param type type of the group
98 * @param buckets immutable list of group bucket
99 *
100 */
101 public DefaultGroupDescription(DeviceId deviceId,
102 GroupDescription.Type type,
103 GroupBuckets buckets) {
Saurav Das100e3b82015-04-30 11:12:10 -0700104 this(deviceId, type, buckets, null, null, null);
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800105 }
106
107 /**
108 * Returns type of a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800109 *
110 * @return GroupType group type
111 */
112 @Override
113 public GroupDescription.Type type() {
114 return this.type;
115 }
116
117 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800118 * Returns device identifier on which this group object is created.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800119 *
120 * @return DeviceId device identifier
121 */
122 @Override
123 public DeviceId deviceId() {
124 return this.deviceId;
125 }
126
127 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800128 * Returns application identifier that has created this group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800129 *
130 * @return ApplicationId application identifier
131 */
132 @Override
133 public ApplicationId appId() {
134 return this.appId;
135 }
136
137 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800138 * Returns application cookie associated with a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800139 *
140 * @return GroupKey application cookie
141 */
142 @Override
143 public GroupKey appCookie() {
144 return this.appCookie;
145 }
146
147 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800148 * Returns group buckets of a group.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800149 *
150 * @return GroupBuckets immutable list of group bucket
151 */
152 @Override
153 public GroupBuckets buckets() {
154 return this.buckets;
155 }
156
Saurav Das100e3b82015-04-30 11:12:10 -0700157 /**
158 * Returns groupId passed in by application.
159 *
160 * @return Integer group Id passed in by caller. May be null if caller passed
161 * in null during GroupDescription creation.
162 */
163 @Override
164 public Integer givenGroupId() {
165 return this.givenGroupId;
166 }
167
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800168 @Override
169 /*
170 * The deviceId, type and buckets are used for hash.
171 *
172 * (non-Javadoc)
173 * @see java.lang.Object#equals(java.lang.Object)
174 */
175 public int hashCode() {
176 return Objects.hash(deviceId, type, buckets);
177 }
178
179 @Override
180 /*
181 * The deviceId, type and buckets should be same.
182 *
183 * (non-Javadoc)
184 * @see java.lang.Object#equals(java.lang.Object)
185 */
186 public boolean equals(Object obj) {
187 if (this == obj) {
188 return true;
189 }
Ray Milkey02ce2742015-05-26 14:44:10 -0700190 if (obj instanceof DefaultGroupDescription) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800191 DefaultGroupDescription that = (DefaultGroupDescription) obj;
192 return Objects.equals(deviceId, that.deviceId) &&
193 Objects.equals(type, that.type) &&
194 Objects.equals(buckets, that.buckets);
195
196 }
197 return false;
198 }
199
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800200 @Override
201 public String toString() {
202 return toStringHelper(this)
203 .add("deviceId", deviceId)
204 .add("type", type)
205 .add("buckets", buckets)
206 .add("appId", appId)
Yuta HIGUCHI625fb642016-09-01 16:10:24 -0700207 .add("appCookie", appCookie)
Saurav Das100e3b82015-04-30 11:12:10 -0700208 .add("givenGroupId", givenGroupId)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800209 .toString();
210 }
Ray Milkey02ce2742015-05-26 14:44:10 -0700211}