blob: e4173b30a91d7572d138c6084ec7e18f42fc5c5b [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;
34
35 public enum Type {
36 /**
37 * Create a group in a device with the specified parameters.
38 */
39 ADD,
40 /**
41 * Modify a group in a device with the specified parameters.
42 */
43 MODIFY,
44 /**
45 * Delete a specified group.
46 */
47 DELETE
48 }
49
50 /**
51 * Group operation constructor with the parameters.
52 *
53 * @param opType group operation type
54 * @param groupId group Identifier
55 * @param groupType type of the group
56 * @param buckets immutable list of group buckets to be part of group
57 */
58 private GroupOperation(Type opType,
59 GroupId groupId,
60 GroupDescription.Type groupType,
61 GroupBuckets buckets) {
62 this.opType = checkNotNull(opType);
63 this.groupId = checkNotNull(groupId);
64 this.groupType = checkNotNull(groupType);
65 this.buckets = buckets;
66 }
67
68 /**
69 * Creates ADD group operation object.
70 *
71 * @param groupId group Identifier
72 * @param groupType type of the group
73 * @param buckets immutable list of group buckets to be part of group
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080074 * @return add group operation object
75 */
76 public static GroupOperation createAddGroupOperation(GroupId groupId,
77 GroupDescription.Type groupType,
78 GroupBuckets buckets) {
79 checkNotNull(buckets);
80 return new GroupOperation(Type.ADD, groupId, groupType, buckets);
81 }
82
83 /**
84 * Creates MODIFY group operation object.
85 *
86 * @param groupId group Identifier
87 * @param groupType type of the group
88 * @param buckets immutable list of group buckets to be part of group
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080089 * @return modify group operation object
90 */
91 public static GroupOperation createModifyGroupOperation(GroupId groupId,
92 GroupDescription.Type groupType,
93 GroupBuckets buckets) {
94 checkNotNull(buckets);
95 return new GroupOperation(Type.MODIFY, groupId, groupType, buckets);
96
97 }
98
99 /**
100 * Creates DELETE group operation object.
101 *
102 * @param groupId group Identifier
103 * @param groupType type of the group
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800104 * @return delete group operation object
105 */
106 public static GroupOperation createDeleteGroupOperation(GroupId groupId,
107 GroupDescription.Type groupType) {
108 return new GroupOperation(Type.DELETE, groupId, groupType, null);
109
110 }
111
112 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800113 * Returns group operation type.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800114 *
115 * @return GroupOpType group operation type
116 */
117 public Type opType() {
118 return this.opType;
119 }
120
121 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800122 * Returns group identifier attribute of the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800123 *
124 * @return GroupId group identifier
125 */
126 public GroupId groupId() {
127 return this.groupId;
128 }
129
130 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800131 * Returns group type attribute of the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800132 *
133 * @return GroupType group type
134 */
135 public GroupDescription.Type groupType() {
136 return this.groupType;
137 }
138
139 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800140 * Returns group buckets associated with the operation.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800141 *
142 * @return GroupBuckets group buckets
143 */
144 public GroupBuckets buckets() {
145 return this.buckets;
146 }
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800147
148 @Override
149 /*
150 * The deviceId, type and buckets are used for hash.
151 *
152 * (non-Javadoc)
153 * @see java.lang.Object#equals(java.lang.Object)
154 */
155 public int hashCode() {
156 return (buckets != null) ? Objects.hash(groupId, opType, buckets) :
157 Objects.hash(groupId, opType);
158 }
159
160 @Override
161 /*
162 * The deviceId, type and buckets should be same.
163 *
164 * (non-Javadoc)
165 * @see java.lang.Object#equals(java.lang.Object)
166 */
167 public boolean equals(Object obj) {
168 if (this == obj) {
169 return true;
170 }
171 if (obj instanceof GroupOperation) {
172 GroupOperation that = (GroupOperation) obj;
173 return Objects.equals(groupId, that.groupId) &&
Ray Milkeye8c632f2015-05-20 16:34:29 -0700174 Objects.equals(groupType, that.groupType) &&
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800175 Objects.equals(opType, that.opType) &&
176 Objects.equals(buckets, that.buckets);
177
178 }
179 return false;
180 }
Ray Milkeye8c632f2015-05-20 16:34:29 -0700181}