2 * Copyright IBM Corporation, 2015. All rights reserved.
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
9 package org.opendaylight.neutron.northbound.api;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
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;
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;
40 * Neutron Northbound REST APIs for VPN IKE Policy.<br>
41 * This class provides REST APIs for managing neutron VPN IKE Policies
45 * Authentication scheme : <b>HTTP Basic</b><br>
46 * Authentication realm : <b>opendaylight</b><br>
47 * Transport : <b>HTTP and HTTPS</b><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>
53 * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
57 @Path("/vpn/ikepolicies")
58 public class NeutronVPNIKEPoliciesNorthbound {
60 private NeutronVPNIKEPolicy extractFields(NeutronVPNIKEPolicy o, List<String> fields) {
61 return o.extractFields(fields);
68 * Returns a list of all VPN IKE Policies */
71 @Produces({ MediaType.APPLICATION_JSON })
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(
79 @QueryParam("fields") List<String> 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
92 INeutronVPNIKEPolicyCRUD labelInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
93 if (labelInterface == null) {
94 throw new ServiceUnavailableException("NeutronVPNIKEPolicy CRUD Interface "
95 + RestMessages.SERVICEUNAVAILABLE.toString());
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));
117 //TODO: apply pagination to results
118 return Response.status(200).entity(
119 new NeutronVPNIKEPolicyRequest(ans)).build();
123 * Returns a specific VPN IKE Policy */
127 @Produces({ MediaType.APPLICATION_JSON })
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,
138 @QueryParam("fields") List<String> fields
140 INeutronVPNIKEPolicyCRUD policyInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
141 if (policyInterface == null) {
142 throw new ServiceUnavailableException("VPNIKEPolicy CRUD Interface "
143 + RestMessages.SERVICEUNAVAILABLE.toString());
145 if (!policyInterface.neutronVPNIKEPolicyExists(policyUUID)) {
146 throw new ResourceNotFoundException("VPNIKEPolicy UUID not found");
148 if (fields.size() > 0) {
149 NeutronVPNIKEPolicy ans = policyInterface.getNeutronVPNIKEPolicy(policyUUID);
150 return Response.status(200).entity(
151 new NeutronVPNIKEPolicyRequest(extractFields(ans, fields))).build();
153 return Response.status(200).entity(
154 new NeutronVPNIKEPolicyRequest(policyInterface.getNeutronVPNIKEPolicy(policyUUID))).build();
159 * Creates new VPN IKE Policy */
161 @Produces({ MediaType.APPLICATION_JSON })
162 @Consumes({ MediaType.APPLICATION_JSON })
163 @TypeHint(NeutronVPNIKEPolicy.class)
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());
176 if (input.isSingleton()) {
177 NeutronVPNIKEPolicy singleton = input.getSingleton();
180 * verify that the ikePolicy doesn't already exist (issue: is deeper inspection necessary?)
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();
194 throw new ServiceUnavailableException("No providers registered. Please try again later");
197 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
201 * add ikePolicy to the cache
203 ikePolicyInterface.addNeutronVPNIKEPolicy(singleton);
204 if (instances != null) {
205 for (Object instance : instances) {
206 INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
207 service.neutronVPNIKEPolicyCreated(singleton);
213 * only singleton ikePolicy creates supported
215 throw new BadRequestException("Only singleton ikePolicy creates supported");
217 return Response.status(201).entity(input).build();
221 * Updates a VPN IKE Policy */
224 @Produces({ MediaType.APPLICATION_JSON })
225 @Consumes({ MediaType.APPLICATION_JSON })
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
236 INeutronVPNIKEPolicyCRUD ikePolicyInterface = NeutronCRUDInterfaces.getINeutronVPNIKEPolicyCRUD(this);
237 if (ikePolicyInterface == null) {
238 throw new ServiceUnavailableException("VPNIKEPolicy CRUD Interface "
239 + RestMessages.SERVICEUNAVAILABLE.toString());
243 * ikePolicy has to exist and only a single delta can be supplied
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);
253 * attribute changes blocked by Neutron
255 if (singleton.getID() != null || singleton.getTenantID() != null)
256 throw new BadRequestException("Request attribute change not allowed");
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();
268 throw new ServiceUnavailableException("No providers registered. Please try again later");
271 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
274 * update the ikePolicy entry and return the modified object
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);
284 return Response.status(200).entity(
285 new NeutronVPNIKEPolicyRequest(ikePolicyInterface.getNeutronVPNIKEPolicy(policyUUID))).build();
289 * Deletes a VPN IKE Policy */
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());
309 * verify that the policy exists and is not in use before removing it
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();
324 throw new ServiceUnavailableException("No providers registered. Please try again later");
327 throw new ServiceUnavailableException("Couldn't get providers list. Please try again later");
329 policyInterface.removeNeutronVPNIKEPolicy(policyUUID);
330 if (instances != null) {
331 for (Object instance : instances) {
332 INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
333 service.neutronVPNIKEPolicyDeleted(singleton);
336 return Response.status(204).build();