Merge "Fix for bug 2648"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronVPNIKEPoliciesNorthbound.java
1 /*
2  * Copyright IBM Corporation, 2015.  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.util.ArrayList;
12 import java.util.HashMap;
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.DefaultValue;
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.INeutronVPNIKEPolicyAware;
35 import org.opendaylight.neutron.spi.INeutronVPNIKEPolicyCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronVPNIKEPolicy;
38
39 /**
40  * Neutron Northbound REST APIs for VPN IKE Policy.<br>
41  * This class provides REST APIs for managing neutron VPN IKE Policies
42  *
43  * <br>
44  * <br>
45  * Authentication scheme : <b>HTTP Basic</b><br>
46  * Authentication realm : <b>opendaylight</b><br>
47  * Transport : <b>HTTP and HTTPS</b><br>
48  * <br>
49  * HTTPS Authentication is disabled by default. Administrator can enable it in
50  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
51  * trusted authority.<br>
52  * More info :
53  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
54  *
55  */
56
57 @Path("/vpn/ikepolicies")
58 public class NeutronVPNIKEPoliciesNorthbound {
59
60     private NeutronVPNIKEPolicy extractFields(NeutronVPNIKEPolicy o, List<String> fields) {
61         return o.extractFields(fields);
62     }
63
64     @Context
65     UriInfo uriInfo;
66
67     /**
68      * Returns a list of all VPN IKE Policies */
69
70     @GET
71     @Produces({ MediaType.APPLICATION_JSON })
72     @StatusCodes({
73             @ResponseCode(code = 200, condition = "Operation successful"),
74             @ResponseCode(code = 401, condition = "Unauthorized"),
75             @ResponseCode(code = 501, condition = "Not Implemented"),
76             @ResponseCode(code = 503, condition = "No providers available") })
77     public Response listVPNIKEPolicies(
78             // return fields
79             @QueryParam("fields") List<String> fields,
80             // filter fields
81             @QueryParam("id") String queryID,
82             @QueryParam("name") String queryName,
83             @QueryParam("tenant_id") String queryTenantID,
84             @QueryParam("description") String queryDescription,
85             @QueryParam("auth_algorithm") String queryAuthAlgorithm,
86             @QueryParam("encryption_algorithm") String queryEncryptionAlgorithm,
87             @QueryParam("phase1_negotiation_mode") String queryPhase1NegotiationMode,
88             @QueryParam("pfs") String queryPFS,
89             @QueryParam("ike_version") String queryIKEVersion
90             // pagination and sorting are TODO
91             ) {
92         INeutronVPNIKEPolicyCRUD labelInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
93         if (labelInterface == null) {
94             throw new ServiceUnavailableException("NeutronVPNIKEPolicy CRUD Interface "
95                     + RestMessages.SERVICEUNAVAILABLE.toString());
96         }
97         List<NeutronVPNIKEPolicy> allNeutronVPNIKEPolicies = labelInterface.getAllNeutronVPNIKEPolicies();
98         List<NeutronVPNIKEPolicy> ans = new ArrayList<NeutronVPNIKEPolicy>();
99         Iterator<NeutronVPNIKEPolicy> i = allNeutronVPNIKEPolicies.iterator();
100         while (i.hasNext()) {
101             NeutronVPNIKEPolicy oSS = i.next();
102             if ((queryID == null || queryID.equals(oSS.getID())) &&
103                     (queryName == null || queryName.equals(oSS.getName())) &&
104                     (queryDescription == null || queryDescription.equals(oSS.getDescription())) &&
105                     (queryAuthAlgorithm == null || queryAuthAlgorithm.equals(oSS.getAuthAlgorithm())) &&
106                     (queryEncryptionAlgorithm == null || queryEncryptionAlgorithm.equals(oSS.getEncryptionAlgorithm())) &&
107                     (queryPhase1NegotiationMode == null || queryPhase1NegotiationMode.equals(oSS.getPhase1NegotiationMode())) &&
108                     (queryPFS == null || queryPFS.equals(oSS.getPerfectForwardSecrecy())) &&
109                     (queryIKEVersion == null || queryIKEVersion.equals(oSS.getIkeVersion())) &&
110                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
111                 if (fields.size() > 0)
112                     ans.add(extractFields(oSS,fields));
113                 else
114                     ans.add(oSS);
115             }
116         }
117         //TODO: apply pagination to results
118         return Response.status(200).entity(
119                 new NeutronVPNIKEPolicyRequest(ans)).build();
120     }
121
122     /**
123      * Returns a specific VPN IKE Policy */
124
125     @Path("{policyID}")
126     @GET
127     @Produces({ MediaType.APPLICATION_JSON })
128     @StatusCodes({
129             @ResponseCode(code = 200, condition = "Operation successful"),
130             @ResponseCode(code = 401, condition = "Unauthorized"),
131             @ResponseCode(code = 403, condition = "Forbidden"),
132             @ResponseCode(code = 404, condition = "Not Found"),
133             @ResponseCode(code = 501, condition = "Not Implemented"),
134             @ResponseCode(code = 503, condition = "No providers available") })
135     public Response showVPNIKEPolicy(
136             @PathParam("policyID") String policyUUID,
137             // return fields
138             @QueryParam("fields") List<String> fields
139             ) {
140         INeutronVPNIKEPolicyCRUD policyInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
141         if (policyInterface == null) {
142             throw new ServiceUnavailableException("VPNIKEPolicy CRUD Interface "
143                     + RestMessages.SERVICEUNAVAILABLE.toString());
144         }
145         if (!policyInterface.neutronVPNIKEPolicyExists(policyUUID)) {
146             throw new ResourceNotFoundException("VPNIKEPolicy UUID not found");
147         }
148         if (fields.size() > 0) {
149             NeutronVPNIKEPolicy ans = policyInterface.getNeutronVPNIKEPolicy(policyUUID);
150             return Response.status(200).entity(
151                     new NeutronVPNIKEPolicyRequest(extractFields(ans, fields))).build();
152         } else {
153             return Response.status(200).entity(
154                     new NeutronVPNIKEPolicyRequest(policyInterface.getNeutronVPNIKEPolicy(policyUUID))).build();
155         }
156     }
157
158     /**
159      * Creates new VPN IKE Policy */
160     @POST
161     @Produces({ MediaType.APPLICATION_JSON })
162     @Consumes({ MediaType.APPLICATION_JSON })
163     @TypeHint(NeutronVPNIKEPolicy.class)
164     @StatusCodes({
165             @ResponseCode(code = 201, condition = "Created"),
166             @ResponseCode(code = 400, condition = "Bad Request"),
167             @ResponseCode(code = 401, condition = "Unauthorized"),
168             @ResponseCode(code = 501, condition = "Not Implemented"),
169             @ResponseCode(code = 503, condition = "No providers available") })
170     public Response createVPNIKEPolicy(final NeutronVPNIKEPolicyRequest input) {
171         INeutronVPNIKEPolicyCRUD ikePolicyInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
172         if (ikePolicyInterface == null) {
173             throw new ServiceUnavailableException("VPNIKEPolicy CRUD Interface "
174                     + RestMessages.SERVICEUNAVAILABLE.toString());
175         }
176         if (input.isSingleton()) {
177             NeutronVPNIKEPolicy singleton = input.getSingleton();
178
179             /*
180              * verify that the ikePolicy doesn't already exist (issue: is deeper inspection necessary?)
181              */
182             if (ikePolicyInterface.neutronVPNIKEPolicyExists(singleton.getID()))
183                 throw new BadRequestException("ikePolicy UUID already exists");
184             Object[] instances = NeutronUtil.getInstances(INeutronVPNIKEPolicyAware.class, this);
185             if (instances != null) {
186                 if (instances.length > 0) {
187                     for (Object instance : instances) {
188                         INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
189                         int status = service.canCreateNeutronVPNIKEPolicy(singleton);
190                         if (status < 200 || status > 299)
191                             return Response.status(status).build();
192                     }
193                 } else {
194                     throw new ServiceUnavailableException("No providers registered.  Please try again later");
195                 }
196             } else {
197                 throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
198             }
199
200             /*
201              * add ikePolicy to the cache
202              */
203             ikePolicyInterface.addNeutronVPNIKEPolicy(singleton);
204             if (instances != null) {
205                 for (Object instance : instances) {
206                     INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
207                     service.neutronVPNIKEPolicyCreated(singleton);
208                 }
209             }
210         } else {
211
212             /*
213              * only singleton ikePolicy creates supported
214              */
215             throw new BadRequestException("Only singleton ikePolicy creates supported");
216         }
217         return Response.status(201).entity(input).build();
218     }
219
220     /**
221      * Updates a VPN IKE Policy */
222     @Path("{policyID}")
223     @PUT
224     @Produces({ MediaType.APPLICATION_JSON })
225     @Consumes({ MediaType.APPLICATION_JSON })
226     @StatusCodes({
227             @ResponseCode(code = 200, condition = "Operation successful"),
228             @ResponseCode(code = 400, condition = "Bad Request"),
229             @ResponseCode(code = 401, condition = "Unauthorized"),
230             @ResponseCode(code = 404, condition = "Not Found"),
231             @ResponseCode(code = 501, condition = "Not Implemented"),
232             @ResponseCode(code = 503, condition = "No providers available") })
233     public Response updateVPNIKEPolicy(
234             @PathParam("policyID") String policyUUID, final NeutronVPNIKEPolicyRequest input
235             ) {
236         INeutronVPNIKEPolicyCRUD ikePolicyInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
237         if (ikePolicyInterface == null) {
238             throw new ServiceUnavailableException("VPNIKEPolicy CRUD Interface "
239                     + RestMessages.SERVICEUNAVAILABLE.toString());
240         }
241
242         /*
243          * ikePolicy has to exist and only a single delta can be supplied
244          */
245         if (!ikePolicyInterface.neutronVPNIKEPolicyExists(policyUUID))
246             throw new ResourceNotFoundException("VPNIKEPolicy UUID not found");
247         if (!input.isSingleton())
248             throw new BadRequestException("Only single ikePolicy deltas supported");
249         NeutronVPNIKEPolicy singleton = input.getSingleton();
250         NeutronVPNIKEPolicy original = ikePolicyInterface.getNeutronVPNIKEPolicy(policyUUID);
251
252         /*
253          * attribute changes blocked by Neutron
254          */
255         if (singleton.getID() != null || singleton.getTenantID() != null)
256             throw new BadRequestException("Request attribute change not allowed");
257
258         Object[] instances = NeutronUtil.getInstances(INeutronVPNIKEPolicyAware.class, this);
259         if (instances != null) {
260             if (instances.length > 0) {
261                 for (Object instance : instances) {
262                     INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
263                     int status = service.canUpdateNeutronVPNIKEPolicy(singleton, original);
264                     if (status < 200 || status > 299)
265                         return Response.status(status).build();
266                 }
267             } else {
268                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
269             }
270         } else {
271             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
272         }
273         /*
274          * update the ikePolicy entry and return the modified object
275          */
276         ikePolicyInterface.updateNeutronVPNIKEPolicy(policyUUID, singleton);
277         NeutronVPNIKEPolicy updatedVPNIKEPolicy = ikePolicyInterface.getNeutronVPNIKEPolicy(policyUUID);
278         if (instances != null) {
279             for (Object instance : instances) {
280                 INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
281                 service.neutronVPNIKEPolicyUpdated(updatedVPNIKEPolicy);
282             }
283         }
284         return Response.status(200).entity(
285                 new NeutronVPNIKEPolicyRequest(ikePolicyInterface.getNeutronVPNIKEPolicy(policyUUID))).build();
286     }
287
288     /**
289      * Deletes a VPN IKE Policy */
290
291     @Path("{policyID}")
292     @DELETE
293     @StatusCodes({
294             @ResponseCode(code = 204, condition = "No Content"),
295             @ResponseCode(code = 401, condition = "Unauthorized"),
296             @ResponseCode(code = 404, condition = "Not Found"),
297             @ResponseCode(code = 409, condition = "Conflict"),
298             @ResponseCode(code = 501, condition = "Not Implemented"),
299             @ResponseCode(code = 503, condition = "No providers available") })
300     public Response deleteVPNIKEPolicy(
301             @PathParam("policyID") String policyUUID) {
302         INeutronVPNIKEPolicyCRUD policyInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
303         if (policyInterface == null) {
304             throw new ServiceUnavailableException("VPNIKEPolicy CRUD Interface "
305                     + RestMessages.SERVICEUNAVAILABLE.toString());
306         }
307
308         /*
309          * verify that the policy exists and is not in use before removing it
310          */
311         if (!policyInterface.neutronVPNIKEPolicyExists(policyUUID))
312             throw new ResourceNotFoundException("VPNIKEPolicy UUID not found");
313         NeutronVPNIKEPolicy singleton = policyInterface.getNeutronVPNIKEPolicy(policyUUID);
314         Object[] instances = NeutronUtil.getInstances(INeutronVPNIKEPolicyAware.class, this);
315         if (instances != null) {
316             if (instances.length > 0) {
317                 for (Object instance : instances) {
318                     INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
319                     int status = service.canDeleteNeutronVPNIKEPolicy(singleton);
320                     if (status < 200 || status > 299)
321                         return Response.status(status).build();
322                 }
323             } else {
324                 throw new ServiceUnavailableException("No providers registered.  Please try again later");
325             }
326         } else {
327             throw new ServiceUnavailableException("Couldn't get providers list.  Please try again later");
328         }
329         policyInterface.removeNeutronVPNIKEPolicy(policyUUID);
330         if (instances != null) {
331             for (Object instance : instances) {
332                 INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
333                 service.neutronVPNIKEPolicyDeleted(singleton);
334             }
335         }
336         return Response.status(204).build();
337     }
338 }