remove irrelevant old JavaDoc from a long bygone era in northbound.api
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronPortsNorthbound.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 package org.opendaylight.neutron.northbound.api;
9
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.ws.rs.Consumes;
14 import javax.ws.rs.DELETE;
15 import javax.ws.rs.DefaultValue;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.Context;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.UriInfo;
27 import org.codehaus.enunciate.jaxrs.ResponseCode;
28 import org.codehaus.enunciate.jaxrs.StatusCodes;
29 import org.opendaylight.neutron.spi.INeutronPortCRUD;
30 import org.opendaylight.neutron.spi.NeutronPort;
31
32 /**
33  * Neutron Northbound REST APIs for managing neutron port objects.
34  */
35 @Path("/ports")
36 public final class NeutronPortsNorthbound
37         extends AbstractNeutronNorthbound<NeutronPort, NeutronPortRequest, INeutronPortCRUD> {
38
39     private static final String RESOURCE_NAME = "Port";
40
41     @Override
42     protected String getResourceName() {
43         return RESOURCE_NAME;
44     }
45
46     @Context
47     UriInfo uriInfo;
48
49     /**
50      * Returns a list of all Ports.
51      */
52     @GET
53     @Produces({ MediaType.APPLICATION_JSON })
54     //@TypeHint(OpenStackPorts.class)
55     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
56             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
57             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
58             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
59     public Response listPorts(
60             // return fields
61             @QueryParam("fields") List<String> fields,
62             // note: openstack isn't clear about filtering on lists, so we aren't handling them
63             @QueryParam("id") String queryID,
64             @QueryParam("network_id") String queryNetworkID,
65             @QueryParam("name") String queryName,
66             @QueryParam("admin_state_up") Boolean queryAdminStateUp,
67             @QueryParam("status") String queryStatus,
68             @QueryParam("mac_address") String queryMACAddress,
69             @QueryParam("device_id") String queryDeviceID,
70             @QueryParam("device_owner") String queryDeviceOwner,
71             @QueryParam("tenant_id") String queryTenantID,
72             @QueryParam("port_security_enabled") Boolean queryPortSecurityEnabled,
73             @QueryParam("qos_policy_id") String queryQosPolicyId,
74             // linkTitle
75             @QueryParam("limit") Integer limit,
76             @QueryParam("marker") String marker,
77             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
78     // sorting not supported
79     ) {
80         INeutronPortCRUD portInterface = getNeutronCRUD();
81         List<NeutronPort> allPorts = portInterface.getAll();
82         List<NeutronPort> ans = new ArrayList<>();
83         for (NeutronPort port: allPorts) {
84             if ((queryID == null || queryID.equals(port.getID()))
85                     && (queryNetworkID == null || queryNetworkID.equals(port.getNetworkUUID()))
86                     && (queryName == null || queryName.equals(port.getName()))
87                     && (queryAdminStateUp == null || queryAdminStateUp.equals(port.getAdminStateUp()))
88                     && (queryStatus == null || queryStatus.equals(port.getStatus()))
89                     && (queryMACAddress == null || queryMACAddress.equals(port.getMacAddress()))
90                     && (queryDeviceID == null || queryDeviceID.equals(port.getDeviceID()))
91                     && (queryDeviceOwner == null || queryDeviceOwner.equals(port.getDeviceOwner()))
92                     && (queryTenantID == null || queryTenantID.equals(port.getTenantID()))
93                     && (queryPortSecurityEnabled == null
94                             || queryPortSecurityEnabled.equals(port.getPortSecurityEnabled()))
95                     && (queryQosPolicyId == null || queryQosPolicyId.equals(port.getQosPolicyId()))) {
96                 if (fields.size() > 0) {
97                     ans.add(port.extractFields(fields));
98                 } else {
99                     ans.add(port);
100                 }
101             }
102         }
103
104         if (limit != null && ans.size() > 1) {
105             // Return a paginated request
106             NeutronPortRequest request = (NeutronPortRequest) PaginatedRequestFactory.createRequest(limit, marker,
107                     pageReverse, uriInfo, ans, NeutronPort.class);
108             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
109         }
110
111         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronPortRequest(ans)).build();
112     }
113
114     /**
115      * Returns a specific Port.
116      */
117     @Path("{portUUID}")
118     @GET
119     @Produces({ MediaType.APPLICATION_JSON })
120     //@TypeHint(OpenStackPorts.class)
121     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
122             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
123             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
124             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
125             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
126     public Response showPort(@PathParam("portUUID") String portUUID,
127             // return fields
128             @QueryParam("fields") List<String> fields) {
129         return show(portUUID, fields);
130     }
131
132     /**
133      * Creates new Ports.
134      */
135     @POST
136     @Produces({ MediaType.APPLICATION_JSON })
137     @Consumes({ MediaType.APPLICATION_JSON })
138     //@TypeHint(OpenStackPorts.class)
139     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
140             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
141     public Response createPorts(final NeutronPortRequest input) {
142         return create(input);
143     }
144
145     @Override
146     protected void updateDelta(String uuid, NeutronPort delta, NeutronPort original) {
147         /*
148          * note: what we would like to get is the complete object as it
149          * is known by neutron.  Until then, patch what we *do* get
150          * so that we don't lose already known information
151          */
152         if (delta.getID() == null) {
153             delta.setID(uuid);
154         }
155         if (delta.getTenantID() == null) {
156             delta.setTenantID(original.getTenantID());
157         }
158         if (delta.getNetworkUUID() == null) {
159             delta.setNetworkUUID(original.getNetworkUUID());
160         }
161         if (delta.getMacAddress() == null) {
162             delta.setMacAddress(original.getMacAddress());
163         }
164         if (delta.getFixedIps() == null) {
165             delta.setFixedIps(original.getFixedIps());
166         }
167     }
168
169     /**
170      * Updates a Port.
171      */
172     @Path("{portUUID}")
173     @PUT
174     @Produces({ MediaType.APPLICATION_JSON })
175     @Consumes({ MediaType.APPLICATION_JSON })
176     //@TypeHint(OpenStackPorts.class)
177     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
178             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
179             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
180     public Response updatePort(@PathParam("portUUID") String portUUID, NeutronPortRequest input) {
181         //        TODO: Support change of security groups
182         // update the port and return the modified object
183         return update(portUUID, input);
184     }
185
186     /**
187      * Deletes a Port.
188      */
189     @Path("{portUUID}")
190     @DELETE
191     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
192             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
193             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
194     public Response deletePort(@PathParam("portUUID") String portUUID) {
195         return delete(portUUID);
196     }
197 }