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