37d20dbf9c50ef937d9cb7357ad4678fa098c068
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronVPNIPSECPoliciesNorthbound.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
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.QueryParam;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.UriInfo;
30
31 import org.codehaus.enunciate.jaxrs.ResponseCode;
32 import org.codehaus.enunciate.jaxrs.StatusCodes;
33 import org.codehaus.enunciate.jaxrs.TypeHint;
34 import org.opendaylight.neutron.spi.INeutronVPNIPSECPolicyCRUD;
35 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
36 import org.opendaylight.neutron.spi.NeutronVPNIPSECPolicy;
37
38 /**
39  * Neutron Northbound REST APIs for VPN IPSEC Policy.<br>
40  * This class provides REST APIs for managing neutron VPN IPSEC Policies
41  *
42  * <br>
43  * <br>
44  * Authentication scheme : <b>HTTP Basic</b><br>
45  * Authentication realm : <b>opendaylight</b><br>
46  * Transport : <b>HTTP and HTTPS</b><br>
47  * <br>
48  * HTTPS Authentication is disabled by default. Administrator can enable it in
49  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
50  * trusted authority.<br>
51  * More info :
52  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
53  *
54  */
55
56 @Path("/vpn/ipsecpolicies")
57 public class NeutronVPNIPSECPoliciesNorthbound
58     extends AbstractNeutronNorthbound<NeutronVPNIPSECPolicy, NeutronVPNIPSECPolicyRequest, INeutronVPNIPSECPolicyCRUD> {
59
60     private static final String RESOURCE_NAME = "VPNIPSECPolicy";
61
62     @Override
63     protected String getResourceName() {
64         return RESOURCE_NAME;
65     }
66
67     @Override
68     protected NeutronVPNIPSECPolicy extractFields(NeutronVPNIPSECPolicy o, List<String> fields) {
69         return o.extractFields(fields);
70     }
71
72     @Override
73     protected INeutronVPNIPSECPolicyCRUD getNeutronCRUD() {
74         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronVPNIPSECPolicyCRUD(this);
75         if (answer.getVPNIPSECPolicyInterface() == null) {
76             throw new ServiceUnavailableException(serviceUnavailable());
77         }
78         return answer.getVPNIPSECPolicyInterface();
79     }
80
81     @Override
82     protected NeutronVPNIPSECPolicyRequest newNeutronRequest(NeutronVPNIPSECPolicy o) {
83         return new NeutronVPNIPSECPolicyRequest(o);
84     }
85
86     @Context
87     UriInfo uriInfo;
88
89     /**
90      * Returns a list of all VPN IPSEC Policies */
91
92     @GET
93     @Produces({ MediaType.APPLICATION_JSON })
94     @StatusCodes({
95             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
96             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
97             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
98             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
99     public Response listVPNIPSECPolicies(
100             // return fields
101             @QueryParam("fields") List<String> fields,
102             // filter fields
103             @QueryParam("id") String queryID,
104             @QueryParam("tenant_id") String queryTenantID,
105             @QueryParam("name") String queryName,
106             @QueryParam("transform_protocol") String queryTransformProtocol,
107             @QueryParam("encapsulation_mode") String queryEncapsulationMode,
108             @QueryParam("auth_algorithm") String queryAuthAlgorithm,
109             @QueryParam("encryption_algorithm") String queryEncryptionAlgorithm,
110             @QueryParam("pfs") String queryPFS
111             // pagination and sorting are TODO
112             ) {
113         INeutronVPNIPSECPolicyCRUD policyInterface = getNeutronCRUD();
114         List<NeutronVPNIPSECPolicy> allNeutronVPNIPSECPolicies = policyInterface.getAll();
115         List<NeutronVPNIPSECPolicy> ans = new ArrayList<NeutronVPNIPSECPolicy>();
116         Iterator<NeutronVPNIPSECPolicy> i = allNeutronVPNIPSECPolicies.iterator();
117         while (i.hasNext()) {
118             NeutronVPNIPSECPolicy oSS = i.next();
119             if ((queryID == null || queryID.equals(oSS.getID())) &&
120                     (queryName == null || queryName.equals(oSS.getName())) &&
121                     (queryAuthAlgorithm == null || queryAuthAlgorithm.equals(oSS.getAuthAlgorithm())) &&
122                     (queryEncryptionAlgorithm == null || queryEncryptionAlgorithm.equals(oSS.getEncryptionAlgorithm())) &&
123                     (queryPFS == null || queryPFS.equals(oSS.getPerfectForwardSecrecy())) &&
124                     (queryTransformProtocol == null || queryTransformProtocol.equals(oSS.getTransformProtocol())) &&
125                     (queryEncapsulationMode == null || queryEncapsulationMode.equals(oSS.getEncapsulationMode())) &&
126                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
127                 if (fields.size() > 0) {
128                     ans.add(extractFields(oSS,fields));
129                 } else {
130                     ans.add(oSS);
131                 }
132             }
133         }
134         //TODO: apply pagination to results
135         return Response.status(HttpURLConnection.HTTP_OK).entity(
136                 new NeutronVPNIPSECPolicyRequest(ans)).build();
137     }
138
139     /**
140      * Returns a specific VPN IPSEC Policy */
141
142     @Path("{policyID}")
143     @GET
144     @Produces({ MediaType.APPLICATION_JSON })
145     @StatusCodes({
146             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
147             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
148             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
149             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
150             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
151             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
152     public Response showVPNIPSECPolicy(
153             @PathParam("policyID") String policyUUID,
154             // return fields
155             @QueryParam("fields") List<String> fields
156             ) {
157         return show(policyUUID, fields);
158     }
159
160     /**
161      * Creates new VPN IPSEC Policy */
162     @POST
163     @Produces({ MediaType.APPLICATION_JSON })
164     @Consumes({ MediaType.APPLICATION_JSON })
165     @TypeHint(NeutronVPNIPSECPolicy.class)
166     @StatusCodes({
167             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
168             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
169     public Response createVPNIPSECPolicy(final NeutronVPNIPSECPolicyRequest input) {
170         return create(input);
171     }
172
173     /**
174      * Updates a VPN IPSEC Policy */
175     @Path("{policyID}")
176     @PUT
177     @Produces({ MediaType.APPLICATION_JSON })
178     @Consumes({ MediaType.APPLICATION_JSON })
179     @StatusCodes({
180             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
181             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
182             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
183     public Response updateVPNIPSECPolicy(
184             @PathParam("policyID") String policyUUID, final NeutronVPNIPSECPolicyRequest input
185             ) {
186         return update(policyUUID, input);
187     }
188
189     /**
190      * Deletes a VPN IPSEC Policy */
191
192     @Path("{policyID}")
193     @DELETE
194     @StatusCodes({
195             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
196             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
197             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
198     public Response deleteVPNIPSECPolicy(
199             @PathParam("policyID") String policyUUID) {
200         return delete(policyUUID);
201     }
202 }