bcadba202a049f9948d48270964beb94b9d7ea9b
[controller.git] / opendaylight / networkconfiguration / neutron / src / main / java / org / opendaylight / controller / networkconfig / neutron / 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.controller.networkconfig.neutron;
10
11 import java.io.Serializable;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import javax.xml.bind.annotation.XmlAccessType;
18 import javax.xml.bind.annotation.XmlAccessorType;
19 import javax.xml.bind.annotation.XmlElement;
20 import javax.xml.bind.annotation.XmlRootElement;
21
22
23 @XmlRootElement
24 @XmlAccessorType(XmlAccessType.NONE)
25
26 public class NeutronPort implements Serializable {
27     private static final long serialVersionUID = 1L;
28
29     // See OpenStack Network API v2.0 Reference for description of
30     // annotated attributes
31
32     @XmlElement (name="id")
33     String portUUID;
34
35     @XmlElement (name="network_id")
36     String networkUUID;
37
38     @XmlElement (name="name")
39     String name;
40
41     @XmlElement (defaultValue="true", name="admin_state_up")
42     Boolean adminStateUp;
43
44     @XmlElement (name="status")
45     String status;
46
47     @XmlElement (name="mac_address")
48     String macAddress;
49
50     @XmlElement (name="fixed_ips")
51     List<Neutron_IPs> fixedIPs;
52
53     @XmlElement (name="device_id")
54     String deviceID;
55
56     @XmlElement (name="device_owner")
57     String deviceOwner;
58
59     @XmlElement (name="tenant_id")
60     String tenantID;
61
62     // TODO: add security groups
63     //        @XmlElement (name="security_groups")
64     //        List<String> securityGroups;
65
66     /* this attribute stores the floating IP address assigned to
67      * each fixed IP address
68      */
69
70     HashMap<String, NeutronFloatingIP> floatingIPMap;
71
72     public NeutronPort() {
73         floatingIPMap = new HashMap<String, NeutronFloatingIP>();
74     }
75
76     public String getID() { return portUUID; }
77
78     public String getPortUUID() {
79         return portUUID;
80     }
81
82     public void setPortUUID(String portUUID) {
83         this.portUUID = portUUID;
84     }
85
86     public String getNetworkUUID() {
87         return networkUUID;
88     }
89
90     public void setNetworkUUID(String networkUUID) {
91         this.networkUUID = networkUUID;
92     }
93
94     public String getName() {
95         return name;
96     }
97
98     public void setName(String name) {
99         this.name = name;
100     }
101
102     public boolean isAdminStateUp() {
103         if (adminStateUp == null) {
104             return true;
105         }
106         return adminStateUp;
107     }
108
109     public Boolean getAdminStateUp() { return adminStateUp; }
110
111     public void setAdminStateUp(Boolean newValue) {
112             adminStateUp = newValue;
113     }
114
115     public String getStatus() {
116         return status;
117     }
118
119     public void setStatus(String status) {
120         this.status = status;
121     }
122
123     public String getMacAddress() {
124         return macAddress;
125     }
126
127     public void setMacAddress(String macAddress) {
128         this.macAddress = macAddress;
129     }
130
131     public List<Neutron_IPs> getFixedIPs() {
132         return fixedIPs;
133     }
134
135     public void setFixedIPs(List<Neutron_IPs> fixedIPs) {
136         this.fixedIPs = fixedIPs;
137     }
138
139     public String getDeviceID() {
140         return deviceID;
141     }
142
143     public void setDeviceID(String deviceID) {
144         this.deviceID = deviceID;
145     }
146
147     public String getDeviceOwner() {
148         return deviceOwner;
149     }
150
151     public void setDeviceOwner(String deviceOwner) {
152         this.deviceOwner = deviceOwner;
153     }
154
155     public String getTenantID() {
156         return tenantID;
157     }
158
159     public void setTenantID(String tenantID) {
160         this.tenantID = tenantID;
161     }
162
163     public NeutronFloatingIP getFloatingIP(String key) {
164         if (!floatingIPMap.containsKey(key)) {
165             return null;
166         }
167         return floatingIPMap.get(key);
168     }
169
170     public void removeFloatingIP(String key) {
171         floatingIPMap.remove(key);
172     }
173
174     public void addFloatingIP(String key, NeutronFloatingIP floatingIP) {
175         if (!floatingIPMap.containsKey(key)) {
176             floatingIPMap.put(key, floatingIP);
177         }
178     }
179
180     /**
181      * This method copies selected fields from the object and returns them
182      * as a new object, suitable for marshaling.
183      *
184      * @param fields
185      *            List of attributes to be extracted
186      * @return an OpenStackPorts object with only the selected fields
187      * populated
188      */
189
190     public NeutronPort extractFields(List<String> fields) {
191         NeutronPort ans = new NeutronPort();
192         Iterator<String> i = fields.iterator();
193         while (i.hasNext()) {
194             String s = i.next();
195             if (s.equals("id")) {
196                 ans.setPortUUID(this.getPortUUID());
197             }
198             if (s.equals("network_id")) {
199                 ans.setNetworkUUID(this.getNetworkUUID());
200             }
201             if (s.equals("name")) {
202                 ans.setName(this.getName());
203             }
204             if (s.equals("admin_state_up")) {
205                 ans.setAdminStateUp(this.getAdminStateUp());
206             }
207             if (s.equals("status")) {
208                 ans.setStatus(this.getStatus());
209             }
210             if (s.equals("mac_address")) {
211                 ans.setMacAddress(this.getMacAddress());
212             }
213             if (s.equals("fixed_ips")) {
214                 List<Neutron_IPs> fixedIPs = new ArrayList<Neutron_IPs>();
215                 fixedIPs.addAll(this.getFixedIPs());
216                 ans.setFixedIPs(fixedIPs);
217             }
218             if (s.equals("device_id")) {
219                 ans.setDeviceID(this.getDeviceID());
220             }
221             if (s.equals("device_owner")) {
222                 ans.setDeviceOwner(this.getDeviceOwner());
223             }
224             if (s.equals("tenant_id")) {
225                 ans.setTenantID(this.getTenantID());
226             }
227         }
228         return ans;
229     }
230
231     public void initDefaults() {
232         adminStateUp = true;
233         if (status == null) {
234             status = "ACTIVE";
235         }
236         if (fixedIPs == null) {
237             fixedIPs = new ArrayList<Neutron_IPs>();
238         }
239     }
240
241     /**
242      * This method checks to see if the port has a floating IPv4 address
243      * associated with the supplied fixed IPv4 address
244      *
245      * @param fixedIP
246      *            fixed IPv4 address in dotted decimal format
247      * @return a boolean indicating if there is a floating IPv4 address bound
248      * to the fixed IPv4 address
249      */
250
251     public boolean isBoundToFloatingIP(String fixedIP) {
252         return floatingIPMap.containsKey(fixedIP);
253     }
254
255     @Override
256     public String toString() {
257         return "NeutronPort [portUUID=" + portUUID + ", networkUUID=" + networkUUID + ", name=" + name
258                 + ", adminStateUp=" + adminStateUp + ", status=" + status + ", macAddress=" + macAddress
259                 + ", fixedIPs=" + fixedIPs + ", deviceID=" + deviceID + ", deviceOwner=" + deviceOwner + ", tenantID="
260                 + tenantID + ", floatingIPMap=" + floatingIPMap + "]";
261     }
262 }