| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.ec2.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Map.Entry; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import com.google.common.collect.ImmutableMultimap; |
| 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.Iterables; |
| 29 | import com.google.common.collect.LinkedHashMultimap; |
| 30 | import com.google.common.collect.Multimap; |
| 31 | import com.google.common.collect.Sets; |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | * @see <a href= |
| 36 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-ItemType-IpPermissionType.html" |
| 37 | * /> |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | public class IpPermissionImpl implements IpPermission { |
| 41 | public static Builder builder() { |
| 42 | return new Builder(); |
| 43 | } |
| 44 | |
| 45 | public static class Builder { |
| 46 | private int fromPort; |
| 47 | private int toPort; |
| 48 | private IpProtocol ipProtocol; |
| 49 | private Multimap<String, String> userIdGroupPairs = LinkedHashMultimap.create(); |
| 50 | private Set<String> groupIds = Sets.newLinkedHashSet(); |
| 51 | private Set<String> ipRanges = Sets.newLinkedHashSet(); |
| 52 | |
| 53 | public Builder fromPort(int fromPort) { |
| 54 | this.fromPort = fromPort; |
| 55 | return this; |
| 56 | } |
| 57 | |
| 58 | public Builder toPort(int toPort) { |
| 59 | this.fromPort = toPort; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | public Builder ipProtocol(IpProtocol ipProtocol) { |
| 64 | this.ipProtocol = ipProtocol; |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | public Builder userIdGroupPair(String userId, String groupNameOrId) { |
| 69 | this.userIdGroupPairs.put(userId, groupNameOrId); |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | public Builder userIdGroupPairs(Multimap<String, String> userIdGroupPairs) { |
| 74 | this.userIdGroupPairs.putAll(userIdGroupPairs); |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | public Builder ipRange(String ipRange) { |
| 79 | this.ipRanges.add(ipRange); |
| 80 | return this; |
| 81 | } |
| 82 | |
| 83 | public Builder ipRanges(Iterable<String> ipRanges) { |
| 84 | Iterables.addAll(this.ipRanges, ipRanges); |
| 85 | return this; |
| 86 | } |
| 87 | |
| 88 | public Builder groupId(String groupId) { |
| 89 | this.groupIds.add(groupId); |
| 90 | return this; |
| 91 | } |
| 92 | |
| 93 | public Builder groupIds(Iterable<String> groupIds) { |
| 94 | Iterables.addAll(this.groupIds, groupIds); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | public IpPermission build() { |
| 99 | return new IpPermissionImpl(ipProtocol, fromPort, toPort, userIdGroupPairs, groupIds, ipRanges); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private final int fromPort; |
| 104 | private final int toPort; |
| 105 | private final Multimap<String, String> userIdGroupPairs; |
| 106 | private final Set<String> groupIds; |
| 107 | private final IpProtocol ipProtocol; |
| 108 | private final Set<String> ipRanges; |
| 109 | |
| 110 | public IpPermissionImpl(IpProtocol ipProtocol, int fromPort, int toPort, |
| 111 | Multimap<String, String> userIdGroupPairs, Iterable<String> groupIds, Iterable<String> ipRanges) { |
| 112 | this.fromPort = fromPort; |
| 113 | this.toPort = toPort; |
| 114 | this.userIdGroupPairs = ImmutableMultimap.copyOf(checkNotNull(userIdGroupPairs, "userIdGroupPairs")); |
| 115 | this.ipProtocol = checkNotNull(ipProtocol, "ipProtocol"); |
| 116 | this.groupIds = ImmutableSet.copyOf(checkNotNull(groupIds, "groupIds")); |
| 117 | this.ipRanges = ImmutableSet.copyOf(checkNotNull(ipRanges, "ipRanges")); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * {@inheritDoc} |
| 122 | */ |
| 123 | public int compareTo(IpPermission o) { |
| 124 | return (this == o) ? 0 : getIpProtocol().compareTo(o.getIpProtocol()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * {@inheritDoc} |
| 129 | */ |
| 130 | @Override |
| 131 | public int getFromPort() { |
| 132 | return fromPort; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * {@inheritDoc} |
| 137 | */ |
| 138 | @Override |
| 139 | public int getToPort() { |
| 140 | return toPort; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * {@inheritDoc} |
| 145 | */ |
| 146 | @Override |
| 147 | @Deprecated |
| 148 | public Set<UserIdGroupPair> getGroups() { |
| 149 | ImmutableSet.Builder<UserIdGroupPair> groups = ImmutableSet.<UserIdGroupPair> builder(); |
| 150 | for (Entry<String, String> pair : userIdGroupPairs.entries()) |
| 151 | groups.add(new UserIdGroupPair(pair.getKey(), pair.getValue())); |
| 152 | return groups.build(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * {@inheritDoc} |
| 157 | */ |
| 158 | @Override |
| 159 | public Multimap<String, String> getUserIdGroupPairs() { |
| 160 | return userIdGroupPairs; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * {@inheritDoc} |
| 165 | */ |
| 166 | @Override |
| 167 | public Set<String> getGroupIds() { |
| 168 | return groupIds; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * {@inheritDoc} |
| 173 | */ |
| 174 | @Override |
| 175 | public IpProtocol getIpProtocol() { |
| 176 | return ipProtocol; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * {@inheritDoc} |
| 181 | */ |
| 182 | @Override |
| 183 | public Set<String> getIpRanges() { |
| 184 | return ipRanges; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public int hashCode() { |
| 189 | final int prime = 31; |
| 190 | int result = 1; |
| 191 | result = prime * result + fromPort; |
| 192 | result = prime * result + ((groupIds == null) ? 0 : groupIds.hashCode()); |
| 193 | result = prime * result + ((ipProtocol == null) ? 0 : ipProtocol.hashCode()); |
| 194 | result = prime * result + ((ipRanges == null) ? 0 : ipRanges.hashCode()); |
| 195 | result = prime * result + toPort; |
| 196 | result = prime * result + ((userIdGroupPairs == null) ? 0 : userIdGroupPairs.hashCode()); |
| 197 | return result; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean equals(Object obj) { |
| 202 | if (this == obj) |
| 203 | return true; |
| 204 | if (obj == null) |
| 205 | return false; |
| 206 | if (getClass() != obj.getClass()) |
| 207 | return false; |
| 208 | IpPermissionImpl other = (IpPermissionImpl) obj; |
| 209 | if (fromPort != other.fromPort) |
| 210 | return false; |
| 211 | if (groupIds == null) { |
| 212 | if (other.groupIds != null) |
| 213 | return false; |
| 214 | } else if (!groupIds.equals(other.groupIds)) |
| 215 | return false; |
| 216 | if (ipProtocol != other.ipProtocol) |
| 217 | return false; |
| 218 | if (ipRanges == null) { |
| 219 | if (other.ipRanges != null) |
| 220 | return false; |
| 221 | } else if (!ipRanges.equals(other.ipRanges)) |
| 222 | return false; |
| 223 | if (toPort != other.toPort) |
| 224 | return false; |
| 225 | if (userIdGroupPairs == null) { |
| 226 | if (other.userIdGroupPairs != null) |
| 227 | return false; |
| 228 | } else if (!userIdGroupPairs.equals(other.userIdGroupPairs)) |
| 229 | return false; |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public String toString() { |
| 235 | return "[fromPort=" + fromPort + ", toPort=" + toPort + ", userIdGroupPairs=" + userIdGroupPairs + ", groupIds=" |
| 236 | + groupIds + ", ipProtocol=" + ipProtocol + ", ipRanges=" + ipRanges + "]"; |
| 237 | } |
| 238 | |
| 239 | } |