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