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 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 22 | import org.onosproject.core.GroupId; |
| 23 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Group operation definition to be used between core and provider |
| 26 | * layers of group subsystem. |
| 27 | * |
| 28 | */ |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 29 | public final class GroupOperation { |
| 30 | private final Type opType; |
| 31 | private final GroupId groupId; |
| 32 | private final GroupDescription.Type groupType; |
| 33 | private final GroupBuckets buckets; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 34 | private final GroupMsgErrorCode failureCode; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 35 | |
| 36 | public enum Type { |
| 37 | /** |
| 38 | * Create a group in a device with the specified parameters. |
| 39 | */ |
| 40 | ADD, |
| 41 | /** |
| 42 | * Modify a group in a device with the specified parameters. |
| 43 | */ |
| 44 | MODIFY, |
| 45 | /** |
| 46 | * Delete a specified group. |
| 47 | */ |
| 48 | DELETE |
| 49 | } |
| 50 | |
| 51 | /** |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 52 | * Possible error codes for a failure of a group operation. |
| 53 | * |
| 54 | */ |
| 55 | public enum GroupMsgErrorCode { |
| 56 | GROUP_EXISTS, |
| 57 | INVALID_GROUP, |
| 58 | WEIGHT_UNSUPPORTED, |
| 59 | OUT_OF_GROUPS, |
| 60 | OUT_OF_BUCKETS, |
| 61 | CHAINING_UNSUPPORTED, |
| 62 | WATCH_UNSUPPORTED, |
| 63 | LOOP, |
| 64 | UNKNOWN_GROUP, |
| 65 | CHAINED_GROUP, |
| 66 | BAD_TYPE, |
| 67 | BAD_COMMAND, |
| 68 | BAD_BUCKET, |
| 69 | BAD_WATCH, |
Sho SHIMIZU | 32ae9c5 | 2016-08-12 15:04:08 -0700 | [diff] [blame] | 70 | EPERM |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 74 | * Group operation constructor with the parameters. |
| 75 | * |
| 76 | * @param opType group operation type |
| 77 | * @param groupId group Identifier |
| 78 | * @param groupType type of the group |
| 79 | * @param buckets immutable list of group buckets to be part of group |
| 80 | */ |
| 81 | private GroupOperation(Type opType, |
| 82 | GroupId groupId, |
| 83 | GroupDescription.Type groupType, |
| 84 | GroupBuckets buckets) { |
| 85 | this.opType = checkNotNull(opType); |
| 86 | this.groupId = checkNotNull(groupId); |
| 87 | this.groupType = checkNotNull(groupType); |
| 88 | this.buckets = buckets; |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 89 | this.failureCode = null; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Group operation copy-constructor with additional field to set failure code. |
| 94 | * Typically used by provider to return information to the core about |
| 95 | * the failure reason for a group operation. |
| 96 | * |
| 97 | * @param groupOp the original group operation |
| 98 | * @param failureCode failure code for a failed group operation |
| 99 | */ |
| 100 | private GroupOperation(GroupOperation groupOp, GroupMsgErrorCode failureCode) { |
| 101 | this.opType = groupOp.opType; |
| 102 | this.groupId = groupOp.groupId; |
| 103 | this.groupType = groupOp.groupType; |
| 104 | this.buckets = groupOp.buckets; |
| 105 | this.failureCode = failureCode; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Creates ADD group operation object. |
| 110 | * |
| 111 | * @param groupId group Identifier |
| 112 | * @param groupType type of the group |
| 113 | * @param buckets immutable list of group buckets to be part of group |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 114 | * @return add group operation object |
| 115 | */ |
| 116 | public static GroupOperation createAddGroupOperation(GroupId groupId, |
| 117 | GroupDescription.Type groupType, |
| 118 | GroupBuckets buckets) { |
| 119 | checkNotNull(buckets); |
| 120 | return new GroupOperation(Type.ADD, groupId, groupType, buckets); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Creates MODIFY group operation object. |
| 125 | * |
| 126 | * @param groupId group Identifier |
| 127 | * @param groupType type of the group |
| 128 | * @param buckets immutable list of group buckets to be part of group |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 129 | * @return modify group operation object |
| 130 | */ |
| 131 | public static GroupOperation createModifyGroupOperation(GroupId groupId, |
| 132 | GroupDescription.Type groupType, |
| 133 | GroupBuckets buckets) { |
| 134 | checkNotNull(buckets); |
| 135 | return new GroupOperation(Type.MODIFY, groupId, groupType, buckets); |
| 136 | |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Creates DELETE group operation object. |
| 141 | * |
| 142 | * @param groupId group Identifier |
| 143 | * @param groupType type of the group |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 144 | * @return delete group operation object |
| 145 | */ |
| 146 | public static GroupOperation createDeleteGroupOperation(GroupId groupId, |
| 147 | GroupDescription.Type groupType) { |
| 148 | return new GroupOperation(Type.DELETE, groupId, groupType, null); |
| 149 | |
| 150 | } |
| 151 | |
sangyun-han | f53bc2b | 2016-03-30 11:11:06 +0900 | [diff] [blame] | 152 | /** |
| 153 | * Creates failure group operation object by setting failure code |
| 154 | * to inform the failure reason. |
| 155 | * |
| 156 | * @param groupOperation the original group operation |
| 157 | * @param failureCode failure code for a failed group operation |
| 158 | * @return failed group operation object |
| 159 | */ |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 160 | public static GroupOperation createFailedGroupOperation(GroupOperation groupOperation, |
| 161 | GroupMsgErrorCode failureCode) { |
| 162 | checkNotNull(groupOperation); |
| 163 | return new GroupOperation(groupOperation, failureCode); |
| 164 | } |
| 165 | |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 166 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 167 | * Returns group operation type. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 168 | * |
| 169 | * @return GroupOpType group operation type |
| 170 | */ |
| 171 | public Type opType() { |
| 172 | return this.opType; |
| 173 | } |
| 174 | |
| 175 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 176 | * Returns group identifier attribute of the operation. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 177 | * |
| 178 | * @return GroupId group identifier |
| 179 | */ |
| 180 | public GroupId groupId() { |
| 181 | return this.groupId; |
| 182 | } |
| 183 | |
| 184 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 185 | * Returns group type attribute of the operation. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 186 | * |
| 187 | * @return GroupType group type |
| 188 | */ |
| 189 | public GroupDescription.Type groupType() { |
| 190 | return this.groupType; |
| 191 | } |
| 192 | |
| 193 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 194 | * Returns group buckets associated with the operation. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 195 | * |
| 196 | * @return GroupBuckets group buckets |
| 197 | */ |
| 198 | public GroupBuckets buckets() { |
| 199 | return this.buckets; |
| 200 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 201 | |
Saurav Das | 0fd79d9 | 2016-03-07 10:58:36 -0800 | [diff] [blame] | 202 | /** |
| 203 | * Returns the failure code representing the failure of a group operation. |
| 204 | * |
| 205 | * @return error code for failure of group operation |
| 206 | */ |
| 207 | public GroupMsgErrorCode failureCode() { |
| 208 | return failureCode; |
| 209 | } |
| 210 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 211 | @Override |
| 212 | /* |
| 213 | * The deviceId, type and buckets are used for hash. |
| 214 | * |
| 215 | * (non-Javadoc) |
| 216 | * @see java.lang.Object#equals(java.lang.Object) |
| 217 | */ |
| 218 | public int hashCode() { |
| 219 | return (buckets != null) ? Objects.hash(groupId, opType, buckets) : |
| 220 | Objects.hash(groupId, opType); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | /* |
| 225 | * The deviceId, type and buckets should be same. |
| 226 | * |
| 227 | * (non-Javadoc) |
| 228 | * @see java.lang.Object#equals(java.lang.Object) |
| 229 | */ |
| 230 | public boolean equals(Object obj) { |
| 231 | if (this == obj) { |
| 232 | return true; |
| 233 | } |
| 234 | if (obj instanceof GroupOperation) { |
| 235 | GroupOperation that = (GroupOperation) obj; |
| 236 | return Objects.equals(groupId, that.groupId) && |
Ray Milkey | e8c632f | 2015-05-20 16:34:29 -0700 | [diff] [blame] | 237 | Objects.equals(groupType, that.groupType) && |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 238 | Objects.equals(opType, that.opType) && |
| 239 | Objects.equals(buckets, that.buckets); |
| 240 | |
| 241 | } |
| 242 | return false; |
| 243 | } |
Ray Milkey | e8c632f | 2015-05-20 16:34:29 -0700 | [diff] [blame] | 244 | } |