Merge "sort out signature of extraceField method"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronL2gatewayConnectionNorthbound.java
1 /*
2  * Copyright (c) 2015 Hewlett-Packard Development Company, L.P. 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.Iterator;
14 import java.util.List;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
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.INeutronL2gatewayConnectionCRUD;
30 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
31 import org.opendaylight.neutron.spi.NeutronL2gatewayConnection;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Neutron Northbound REST APIs for L2 gateway Connection.<br>
37  * This class provides REST APIs for managing L2 gateway Connection
38  * * *
39  * <br>
40  * <br>
41  * Authentication scheme : <b>HTTP Basic</b><br>
42  * Authentication realm : <b>opendaylight</b><br>
43  * Transport : <b>HTTP and HTTPS</b><br>
44  * <br>
45  * HTTPS Authentication is disabled by default. Administrator can enable it in
46  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
47  * trusted authority.<br>
48  * More info :
49  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
50  */
51
52 @Path("/l2gateway-connections")
53 public class NeutronL2gatewayConnectionNorthbound extends AbstractNeutronNorthbound<NeutronL2gatewayConnection,
54         NeutronL2gatewayConnectionRequest, INeutronL2gatewayConnectionCRUD> {
55
56     static final Logger logger = LoggerFactory.getLogger(NeutronL2gatewayConnectionNorthbound.class);
57
58     @Context
59     UriInfo uriInfo;
60
61     private static final String RESOURCE_NAME = "L2gatewayConnection";
62
63     @Override
64     protected String getResourceName() {
65         return RESOURCE_NAME;
66     }
67
68     @Override
69     protected NeutronL2gatewayConnectionRequest newNeutronRequest(NeutronL2gatewayConnection o) {
70         return new NeutronL2gatewayConnectionRequest(o);
71     }
72
73     @Override
74     protected INeutronL2gatewayConnectionCRUD getNeutronCRUD() {
75         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronL2gatewayConnectionCRUD(this);
76         if (answer.getL2gatewayConnectionInterface() == null) {
77             throw new ServiceUnavailableException(serviceUnavailable());
78         }
79         return answer.getL2gatewayConnectionInterface();
80     }
81
82     /**
83      * Creates L2gateway Connection
84      * @param  input contains connection details
85      * @return status
86      */
87     @POST
88     @Produces({ MediaType.APPLICATION_JSON })
89     @Consumes({ MediaType.APPLICATION_JSON })
90     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
91             @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
92             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
93             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
94             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
95             @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
96             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
97             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
98
99     public Response createL2gatewayConnection(final NeutronL2gatewayConnectionRequest input) {
100         logger.debug("createL2GatewayConnection   NeutronL2GatewayConnectionRequest");
101         return create(input);
102     }
103
104     /**
105      * Returns a list of all L2gateway Connections.
106      *
107      */
108     @GET
109     @Produces({ MediaType.APPLICATION_JSON })
110     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
111             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
112             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
113             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
114     public Response listL2gatewayConnections(
115             // return fields
116             @QueryParam("fields") List<String> fields,
117             @QueryParam("tenant_id") String queryTenantID,
118             @QueryParam("connection_id") String queryConnectionID,
119             @QueryParam("l2gateway_id") String queryL2gatewayID,
120             @QueryParam("network_id") String queryNetworkID,
121             @QueryParam("segment_id") String querySegmentID,
122             @QueryParam("port_id") String queryPortID,
123             @QueryParam("limit") String limit,
124             @QueryParam("marker") String marker,
125             @QueryParam("page_reverse") String pageReverse
126     // sorting not supported
127     ) {
128         INeutronL2gatewayConnectionCRUD l2gatewayConnectionInterface = getNeutronCRUD();
129         List<NeutronL2gatewayConnection> allL2gatewayConnections = l2gatewayConnectionInterface.getAll();
130         List<NeutronL2gatewayConnection> ans = new ArrayList<>();
131         Iterator<NeutronL2gatewayConnection> i = allL2gatewayConnections.iterator();
132         while (i.hasNext()) {
133             NeutronL2gatewayConnection oSS = i.next();
134             if ((queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))
135                     && (queryConnectionID == null || queryConnectionID.equals(oSS.getID()))
136                     && (queryL2gatewayID == null || queryL2gatewayID.equals(oSS.getL2gatewayID()))
137                     && (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkID()))
138                     && (querySegmentID == null || querySegmentID.equals(oSS.getSegmentID()))
139                     && (queryPortID == null || queryPortID.equals(oSS.getPortID()))) {
140                 if (fields.size() > 0) {
141                     ans.add(oSS.extractFields(fields));
142                 } else {
143                     ans.add(oSS);
144                 }
145             }
146         }
147         // TODO: apply pagination to results
148         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronL2gatewayConnectionRequest(ans)).build();
149     }
150
151     /**
152      * Returns a specific L2gateway Connection.
153      * @param l2gatewayConnectionID gateway connectID to fetch
154      * @param fields attributes used for querying
155      * @return status
156      */
157     @Path("{l2gatewayConnectionID}")
158     @GET
159     @Produces({ MediaType.APPLICATION_JSON })
160     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
161             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
162             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
163             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
164             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
165     public Response showL2gatewayID(@PathParam("l2gatewayConnectionID") String l2gatewayConnectionID,
166             // return fields
167             @QueryParam("fields") List<String> fields) {
168         return show(l2gatewayConnectionID, fields);
169     }
170
171     /**
172      * Deletes a L2gateway Connection
173      * @param  l2gatewayConnectionID  connection ID to delete
174      * @return status
175      */
176     @Path("{l2gatewayConnectionID}")
177     @DELETE
178     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
179             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
180             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
181     public Response deleteL2gatewayConnection(@PathParam("l2gatewayConnectionID") String l2gatewayConnectionID) {
182         return delete(l2gatewayConnectionID);
183     }
184
185     // l2gwconnection API doesn't have update method
186 }