blob: 42ef9dddd5e711d14f7fa8491be855e5e3ec5e0a [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
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 */
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
Saurav Das0fd79d92016-03-07 10:58:36 -0800152 public static GroupOperation createFailedGroupOperation(GroupOperation groupOperation,
153 GroupMsgErrorCode failureCode) {
154 checkNotNull(groupOperation);
155 return new GroupOperation(groupOperation, failureCode);
156 }
157
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800158 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800159 * Returns group operation type.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800160 *
161 * @return GroupOpType group operation type
162 */
163 public Type opType() {
164 return this.opType;
165 }
166
167 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800168 * Returns group identifier attribute of the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800169 *
170 * @return GroupId group identifier
171 */
172 public GroupId groupId() {
173 return this.groupId;
174 }
175
176 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800177 * Returns group type attribute of the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800178 *
179 * @return GroupType group type
180 */
181 public GroupDescription.Type groupType() {
182 return this.groupType;
183 }
184
185 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800186 * Returns group buckets associated with the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800187 *
188 * @return GroupBuckets group buckets
189 */
190 public GroupBuckets buckets() {
191 return this.buckets;
192 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800193
Saurav Das0fd79d92016-03-07 10:58:36 -0800194 /**
195 * Returns the failure code representing the failure of a group operation.
196 *
197 * @return error code for failure of group operation
198 */
199 public GroupMsgErrorCode failureCode() {
200 return failureCode;
201 }
202
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800203 @Override
204 /*
205 * The deviceId, type and buckets are used for hash.
206 *
207 * (non-Javadoc)
208 * @see java.lang.Object#equals(java.lang.Object)
209 */
210 public int hashCode() {
211 return (buckets != null) ? Objects.hash(groupId, opType, buckets) :
212 Objects.hash(groupId, opType);
213 }
214
215 @Override
216 /*
217 * The deviceId, type and buckets should be same.
218 *
219 * (non-Javadoc)
220 * @see java.lang.Object#equals(java.lang.Object)
221 */
222 public boolean equals(Object obj) {
223 if (this == obj) {
224 return true;
225 }
226 if (obj instanceof GroupOperation) {
227 GroupOperation that = (GroupOperation) obj;
228 return Objects.equals(groupId, that.groupId) &&
Ray Milkeye8c632f2015-05-20 16:34:29 -0700229 Objects.equals(groupType, that.groupType) &&
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800230 Objects.equals(opType, that.opType) &&
231 Objects.equals(buckets, that.buckets);
232
233 }
234 return false;
235 }
Ray Milkeye8c632f2015-05-20 16:34:29 -0700236}