Checkstyle formatting issues fix (Northbound API)
[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     private static final String INTERFACE_NAME = "L2gatewayConnection CRUD Interface";
63
64     @Override
65     protected String getResourceName() {
66         return RESOURCE_NAME;
67     }
68
69     @Override
70     protected NeutronL2gatewayConnection extractFields(NeutronL2gatewayConnection o, List<String> fields) {
71         return o.extractFields(fields);
72     }
73
74     @Override
75     protected NeutronL2gatewayConnectionRequest newNeutronRequest(NeutronL2gatewayConnection o) {
76         return new NeutronL2gatewayConnectionRequest(o);
77     }
78
79     @Override
80     protected INeutronL2gatewayConnectionCRUD getNeutronCRUD() {
81         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronL2gatewayConnectionCRUD(this);
82         if (answer.getL2gatewayConnectionInterface() == null) {
83             throw new ServiceUnavailableException(serviceUnavailable());
84         }
85         return answer.getL2gatewayConnectionInterface();
86     }
87
88     /**
89      * Creates L2gateway Connection
90      * @param  input contains connection details
91      * @return status
92      */
93     @POST
94     @Produces({ MediaType.APPLICATION_JSON })
95     @Consumes({ MediaType.APPLICATION_JSON })
96     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
97             @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
98             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
99             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
100             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
101             @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
102             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
103             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
104
105     public Response createL2gatewayConnection(final NeutronL2gatewayConnectionRequest input) {
106         logger.debug("createL2GatewayConnection   NeutronL2GatewayConnectionRequest");
107         return create(input);
108     }
109
110     /**
111      * Returns a list of all L2gateway Connections.
112      *
113      */
114     @GET
115     @Produces({ MediaType.APPLICATION_JSON })
116     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
117             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
118             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
119             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
120     public Response listL2gatewayConnections(
121             // return fields
122             @QueryParam("fields") List<String> fields,
123             @QueryParam("tenant_id") String queryTenantID,
124             @QueryParam("connection_id") String queryConnectionID,
125             @QueryParam("l2gateway_id") String queryL2gatewayID,
126             @QueryParam("network_id") String queryNetworkID,
127             @QueryParam("segment_id") String querySegmentID,
128             @QueryParam("port_id") String queryPortID,
129             @QueryParam("limit") String limit,
130             @QueryParam("marker") String marker,
131             @QueryParam("page_reverse") String pageReverse
132     // sorting not supported
133     ) {
134         INeutronL2gatewayConnectionCRUD l2gatewayConnectionInterface = getNeutronCRUD();
135         List<NeutronL2gatewayConnection> allL2gatewayConnections = l2gatewayConnectionInterface.getAll();
136         List<NeutronL2gatewayConnection> ans = new ArrayList<>();
137         Iterator<NeutronL2gatewayConnection> i = allL2gatewayConnections.iterator();
138         while (i.hasNext()) {
139             NeutronL2gatewayConnection oSS = i.next();
140             if ((queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))
141                     && (queryConnectionID == null || queryConnectionID.equals(oSS.getID()))
142                     && (queryL2gatewayID == null || queryL2gatewayID.equals(oSS.getL2gatewayID()))
143                     && (queryNetworkID == null || queryNetworkID.equals(oSS.getNetworkID()))
144                     && (querySegmentID == null || querySegmentID.equals(oSS.getSegmentID()))
145                     && (queryPortID == null || queryPortID.equals(oSS.getPortID()))) {
146                 if (fields.size() > 0) {
147                     ans.add(extractFields(oSS, fields));
148                 } else {
149                     ans.add(oSS);
150                 }
151             }
152         }
153         // TODO: apply pagination to results
154         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronL2gatewayConnectionRequest(ans)).build();
155     }
156
157     /**
158      * Returns a specific L2gateway Connection.
159      * @param l2gatewayConnectionID gateway connectID to fetch
160      * @param fields attributes used for querying
161      * @return status
162      */
163     @Path("{l2gatewayConnectionID}")
164     @GET
165     @Produces({ MediaType.APPLICATION_JSON })
166     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
167             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
168             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
169             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
170             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
171     public Response showL2gatewayID(@PathParam("l2gatewayConnectionID") String l2gatewayConnectionID,
172             // return fields
173             @QueryParam("fields") List<String> fields) {
174         return show(l2gatewayConnectionID, fields);
175     }
176
177     /**
178      * Deletes a L2gateway Connection
179      * @param  l2gatewayConnectionID  connection ID to delete
180      * @return status
181      */
182     @Path("{l2gatewayConnectionID}")
183     @DELETE
184     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
185             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
186             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
187     public Response deleteL2gatewayConnection(@PathParam("l2gatewayConnectionID") String l2gatewayConnectionID) {
188         return delete(l2gatewayConnectionID);
189     }
190
191     // l2gwconnection API doesn't have update method
192 }