complete dependency inject CRUD implementations
[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 package org.opendaylight.neutron.northbound.api;
9
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
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.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.opendaylight.neutron.spi.INeutronL2gatewayConnectionCRUD;
28 import org.opendaylight.neutron.spi.NeutronL2gatewayConnection;
29 import org.ops4j.pax.cdi.api.OsgiService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Neutron Northbound REST APIs for L2 gateway Connection.
35  */
36 @Singleton
37 @Path("/l2gateway-connections")
38 public final class NeutronL2gatewayConnectionNorthbound extends AbstractNeutronNorthbound<NeutronL2gatewayConnection,
39         NeutronL2gatewayConnectionRequest, INeutronL2gatewayConnectionCRUD> {
40
41     private static final Logger LOG = LoggerFactory.getLogger(NeutronL2gatewayConnectionNorthbound.class);
42
43     private static final String RESOURCE_NAME = "L2gatewayConnection";
44
45     @Inject
46     public NeutronL2gatewayConnectionNorthbound(@OsgiService INeutronL2gatewayConnectionCRUD neutronCRUD) {
47         super(neutronCRUD);
48     }
49
50     @Override
51     protected String getResourceName() {
52         return RESOURCE_NAME;
53     }
54
55     /**
56      * Creates L2gateway Connection.
57      * @param  input contains connection details
58      * @return status
59      */
60     @POST
61     @Produces({ MediaType.APPLICATION_JSON })
62     @Consumes({ MediaType.APPLICATION_JSON })
63     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
64             @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
65             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
66             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
67             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
68             @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
69             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
70             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
71
72     public Response createL2gatewayConnection(final NeutronL2gatewayConnectionRequest input) {
73         LOG.debug("createL2GatewayConnection   NeutronL2GatewayConnectionRequest");
74         return create(input);
75     }
76
77     /**
78      * Returns a list of all L2gateway Connections.
79      */
80     @GET
81     @Produces({ MediaType.APPLICATION_JSON })
82     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
83             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
84             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
85             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
86     public Response listL2gatewayConnections(
87             // return fields
88             @QueryParam("fields") List<String> fields,
89             @QueryParam("tenant_id") String queryTenantID,
90             @QueryParam("connection_id") String queryConnectionID,
91             @QueryParam("l2gateway_id") String queryL2gatewayID,
92             @QueryParam("network_id") String queryNetworkID,
93             @QueryParam("segment_id") String querySegmentID,
94             @QueryParam("port_id") String queryPortID,
95             @QueryParam("limit") String limit,
96             @QueryParam("marker") String marker,
97             @QueryParam("page_reverse") String pageReverse
98     // sorting not supported
99     ) {
100         INeutronL2gatewayConnectionCRUD l2gatewayConnectionInterface = getNeutronCRUD();
101         List<NeutronL2gatewayConnection> allL2gatewayConnections = l2gatewayConnectionInterface.getAll();
102         List<NeutronL2gatewayConnection> ans = new ArrayList<>();
103         for (NeutronL2gatewayConnection connection : allL2gatewayConnections) {
104             if ((queryTenantID == null || queryTenantID.equals(connection.getTenantID()))
105                     && (queryConnectionID == null || queryConnectionID.equals(connection.getID()))
106                     && (queryL2gatewayID == null || queryL2gatewayID.equals(connection.getL2gatewayID()))
107                     && (queryNetworkID == null || queryNetworkID.equals(connection.getNetworkID()))
108                     && (querySegmentID == null || Integer.valueOf(querySegmentID).equals(connection.getSegmentID()))
109                     && (queryPortID == null || queryPortID.equals(connection.getPortID()))) {
110                 if (fields.size() > 0) {
111                     ans.add(connection.extractFields(fields));
112                 } else {
113                     ans.add(connection);
114                 }
115             }
116         }
117         // TODO: apply pagination to results
118         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronL2gatewayConnectionRequest(ans)).build();
119     }
120
121     /**
122      * Returns a specific L2gateway Connection.
123      * @param l2gatewayConnectionID gateway connectID to fetch
124      * @param fields attributes used for querying
125      * @return status
126      */
127     @Path("{l2gatewayConnectionID}")
128     @GET
129     @Produces({ MediaType.APPLICATION_JSON })
130     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
131             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
132             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
133             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
134             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
135     public Response showL2gatewayID(@PathParam("l2gatewayConnectionID") String l2gatewayConnectionID,
136             // return fields
137             @QueryParam("fields") List<String> fields) {
138         return show(l2gatewayConnectionID, fields);
139     }
140
141     /**
142      * Deletes a L2gateway Connection.
143      * @param  l2gatewayConnectionID  connection ID to delete
144      * @return status
145      */
146     @Path("{l2gatewayConnectionID}")
147     @DELETE
148     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
149             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
150             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
151     public Response deleteL2gatewayConnection(@PathParam("l2gatewayConnectionID") String l2gatewayConnectionID) {
152         return delete(l2gatewayConnectionID);
153     }
154
155     // l2gwconnection API doesn't have update method
156 }