2 * Copyright (c) 2015 IBM Corporation and others. 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.INeutronVPNIPSECPolicyAware;
35 import org.opendaylight.neutron.spi.INeutronVPNIPSECPolicyCRUD;
36 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
37 import org.opendaylight.neutron.spi.NeutronVPNIPSECPolicy;
40 * Neutron Northbound REST APIs for VPN IPSEC Policy.<br>
41 * This class provides REST APIs for managing neutron VPN IPSEC 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/ipsecpolicies")
58 public class NeutronVPNIPSECPoliciesNorthbound extends AbstractNeutronNorthbound {
60 private static final String RESOURCE_NAME = "VPNIPSECPolicy";
62 private NeutronVPNIPSECPolicy extractFields(NeutronVPNIPSECPolicy o, List<String> fields) {
63 return o.extractFields(fields);
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());
79 * Returns a list of all VPN IPSEC Policies */
82 @Produces({ MediaType.APPLICATION_JSON })
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(
90 @QueryParam("fields") List<String> 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
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));
125 //TODO: apply pagination to results
126 return Response.status(HttpURLConnection.HTTP_OK).entity(
127 new NeutronVPNIPSECPolicyRequest(ans)).build();
131 * Returns a specific VPN IPSEC Policy */
135 @Produces({ MediaType.APPLICATION_JSON })
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,
146 @QueryParam("fields") List<String> fields
148 INeutronVPNIPSECPolicyCRUD policyInterface = getNeutronInterfaces().getVPNIPSECPolicyInterface();
149 if (!policyInterface.neutronVPNIPSECPolicyExists(policyUUID)) {
150 throw new ResourceNotFoundException(uuidNoExist(RESOURCE_NAME));
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();
157 return Response.status(HttpURLConnection.HTTP_OK).entity(
158 new NeutronVPNIPSECPolicyRequest(policyInterface.getNeutronVPNIPSECPolicy(policyUUID))).build();
163 * Creates new VPN IPSEC Policy */
165 @Produces({ MediaType.APPLICATION_JSON })
166 @Consumes({ MediaType.APPLICATION_JSON })
167 @TypeHint(NeutronVPNIPSECPolicy.class)
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();
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();
187 throw new ServiceUnavailableException(NO_PROVIDERS);
190 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
194 * add ipsecPolicy to the cache
196 ipsecPolicyInterface.addNeutronVPNIPSECPolicy(singleton);
197 if (instances != null) {
198 for (Object instance : instances) {
199 INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
200 service.neutronVPNIPSECPolicyCreated(singleton);
206 * only singleton ipsecPolicy creates supported
208 throw new BadRequestException("Only singleton ipsecPolicy creates supported");
210 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
214 * Updates a VPN IPSEC Policy */
217 @Produces({ MediaType.APPLICATION_JSON })
218 @Consumes({ MediaType.APPLICATION_JSON })
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
225 INeutronVPNIPSECPolicyCRUD ipsecPolicyInterface = getNeutronInterfaces().getVPNIPSECPolicyInterface();
227 NeutronVPNIPSECPolicy singleton = input.getSingleton();
228 NeutronVPNIPSECPolicy original = ipsecPolicyInterface.getNeutronVPNIPSECPolicy(policyUUID);
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();
241 throw new ServiceUnavailableException(NO_PROVIDERS);
244 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
247 * update the ipsecPolicy entry and return the modified object
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);
257 return Response.status(HttpURLConnection.HTTP_OK).entity(
258 new NeutronVPNIPSECPolicyRequest(ipsecPolicyInterface.getNeutronVPNIPSECPolicy(policyUUID))).build();
262 * Deletes a VPN IPSEC Policy */
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();
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();
285 throw new ServiceUnavailableException(NO_PROVIDERS);
288 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
290 policyInterface.removeNeutronVPNIPSECPolicy(policyUUID);
291 if (instances != null) {
292 for (Object instance : instances) {
293 INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
294 service.neutronVPNIPSECPolicyDeleted(singleton);
297 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();