2 * Copyright (c) 2013, 2015 IBM Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.northbound.api;
11 import java.net.HttpURLConnection;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.DefaultValue;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.POST;
22 import javax.ws.rs.PUT;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.PathParam;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.QueryParam;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.UriInfo;
32 import org.codehaus.enunciate.jaxrs.ResponseCode;
33 import org.codehaus.enunciate.jaxrs.StatusCodes;
34 import org.opendaylight.neutron.spi.INeutronPortCRUD;
35 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
36 import org.opendaylight.neutron.spi.NeutronPort;
39 * Neutron Northbound REST APIs.<br>
40 * This class provides REST APIs for managing neutron port objects
44 * Authentication scheme : <b>HTTP Basic</b><br>
45 * Authentication realm : <b>opendaylight</b><br>
46 * Transport : <b>HTTP and HTTPS</b><br>
48 * HTTPS Authentication is disabled by default. Administrator can enable it in
49 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
50 * trusted authority.<br>
52 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
57 public class NeutronPortsNorthbound
58 extends AbstractNeutronNorthbound<NeutronPort, NeutronPortRequest, INeutronPortCRUD> {
60 private static final String RESOURCE_NAME = "Port";
63 protected String getResourceName() {
68 protected NeutronPort extractFields(NeutronPort o, List<String> fields) {
69 return o.extractFields(fields);
72 private NeutronCRUDInterfaces getNeutronInterfaces(boolean needNetworks, boolean needSubnets) {
73 NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronPortCRUD(this);
74 if (answer.getPortInterface() == null) {
75 throw new ServiceUnavailableException(serviceUnavailable());
78 answer = answer.fetchINeutronNetworkCRUD( this);
79 if (answer.getNetworkInterface() == null) {
80 throw new ServiceUnavailableException("Network CRUD Interface "
81 + RestMessages.SERVICEUNAVAILABLE.toString());
85 answer = answer.fetchINeutronSubnetCRUD( this);
86 if (answer.getSubnetInterface() == null) {
87 throw new ServiceUnavailableException("Subnet CRUD Interface "
88 + RestMessages.SERVICEUNAVAILABLE.toString());
95 protected INeutronPortCRUD getNeutronCRUD() {
96 return getNeutronInterfaces(false, false).getPortInterface();
100 protected NeutronPortRequest newNeutronRequest(NeutronPort o) {
101 return new NeutronPortRequest(o);
108 * Returns a list of all Ports */
111 @Produces({ MediaType.APPLICATION_JSON })
112 //@TypeHint(OpenStackPorts.class)
114 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
115 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
116 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
117 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
118 public Response listPorts(
120 @QueryParam("fields") List<String> fields,
121 // note: openstack isn't clear about filtering on lists, so we aren't handling them
122 @QueryParam("id") String queryID,
123 @QueryParam("network_id") String queryNetworkID,
124 @QueryParam("name") String queryName,
125 @QueryParam("admin_state_up") Boolean queryAdminStateUp,
126 @QueryParam("status") String queryStatus,
127 @QueryParam("mac_address") String queryMACAddress,
128 @QueryParam("device_id") String queryDeviceID,
129 @QueryParam("device_owner") String queryDeviceOwner,
130 @QueryParam("tenant_id") String queryTenantID,
131 @QueryParam("port_security_enabled") Boolean queryPortSecurityEnabled,
133 @QueryParam("limit") Integer limit,
134 @QueryParam("marker") String marker,
135 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
136 // sorting not supported
138 INeutronPortCRUD portInterface = getNeutronInterfaces(false, false).getPortInterface();
139 List<NeutronPort> allPorts = portInterface.getAll();
140 List<NeutronPort> ans = new ArrayList<NeutronPort>();
141 Iterator<NeutronPort> i = allPorts.iterator();
142 while (i.hasNext()) {
143 NeutronPort oSS = i.next();
144 if ((queryID == null || queryID.equals(oSS.getID())) &&
145 (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkUUID())) &&
146 (queryName == null || queryName.equals(oSS.getName())) &&
147 (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp())) &&
148 (queryStatus == null || queryStatus.equals(oSS.getStatus())) &&
149 (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress())) &&
150 (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID())) &&
151 (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner())) &&
152 (queryTenantID == null || queryTenantID.equals(oSS.getTenantID())) &&
153 (queryPortSecurityEnabled == null || queryPortSecurityEnabled.equals(oSS.getPortSecurityEnabled()))) {
154 if (fields.size() > 0) {
155 ans.add(extractFields(oSS,fields));
162 if (limit != null && ans.size() > 1) {
163 // Return a paginated request
164 NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit,
165 marker, pageReverse, uriInfo, ans, NeutronPort.class);
166 return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
169 return Response.status(HttpURLConnection.HTTP_OK).entity(
170 new NeutronPortRequest(ans)).build();
174 * Returns a specific Port */
178 @Produces({ MediaType.APPLICATION_JSON })
179 //@TypeHint(OpenStackPorts.class)
181 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
182 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
183 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
184 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
185 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
186 public Response showPort(
187 @PathParam("portUUID") String portUUID,
189 @QueryParam("fields") List<String> fields ) {
190 return show(portUUID, fields);
194 * Creates new Ports */
197 @Produces({ MediaType.APPLICATION_JSON })
198 @Consumes({ MediaType.APPLICATION_JSON })
199 //@TypeHint(OpenStackPorts.class)
201 @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
202 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
203 public Response createPorts(final NeutronPortRequest input) {
204 getNeutronInterfaces(true, true); // Ensure that services for networks and subnets are loaded
205 return create(input);
209 protected void updateDelta(String uuid, NeutronPort delta, NeutronPort original) {
211 * note: what we would like to get is the complete object as it
212 * is known by neutron. Until then, patch what we *do* get
213 * so that we don't lose already known information
215 if (delta.getID() == null) {
218 if (delta.getTenantID() == null) {
219 delta.setTenantID(original.getTenantID());
221 if (delta.getNetworkUUID() == null) {
222 delta.setNetworkUUID(original.getNetworkUUID());
224 if (delta.getMacAddress() == null) {
225 delta.setMacAddress(original.getMacAddress());
227 if (delta.getFixedIPs() == null) {
228 delta.setFixedIPs(original.getFixedIPs());
237 @Produces({ MediaType.APPLICATION_JSON })
238 @Consumes({ MediaType.APPLICATION_JSON })
239 //@TypeHint(OpenStackPorts.class)
241 @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
242 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
243 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
244 public Response updatePort(
245 @PathParam("portUUID") String portUUID,
246 NeutronPortRequest input
248 // TODO: Support change of security groups
249 // update the port and return the modified object
250 return update(portUUID, input);
259 @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
260 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
261 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
262 public Response deletePort(
263 @PathParam("portUUID") String portUUID) {
264 return delete(portUUID);