| 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.vcloud.domain.network; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.jclouds.javax.annotation.Nullable; |
| 26 | |
| 27 | import com.google.common.collect.Iterables; |
| 28 | import com.google.common.collect.Sets; |
| 29 | |
| 30 | /** |
| 31 | * The IpScope element defines the address range, gateway, netmask, and other properties of the |
| 32 | * network. |
| 33 | * |
| 34 | */ |
| 35 | public class IpScope { |
| 36 | private final boolean inherited; |
| 37 | @Nullable |
| 38 | private final String gateway; |
| 39 | @Nullable |
| 40 | private final String netmask; |
| 41 | @Nullable |
| 42 | private final String dns1; |
| 43 | @Nullable |
| 44 | private final String dns2; |
| 45 | @Nullable |
| 46 | private final String dnsSuffix; |
| 47 | private final Set<IpRange> ipRanges = Sets.newLinkedHashSet(); |
| 48 | private final Set<String> allocatedIpAddresses = Sets.newLinkedHashSet(); |
| 49 | |
| 50 | public IpScope(boolean inherited, @Nullable String gateway, @Nullable String netmask, @Nullable String dns1, |
| 51 | @Nullable String dns2, @Nullable String dnsSuffix, Iterable<IpRange> ipRanges, |
| 52 | Iterable<String> allocatedIpAddresses) { |
| 53 | this.inherited = inherited; |
| 54 | this.gateway = gateway; |
| 55 | this.netmask = netmask; |
| 56 | this.dns1 = dns1; |
| 57 | this.dns2 = dns2; |
| 58 | this.dnsSuffix = dnsSuffix; |
| 59 | Iterables.addAll(this.ipRanges, checkNotNull(ipRanges, "ipRanges")); |
| 60 | Iterables.addAll(this.allocatedIpAddresses, checkNotNull(allocatedIpAddresses, "allocatedIpAddresses")); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return true of the values in this IpScope element are inherited from the ParentNetwork of the |
| 65 | * containing Configuration |
| 66 | * @since vcloud api 0.9 |
| 67 | */ |
| 68 | public boolean isInherited() { |
| 69 | return inherited; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return IP address of the network gateway |
| 74 | * |
| 75 | * @since vcloud api 0.8 |
| 76 | */ |
| 77 | @Nullable |
| 78 | public String getGateway() { |
| 79 | return gateway; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return netmask to apply to addresses on the network |
| 84 | * |
| 85 | * @since vcloud api 0.8 |
| 86 | */ |
| 87 | @Nullable |
| 88 | public String getNetmask() { |
| 89 | return netmask; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @return IP address of the primary DNS server for this network |
| 94 | * |
| 95 | * @since vcloud api 0.9 |
| 96 | */ |
| 97 | @Nullable |
| 98 | public String getDns1() { |
| 99 | return dns1; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return IP address of the secondary DNS server for this network |
| 104 | * |
| 105 | * @since vcloud api 0.9 |
| 106 | */ |
| 107 | @Nullable |
| 108 | public String getDns2() { |
| 109 | return dns2; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @return suffix to be applied when resolving hostnames that are not fully?qualified. |
| 114 | * |
| 115 | * @since vcloud api 0.9 |
| 116 | */ |
| 117 | @Nullable |
| 118 | public String getDnsSuffix() { |
| 119 | return dnsSuffix; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return A container for IpRange elements. |
| 124 | * |
| 125 | * @since vcloud api 0.9 |
| 126 | */ |
| 127 | public Set<IpRange> getIpRanges() { |
| 128 | return ipRanges; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @return A list of addresses allocated from any of the specified IpRanges |
| 133 | * |
| 134 | * @since vcloud api 0.9 |
| 135 | */ |
| 136 | public Set<String> getAllocatedIpAddresses() { |
| 137 | return allocatedIpAddresses; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public int hashCode() { |
| 142 | final int prime = 31; |
| 143 | int result = 1; |
| 144 | result = prime * result + ((allocatedIpAddresses == null) ? 0 : allocatedIpAddresses.hashCode()); |
| 145 | result = prime * result + ((dns1 == null) ? 0 : dns1.hashCode()); |
| 146 | result = prime * result + ((dns2 == null) ? 0 : dns2.hashCode()); |
| 147 | result = prime * result + ((dnsSuffix == null) ? 0 : dnsSuffix.hashCode()); |
| 148 | result = prime * result + ((gateway == null) ? 0 : gateway.hashCode()); |
| 149 | result = prime * result + (inherited ? 1231 : 1237); |
| 150 | result = prime * result + ((ipRanges == null) ? 0 : ipRanges.hashCode()); |
| 151 | result = prime * result + ((netmask == null) ? 0 : netmask.hashCode()); |
| 152 | return result; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object obj) { |
| 157 | if (this == obj) |
| 158 | return true; |
| 159 | if (obj == null) |
| 160 | return false; |
| 161 | if (getClass() != obj.getClass()) |
| 162 | return false; |
| 163 | IpScope other = (IpScope) obj; |
| 164 | if (allocatedIpAddresses == null) { |
| 165 | if (other.allocatedIpAddresses != null) |
| 166 | return false; |
| 167 | } else if (!allocatedIpAddresses.equals(other.allocatedIpAddresses)) |
| 168 | return false; |
| 169 | if (dns1 == null) { |
| 170 | if (other.dns1 != null) |
| 171 | return false; |
| 172 | } else if (!dns1.equals(other.dns1)) |
| 173 | return false; |
| 174 | if (dns2 == null) { |
| 175 | if (other.dns2 != null) |
| 176 | return false; |
| 177 | } else if (!dns2.equals(other.dns2)) |
| 178 | return false; |
| 179 | if (dnsSuffix == null) { |
| 180 | if (other.dnsSuffix != null) |
| 181 | return false; |
| 182 | } else if (!dnsSuffix.equals(other.dnsSuffix)) |
| 183 | return false; |
| 184 | if (gateway == null) { |
| 185 | if (other.gateway != null) |
| 186 | return false; |
| 187 | } else if (!gateway.equals(other.gateway)) |
| 188 | return false; |
| 189 | if (inherited != other.inherited) |
| 190 | return false; |
| 191 | if (ipRanges == null) { |
| 192 | if (other.ipRanges != null) |
| 193 | return false; |
| 194 | } else if (!ipRanges.equals(other.ipRanges)) |
| 195 | return false; |
| 196 | if (netmask == null) { |
| 197 | if (other.netmask != null) |
| 198 | return false; |
| 199 | } else if (!netmask.equals(other.netmask)) |
| 200 | return false; |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public String toString() { |
| 206 | return "[allocatedIpAddresses=" + allocatedIpAddresses + ", dns1=" + dns1 + ", dns2=" + dns2 + ", dnsSuffix=" |
| 207 | + dnsSuffix + ", gateway=" + gateway + ", inherited=" + inherited + ", ipRanges=" + ipRanges |
| 208 | + ", netmask=" + netmask + "]"; |
| 209 | } |
| 210 | } |