blob: 8436750414911b85973700b845dbfa887a09f3df [file] [log] [blame]
sangho5db8e052016-01-29 16:08:23 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
sangho5db8e052016-01-29 16:08:23 +09003 *
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 */
sangho93447f12016-02-24 00:33:22 +090016package org.onosproject.openstackinterface;
sangho0c2a3da2016-02-16 13:39:07 +090017
sangho90088532016-02-25 18:06:12 +090018import org.onlab.packet.IpPrefix;
19
sangho0c2a3da2016-02-16 13:39:07 +090020import java.util.Objects;
21
22import static com.google.common.base.Preconditions.checkNotNull;
sangho5db8e052016-01-29 16:08:23 +090023
24/**
25 * Represents Openstack Security Group Rules.
26 */
27public final class OpenstackSecurityGroupRule {
28
sangho90088532016-02-25 18:06:12 +090029 private final Direction direction;
sangho0c2a3da2016-02-16 13:39:07 +090030 private final String ethertype;
31 private final String id;
sangho90088532016-02-25 18:06:12 +090032 private final int portRangeMax;
33 private final int portRangeMin;
sangho0c2a3da2016-02-16 13:39:07 +090034 private final String protocol;
35 private final String remoteGroupId;
sangho90088532016-02-25 18:06:12 +090036 private final IpPrefix remoteIpPrefix;
sangho0c2a3da2016-02-16 13:39:07 +090037 private final String secuityGroupId;
38 private final String tenantId;
sangho5db8e052016-01-29 16:08:23 +090039
sangho90088532016-02-25 18:06:12 +090040 /**
41 * Direction of the Security Group.
42 *
43 */
44 public enum Direction {
45 INGRESS,
46 EGRESS
47 }
48
49 private OpenstackSecurityGroupRule(Direction direction,
sangho5db8e052016-01-29 16:08:23 +090050 String ethertype,
51 String id,
sangho90088532016-02-25 18:06:12 +090052 int portRangeMax,
53 int portRangeMin,
sangho5db8e052016-01-29 16:08:23 +090054 String protocol,
55 String remoteGroupId,
sangho90088532016-02-25 18:06:12 +090056 IpPrefix remoteIpPrefix,
sangho5db8e052016-01-29 16:08:23 +090057 String securityGroupId,
58 String tenantId) {
59 this.direction = direction;
60 this.ethertype = ethertype;
sangho0c2a3da2016-02-16 13:39:07 +090061 this.id = checkNotNull(id);
sangho5db8e052016-01-29 16:08:23 +090062 this.portRangeMax = portRangeMax;
63 this.portRangeMin = portRangeMin;
64 this.protocol = protocol;
65 this.remoteGroupId = remoteGroupId;
66 this.remoteIpPrefix = remoteIpPrefix;
67 this.secuityGroupId = securityGroupId;
68 this.tenantId = tenantId;
69 }
70
sangho90088532016-02-25 18:06:12 +090071 /**
72 * Returns the builder object for the OpenstackSecurityGroupRule.
73 *
74 * @return OpenstackSecurityGroupRule builder object
75 */
76 public static OpenstackSecurityGroupRule.Builder builder() {
77 return new Builder();
78 }
79
80 /**
81 * Returns the direction.
82 *
83 * @return direction
84 */
85 public Direction direction() {
86 return direction;
87 }
88
89 /**
90 * Returns the Ethernet type.
91 *
92 * @return Ethernet type
93 */
94 public String ethertype() {
95 return ethertype;
96 }
97
98 /**
99 * Returns the Security Group ID.
100 *
101 * @return Security Group ID
102 */
103 public String id() {
104 return id;
105 }
106
107 /**
108 * Returns the max of the port range.
109 *
110 * @return max of the port range
111 */
112 public int portRangeMax() {
113 return portRangeMax;
114 }
115
116 /**
117 * Returns the min of the port range.
118 *
119 * @return min of the port range
120 */
121 public int portRangeMin() {
122 return portRangeMin;
123 }
124
125 /**
126 * Returns the IP protocol.
127 *
128 * @return IP protocol
129 */
130 public String protocol() {
131 return protocol;
132 }
133
134 /**
135 * Returns the remote group ID.
136 *
137 * @return remote group ID
138 */
139 public String remoteGroupId() {
140 return remoteGroupId;
141 }
142
143 /**
144 * Returns the remote IP address.
145 *
146 * @return remote IP address
147 */
148 public IpPrefix remoteIpPrefix() {
149 return this.remoteIpPrefix;
150 }
151
152 /**
153 * Returns the Security Group ID.
154 *
155 * @return security group ID
156 */
157 public String secuityGroupId() {
158 return secuityGroupId;
159 }
160
161 /**
162 * Returns the tenant ID.
163 *
164 * @return tenant ID
165 */
166 public String tenantId() {
167 return tenantId;
168 }
169
sangho5db8e052016-01-29 16:08:23 +0900170 @Override
171 public String toString() {
172 return new StringBuilder(" [")
173 .append(direction + ",")
174 .append(ethertype + ",")
175 .append(id + ",")
176 .append(portRangeMax + ",")
177 .append(portRangeMin + ",")
178 .append(protocol + ",'")
179 .append(remoteGroupId + ",")
180 .append(remoteIpPrefix + ",")
181 .append(secuityGroupId + ",")
182 .append(tenantId + "] ")
183 .toString();
184 }
185
sangho0c2a3da2016-02-16 13:39:07 +0900186 @Override
187 public boolean equals(Object o) {
188 if (this == o) {
189 return true;
190 }
191
Eunjin Choidb46ab392016-12-05 16:26:33 +0900192 if (o instanceof OpenstackSecurityGroupRule) {
sangho0c2a3da2016-02-16 13:39:07 +0900193 OpenstackSecurityGroupRule that = (OpenstackSecurityGroupRule) o;
194 return this.direction.equals(that.direction) &&
Shashikanth VHb1e72372016-05-10 18:58:00 +0530195 this.ethertype.equals(that.ethertype) &&
sangho0c2a3da2016-02-16 13:39:07 +0900196 this.id.equals(that.id) &&
sangho90088532016-02-25 18:06:12 +0900197 this.portRangeMax == that.portRangeMax &&
198 this.portRangeMin == that.portRangeMin &&
sangho0c2a3da2016-02-16 13:39:07 +0900199 this.protocol.equals(that.protocol) &&
200 this.remoteGroupId.equals(that.remoteGroupId) &&
201 this.secuityGroupId.equals(that.secuityGroupId) &&
202 this.remoteIpPrefix.equals(that.remoteIpPrefix) &&
203 this.tenantId.equals(that.tenantId);
204 }
205
206 return false;
207 }
208
209 @Override
210 public int hashCode() {
211 return Objects.hash(direction, ethertype, id, portRangeMax, portRangeMin, protocol,
212 remoteGroupId, remoteIpPrefix, secuityGroupId, tenantId);
213 }
214
sangho5db8e052016-01-29 16:08:23 +0900215 /**
216 * Represents a security group rule builder object.
217 */
218 public static final class Builder {
219
220 private String direction;
221 private String etherType;
222 private String id;
223 private String portRangeMax;
224 private String portRangeMin;
225 private String protocol;
226 private String remoteGroupId;
227 private String remoteIpPrefix;
228 private String secuityGroupId;
229 private String tenantId;
230
231
232 /**
233 * Sets the direction of the security group rule.
234 *
235 * @param direction direction (ingress or egress)
236 * @return builder object
237 */
238 public Builder direction(String direction) {
239 this.direction = direction;
240 return this;
241 }
242
243 /**
244 * Sets the Ethernet Type.
245 *
246 * @param etherType Ethernet Type
247 * @return builder object
248 */
249 public Builder etherType(String etherType) {
250 this.etherType = etherType;
251 return this;
252 }
253
254 /**
255 * Sets the Security Group Rule ID.
256 *
257 * @param id security group rule ID
258 * @return builder object
259 */
260 public Builder id(String id) {
261 this.id = id;
262 return this;
263 }
264
265 /**
266 * Sets the port range max value.
267 *
268 * @param portRangeMax port range max value
269 * @return builder object
270 */
271 public Builder portRangeMax(String portRangeMax) {
272 this.portRangeMax = portRangeMax;
273 return this;
274 }
275
276 /**
277 * Sets the port range min value.
278 *
279 * @param portRangeMin port range min value
280 * @return builder object
281 */
282 public Builder portRangeMin(String portRangeMin) {
283 this.portRangeMin = portRangeMin;
284 return this;
285 }
286
287 /**
288 * Sets the protocol.
289 *
290 * @param protocol protocol
291 * @return builder object
292 */
293 public Builder protocol(String protocol) {
294 this.protocol = protocol;
295 return this;
296 }
297
298 /**
299 * Sets the remote security group ID.
300 *
301 * @param remoteGroupId remote security group ID
302 * @return builder
303 */
304 public Builder remoteGroupId(String remoteGroupId) {
305 this.remoteGroupId = remoteGroupId;
306 return this;
307 }
308
309 /**
310 * Sets the remote IP address as prefix.
311 *
312 * @param remoteIpPrefix remote IP address
313 * @return builder object
314 */
315 public Builder remoteIpPrefix(String remoteIpPrefix) {
316 this.remoteIpPrefix = remoteIpPrefix;
317 return this;
318 }
319
320 /**
321 * Sets the Security Group ID.
322 *
323 * @param securityGroupId security group ID
324 * @return builder object
325 */
326 public Builder securityGroupId(String securityGroupId) {
327 this.secuityGroupId = securityGroupId;
328 return this;
329 }
330
331 /**
332 * Sets the tenant ID.
333 *
334 * @param tenantId tenant ID
335 * @return builder object
336 */
337 public Builder tenantId(String tenantId) {
338 this.tenantId = tenantId;
339 return this;
340 }
341
342 /**
343 * Creates a OpenstackSecurityGroupRule instance.
344 *
345 * @return OpenstackSecurityGroupRule object
346 */
347 public OpenstackSecurityGroupRule build() {
sangho90088532016-02-25 18:06:12 +0900348
349 int portRangeMinInt = (portRangeMin == null || portRangeMin.equals("null")) ?
hyungseo Ryu38b5f182016-06-14 16:42:27 +0900350 -1 : Integer.parseInt(portRangeMin);
sangho90088532016-02-25 18:06:12 +0900351 int portRangeMaxInt = (portRangeMax == null || portRangeMax.equals("null")) ?
352 -1 : Integer.parseInt(portRangeMax);
353 IpPrefix ipPrefix = (remoteIpPrefix == null || remoteIpPrefix.equals("null")) ?
354 null : IpPrefix.valueOf(remoteIpPrefix);
355
356 return new OpenstackSecurityGroupRule(Direction.valueOf(direction.toUpperCase()), etherType, id,
357 portRangeMaxInt, portRangeMinInt, protocol, remoteGroupId, ipPrefix, secuityGroupId, tenantId);
sangho5db8e052016-01-29 16:08:23 +0900358 }
359 }
360}