Update .gitreview for new repo
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / netvirt / openstack / netvirt / api / LoadBalancerConfiguration.java
1 /*
2  * Copyright (c) 2014, 2015 SDN Hub, LLC. and others. 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
9 package org.opendaylight.netvirt.openstack.netvirt.api;
10
11 import com.google.common.collect.Maps;
12
13 import java.util.Map;
14
15 /**
16  * Store configuration for each load balancer instance created.
17  */
18
19 public class LoadBalancerConfiguration {
20     public static final String PROTOCOL_TCP = "TCP";
21     public static final String PROTOCOL_HTTP = "HTTP";
22     public static final String PROTOCOL_HTTPS = "HTTPS";
23
24     public class LoadBalancerPoolMember {
25         String ipAddr;
26         String macAddr;
27         String protocol;
28         Integer port;
29         int index;
30
31         public LoadBalancerPoolMember(String ipAddr, String macAddr, String protocol, Integer port) {
32             this.ipAddr = ipAddr;
33             this.macAddr = macAddr;
34             this.protocol = protocol;
35             this.port = port;
36             this.index = -1;
37         }
38         public LoadBalancerPoolMember(String ipAddr, String macAddr, String protocol, Integer port, int index) {
39             this.ipAddr = ipAddr;
40             this.macAddr = macAddr;
41             this.protocol = protocol;
42             this.port = port;
43             this.index = index;
44         }
45         public String getIP() {
46             return ipAddr;
47         }
48         public String getMAC() {
49             return macAddr;
50         }
51         public String getProtocol() {
52             return protocol;
53         }
54         public Integer getPort() {
55             return port;
56         }
57         public int getIndex() {
58             return index;
59         }
60         public void setIndex(int index) {
61             this.index = index;
62         }
63
64         /**
65          * Overridden equals() where index is not checked.
66          */
67         @Override
68         public boolean equals(Object obj) {
69             if (this == obj) {
70                 return true;
71             }
72             if (obj == null) {
73                 return false;
74             }
75             if (getClass() != obj.getClass()) {
76                 return false;
77             }
78             LoadBalancerPoolMember other = (LoadBalancerPoolMember) obj;
79             if (ipAddr == null) {
80                 if (other.ipAddr != null) {
81                     return false;
82                 }
83             } else if (!ipAddr.equals(other.ipAddr)) {
84                 return false;
85             }
86             if (macAddr == null) {
87                 if (other.macAddr != null) {
88                     return false;
89                 }
90             } else if (!macAddr.equals(other.macAddr)) {
91                 return false;
92             }
93             if (port == null) {
94                 if (other.port != null) {
95                     return false;
96                 }
97             } else if (!port.equals(other.port)) {
98                 return false;
99             }
100             if (protocol == null) {
101                 if (other.protocol != null) {
102                     return false;
103                 }
104             } else if (!protocol.equals(other.protocol)) {
105                 return false;
106             }
107             return true;
108         }
109
110         @Override
111         public String toString() {
112             return "LoadBalancerPoolMember [ip=" + ipAddr + ", mac=" + macAddr +
113                     ", protocol=" + protocol + ", port=" + port + ", index=" + index + "]";
114         }
115
116         @Override
117         public int hashCode() {
118             final int prime = 31;
119             int result = 1;
120             result = prime * result + ((ipAddr == null) ? 0 : ipAddr.hashCode());
121             result = prime * result + ((macAddr == null) ? 0 : macAddr.hashCode());
122             result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
123             result = prime * result + ((port == null) ? 0 : port.hashCode());
124             return result;
125         }
126     }
127
128     private String name;
129     private String vip;
130     private String vmac; //Used when a dummy neutron port is created for the VIP
131     private String providerNetworkType;
132     private String providerSegmentationId;
133     private Map <String, LoadBalancerPoolMember> members;
134
135     public LoadBalancerConfiguration() {
136         this.members = Maps.newHashMap();
137     }
138
139     public LoadBalancerConfiguration(String name, String vip) {
140         this.members = Maps.newHashMap();
141         this.name = name;
142         this.vip = vip;
143         this.vmac = null;
144     }
145
146     public LoadBalancerConfiguration(String name, String vip, String vmac) {
147         this.members = Maps.newHashMap();
148         this.name = name;
149         this.vip = vip;
150         this.vmac = vmac;
151     }
152
153     public LoadBalancerConfiguration(LoadBalancerConfiguration lbConfig) {
154         this.members = Maps.newHashMap(lbConfig.getMembers());
155         this.name = lbConfig.getName();
156         this.vip = lbConfig.getVip();
157         this.vmac = lbConfig.getVmac();
158         this.providerNetworkType = lbConfig.getProviderNetworkType();
159         this.providerSegmentationId = lbConfig.getProviderSegmentationId();
160     }
161
162     public Map<String, LoadBalancerPoolMember> getMembers() {
163         return this.members;
164     }
165
166     public Map<String, LoadBalancerPoolMember> addMember(String uuid, LoadBalancerPoolMember member) {
167         //If index is not set for this object, update it before inserting
168         if (member.getIndex() == -1) {
169             member.setIndex(members.size());
170         }
171         this.members.put(uuid, member);
172         return this.members;
173     }
174     public Map<String, LoadBalancerPoolMember> addMember(String uuid, String ipAddr, String macAddr, String protocol, Integer port) {
175         this.members.put(uuid,
176                 new LoadBalancerPoolMember(ipAddr, macAddr, protocol, port, members.size()));
177         return this.members;
178     }
179     public Map<String, LoadBalancerPoolMember> removeMember(String uuid) {
180         this.members.remove(uuid);
181         /* Update indices of all other members
182          */
183         int index = 0;
184         for(Map.Entry<String, LoadBalancerPoolMember> entry : this.getMembers().entrySet()) {
185             entry.getValue().setIndex(index++);
186         }
187         return this.members;
188     }
189
190     public boolean isValid() {
191         if (members.size() == 0) {
192             return false;
193         } else if (providerNetworkType == null) {
194             return false;
195         }
196         return true;
197     }
198
199     public void setVip(String vip) {
200         this.vip = vip;
201     }
202
203     public String getVip() {
204         return this.vip;
205     }
206
207     public void setVmac(String vmac) {
208         this.vmac = vmac;
209     }
210
211     public String getVmac() {
212         return this.vmac;
213     }
214
215     public void setName(String name) {
216         this.name = name;
217     }
218
219     public String getName() {
220         return this.name;
221     }
222
223     public void setProviderSegmentationId(String providerSegmentationId) {
224         this.providerSegmentationId = providerSegmentationId;
225     }
226
227     public String getProviderSegmentationId() {
228         return this.providerSegmentationId;
229     }
230     public void setProviderNetworkType(String providerNetworkType) {
231         this.providerNetworkType = providerNetworkType;
232     }
233
234     public String getProviderNetworkType() {
235         return this.providerNetworkType;
236     }
237
238     @Override
239     public String toString() {
240         return "LoadBalancerConfiguration [name=" + name +
241                 ", vip=" + vip + ", vmac=" + vmac +
242                 ", networkType=" + providerNetworkType +
243                 ", segmentationId=" + providerSegmentationId +
244                 ", members=" + members + "]";
245     }
246 }