Remove vip from the vips collections instead of members.
[controller.git] / opendaylight / samples / loadbalancer / src / main / java / org / opendaylight / controller / samples / loadbalancer / entities / Pool.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.samples.loadbalancer.entities;
9
10 import java.util.ArrayList;
11
12 import javax.xml.bind.annotation.XmlAccessType;
13 import javax.xml.bind.annotation.XmlAccessorType;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlRootElement;
16
17 /**
18  * This class represents the pool of hosts among which incoming traffic
19  * will be load balanced. Each pool will load balance the traffic among its pool members
20  * based on the loadbalancing policy set for the pool.
21  * Currently, the pool supports two load balancing policies:
22  * 1. Round Robin Policy{@link org.opendaylight.controller.samples.loadbalancer.policies.RoundRobinLBPolicy}
23  * 2. Random Policy {@link org.opendaylight.controller.samples.loadbalancer.policies.RandomLBPolicy}
24  *
25  * NOTE: After creation of the pool, user can't update (change) its load balancing policy.
26  * NOTE: Each Pool should have a unique name.
27  */
28
29 @XmlRootElement(name="pool")
30 @XmlAccessorType(XmlAccessType.NONE)
31 public class Pool {
32
33     /*
34      * Unique name of the pool
35      */
36     @XmlElement
37     private String name;
38
39     /*
40      * Associated load balancing policy
41      */
42     @XmlElement(name="lbmethod")
43     private String lbMethod;
44
45     /*
46      * Status of the pool (active/inactive)
47      */
48     @XmlElement
49     private String status;
50
51     /*
52      * List of all the VIPs using this pool for load balancing their traffic - more than
53      * one VIP can be mapped to each pool.
54      */
55     @XmlElement
56     private ArrayList<VIP> vips = new ArrayList<VIP>();
57
58     /*
59      * List of all the pool members used for load balancing the traffic
60      */
61     @XmlElement
62     private ArrayList<PoolMember> members = new ArrayList<PoolMember>();
63
64     /*
65      * Private constructor used for JAXB mapping
66      */
67     @SuppressWarnings("unused")
68     private Pool() {}
69
70     /**
71      * Getter/ Setter methods
72      */
73
74     public Pool(String name,
75                     String lbMethod) {
76         this.name = name;
77         this.lbMethod = lbMethod;
78     }
79
80     /**
81      * @return the name
82      */
83     public String getName() {
84         return name;
85     }
86
87     /**
88      * @param name the name to set
89      */
90     public void setName(String name) {
91         this.name = name;
92     }
93
94     /**
95      * @return the lbMethod
96      */
97     public String getLbMethod() {
98         return lbMethod;
99     }
100
101     /**
102      * @param lbMethod the lbMethod to set
103      */
104     public void setLbMethod(String lbMethod) {
105         this.lbMethod = lbMethod;
106     }
107
108     /**
109      * @return the status
110      */
111     public String getStatus() {
112         return status;
113     }
114
115     /**
116      * @param status the status to set
117      */
118     public void setStatus(String status) {
119         this.status = status;
120     }
121
122     /**
123      * @return the vip
124      */
125     public ArrayList<VIP> getAllVip() {
126         return vips;
127     }
128
129     /**
130      * @param vip the vip to set
131      */
132     public void setVips(ArrayList<VIP> vips) {
133         this.vips = vips;
134     }
135
136     /**
137      * @return the members
138      */
139     public ArrayList<PoolMember> getAllMembers() {
140         return members;
141     }
142
143     /**
144      * @param members the members to set
145      */
146     public void setMembers(ArrayList<PoolMember> members) {
147         this.members = members;
148     }
149
150     /**
151      * Add new VIP to the VIP list
152      * @param vip       new VIP to add
153      */
154     public void addVIP(VIP vip){
155         this.vips.add(vip);
156     }
157
158     /**
159      * Remove VIP with given name from the VIP list of the pool
160      * @param name      Name of the VIP
161      * @return  true     If VIP was using this pool and removed
162      *          false   IF VIP is not using this pool
163      */
164     public boolean removeVIP(String name){
165         for(VIP vip: this.vips){
166             if(vip.getName().equals(name)){
167                 this.vips.remove(vip);
168                 return true;
169             }
170         }
171         return false;
172     }
173
174     /**
175      * Check if the given pool member is part of this pool
176      * @param pm        Search for this pool member
177      * @return  true     If pool member is attached to this pool
178      *          false   else
179      */
180     public boolean poolMemberExists(PoolMember pm){
181         return this.members.contains(pm);
182     }
183
184     /**
185      * Returns the pool member with the given name
186      * @param name      Search for this pool member
187      * @return  PoolMember       If pool member is attached to this pool
188      *          null            else
189      */
190     public PoolMember getMember(String name){
191
192         for(PoolMember pm: this.members){
193             if(pm.getName().equals(name)){
194                 return pm;
195             }
196         }
197         return null;
198     }
199
200     /**
201      * Add new pool member to the pool
202      * @param pm        Add this new pool
203      */
204     public void addMember(PoolMember pm){
205         this.members.add(pm);
206     }
207
208     /**
209      * Remove pool member from the pool list
210      * @param name Remove this pool member
211      * @return  true    If pool member was attached to this pool and successfully removed
212      *          false   If pool member is not attached to this pool
213      */
214     public boolean removeMember(String name){
215         for(PoolMember pm: this.members){
216             if(pm.getName().equals(name)){
217                 this.members.remove(pm);
218                 return true;
219             }
220         }
221         return false;
222     }
223
224     /* (non-Javadoc)
225      * @see java.lang.Object#hashCode()
226      */
227     @Override
228     public int hashCode() {
229         final int prime = 31;
230         int result = 1;
231         result = prime * result+ ((lbMethod == null) ? 0 : lbMethod.hashCode());
232         result = prime * result + ((name == null) ? 0 : name.hashCode());
233         return result;
234     }
235
236     /* (non-Javadoc)
237      * @see java.lang.Object#equals(java.lang.Object)
238      */
239     @Override
240     public boolean equals(Object obj) {
241         if (this == obj) {
242             return true;
243         }
244         if (obj == null) {
245             return false;
246         }
247         if (!(obj instanceof Pool)) {
248             return false;
249         }
250         Pool other = (Pool) obj;
251         if (lbMethod == null) {
252             if (other.lbMethod != null) {
253                 return false;
254             }
255         }else if (!lbMethod.equals(other.lbMethod)) {
256             return false;
257         }
258         if (name == null) {
259             if (other.name != null) {
260                 return false;
261             }
262         }else if (!name.equals(other.name)) {
263             return false;
264         }
265         return true;
266     }
267
268     /* (non-Javadoc)
269      * @see java.lang.Object#toString()
270      */
271     @Override
272     public String toString() {
273         return "Pool [name=" + name + ", lbMethod=" + lbMethod + ", status="
274                             + status + ", vips=" + vips + ", members=" + members + "]";
275     }
276 }