fix for bug 2928
[neutron.git] / neutron-spi / src / main / java / org / opendaylight / neutron / spi / NeutronPort.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
9 package org.opendaylight.neutron.spi;
10
11
12 import java.io.Serializable;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlRootElement;
22
23
24 @XmlRootElement
25 @XmlAccessorType(XmlAccessType.NONE)
26
27 public class NeutronPort implements Serializable, INeutronObject {
28     private static final long serialVersionUID = 1L;
29
30     // See OpenStack Network API v2.0 Reference for description of
31     // annotated attributes
32
33     @XmlElement (name = "id")
34     String portUUID;
35
36     @XmlElement (name = "network_id")
37     String networkUUID;
38
39     @XmlElement (name = "name")
40     String name;
41
42     @XmlElement (defaultValue = "true", name = "admin_state_up")
43     Boolean adminStateUp;
44
45     @XmlElement (name = "status")
46     String status;
47
48     @XmlElement (name = "mac_address")
49     String macAddress;
50
51     @XmlElement (name = "fixed_ips")
52     List<Neutron_IPs> fixedIPs;
53
54     @XmlElement (name = "device_id")
55     String deviceID;
56
57     @XmlElement (name = "device_owner")
58     String deviceOwner;
59
60     @XmlElement (name = "tenant_id")
61     String tenantID;
62
63     @XmlElement (name = "security_groups")
64     List<NeutronSecurityGroup> securityGroups;
65
66     @XmlElement (name = "allowed_address_pairs")
67     List<NeutronPort_AllowedAddressPairs> allowedAddressPairs;
68
69     //@XmlElement (name = "binding:host_id")
70     @XmlElement (namespace = "binding", name = "host_id")
71     String bindinghostID;
72
73     //@XmlElement (name = "binding:vnic_type")
74     @XmlElement (namespace = "binding", name = "vnic_type")
75     String bindingvnicType;
76
77     //@XmlElement (name = "binding:vif_type")
78     @XmlElement (namespace = "binding", name = "vif_type")
79     String bindingvifType;
80
81     //@XmlElement (name = "binding:vif_details")
82     @XmlElement (namespace = "binding", name = "vif_details")
83     List<NeutronPort_VIFDetail> vifDetails;
84
85     @XmlElement (name = "extra_dhcp_opts")
86     List<NeutronPort_ExtraDHCPOption> extraDHCPOptions;
87
88     /* this attribute stores the floating IP address assigned to
89      * each fixed IP address
90      */
91
92     HashMap<String, NeutronFloatingIP> floatingIPMap;
93
94     public NeutronPort() {
95         floatingIPMap = new HashMap<String, NeutronFloatingIP>();
96     }
97
98     public String getID() { return portUUID; }
99
100     public void setID(String id) { this.portUUID = id; }
101
102     public String getPortUUID() {
103         return portUUID;
104     }
105
106     public void setPortUUID(String portUUID) {
107         this.portUUID = portUUID;
108     }
109
110     public String getNetworkUUID() {
111         return networkUUID;
112     }
113
114     public void setNetworkUUID(String networkUUID) {
115         this.networkUUID = networkUUID;
116     }
117
118     public String getName() {
119         return name;
120     }
121
122     public void setName(String name) {
123         this.name = name;
124     }
125
126     public boolean isAdminStateUp() {
127         if (adminStateUp == null) {
128             return true;
129         }
130         return adminStateUp;
131     }
132
133     public Boolean getAdminStateUp() { return adminStateUp; }
134
135     public void setAdminStateUp(Boolean newValue) {
136             adminStateUp = newValue;
137     }
138
139     public String getStatus() {
140         return status;
141     }
142
143     public void setStatus(String status) {
144         this.status = status;
145     }
146
147     public String getMacAddress() {
148         return macAddress;
149     }
150
151     public void setMacAddress(String macAddress) {
152         this.macAddress = macAddress;
153     }
154
155     public List<Neutron_IPs> getFixedIPs() {
156         return fixedIPs;
157     }
158
159     public void setFixedIPs(List<Neutron_IPs> fixedIPs) {
160         this.fixedIPs = fixedIPs;
161     }
162
163     public String getDeviceID() {
164         return deviceID;
165     }
166
167     public void setDeviceID(String deviceID) {
168         this.deviceID = deviceID;
169     }
170
171     public String getDeviceOwner() {
172         return deviceOwner;
173     }
174
175     public void setDeviceOwner(String deviceOwner) {
176         this.deviceOwner = deviceOwner;
177     }
178
179     public String getTenantID() {
180         return tenantID;
181     }
182
183     public void setTenantID(String tenantID) {
184         this.tenantID = tenantID;
185     }
186
187     public List<NeutronSecurityGroup> getSecurityGroups() {
188         return securityGroups;
189     }
190
191     public void setSecurityGroups(List<NeutronSecurityGroup> securityGroups) {
192         this.securityGroups = securityGroups;
193     }
194
195     public List<NeutronPort_AllowedAddressPairs> getAllowedAddressPairs() {
196         return allowedAddressPairs;
197     }
198
199     public void setAllowedAddressPairs(List<NeutronPort_AllowedAddressPairs> allowedAddressPairs) {
200         this.allowedAddressPairs = allowedAddressPairs;
201     }
202
203     public List<NeutronPort_ExtraDHCPOption> getExtraDHCPOptions() {
204         return extraDHCPOptions;
205     }
206
207     public void setExtraDHCPOptions(List<NeutronPort_ExtraDHCPOption> extraDHCPOptions) {
208         this.extraDHCPOptions = extraDHCPOptions;
209     }
210
211     public List<NeutronPort_VIFDetail> getVIFDetail() {
212         return vifDetails;
213     }
214
215     public void setVIFDetail(List<NeutronPort_VIFDetail> vifDetails) {
216         this.vifDetails = vifDetails;
217     }
218
219     public String getBindinghostID() {
220       return bindinghostID;
221     }
222
223     public void setBindinghostID(String bindinghostID) {
224       this.bindinghostID = bindinghostID;
225     }
226
227   public String getBindingvnicType() {
228     return bindingvnicType;
229   }
230
231   public void setBindingvnicType(String bindingvnicType) {
232     this.bindingvnicType = bindingvnicType;
233   }
234
235   public String getBindingvifType() {
236     return bindingvifType;
237   }
238
239   public void setBindingvifType(String bindingvifType) {
240     this.bindingvifType = bindingvifType;
241   }
242
243     public NeutronFloatingIP getFloatingIP(String key) {
244         if (!floatingIPMap.containsKey(key)) {
245             return null;
246         }
247         return floatingIPMap.get(key);
248     }
249
250     public void removeFloatingIP(String key) {
251         floatingIPMap.remove(key);
252     }
253
254     public void addFloatingIP(String key, NeutronFloatingIP floatingIP) {
255         if (!floatingIPMap.containsKey(key)) {
256             floatingIPMap.put(key, floatingIP);
257         }
258     }
259
260     /**
261      * This method copies selected fields from the object and returns them
262      * as a new object, suitable for marshaling.
263      *
264      * @param fields
265      *            List of attributes to be extracted
266      * @return an OpenStackPorts object with only the selected fields
267      * populated
268      */
269
270     public NeutronPort extractFields(List<String> fields) {
271         NeutronPort ans = new NeutronPort();
272         Iterator<String> i = fields.iterator();
273         while (i.hasNext()) {
274             String s = i.next();
275             if (s.equals("id")) {
276                 ans.setPortUUID(this.getPortUUID());
277             }
278             if (s.equals("network_id")) {
279                 ans.setNetworkUUID(this.getNetworkUUID());
280             }
281             if (s.equals("name")) {
282                 ans.setName(this.getName());
283             }
284             if (s.equals("admin_state_up")) {
285                 ans.setAdminStateUp(this.getAdminStateUp());
286             }
287             if (s.equals("status")) {
288                 ans.setStatus(this.getStatus());
289             }
290             if (s.equals("mac_address")) {
291                 ans.setMacAddress(this.getMacAddress());
292             }
293             if (s.equals("fixed_ips")) {
294                 List<Neutron_IPs> fixedIPs = new ArrayList<Neutron_IPs>();
295                 fixedIPs.addAll(this.getFixedIPs());
296                 ans.setFixedIPs(fixedIPs);
297             }
298             if (s.equals("device_id")) {
299                 ans.setDeviceID(this.getDeviceID());
300             }
301             if (s.equals("device_owner")) {
302                 ans.setDeviceOwner(this.getDeviceOwner());
303             }
304             if (s.equals("tenant_id")) {
305                 ans.setTenantID(this.getTenantID());
306             }
307             if (s.equals("security_groups")) {
308                 List<NeutronSecurityGroup> securityGroups = new ArrayList<NeutronSecurityGroup>();
309                 securityGroups.addAll(this.getSecurityGroups());
310                 ans.setSecurityGroups(securityGroups);
311             }
312         }
313         return ans;
314     }
315
316     public void initDefaults() {
317         adminStateUp = true;
318         if (status == null) {
319             status = "ACTIVE";
320         }
321         if (fixedIPs == null) {
322             fixedIPs = new ArrayList<Neutron_IPs>();
323         }
324     }
325
326     /**
327      * This method checks to see if the port has a floating IPv4 address
328      * associated with the supplied fixed IPv4 address
329      *
330      * @param fixedIP
331      *            fixed IPv4 address in dotted decimal format
332      * @return a boolean indicating if there is a floating IPv4 address bound
333      * to the fixed IPv4 address
334      */
335
336     public boolean isBoundToFloatingIP(String fixedIP) {
337         return floatingIPMap.containsKey(fixedIP);
338     }
339
340     @Override
341     public String toString() {
342         return "NeutronPort [portUUID=" + portUUID + ", networkUUID=" + networkUUID + ", name=" + name
343                 + ", adminStateUp=" + adminStateUp + ", status=" + status + ", macAddress=" + macAddress
344                 + ", fixedIPs=" + fixedIPs + ", deviceID=" + deviceID + ", deviceOwner=" + deviceOwner + ", tenantID="
345                 + tenantID + ", floatingIPMap=" + floatingIPMap + ", securityGroups=" + securityGroups
346                 + ", bindinghostID=" + bindinghostID + ", bindingvnicType=" + bindingvnicType
347                 + ", bindingvnicType=" + bindingvnicType + "]";
348     }
349 }