Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 20 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Default implementation of group description interface. |
| 28 | */ |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 29 | public 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 Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 35 | private final Integer givenGroupId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 38 | * Constructor to be used by north bound applications. |
| 39 | * NOTE: The caller of this subsystem MUST ensure the appCookie |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 40 | * 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 Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 46 | * |
| 47 | * @param deviceId device identifier |
| 48 | * @param type type of the group |
| 49 | * @param buckets immutable list of group bucket |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 50 | * @param appCookie immutable application cookie of type DefaultGroupKey |
| 51 | * to be associated with the group |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 52 | * @param appId application id |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 53 | */ |
| 54 | public DefaultGroupDescription(DeviceId deviceId, |
| 55 | GroupDescription.Type type, |
| 56 | GroupBuckets buckets, |
| 57 | GroupKey appCookie, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 58 | Integer groupId, |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 59 | ApplicationId appId) { |
| 60 | this.type = checkNotNull(type); |
| 61 | this.deviceId = checkNotNull(deviceId); |
| 62 | this.buckets = checkNotNull(buckets); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 63 | this.appCookie = appCookie; |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 64 | this.givenGroupId = groupId; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 65 | this.appId = appId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 69 | * Constructor to be used by group subsystem internal components. |
| 70 | * Creates group description object from another object of same type. |
| 71 | * |
| 72 | * @param groupDesc group description object |
| 73 | * |
| 74 | */ |
| 75 | public DefaultGroupDescription(GroupDescription groupDesc) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 76 | this.type = groupDesc.type(); |
| 77 | this.deviceId = groupDesc.deviceId(); |
| 78 | this.buckets = groupDesc.buckets(); |
| 79 | this.appCookie = groupDesc.appCookie(); |
| 80 | this.appId = groupDesc.appId(); |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 81 | this.givenGroupId = groupDesc.givenGroupId(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Constructor to be used by group subsystem internal components. |
| 86 | * Creates group description object from the information retrieved |
| 87 | * from data plane. |
| 88 | * |
| 89 | * @param deviceId device identifier |
| 90 | * @param type type of the group |
| 91 | * @param buckets immutable list of group bucket |
| 92 | * |
| 93 | */ |
| 94 | public DefaultGroupDescription(DeviceId deviceId, |
| 95 | GroupDescription.Type type, |
| 96 | GroupBuckets buckets) { |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 97 | this(deviceId, type, buckets, null, null, null); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns type of a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 102 | * |
| 103 | * @return GroupType group type |
| 104 | */ |
| 105 | @Override |
| 106 | public GroupDescription.Type type() { |
| 107 | return this.type; |
| 108 | } |
| 109 | |
| 110 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 111 | * Returns device identifier on which this group object is created. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 112 | * |
| 113 | * @return DeviceId device identifier |
| 114 | */ |
| 115 | @Override |
| 116 | public DeviceId deviceId() { |
| 117 | return this.deviceId; |
| 118 | } |
| 119 | |
| 120 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 121 | * Returns application identifier that has created this group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 122 | * |
| 123 | * @return ApplicationId application identifier |
| 124 | */ |
| 125 | @Override |
| 126 | public ApplicationId appId() { |
| 127 | return this.appId; |
| 128 | } |
| 129 | |
| 130 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 131 | * Returns application cookie associated with a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 132 | * |
| 133 | * @return GroupKey application cookie |
| 134 | */ |
| 135 | @Override |
| 136 | public GroupKey appCookie() { |
| 137 | return this.appCookie; |
| 138 | } |
| 139 | |
| 140 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 141 | * Returns group buckets of a group. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 142 | * |
| 143 | * @return GroupBuckets immutable list of group bucket |
| 144 | */ |
| 145 | @Override |
| 146 | public GroupBuckets buckets() { |
| 147 | return this.buckets; |
| 148 | } |
| 149 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 150 | /** |
| 151 | * Returns groupId passed in by application. |
| 152 | * |
| 153 | * @return Integer group Id passed in by caller. May be null if caller passed |
| 154 | * in null during GroupDescription creation. |
| 155 | */ |
| 156 | @Override |
| 157 | public Integer givenGroupId() { |
| 158 | return this.givenGroupId; |
| 159 | } |
| 160 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 161 | @Override |
| 162 | /* |
| 163 | * The deviceId, type and buckets are used for hash. |
| 164 | * |
| 165 | * (non-Javadoc) |
| 166 | * @see java.lang.Object#equals(java.lang.Object) |
| 167 | */ |
| 168 | public int hashCode() { |
| 169 | return Objects.hash(deviceId, type, buckets); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | /* |
| 174 | * The deviceId, type and buckets should be same. |
| 175 | * |
| 176 | * (non-Javadoc) |
| 177 | * @see java.lang.Object#equals(java.lang.Object) |
| 178 | */ |
| 179 | public boolean equals(Object obj) { |
| 180 | if (this == obj) { |
| 181 | return true; |
| 182 | } |
| 183 | if (obj instanceof DefaultGroupDescription) { |
| 184 | DefaultGroupDescription that = (DefaultGroupDescription) obj; |
| 185 | return Objects.equals(deviceId, that.deviceId) && |
| 186 | Objects.equals(type, that.type) && |
| 187 | Objects.equals(buckets, that.buckets); |
| 188 | |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 193 | @Override |
| 194 | public String toString() { |
| 195 | return toStringHelper(this) |
| 196 | .add("deviceId", deviceId) |
| 197 | .add("type", type) |
| 198 | .add("buckets", buckets) |
| 199 | .add("appId", appId) |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame^] | 200 | .add("givenGroupId", givenGroupId) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 201 | .toString(); |
| 202 | } |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 203 | } |