1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
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
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
157
158
159
160
161
162
163
164
165
166 public static enum Status {
167
168
169
170
171 BUILD,
172
173
174
175
176 ACTIVE,
177
178
179
180
181 PENDING_UPDATE,
182
183
184
185 SUSPENDED,
186
187
188
189
190 PENDING_DELETE,
191
192
193
194 DELETED,
195
196
197
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 }