4da69d56e8c00ca04ae76ba7881b3e1193bceb65
[neutron.git] / neutron-spi / src / main / java / org / opendaylight / neutron / spi / NeutronPort.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation 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.neutron.spi;
10
11 import java.io.Serializable;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import javax.xml.bind.annotation.XmlAccessType;
17 import javax.xml.bind.annotation.XmlAccessorType;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlRootElement;
20 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 @XmlRootElement
25 @XmlAccessorType(XmlAccessType.NONE)
26 public final class NeutronPort extends NeutronAdminAttributes<NeutronPort> implements Serializable {
27     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronPort.class);
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 = "network_id")
34     String networkUUID;
35
36     @XmlElement(name = "mac_address")
37     String macAddress;
38
39     @XmlElement(name = "fixed_ips")
40     List<NeutronIps> fixedIps;
41
42     @XmlElement(name = "device_id")
43     String deviceID;
44
45     @XmlElement(name = "device_owner")
46     String deviceOwner;
47
48     @XmlElement(name = "security_groups")
49     List<NeutronSecurityGroup> securityGroups;
50
51     @XmlElement(name = "allowed_address_pairs")
52     List<NeutronPortAllowedAddressPairs> allowedAddressPairs;
53
54     //@XmlElement (name = "binding:host_id")
55     @XmlElement(namespace = "binding", name = "host_id")
56     String bindinghostID;
57
58     //@XmlElement (name = "binding:vnic_type")
59     @XmlElement(namespace = "binding", name = "vnic_type")
60     String bindingvnicType;
61
62     //@XmlElement (name = "binding:vif_type")
63     @XmlElement(namespace = "binding", name = "vif_type")
64     String bindingvifType;
65
66     //@XmlElement (name = "binding:vif_details")
67     @XmlElement(namespace = "binding", name = "vif_details")
68     @XmlJavaTypeAdapter(NeutronResourceMapPropertyAdapter.class)
69     Map<String, String> vifDetails;
70
71     @XmlElement(name = "extra_dhcp_opts")
72     List<NeutronPortExtraDHCPOption> extraDHCPOptions;
73
74     //Port security is enabled by default for backward compatibility.
75     @XmlElement(defaultValue = "true", name = "port_security_enabled")
76     Boolean portSecurityEnabled;
77
78     @XmlElement(name = "qos_policy_id")
79     String qosPolicyId;
80
81     public NeutronPort() {
82     }
83
84     public String getNetworkUUID() {
85         return networkUUID;
86     }
87
88     public void setNetworkUUID(String networkUUID) {
89         this.networkUUID = networkUUID;
90     }
91
92     public boolean isAdminStateUp() {
93         if (adminStateUp == null) {
94             return true;
95         }
96         return adminStateUp;
97     }
98
99     public String getMacAddress() {
100         return macAddress;
101     }
102
103     public void setMacAddress(String macAddress) {
104         this.macAddress = macAddress;
105     }
106
107     public List<NeutronIps> getFixedIps() {
108         return fixedIps;
109     }
110
111     public void setFixedIps(List<NeutronIps> fixedIps) {
112         this.fixedIps = fixedIps;
113     }
114
115     public String getDeviceID() {
116         return deviceID;
117     }
118
119     public void setDeviceID(String deviceID) {
120         this.deviceID = deviceID;
121     }
122
123     public String getDeviceOwner() {
124         return deviceOwner;
125     }
126
127     public void setDeviceOwner(String deviceOwner) {
128         this.deviceOwner = deviceOwner;
129     }
130
131     public List<NeutronSecurityGroup> getSecurityGroups() {
132         return securityGroups;
133     }
134
135     public void setSecurityGroups(List<NeutronSecurityGroup> securityGroups) {
136         this.securityGroups = securityGroups;
137     }
138
139     public List<NeutronPortAllowedAddressPairs> getAllowedAddressPairs() {
140         return allowedAddressPairs;
141     }
142
143     public void setAllowedAddressPairs(List<NeutronPortAllowedAddressPairs> allowedAddressPairs) {
144         this.allowedAddressPairs = allowedAddressPairs;
145     }
146
147     public List<NeutronPortExtraDHCPOption> getExtraDHCPOptions() {
148         return extraDHCPOptions;
149     }
150
151     public void setExtraDHCPOptions(List<NeutronPortExtraDHCPOption> extraDHCPOptions) {
152         this.extraDHCPOptions = extraDHCPOptions;
153     }
154
155     public Map<String, String> getVIFDetails() {
156         return vifDetails;
157     }
158
159     public void setVIFDetails(Map<String, String> vifDetails) {
160         this.vifDetails = vifDetails;
161     }
162
163     public String getBindinghostID() {
164         return bindinghostID;
165     }
166
167     public void setBindinghostID(String bindinghostID) {
168         this.bindinghostID = bindinghostID;
169     }
170
171     public String getBindingvnicType() {
172         return bindingvnicType;
173     }
174
175     public void setBindingvnicType(String bindingvnicType) {
176         this.bindingvnicType = bindingvnicType;
177     }
178
179     public String getBindingvifType() {
180         return bindingvifType;
181     }
182
183     public void setBindingvifType(String bindingvifType) {
184         this.bindingvifType = bindingvifType;
185     }
186
187     public Boolean getPortSecurityEnabled() {
188         if (portSecurityEnabled == null) {
189             return true;
190         }
191         return portSecurityEnabled;
192     }
193
194     public String getQosPolicyId() {
195         return qosPolicyId;
196     }
197
198     public void setQosPolicyId(String qosPolicyId) {
199         this.qosPolicyId = qosPolicyId;
200     }
201
202     public void setPortSecurityEnabled(Boolean newValue) {
203         portSecurityEnabled = newValue;
204     }
205
206     /**
207      * This method copies selected fields from the object and returns them
208      * as a new object, suitable for marshaling.
209      *
210      * @param fields
211      *            List of attributes to be extracted
212      * @return an OpenStackPorts object with only the selected fields
213      *             populated
214      */
215
216     public NeutronPort extractFields(List<String> fields) {
217         NeutronPort ans = new NeutronPort();
218         for (String s : fields) {
219             if (extractField(s, ans)) {
220                 continue;
221             }
222             switch (s) {
223                 case "network_id":
224                     ans.setNetworkUUID(this.getNetworkUUID());
225                     break;
226                 case "mac_address":
227                     ans.setMacAddress(this.getMacAddress());
228                     break;
229                 case "fixed_ips":
230                     ans.setFixedIps(new ArrayList<NeutronIps>(this.getFixedIps()));
231                     break;
232                 case "device_id":
233                     ans.setDeviceID(this.getDeviceID());
234                     break;
235                 case "device_owner":
236                     ans.setDeviceOwner(this.getDeviceOwner());
237                     break;
238                 case "security_groups":
239                     ans.setSecurityGroups(new ArrayList<NeutronSecurityGroup>(this.getSecurityGroups()));
240                     break;
241                 case "allowed_address_pairs":
242                     ans.setAllowedAddressPairs(
243                             new ArrayList<NeutronPortAllowedAddressPairs>(this.getAllowedAddressPairs()));
244                     break;
245                 case "binding:host_id":
246                     ans.setBindinghostID(this.getBindinghostID());
247                     break;
248                 case "binding:vnic_type":
249                     ans.setBindingvnicType(this.getBindingvnicType());
250                     break;
251                 case "binding:vif_type":
252                     ans.setBindingvifType(this.getBindingvifType());
253                     break;
254                 case "binding:vif_details":
255                     ans.setVIFDetails(new HashMap<String, String>(this.getVIFDetails()));
256                     break;
257                 case "extra_dhcp_opts":
258                     ans.setExtraDHCPOptions(new ArrayList<NeutronPortExtraDHCPOption>(this.getExtraDHCPOptions()));
259                     break;
260                 case "port_security_enabled":
261                     ans.setPortSecurityEnabled(this.getPortSecurityEnabled());
262                     break;
263                 case "qos_policy_id":
264                     ans.setQosPolicyId(this.getQosPolicyId());
265                     break;
266                 default:
267                     LOGGER.warn("{} is not a NeutronPort suitable field.", s);
268                     break;
269             }
270         }
271         return ans;
272     }
273
274     @Override
275     public void initDefaults() {
276         super.initDefaults();
277         if (portSecurityEnabled == null) {
278             portSecurityEnabled = true;
279         }
280         if (fixedIps == null) {
281             fixedIps = new ArrayList<NeutronIps>();
282         }
283     }
284
285     @Override
286     public String toString() {
287         return "NeutronPort [portUUID=" + uuid + ", networkUUID=" + networkUUID + ", name=" + name + ", adminStateUp="
288                 + adminStateUp + ", status=" + status + ", macAddress=" + macAddress + ", fixedIps=" + fixedIps
289                 + ", deviceID=" + deviceID + ", deviceOwner=" + deviceOwner + ", tenantID=" + tenantID
290                 + ", securityGroups=" + securityGroups + ", allowedAddressPairs" + allowedAddressPairs
291                 + ", bindinghostID=" + bindinghostID + ", bindingvnicType=" + bindingvnicType + ", bindingvifType="
292                 + bindingvifType + ", vifDetails=" + vifDetails + ", extraDHCPOptions=" + extraDHCPOptions
293                 + ", portSecurityEnabled=" + portSecurityEnabled + ", qosPolicyId=" + qosPolicyId + "]";
294     }
295 }