Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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"); |
| 68 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 69 | this.appCookie = appCookie; |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 70 | this.givenGroupId = groupId; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 71 | this.appId = appId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 75 | * 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 Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 82 | 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 Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 87 | this.givenGroupId = groupDesc.givenGroupId(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 88 | } |
| 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 Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 103 | this(deviceId, type, buckets, null, null, null); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns type of a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 108 | * |
| 109 | * @return GroupType group type |
| 110 | */ |
| 111 | @Override |
| 112 | public GroupDescription.Type type() { |
| 113 | return this.type; |
| 114 | } |
| 115 | |
| 116 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 117 | * Returns device identifier on which this group object is created. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 118 | * |
| 119 | * @return DeviceId device identifier |
| 120 | */ |
| 121 | @Override |
| 122 | public DeviceId deviceId() { |
| 123 | return this.deviceId; |
| 124 | } |
| 125 | |
| 126 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 127 | * Returns application identifier that has created this group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 128 | * |
| 129 | * @return ApplicationId application identifier |
| 130 | */ |
| 131 | @Override |
| 132 | public ApplicationId appId() { |
| 133 | return this.appId; |
| 134 | } |
| 135 | |
| 136 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 137 | * Returns application cookie associated with a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 138 | * |
| 139 | * @return GroupKey application cookie |
| 140 | */ |
| 141 | @Override |
| 142 | public GroupKey appCookie() { |
| 143 | return this.appCookie; |
| 144 | } |
| 145 | |
| 146 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 147 | * Returns group buckets of a group. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 148 | * |
| 149 | * @return GroupBuckets immutable list of group bucket |
| 150 | */ |
| 151 | @Override |
| 152 | public GroupBuckets buckets() { |
| 153 | return this.buckets; |
| 154 | } |
| 155 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 156 | /** |
| 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 Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 167 | @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 Milkey | 02ce274 | 2015-05-26 14:44:10 -0700 | [diff] [blame] | 189 | if (obj instanceof DefaultGroupDescription) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 190 | 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 Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 199 | @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) |
Yuta HIGUCHI | 625fb64 | 2016-09-01 16:10:24 -0700 | [diff] [blame] | 206 | .add("appCookie", appCookie) |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 207 | .add("givenGroupId", givenGroupId) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 208 | .toString(); |
| 209 | } |
Ray Milkey | 02ce274 | 2015-05-26 14:44:10 -0700 | [diff] [blame] | 210 | } |