blob: fe4041bbe20232ea23725c5765995b6d71302ab6 [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
18import static com.google.common.base.Preconditions.checkNotNull;
19
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080020import java.util.Objects;
21
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080022import org.onosproject.core.GroupId;
23
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080024/**
25 * Group operation definition to be used between core and provider
26 * layers of group subsystem.
27 *
28 */
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080029public 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 Das0fd79d92016-03-07 10:58:36 -080034 private final GroupMsgErrorCode failureCode;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080035
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 Das0fd79d92016-03-07 10:58:36 -080052 * 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,
70 EPERM;
71 }
72
73 /**
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080074 * 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 Das0fd79d92016-03-07 10:58:36 -080089 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 Vavilapalli56db94f2015-01-22 22:30:17 -0800106 }
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 Vavilapalli56db94f2015-01-22 22:30:17 -0800114 * @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 Vavilapalli56db94f2015-01-22 22:30:17 -0800129 * @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 Vavilapalli56db94f2015-01-22 22:30:17 -0800144 * @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-hanf53bc2b2016-03-30 11:11:06 +0900152 /**
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 Das0fd79d92016-03-07 10:58:36 -0800160 public static GroupOperation createFailedGroupOperation(GroupOperation groupOperation,
161 GroupMsgErrorCode failureCode) {
162 checkNotNull(groupOperation);
163 return new GroupOperation(groupOperation, failureCode);
164 }
165
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800166 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800167 * Returns group operation type.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800168 *
169 * @return GroupOpType group operation type
170 */
171 public Type opType() {
172 return this.opType;
173 }
174
175 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800176 * Returns group identifier attribute of the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800177 *
178 * @return GroupId group identifier
179 */
180 public GroupId groupId() {
181 return this.groupId;
182 }
183
184 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800185 * Returns group type attribute of the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800186 *
187 * @return GroupType group type
188 */
189 public GroupDescription.Type groupType() {
190 return this.groupType;
191 }
192
193 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800194 * Returns group buckets associated with the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800195 *
196 * @return GroupBuckets group buckets
197 */
198 public GroupBuckets buckets() {
199 return this.buckets;
200 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800201
Saurav Das0fd79d92016-03-07 10:58:36 -0800202 /**
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 Vavilapalli45c27c82015-01-30 12:57:56 -0800211 @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 Milkeye8c632f2015-05-20 16:34:29 -0700237 Objects.equals(groupType, that.groupType) &&
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800238 Objects.equals(opType, that.opType) &&
239 Objects.equals(buckets, that.buckets);
240
241 }
242 return false;
243 }
Ray Milkeye8c632f2015-05-20 16:34:29 -0700244}