2d871f56e30a5337c137d775bc65785c6113907e
[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 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;
32
33 /**
34  * Neutron Northbound REST APIs.<br>
35  * This class provides REST APIs for managing neutron port objects
36  *
37  * <br>
38  * <br>
39  * Authentication scheme : <b>HTTP Basic</b><br>
40  * Authentication realm : <b>opendaylight</b><br>
41  * Transport : <b>HTTP and HTTPS</b><br>
42  * <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>
46  * More info :
47  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
48  *
49  */
50
51 @Path("/ports")
52 public final class NeutronPortsNorthbound
53         extends AbstractNeutronNorthbound<NeutronPort, NeutronPortRequest, INeutronPortCRUD> {
54
55     private static final String RESOURCE_NAME = "Port";
56
57     @Override
58     protected String getResourceName() {
59         return RESOURCE_NAME;
60     }
61
62     @Context
63     UriInfo uriInfo;
64
65     /**
66      * Returns a list of all Ports.
67      */
68
69     @GET
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(
77             // return fields
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,
91             // linkTitle
92             @QueryParam("limit") Integer limit,
93             @QueryParam("marker") String marker,
94             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
95     // sorting not supported
96     ) {
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));
115                 } else {
116                     ans.add(port);
117                 }
118             }
119         }
120
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();
126         }
127
128         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronPortRequest(ans)).build();
129     }
130
131     /**
132      * Returns a specific Port.
133      */
134
135     @Path("{portUUID}")
136     @GET
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,
145             // return fields
146             @QueryParam("fields") List<String> fields) {
147         return show(portUUID, fields);
148     }
149
150     /**
151      * Creates new Ports.
152      */
153
154     @POST
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);
162     }
163
164     @Override
165     protected void updateDelta(String uuid, NeutronPort delta, NeutronPort original) {
166         /*
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
170          */
171         if (delta.getID() == null) {
172             delta.setID(uuid);
173         }
174         if (delta.getTenantID() == null) {
175             delta.setTenantID(original.getTenantID());
176         }
177         if (delta.getNetworkUUID() == null) {
178             delta.setNetworkUUID(original.getNetworkUUID());
179         }
180         if (delta.getMacAddress() == null) {
181             delta.setMacAddress(original.getMacAddress());
182         }
183         if (delta.getFixedIps() == null) {
184             delta.setFixedIps(original.getFixedIps());
185         }
186     }
187
188     /**
189      * Updates a Port.
190      */
191
192     @Path("{portUUID}")
193     @PUT
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);
204     }
205
206     /**
207      * Deletes a Port.
208      */
209
210     @Path("{portUUID}")
211     @DELETE
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);
217     }
218 }