sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [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 | */ |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 16 | package org.onosproject.openstackinterface; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 17 | |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 18 | import org.onlab.packet.IpPrefix; |
| 19 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Represents Openstack Security Group Rules. |
| 26 | */ |
| 27 | public final class OpenstackSecurityGroupRule { |
| 28 | |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 29 | private final Direction direction; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 30 | private final String ethertype; |
| 31 | private final String id; |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 32 | private final int portRangeMax; |
| 33 | private final int portRangeMin; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 34 | private final String protocol; |
| 35 | private final String remoteGroupId; |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 36 | private final IpPrefix remoteIpPrefix; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 37 | private final String secuityGroupId; |
| 38 | private final String tenantId; |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 39 | |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 40 | /** |
| 41 | * Direction of the Security Group. |
| 42 | * |
| 43 | */ |
| 44 | public enum Direction { |
| 45 | INGRESS, |
| 46 | EGRESS |
| 47 | } |
| 48 | |
| 49 | private OpenstackSecurityGroupRule(Direction direction, |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 50 | String ethertype, |
| 51 | String id, |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 52 | int portRangeMax, |
| 53 | int portRangeMin, |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 54 | String protocol, |
| 55 | String remoteGroupId, |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 56 | IpPrefix remoteIpPrefix, |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 57 | String securityGroupId, |
| 58 | String tenantId) { |
| 59 | this.direction = direction; |
| 60 | this.ethertype = ethertype; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 61 | this.id = checkNotNull(id); |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 62 | 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 | |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 71 | /** |
| 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 | |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 170 | @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 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 186 | @Override |
| 187 | public boolean equals(Object o) { |
| 188 | if (this == o) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | if (this instanceof OpenstackSecurityGroupRule) { |
| 193 | OpenstackSecurityGroupRule that = (OpenstackSecurityGroupRule) o; |
| 194 | return this.direction.equals(that.direction) && |
| 195 | this.ethertype.equals(that.direction) && |
| 196 | this.id.equals(that.id) && |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 197 | this.portRangeMax == that.portRangeMax && |
| 198 | this.portRangeMin == that.portRangeMin && |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 199 | 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 | |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 215 | /** |
| 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() { |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 348 | |
| 349 | int portRangeMinInt = (portRangeMin == null || portRangeMin.equals("null")) ? |
| 350 | -1 : Integer.parseInt(portRangeMax); |
| 351 | 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); |
sangho | 5db8e05 | 2016-01-29 16:08:23 +0900 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | } |