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;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.DefaultValue;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.Context;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.UriInfo;
28 import org.codehaus.enunciate.jaxrs.ResponseCode;
29 import org.codehaus.enunciate.jaxrs.StatusCodes;
30 import org.opendaylight.neutron.spi.INeutronPortCRUD;
31 import org.opendaylight.neutron.spi.NeutronPort;
34 * Neutron Northbound REST APIs.<br>
35 * This class provides REST APIs for managing neutron port objects
39 * Authentication scheme : <b>HTTP Basic</b><br>
40 * Authentication realm : <b>opendaylight</b><br>
41 * Transport : <b>HTTP and HTTPS</b><br>
43 * HTTPS Authentication is disabled by default. Administrator can enable it in
44 * tomcat-server.xml after adding a proper keystore / SSL certificate from a
45 * trusted authority.<br>
47 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
52 public final class NeutronPortsNorthbound
53 extends AbstractNeutronNorthbound<NeutronPort, NeutronPortRequest, INeutronPortCRUD> {
55 private static final String RESOURCE_NAME = "Port";
58 protected String getResourceName() {
66 * Returns a list of all Ports.
70 @Produces({ MediaType.APPLICATION_JSON })
71 //@TypeHint(OpenStackPorts.class)
72 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
73 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
74 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
75 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
76 public Response listPorts(
78 @QueryParam("fields") List<String> fields,
79 // note: openstack isn't clear about filtering on lists, so we aren't handling them
80 @QueryParam("id") String queryID,
81 @QueryParam("network_id") String queryNetworkID,
82 @QueryParam("name") String queryName,
83 @QueryParam("admin_state_up") Boolean queryAdminStateUp,
84 @QueryParam("status") String queryStatus,
85 @QueryParam("mac_address") String queryMACAddress,
86 @QueryParam("device_id") String queryDeviceID,
87 @QueryParam("device_owner") String queryDeviceOwner,
88 @QueryParam("tenant_id") String queryTenantID,
89 @QueryParam("port_security_enabled") Boolean queryPortSecurityEnabled,
90 @QueryParam("qos_policy_id") String queryQosPolicyId,
92 @QueryParam("limit") Integer limit,
93 @QueryParam("marker") String marker,
94 @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
95 // sorting not supported
97 INeutronPortCRUD portInterface = getNeutronCRUD();
98 List<NeutronPort> allPorts = portInterface.getAll();
99 List<NeutronPort> ans = new ArrayList<>();
100 for (NeutronPort port: allPorts) {
101 if ((queryID == null || queryID.equals(port.getID()))
102 && (queryNetworkID == null || queryNetworkID.equals(port.getNetworkUUID()))
103 && (queryName == null || queryName.equals(port.getName()))
104 && (queryAdminStateUp == null || queryAdminStateUp.equals(port.getAdminStateUp()))
105 && (queryStatus == null || queryStatus.equals(port.getStatus()))
106 && (queryMACAddress == null || queryMACAddress.equals(port.getMacAddress()))
107 && (queryDeviceID == null || queryDeviceID.equals(port.getDeviceID()))
108 && (queryDeviceOwner == null || queryDeviceOwner.equals(port.getDeviceOwner()))
109 && (queryTenantID == null || queryTenantID.equals(port.getTenantID()))
110 && (queryPortSecurityEnabled == null
111 || queryPortSecurityEnabled.equals(port.getPortSecurityEnabled()))
112 && (queryQosPolicyId == null || queryQosPolicyId.equals(port.getQosPolicyId()))) {
113 if (fields.size() > 0) {
114 ans.add(port.extractFields(fields));
121 if (limit != null && ans.size() > 1) {
122 // Return a paginated request
123 NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit, marker,
124 pageReverse, uriInfo, ans, NeutronPort.class);
125 return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
128 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronPortRequest(ans)).build();
132 * Returns a specific Port.
137 @Produces({ MediaType.APPLICATION_JSON })
138 //@TypeHint(OpenStackPorts.class)
139 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
140 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
141 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
142 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
143 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
144 public Response showPort(@PathParam("portUUID") String portUUID,
146 @QueryParam("fields") List<String> fields) {
147 return show(portUUID, fields);
155 @Produces({ MediaType.APPLICATION_JSON })
156 @Consumes({ MediaType.APPLICATION_JSON })
157 //@TypeHint(OpenStackPorts.class)
158 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
159 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
160 public Response createPorts(final NeutronPortRequest input) {
161 return create(input);
165 protected void updateDelta(String uuid, NeutronPort delta, NeutronPort original) {
167 * note: what we would like to get is the complete object as it
168 * is known by neutron. Until then, patch what we *do* get
169 * so that we don't lose already known information
171 if (delta.getID() == null) {
174 if (delta.getTenantID() == null) {
175 delta.setTenantID(original.getTenantID());
177 if (delta.getNetworkUUID() == null) {
178 delta.setNetworkUUID(original.getNetworkUUID());
180 if (delta.getMacAddress() == null) {
181 delta.setMacAddress(original.getMacAddress());
183 if (delta.getFixedIps() == null) {
184 delta.setFixedIps(original.getFixedIps());
194 @Produces({ MediaType.APPLICATION_JSON })
195 @Consumes({ MediaType.APPLICATION_JSON })
196 //@TypeHint(OpenStackPorts.class)
197 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
198 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
199 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
200 public Response updatePort(@PathParam("portUUID") String portUUID, NeutronPortRequest input) {
201 // TODO: Support change of security groups
202 // update the port and return the modified object
203 return update(portUUID, input);
212 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
213 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
214 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
215 public Response deletePort(@PathParam("portUUID") String portUUID) {
216 return delete(portUUID);