northbound: introduce a base class for nortubhound classes
[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.INeutronVPNIPSECPolicyAware;
35 import org.opendaylight.neutron.spi.INeutronVPNIPSECPolicyCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronVPNIPSECPolicy;
38
39 /**
40  * Neutron Northbound REST APIs for VPN IPSEC Policy.<br>
41  * This class provides REST APIs for managing neutron VPN IPSEC 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/ipsecpolicies")
58 public class NeutronVPNIPSECPoliciesNorthbound extends AbstractNeutronNorthbound {
59
60     private static final String RESOURCE_NAME = "VPNIPSECPolicy";
61
62     private NeutronVPNIPSECPolicy extractFields(NeutronVPNIPSECPolicy o, List<String> fields) {
63         return o.extractFields(fields);
64     }
65
66     private NeutronCRUDInterfaces getNeutronInterfaces() {
67         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronVPNIPSECPolicyCRUD(this);
68         if (answer.getVPNIPSECPolicyInterface() == null) {
69             throw new ServiceUnavailableException("NeutronVPNIPSECPolicy CRUD Interface "
70                 + RestMessages.SERVICEUNAVAILABLE.toString());
71         }
72         return answer;
73     }
74
75     @Context
76     UriInfo uriInfo;
77
78     /**
79      * Returns a list of all VPN IPSEC Policies */
80
81     @GET
82     @Produces({ MediaType.APPLICATION_JSON })
83     @StatusCodes({
84             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
85             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
86             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
87             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
88     public Response listVPNIPSECPolicies(
89             // return fields
90             @QueryParam("fields") List<String> fields,
91             // filter fields
92             @QueryParam("id") String queryID,
93             @QueryParam("tenant_id") String queryTenantID,
94             @QueryParam("name") String queryName,
95             @QueryParam("description") String queryDescription,
96             @QueryParam("transform_protocol") String queryTransformProtocol,
97             @QueryParam("encapsulation_mode") String queryEncapsulationMode,
98             @QueryParam("auth_algorithm") String queryAuthAlgorithm,
99             @QueryParam("encryption_algorithm") String queryEncryptionAlgorithm,
100             @QueryParam("pfs") String queryPFS
101             // pagination and sorting are TODO
102             ) {
103         INeutronVPNIPSECPolicyCRUD policyInterface = getNeutronInterfaces().getVPNIPSECPolicyInterface();
104         List<NeutronVPNIPSECPolicy> allNeutronVPNIPSECPolicies = policyInterface.getAllNeutronVPNIPSECPolicies();
105         List<NeutronVPNIPSECPolicy> ans = new ArrayList<NeutronVPNIPSECPolicy>();
106         Iterator<NeutronVPNIPSECPolicy> i = allNeutronVPNIPSECPolicies.iterator();
107         while (i.hasNext()) {
108             NeutronVPNIPSECPolicy oSS = i.next();
109             if ((queryID == null || queryID.equals(oSS.getID())) &&
110                     (queryName == null || queryName.equals(oSS.getName())) &&
111                     (queryDescription == null || queryDescription.equals(oSS.getDescription())) &&
112                     (queryAuthAlgorithm == null || queryAuthAlgorithm.equals(oSS.getAuthAlgorithm())) &&
113                     (queryEncryptionAlgorithm == null || queryEncryptionAlgorithm.equals(oSS.getEncryptionAlgorithm())) &&
114                     (queryPFS == null || queryPFS.equals(oSS.getPerfectForwardSecrecy())) &&
115                     (queryTransformProtocol == null || queryTransformProtocol.equals(oSS.getTransformProtocol())) &&
116                     (queryEncapsulationMode == null || queryEncapsulationMode.equals(oSS.getEncapsulationMode())) &&
117                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
118                 if (fields.size() > 0) {
119                     ans.add(extractFields(oSS,fields));
120                 } else {
121                     ans.add(oSS);
122                 }
123             }
124         }
125         //TODO: apply pagination to results
126         return Response.status(HttpURLConnection.HTTP_OK).entity(
127                 new NeutronVPNIPSECPolicyRequest(ans)).build();
128     }
129
130     /**
131      * Returns a specific VPN IPSEC Policy */
132
133     @Path("{policyID}")
134     @GET
135     @Produces({ MediaType.APPLICATION_JSON })
136     @StatusCodes({
137             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
138             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
139             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
140             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
141             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
142             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
143     public Response showVPNIPSECPolicy(
144             @PathParam("policyID") String policyUUID,
145             // return fields
146             @QueryParam("fields") List<String> fields
147             ) {
148         INeutronVPNIPSECPolicyCRUD policyInterface = getNeutronInterfaces().getVPNIPSECPolicyInterface();
149         if (!policyInterface.neutronVPNIPSECPolicyExists(policyUUID)) {
150             throw new ResourceNotFoundException(uuidNoExist(RESOURCE_NAME));
151         }
152         if (fields.size() > 0) {
153             NeutronVPNIPSECPolicy ans = policyInterface.getNeutronVPNIPSECPolicy(policyUUID);
154             return Response.status(HttpURLConnection.HTTP_OK).entity(
155                     new NeutronVPNIPSECPolicyRequest(extractFields(ans, fields))).build();
156         } else {
157             return Response.status(HttpURLConnection.HTTP_OK).entity(
158                     new NeutronVPNIPSECPolicyRequest(policyInterface.getNeutronVPNIPSECPolicy(policyUUID))).build();
159         }
160     }
161
162     /**
163      * Creates new VPN IPSEC Policy */
164     @POST
165     @Produces({ MediaType.APPLICATION_JSON })
166     @Consumes({ MediaType.APPLICATION_JSON })
167     @TypeHint(NeutronVPNIPSECPolicy.class)
168     @StatusCodes({
169             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
170             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
171     public Response createVPNIPSECPolicy(final NeutronVPNIPSECPolicyRequest input) {
172         INeutronVPNIPSECPolicyCRUD ipsecPolicyInterface = getNeutronInterfaces().getVPNIPSECPolicyInterface();
173         if (input.isSingleton()) {
174             NeutronVPNIPSECPolicy singleton = input.getSingleton();
175
176             Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECPolicyAware.class, this);
177             if (instances != null) {
178                 if (instances.length > 0) {
179                     for (Object instance : instances) {
180                         INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
181                         int status = service.canCreateNeutronVPNIPSECPolicy(singleton);
182                         if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
183                             return Response.status(status).build();
184                         }
185                     }
186                 } else {
187                     throw new ServiceUnavailableException(NO_PROVIDERS);
188                 }
189             } else {
190                 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
191             }
192
193             /*
194              * add ipsecPolicy to the cache
195              */
196             ipsecPolicyInterface.addNeutronVPNIPSECPolicy(singleton);
197             if (instances != null) {
198                 for (Object instance : instances) {
199                     INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
200                     service.neutronVPNIPSECPolicyCreated(singleton);
201                 }
202             }
203         } else {
204
205             /*
206              * only singleton ipsecPolicy creates supported
207              */
208             throw new BadRequestException("Only singleton ipsecPolicy creates supported");
209         }
210         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
211     }
212
213     /**
214      * Updates a VPN IPSEC Policy */
215     @Path("{policyID}")
216     @PUT
217     @Produces({ MediaType.APPLICATION_JSON })
218     @Consumes({ MediaType.APPLICATION_JSON })
219     @StatusCodes({
220             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
221             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
222     public Response updateVPNIPSECPolicy(
223             @PathParam("policyID") String policyUUID, final NeutronVPNIPSECPolicyRequest input
224             ) {
225         INeutronVPNIPSECPolicyCRUD ipsecPolicyInterface = getNeutronInterfaces().getVPNIPSECPolicyInterface();
226
227         NeutronVPNIPSECPolicy singleton = input.getSingleton();
228         NeutronVPNIPSECPolicy original = ipsecPolicyInterface.getNeutronVPNIPSECPolicy(policyUUID);
229
230         Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECPolicyAware.class, this);
231         if (instances != null) {
232             if (instances.length > 0) {
233                 for (Object instance : instances) {
234                     INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
235                     int status = service.canUpdateNeutronVPNIPSECPolicy(singleton, original);
236                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
237                         return Response.status(status).build();
238                     }
239                 }
240             } else {
241                 throw new ServiceUnavailableException(NO_PROVIDERS);
242             }
243         } else {
244             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
245         }
246         /*
247          * update the ipsecPolicy entry and return the modified object
248          */
249         ipsecPolicyInterface.updateNeutronVPNIPSECPolicy(policyUUID, singleton);
250         NeutronVPNIPSECPolicy updatedVPNIPSECPolicy = ipsecPolicyInterface.getNeutronVPNIPSECPolicy(policyUUID);
251         if (instances != null) {
252             for (Object instance : instances) {
253                 INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
254                 service.neutronVPNIPSECPolicyUpdated(updatedVPNIPSECPolicy);
255             }
256         }
257         return Response.status(HttpURLConnection.HTTP_OK).entity(
258                 new NeutronVPNIPSECPolicyRequest(ipsecPolicyInterface.getNeutronVPNIPSECPolicy(policyUUID))).build();
259     }
260
261     /**
262      * Deletes a VPN IPSEC Policy */
263
264     @Path("{policyID}")
265     @DELETE
266     @StatusCodes({
267             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
268             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
269     public Response deleteVPNIPSECPolicy(
270             @PathParam("policyID") String policyUUID) {
271         INeutronVPNIPSECPolicyCRUD policyInterface = getNeutronInterfaces().getVPNIPSECPolicyInterface();
272
273         NeutronVPNIPSECPolicy singleton = policyInterface.getNeutronVPNIPSECPolicy(policyUUID);
274         Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECPolicyAware.class, this);
275         if (instances != null) {
276             if (instances.length > 0) {
277                 for (Object instance : instances) {
278                     INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
279                     int status = service.canDeleteNeutronVPNIPSECPolicy(singleton);
280                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
281                         return Response.status(status).build();
282                     }
283                 }
284             } else {
285                 throw new ServiceUnavailableException(NO_PROVIDERS);
286             }
287         } else {
288             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
289         }
290         policyInterface.removeNeutronVPNIPSECPolicy(policyUUID);
291         if (instances != null) {
292             for (Object instance : instances) {
293                 INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
294                 service.neutronVPNIPSECPolicyDeleted(singleton);
295             }
296         }
297         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
298     }
299 }