| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.ec2.domain; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | /** |
| 24 | * |
| 25 | * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-IpPermissionType.html" |
| 26 | * /> |
| 27 | * @author Adrian Cole |
| 28 | */ |
| 29 | public class IpPermission implements Comparable<IpPermission> { |
| 30 | private final int fromPort; |
| 31 | private final int toPort; |
| 32 | private final Set<UserIdGroupPair> groups; |
| 33 | private final IpProtocol ipProtocol; |
| 34 | private final Set<String> ipRanges; |
| 35 | |
| 36 | public IpPermission(int fromPort, int toPort, Set<UserIdGroupPair> groups, |
| 37 | IpProtocol ipProtocol, Set<String> ipRanges) { |
| 38 | this.fromPort = fromPort; |
| 39 | this.toPort = toPort; |
| 40 | this.groups = groups; |
| 41 | this.ipProtocol = ipProtocol; |
| 42 | this.ipRanges = ipRanges; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritDoc} |
| 47 | */ |
| 48 | public int compareTo(IpPermission o) { |
| 49 | return (this == o) ? 0 : getIpProtocol().compareTo(o.getIpProtocol()); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Start of port range for the TCP and UDP protocols, or an ICMP type number. An ICMP type number |
| 54 | * of -1 indicates a wildcard (i.e., any ICMP type number). |
| 55 | */ |
| 56 | public int getFromPort() { |
| 57 | return fromPort; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * End of port range for the TCP and UDP protocols, or an ICMP code. An ICMP code of -1 indicates |
| 62 | * a wildcard (i.e., any ICMP code). |
| 63 | */ |
| 64 | public int getToPort() { |
| 65 | return toPort; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * List of security group and user ID pairs. |
| 70 | */ |
| 71 | public Set<UserIdGroupPair> getGroups() { |
| 72 | return groups; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * IP protocol |
| 77 | */ |
| 78 | public IpProtocol getIpProtocol() { |
| 79 | return ipProtocol; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * IP ranges. |
| 84 | */ |
| 85 | public Set<String> getIpRanges() { |
| 86 | return ipRanges; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public int hashCode() { |
| 91 | final int prime = 31; |
| 92 | int result = 1; |
| 93 | result = prime * result + fromPort; |
| 94 | result = prime * result + ((groups == null) ? 0 : groups.hashCode()); |
| 95 | result = prime * result + ((ipProtocol == null) ? 0 : ipProtocol.hashCode()); |
| 96 | result = prime * result + ((ipRanges == null) ? 0 : ipRanges.hashCode()); |
| 97 | result = prime * result + toPort; |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean equals(Object obj) { |
| 103 | if (this == obj) |
| 104 | return true; |
| 105 | if (obj == null) |
| 106 | return false; |
| 107 | if (getClass() != obj.getClass()) |
| 108 | return false; |
| 109 | IpPermission other = (IpPermission) obj; |
| 110 | if (fromPort != other.fromPort) |
| 111 | return false; |
| 112 | if (groups == null) { |
| 113 | if (other.groups != null) |
| 114 | return false; |
| 115 | } else if (!groups.equals(other.groups)) |
| 116 | return false; |
| 117 | if (ipProtocol == null) { |
| 118 | if (other.ipProtocol != null) |
| 119 | return false; |
| 120 | } else if (!ipProtocol.equals(other.ipProtocol)) |
| 121 | return false; |
| 122 | if (ipRanges == null) { |
| 123 | if (other.ipRanges != null) |
| 124 | return false; |
| 125 | } else if (!ipRanges.equals(other.ipRanges)) |
| 126 | return false; |
| 127 | if (toPort != other.toPort) |
| 128 | return false; |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return "IpPermission [fromPort=" + fromPort + ", groups=" + groups + ", ipProtocol=" |
| 135 | + ipProtocol + ", ipRanges=" + ipRanges + ", toPort=" + toPort + "]"; |
| 136 | } |
| 137 | |
| 138 | } |