Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.net.group; |
| 17 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkArgument; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 21 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 22 | import java.util.Objects; |
| 23 | |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.net.DeviceId; |
| 26 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Default implementation of group description interface. |
| 29 | */ |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 30 | public 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 Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 36 | private final Integer givenGroupId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 39 | * Constructor to be used by north bound applications. |
| 40 | * NOTE: The caller of this subsystem MUST ensure the appCookie |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 41 | * 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 Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 47 | * |
| 48 | * @param deviceId device identifier |
| 49 | * @param type type of the group |
| 50 | * @param buckets immutable list of group bucket |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 51 | * @param appCookie immutable application cookie of type DefaultGroupKey |
| 52 | * to be associated with the group |
Thomas Vachuska | e1e8d66 | 2015-05-07 17:11:23 -0700 | [diff] [blame] | 53 | * @param groupId group identifier |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 54 | * @param appId application id |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 55 | */ |
| 56 | public DefaultGroupDescription(DeviceId deviceId, |
| 57 | GroupDescription.Type type, |
| 58 | GroupBuckets buckets, |
| 59 | GroupKey appCookie, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 60 | Integer groupId, |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 61 | ApplicationId appId) { |
| 62 | this.type = checkNotNull(type); |
| 63 | this.deviceId = checkNotNull(deviceId); |
| 64 | this.buckets = checkNotNull(buckets); |
Jayasree Ghosh | 2d45985 | 2016-07-02 19:06:52 +0530 | [diff] [blame] | 65 | if (this.type == GroupDescription.Type.INDIRECT) { |
| 66 | checkArgument(buckets.buckets().size() == 1, "Indirect group " + |
| 67 | "should have only one action bucket"); |
Charles Chan | c88ebaa | 2018-11-01 20:08:47 -0700 | [diff] [blame] | 68 | } |
| 69 | checkArgument(buckets.buckets().stream().allMatch(b -> b.type() == type), "Inconsistent bucket type"); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 70 | this.appCookie = appCookie; |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 71 | this.givenGroupId = groupId; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 72 | this.appId = appId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 76 | * 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 Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 83 | 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 Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 88 | this.givenGroupId = groupDesc.givenGroupId(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 89 | } |
| 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 Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 104 | this(deviceId, type, buckets, null, null, null); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns type of a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 109 | * |
| 110 | * @return GroupType group type |
| 111 | */ |
| 112 | @Override |
| 113 | public GroupDescription.Type type() { |
| 114 | return this.type; |
| 115 | } |
| 116 | |
| 117 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 118 | * Returns device identifier on which this group object is created. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 119 | * |
| 120 | * @return DeviceId device identifier |
| 121 | */ |
| 122 | @Override |
| 123 | public DeviceId deviceId() { |
| 124 | return this.deviceId; |
| 125 | } |
| 126 | |
| 127 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 128 | * Returns application identifier that has created this group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 129 | * |
| 130 | * @return ApplicationId application identifier |
| 131 | */ |
| 132 | @Override |
| 133 | public ApplicationId appId() { |
| 134 | return this.appId; |
| 135 | } |
| 136 | |
| 137 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 138 | * Returns application cookie associated with a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 139 | * |
| 140 | * @return GroupKey application cookie |
| 141 | */ |
| 142 | @Override |
| 143 | public GroupKey appCookie() { |
| 144 | return this.appCookie; |
| 145 | } |
| 146 | |
| 147 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 148 | * Returns group buckets of a group. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 149 | * |
| 150 | * @return GroupBuckets immutable list of group bucket |
| 151 | */ |
| 152 | @Override |
| 153 | public GroupBuckets buckets() { |
| 154 | return this.buckets; |
| 155 | } |
| 156 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 157 | /** |
| 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 Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 168 | @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 Milkey | 02ce274 | 2015-05-26 14:44:10 -0700 | [diff] [blame] | 190 | if (obj instanceof DefaultGroupDescription) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 191 | 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 200 | @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 HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 207 | .add("appCookie", appCookie) |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 208 | .add("givenGroupId", givenGroupId) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 209 | .toString(); |
| 210 | } |
Ray Milkey | 02ce274 | 2015-05-26 14:44:10 -0700 | [diff] [blame] | 211 | } |