View Javadoc

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.cloudloadbalancers.domain;
20  
21  import static com.google.common.base.Preconditions.checkArgument;
22  import static com.google.common.base.Preconditions.checkNotNull;
23  
24  /**
25   * A virtual IP (VIP) makes a load balancer accessible by clients. The load balancing service
26   * supports either a public VIP, routable on the public Internet, or a ServiceNet address, routable
27   * only within the region in which the load balancer resides.
28   * 
29   * @author Adrian Cole
30   * @see <a href=
31   *      "http://docs.rackspacecloud.com/loadbalancers/api/v1.0/clb-devguide/content/ch04s03s01.html"
32   *      />
33   */
34  public class VirtualIP implements Comparable<VirtualIP> {
35     public static Builder builder() {
36        return new Builder();
37     }
38  
39     public static class Builder {
40        private int id = -1;
41        private String address;
42        private Type type;
43        private IPVersion ipVersion = IPVersion.IPV4;
44  
45        public Builder id(int id) {
46           this.id = id;
47           return this;
48        }
49  
50        public Builder address(String address) {
51           this.address = address;
52           return this;
53        }
54  
55        public Builder type(Type type) {
56           this.type = type;
57           return this;
58        }
59  
60        public Builder ipVersion(IPVersion ipVersion) {
61           this.ipVersion = ipVersion;
62           return this;
63        }
64  
65        public VirtualIP build() {
66           return new VirtualIP(id, address, type, ipVersion);
67        }
68     }
69  
70     /**
71      * Virtual IP Types
72      */
73     public static enum Type {
74        /**
75         * An address that is routable on the public Internet.
76         */
77        PUBLIC,
78        /**
79         * An address that is routable only on ServiceNet.
80         */
81        SERVICENET, UNRECOGNIZED;
82  
83        public static Type fromValue(String type) {
84           try {
85              return valueOf(checkNotNull(type, "type"));
86           } catch (IllegalArgumentException e) {
87              return UNRECOGNIZED;
88           }
89        }
90  
91     }
92  
93     /**
94      * Virtual IP Versions
95      */
96     public static enum IPVersion {
97  
98        IPV4, IPV6, UNRECOGNIZED;
99  
100       public static IPVersion fromValue(String ipVersion) {
101          try {
102             return valueOf(checkNotNull(ipVersion, "ipVersion"));
103          } catch (IllegalArgumentException e) {
104             return UNRECOGNIZED;
105          }
106       }
107 
108    }
109 
110    // for serialization only
111    VirtualIP() {
112 
113    }
114 
115    private int id;
116    private String address;
117    private Type type;
118    private IPVersion ipVersion;
119 
120    public VirtualIP(int id, String address, Type type, IPVersion ipVersion) {
121       checkArgument(id != -1, "id must be specified");
122       this.id = id;
123       this.address = checkNotNull(address, "address");
124       this.type = checkNotNull(type, "type");
125       this.ipVersion = checkNotNull(ipVersion, "ipVersion");
126    }
127 
128    public int getId() {
129       return id;
130    }
131 
132    public String getAddress() {
133       return address;
134    }
135 
136    public Type getType() {
137       return type;
138    }
139 
140    public IPVersion getIpVersion() {
141       return ipVersion;
142    }
143 
144    @Override
145    public int compareTo(VirtualIP arg0) {
146       return address.compareTo(arg0.address);
147    }
148 
149    @Override
150    public int hashCode() {
151       final int prime = 31;
152       int result = 1;
153       result = prime * result + id;
154       return result;
155    }
156 
157    @Override
158    public boolean equals(Object obj) {
159       if (this == obj)
160          return true;
161       if (obj == null)
162          return false;
163       if (getClass() != obj.getClass())
164          return false;
165       VirtualIP other = (VirtualIP) obj;
166       if (id != other.id)
167          return false;
168       return true;
169    }
170 
171    @Override
172    public String toString() {
173       return String.format("[address=%s, id=%s, ipVersion=%s, type=%s]", address, id, ipVersion, type);
174    }
175 
176 }