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  import java.util.Date;
25  import java.util.Set;
26  
27  import org.jclouds.cloudloadbalancers.domain.internal.BaseLoadBalancer;
28  
29  import com.google.common.collect.ImmutableSet;
30  
31  /**
32   * 
33   * @author Adrian Cole
34   * @see <a href=
35   *      "http://docs.rackspacecloud.com/loadbalancers/api/v1.0/clb-devguide/content/ch04s01s02.html"
36   *      />
37   */
38  public class LoadBalancer extends BaseLoadBalancer<Node, LoadBalancer> {
39  
40     @SuppressWarnings("unchecked")
41     public static Builder builder() {
42        return new Builder();
43     }
44  
45     /**
46      * {@inheritDoc}
47      */
48     @Override
49     public Builder toBuilder() {
50        return new Builder().from(this);
51     }
52  
53     public static class Builder extends BaseLoadBalancer.Builder<Node, LoadBalancer> {
54        private String region;
55        private int id = -1;
56        private Status status;
57        private Set<VirtualIP> virtualIPs = ImmutableSet.<VirtualIP> of();
58        private String sessionPersistenceType;
59        private String clusterName;
60        private Date created;
61        private Date updated;
62        private boolean connectionLoggingEnabled;
63  
64        public Builder region(String region) {
65           this.region = region;
66           return this;
67        }
68  
69        public Builder id(int id) {
70           this.id = id;
71           return this;
72        }
73  
74        public Builder status(Status status) {
75           this.status = status;
76           return this;
77        }
78  
79        public Builder virtualIPs(Iterable<VirtualIP> virtualIPs) {
80           this.virtualIPs = ImmutableSet.<VirtualIP> copyOf(checkNotNull(virtualIPs, "virtualIPs"));
81           return this;
82        }
83  
84        public Builder sessionPersistenceType(String sessionPersistenceType) {
85           this.sessionPersistenceType = sessionPersistenceType;
86           return this;
87        }
88  
89        public Builder clusterName(String clusterName) {
90           this.clusterName = clusterName;
91           return this;
92        }
93  
94        public Builder created(Date created) {
95           this.created = created;
96           return this;
97        }
98  
99        public Builder updated(Date updated) {
100          this.updated = updated;
101          return this;
102       }
103 
104       public Builder connectionLoggingEnabled(boolean connectionLoggingEnabled) {
105          this.connectionLoggingEnabled = connectionLoggingEnabled;
106          return this;
107       }
108 
109       public LoadBalancer build() {
110          return new LoadBalancer(region, id, name, protocol, port, algorithm, status, virtualIPs, nodes,
111                   sessionPersistenceType, clusterName, created, updated, connectionLoggingEnabled);
112       }
113 
114       @Override
115       public Builder nodes(Iterable<Node> nodes) {
116          this.nodes = ImmutableSet.<Node> copyOf(checkNotNull(nodes, "nodes"));
117          return this;
118       }
119 
120       @Override
121       public Builder node(Node nodes) {
122          this.nodes.add(checkNotNull(nodes, "nodes"));
123          return this;
124       }
125 
126       @Override
127       public Builder algorithm(String algorithm) {
128          return Builder.class.cast(super.algorithm(algorithm));
129       }
130 
131       @Override
132       public Builder from(LoadBalancer in) {
133          return Builder.class.cast(super.from(in)).id(in.getId()).status(in.getStatus()).virtualIPs(in.getVirtualIPs())
134                   .clusterName(in.getClusterName()).created(in.getCreated()).updated(in.getUpdated())
135                   .connectionLoggingEnabled(in.isConnectionLoggingEnabled());
136       }
137 
138       @Override
139       public Builder name(String name) {
140          return Builder.class.cast(super.name(name));
141       }
142 
143       @Override
144       public Builder port(Integer port) {
145          return Builder.class.cast(super.port(port));
146       }
147 
148       @Override
149       public Builder protocol(String protocol) {
150          return Builder.class.cast(super.protocol(protocol));
151       }
152 
153    }
154 
155    /**
156     * All load balancers also have a status attribute to signify the current configuration status of
157     * the device. This status is immutable by the caller and is updated automatically based on state
158     * changes within the service. When a load balancer is first created, it will be placed into a
159     * BUILD status while the configuration is being generated and applied based on the request. Once
160     * the configuration is applied and finalized, it will be in an ACTIVE status. In the event of a
161     * configuration change or update, the status of the load balancer will change to PENDING_UPDATE
162     * to signify configuration changes are in progress but have not yet been finalized. Load
163     * balancers in a SUSPENDED status are configured to reject traffic and will not forward requests
164     * to back-end nodess.
165     */
166    public static enum Status {
167       /**
168        * Load balancer is being provisioned for the first time and configuration is being applied to
169        * bring the service online. The service will not yet be ready to serve incoming requests.
170        */
171       BUILD,
172       /**
173        * Load balancer is configured properly and ready to serve traffic to incoming requests via
174        * the configured virtual IPs.
175        */
176       ACTIVE,
177       /**
178        * Load balancer is online but configuration changes are being applied to update the service
179        * based on a previous request.
180        */
181       PENDING_UPDATE,
182       /**
183        * Load balancer has been taken offline and disabled; contact Support.
184        */
185       SUSPENDED,
186       /**
187        * Load balancer is online but configuration changes are being applied to begin deletion of
188        * the service based on a previous request.
189        */
190       PENDING_DELETE,
191       /**
192        * Load balancers in DELETED status can be displayed for at least 90 days after deletion.
193        */
194       DELETED,
195       /**
196        * The system encountered an error when attempting to configure the load balancer; contact
197        * Support.
198        */
199       ERROR, UNRECOGNIZED;
200 
201       public static Status fromValue(String status) {
202          try {
203             return valueOf(checkNotNull(status, "status"));
204          } catch (IllegalArgumentException e) {
205             return UNRECOGNIZED;
206          }
207       }
208 
209    }
210 
211    private final String region;
212    private final int id;
213    private final Status status;
214    private final Set<VirtualIP> virtualIPs;
215    private final String sessionPersistenceType;
216    private final String clusterName;
217    private final Date created;
218    private final Date updated;
219    private final boolean connectionLoggingEnabled;
220 
221    public LoadBalancer(String region, int id, String name, String protocol, Integer port, String algorithm, Status status,
222             Iterable<VirtualIP> virtualIPs, Iterable<Node> nodes, String sessionPersistenceType, String clusterName,
223             Date created, Date updated, boolean connectionLoggingEnabled) {
224       super(name, protocol, port, algorithm, nodes);
225       this.region = checkNotNull(region, "region");
226       checkArgument(id != -1, "id must be specified");
227       this.id = id;
228       this.status = checkNotNull(status, "status");
229       this.virtualIPs = ImmutableSet.copyOf(checkNotNull(virtualIPs, "virtualIPs"));
230       this.sessionPersistenceType = sessionPersistenceType;
231       this.clusterName = clusterName;
232       this.created = checkNotNull(created, "created");
233       this.updated = checkNotNull(updated, "updated");
234       this.connectionLoggingEnabled = connectionLoggingEnabled;
235    }
236 
237    public String getRegion() {
238       return region;
239    }
240 
241    public int getId() {
242       return id;
243    }
244 
245    public Status getStatus() {
246       return status;
247    }
248 
249    public Set<VirtualIP> getVirtualIPs() {
250       return virtualIPs;
251    }
252 
253    public String getClusterName() {
254       return clusterName;
255    }
256 
257    public String getSessionPersistenceType() {
258       return sessionPersistenceType;
259    }
260 
261    public Date getCreated() {
262       return created;
263    }
264 
265    public Date getUpdated() {
266       return updated;
267    }
268 
269    public boolean isConnectionLoggingEnabled() {
270       return connectionLoggingEnabled;
271    }
272 
273    @Override
274    public String toString() {
275       return String
276                .format(
277                         "[region=%s, id=%s, name=%s, protocol=%s, port=%s, algorithm=%s, status=%s, virtualIPs=%s, nodes=%s, sessionPersistenceType=%s, created=%s, updated=%s, clusterName=%s, connectionLoggingEnabled=%s]",
278                         region, id, name, protocol, port, algorithm, status, virtualIPs, nodes, sessionPersistenceType,
279                         created, updated, clusterName, connectionLoggingEnabled);
280    }
281 
282    @Override
283    public int hashCode() {
284       final int prime = 31;
285       int result = super.hashCode();
286       result = prime * result + id;
287       result = prime * result + ((region == null) ? 0 : region.hashCode());
288       return result;
289    }
290 
291    @Override
292    public boolean equals(Object obj) {
293       if (this == obj)
294          return true;
295       if (!super.equals(obj))
296          return false;
297       if (getClass() != obj.getClass())
298          return false;
299       LoadBalancer other = (LoadBalancer) obj;
300       if (id != other.id)
301          return false;
302       if (region == null) {
303          if (other.region != null)
304             return false;
305       } else if (!region.equals(other.region))
306          return false;
307       return true;
308    }
309 
310 }