Merge "remove redundant methods for compatibility"
[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
9 package org.opendaylight.neutron.northbound.api;
10
11 import java.net.HttpURLConnection;
12
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
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;
31
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;
37
38 /**
39  * Neutron Northbound REST APIs.<br>
40  * This class provides REST APIs for managing neutron port objects
41  *
42  * <br>
43  * <br>
44  * Authentication scheme : <b>HTTP Basic</b><br>
45  * Authentication realm : <b>opendaylight</b><br>
46  * Transport : <b>HTTP and HTTPS</b><br>
47  * <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>
51  * More info :
52  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
53  *
54  */
55
56 @Path("/ports")
57 public class NeutronPortsNorthbound
58     extends AbstractNeutronNorthbound<NeutronPort, NeutronPortRequest, INeutronPortCRUD> {
59
60     private static final String RESOURCE_NAME = "Port";
61
62     @Override
63     protected String getResourceName() {
64         return RESOURCE_NAME;
65     }
66
67     @Override
68     protected NeutronPort extractFields(NeutronPort o, List<String> fields) {
69         return o.extractFields(fields);
70     }
71
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());
76         }
77         if (needNetworks) {
78             answer = answer.fetchINeutronNetworkCRUD( this);
79             if (answer.getNetworkInterface() == null) {
80                 throw new ServiceUnavailableException("Network CRUD Interface "
81                         + RestMessages.SERVICEUNAVAILABLE.toString());
82             }
83         }
84         if (needSubnets) {
85             answer = answer.fetchINeutronSubnetCRUD( this);
86             if (answer.getSubnetInterface() == null) {
87                 throw new ServiceUnavailableException("Subnet CRUD Interface "
88                         + RestMessages.SERVICEUNAVAILABLE.toString());
89             }
90         }
91         return answer;
92     }
93
94     @Override
95     protected INeutronPortCRUD getNeutronCRUD() {
96         return getNeutronInterfaces(false, false).getPortInterface();
97     }
98
99     @Override
100     protected NeutronPortRequest newNeutronRequest(NeutronPort o) {
101         return new NeutronPortRequest(o);
102     }
103
104     @Context
105     UriInfo uriInfo;
106
107     /**
108      * Returns a list of all Ports */
109
110     @GET
111     @Produces({ MediaType.APPLICATION_JSON })
112     //@TypeHint(OpenStackPorts.class)
113     @StatusCodes({
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(
119             // return fields
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,
132             // linkTitle
133             @QueryParam("limit") Integer limit,
134             @QueryParam("marker") String marker,
135             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
136             // sorting not supported
137             ) {
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));
156                 } else {
157                     ans.add(oSS);
158                 }
159             }
160         }
161
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();
167         }
168
169         return Response.status(HttpURLConnection.HTTP_OK).entity(
170                 new NeutronPortRequest(ans)).build();
171     }
172
173     /**
174      * Returns a specific Port */
175
176     @Path("{portUUID}")
177     @GET
178     @Produces({ MediaType.APPLICATION_JSON })
179     //@TypeHint(OpenStackPorts.class)
180     @StatusCodes({
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,
188             // return fields
189             @QueryParam("fields") List<String> fields ) {
190         return show(portUUID, fields);
191     }
192
193     /**
194      * Creates new Ports */
195
196     @POST
197     @Produces({ MediaType.APPLICATION_JSON })
198     @Consumes({ MediaType.APPLICATION_JSON })
199     //@TypeHint(OpenStackPorts.class)
200     @StatusCodes({
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);
206     }
207
208     @Override
209     protected void updateDelta(String uuid, NeutronPort delta, NeutronPort original) {
210         /*
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
214          */
215         if (delta.getID() == null) {
216             delta.setID(uuid);
217         }
218         if (delta.getTenantID() == null) {
219             delta.setTenantID(original.getTenantID());
220         }
221         if (delta.getNetworkUUID() == null) {
222             delta.setNetworkUUID(original.getNetworkUUID());
223         }
224         if (delta.getMacAddress() == null) {
225             delta.setMacAddress(original.getMacAddress());
226         }
227         if (delta.getFixedIPs() == null) {
228             delta.setFixedIPs(original.getFixedIPs());
229         }
230     }
231
232     /**
233      * Updates a Port */
234
235     @Path("{portUUID}")
236     @PUT
237     @Produces({ MediaType.APPLICATION_JSON })
238     @Consumes({ MediaType.APPLICATION_JSON })
239     //@TypeHint(OpenStackPorts.class)
240     @StatusCodes({
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
247             ) {
248         //        TODO: Support change of security groups
249         // update the port and return the modified object
250         return update(portUUID, input);
251     }
252
253     /**
254      * Deletes a Port */
255
256     @Path("{portUUID}")
257     @DELETE
258     @StatusCodes({
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);
265     }
266 }