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.net.HttpURLConnection;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
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;
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 {
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";
65 private NeutronVPNIKEPolicy extractFields(NeutronVPNIKEPolicy o, List<String> fields) {
66 return o.extractFields(fields);
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());
82 * Returns a list of all VPN IKE Policies */
85 @Produces({ MediaType.APPLICATION_JSON })
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(
93 @QueryParam("fields") List<String> 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
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));
128 //TODO: apply pagination to results
129 return Response.status(HttpURLConnection.HTTP_OK).entity(
130 new NeutronVPNIKEPolicyRequest(ans)).build();
134 * Returns a specific VPN IKE Policy */
138 @Produces({ MediaType.APPLICATION_JSON })
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,
149 @QueryParam("fields") List<String> fields
151 INeutronVPNIKEPolicyCRUD policyInterface = getNeutronInterfaces().getVPNIKEPolicyInterface();
152 if (!policyInterface.neutronVPNIKEPolicyExists(policyUUID)) {
153 throw new ResourceNotFoundException(UUID_NO_EXIST);
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();
160 return Response.status(HttpURLConnection.HTTP_OK).entity(
161 new NeutronVPNIKEPolicyRequest(policyInterface.getNeutronVPNIKEPolicy(policyUUID))).build();
166 * Creates new VPN IKE Policy */
168 @Produces({ MediaType.APPLICATION_JSON })
169 @Consumes({ MediaType.APPLICATION_JSON })
170 @TypeHint(NeutronVPNIKEPolicy.class)
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();
183 * verify that the ikePolicy doesn't already exist (issue: is deeper inspection necessary?)
185 if (ikePolicyInterface.neutronVPNIKEPolicyExists(singleton.getID())) {
186 throw new BadRequestException("ikePolicy UUID already exists");
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();
199 throw new ServiceUnavailableException(NO_PROVIDERS);
202 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
206 * add ikePolicy to the cache
208 ikePolicyInterface.addNeutronVPNIKEPolicy(singleton);
209 if (instances != null) {
210 for (Object instance : instances) {
211 INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
212 service.neutronVPNIKEPolicyCreated(singleton);
218 * only singleton ikePolicy creates supported
220 throw new BadRequestException("Only singleton ikePolicy creates supported");
222 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
226 * Updates a VPN IKE Policy */
229 @Produces({ MediaType.APPLICATION_JSON })
230 @Consumes({ MediaType.APPLICATION_JSON })
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
241 INeutronVPNIKEPolicyCRUD ikePolicyInterface = getNeutronInterfaces().getVPNIKEPolicyInterface();
244 * ikePolicy has to exist and only a single delta can be supplied
246 if (!ikePolicyInterface.neutronVPNIKEPolicyExists(policyUUID)) {
247 throw new ResourceNotFoundException(UUID_NO_EXIST);
249 if (!input.isSingleton()) {
250 throw new BadRequestException("Only single ikePolicy deltas supported");
252 NeutronVPNIKEPolicy singleton = input.getSingleton();
253 NeutronVPNIKEPolicy original = ikePolicyInterface.getNeutronVPNIKEPolicy(policyUUID);
256 * attribute changes blocked by Neutron
258 if (singleton.getID() != null || singleton.getTenantID() != null) {
259 throw new BadRequestException("Request attribute change not allowed");
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();
273 throw new ServiceUnavailableException(NO_PROVIDERS);
276 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
279 * update the ikePolicy entry and return the modified object
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);
289 return Response.status(HttpURLConnection.HTTP_OK).entity(
290 new NeutronVPNIKEPolicyRequest(ikePolicyInterface.getNeutronVPNIKEPolicy(policyUUID))).build();
294 * Deletes a VPN IKE Policy */
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();
310 * verify that the policy exists and is not in use before removing it
312 if (!policyInterface.neutronVPNIKEPolicyExists(policyUUID)) {
313 throw new ResourceNotFoundException(UUID_NO_EXIST);
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();
327 throw new ServiceUnavailableException(NO_PROVIDERS);
330 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
332 policyInterface.removeNeutronVPNIKEPolicy(policyUUID);
333 if (instances != null) {
334 for (Object instance : instances) {
335 INeutronVPNIKEPolicyAware service = (INeutronVPNIKEPolicyAware) instance;
336 service.neutronVPNIKEPolicyDeleted(singleton);
339 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();