b49a049dfc00654c15f47b2a5828a3916d311af1
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronVpnServicesNorthbound.java
1 /*
2  * Copyright (c) 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.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
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.codehaus.enunciate.jaxrs.TypeHint;
28 import org.opendaylight.neutron.spi.INeutronVpnServiceCRUD;
29 import org.opendaylight.neutron.spi.NeutronVpnService;
30
31 /**
32  * Neutron Northbound REST APIs for VPN Service.<br>
33  * This class provides REST APIs for managing neutron VPN Services
34  *
35  * <br>
36  * <br>
37  * Authentication scheme : <b>HTTP Basic</b><br>
38  * Authentication realm : <b>opendaylight</b><br>
39  * Transport : <b>HTTP and HTTPS</b><br>
40  * <br>
41  * HTTPS Authentication is disabled by default. Administrator can enable it in
42  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
43  * trusted authority.<br>
44  * More info :
45  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
46  *
47  */
48
49 @Path("/vpn/vpnservices")
50 public final class NeutronVpnServicesNorthbound
51         extends AbstractNeutronNorthbound<NeutronVpnService, NeutronVpnServiceRequest, INeutronVpnServiceCRUD> {
52
53     private static final String RESOURCE_NAME = "VpnService";
54
55     @Override
56     protected String getResourceName() {
57         return RESOURCE_NAME;
58     }
59
60     /**
61      * Returns a list of all VPN Services.
62      */
63
64     @GET
65     @Produces({ MediaType.APPLICATION_JSON })
66     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
67             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
68             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
69             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
70     public Response listVPNServices(
71             // return fields
72             @QueryParam("fields") List<String> fields,
73             // OpenStack VPNService attributes
74             @QueryParam("id") String queryID,
75             @QueryParam("tenant_id") String queryTenantID,
76             @QueryParam("name") String queryName,
77             @QueryParam("admin_state_up") Boolean queryAdminStateUp,
78             @QueryParam("router_id") String queryRouterID,
79             @QueryParam("status") String queryStatus,
80             @QueryParam("subnet_id") String querySubnetID,
81             // pagination
82             @QueryParam("limit") String limit,
83             @QueryParam("marker") String marker,
84             @QueryParam("page_reverse") String pageReverse
85     // sorting not supported
86     ) {
87         INeutronVpnServiceCRUD vpnServiceInterface = getNeutronCRUD();
88         List<NeutronVpnService> allVPNService = vpnServiceInterface.getAll();
89         List<NeutronVpnService> ans = new ArrayList<>();
90         for (NeutronVpnService vpnService : allVPNService) {
91             if ((queryID == null || queryID.equals(vpnService.getID()))
92                     && (queryName == null || queryName.equals(vpnService.getName()))
93                     && (queryAdminStateUp == null || queryAdminStateUp.equals(vpnService.getAdminStateUp()))
94                     && (queryStatus == null || queryStatus.equals(vpnService.getStatus()))
95                     && (querySubnetID == null || querySubnetID.equals(vpnService.getSubnetUUID()))
96                     && (queryRouterID == null || queryRouterID.equals(vpnService.getRouterUUID()))
97                     && (queryTenantID == null || queryTenantID.equals(vpnService.getTenantID()))) {
98                 if (fields.size() > 0) {
99                     ans.add(vpnService.extractFields(fields));
100                 } else {
101                     ans.add(vpnService);
102                 }
103             }
104         }
105
106         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronVpnServiceRequest(ans)).build();
107     }
108
109     /**
110      * Returns a specific VPN Service.
111      */
112
113     @Path("{serviceID}")
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_FOUND, condition = "Not Found"),
119             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
120             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
121     public Response showVPNService(@PathParam("serviceID") String serviceID,
122             // return fields
123             @QueryParam("fields") List<String> fields) {
124         return show(serviceID, fields);
125     }
126
127     /**
128      * Creates new VPN Service.
129      */
130     @POST
131     @Produces({ MediaType.APPLICATION_JSON })
132     @Consumes({ MediaType.APPLICATION_JSON })
133     @TypeHint(NeutronVpnService.class)
134     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
135             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
136     public Response createVPNService(final NeutronVpnServiceRequest input) {
137         return create(input);
138     }
139
140     /**
141      * Updates a VPN Service.
142      */
143     @Path("{serviceID}")
144     @PUT
145     @Produces({ MediaType.APPLICATION_JSON })
146     @Consumes({ MediaType.APPLICATION_JSON })
147     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
148             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
149             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
150     public Response updateVPNService(@PathParam("serviceID") String serviceID, final NeutronVpnServiceRequest input) {
151         return update(serviceID, input);
152     }
153
154     /**
155      * Deletes a VPN Service.
156      */
157
158     @Path("{serviceID}")
159     @DELETE
160     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
161             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
162             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
163     public Response deleteVPNService(@PathParam("serviceID") String serviceID) {
164         return delete(serviceID);
165     }
166 }