Neutron*Northbound: consolidate crud logic
[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
59     extends AbstractNeutronNorthbound<NeutronVPNIPSECPolicy, NeutronVPNIPSECPolicyRequest, INeutronVPNIPSECPolicyCRUD, INeutronVPNIPSECPolicyAware> {
60
61     private static final String RESOURCE_NAME = "VPNIPSECPolicy";
62
63     @Override
64     protected String getResourceName() {
65         return RESOURCE_NAME;
66     }
67
68     @Override
69     protected NeutronVPNIPSECPolicy extractFields(NeutronVPNIPSECPolicy o, List<String> fields) {
70         return o.extractFields(fields);
71     }
72
73     @Override
74     protected INeutronVPNIPSECPolicyCRUD getNeutronCRUD() {
75         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronVPNIPSECPolicyCRUD(this);
76         if (answer.getVPNIPSECPolicyInterface() == null) {
77             throw new ServiceUnavailableException(serviceUnavailable());
78         }
79         return answer.getVPNIPSECPolicyInterface();
80     }
81
82     @Override
83     protected NeutronVPNIPSECPolicyRequest newNeutronRequest(NeutronVPNIPSECPolicy o) {
84         return new NeutronVPNIPSECPolicyRequest(o);
85     }
86
87     @Override
88     protected Object[] getInstances() {
89         return NeutronUtil.getInstances(INeutronVPNIPSECPolicyAware.class, this);
90     }
91
92     @Override
93     protected int canCreate(Object instance, NeutronVPNIPSECPolicy singleton) {
94         INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
95         return service.canCreateNeutronVPNIPSECPolicy(singleton);
96     }
97
98     @Override
99     protected void created(Object instance, NeutronVPNIPSECPolicy singleton) {
100         INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
101         service.neutronVPNIPSECPolicyCreated(singleton);
102     }
103
104     @Override
105     protected int canUpdate(Object instance, NeutronVPNIPSECPolicy delta, NeutronVPNIPSECPolicy original) {
106         INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
107         return service.canUpdateNeutronVPNIPSECPolicy(delta, original);
108     }
109
110     @Override
111     protected void updated(Object instance, NeutronVPNIPSECPolicy updated) {
112         INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
113         service.neutronVPNIPSECPolicyUpdated(updated);
114     }
115
116     @Override
117     protected int canDelete(Object instance, NeutronVPNIPSECPolicy singleton) {
118         INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
119         return service.canDeleteNeutronVPNIPSECPolicy(singleton);
120     }
121
122     @Override
123     protected void deleted(Object instance, NeutronVPNIPSECPolicy singleton) {
124         INeutronVPNIPSECPolicyAware service = (INeutronVPNIPSECPolicyAware) instance;
125         service.neutronVPNIPSECPolicyDeleted(singleton);
126     }
127
128     @Context
129     UriInfo uriInfo;
130
131     /**
132      * Returns a list of all VPN IPSEC Policies */
133
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_NOT_IMPLEMENTED, condition = "Not Implemented"),
140             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
141     public Response listVPNIPSECPolicies(
142             // return fields
143             @QueryParam("fields") List<String> fields,
144             // filter fields
145             @QueryParam("id") String queryID,
146             @QueryParam("tenant_id") String queryTenantID,
147             @QueryParam("name") String queryName,
148             @QueryParam("description") String queryDescription,
149             @QueryParam("transform_protocol") String queryTransformProtocol,
150             @QueryParam("encapsulation_mode") String queryEncapsulationMode,
151             @QueryParam("auth_algorithm") String queryAuthAlgorithm,
152             @QueryParam("encryption_algorithm") String queryEncryptionAlgorithm,
153             @QueryParam("pfs") String queryPFS
154             // pagination and sorting are TODO
155             ) {
156         INeutronVPNIPSECPolicyCRUD policyInterface = getNeutronCRUD();
157         List<NeutronVPNIPSECPolicy> allNeutronVPNIPSECPolicies = policyInterface.getAllNeutronVPNIPSECPolicies();
158         List<NeutronVPNIPSECPolicy> ans = new ArrayList<NeutronVPNIPSECPolicy>();
159         Iterator<NeutronVPNIPSECPolicy> i = allNeutronVPNIPSECPolicies.iterator();
160         while (i.hasNext()) {
161             NeutronVPNIPSECPolicy oSS = i.next();
162             if ((queryID == null || queryID.equals(oSS.getID())) &&
163                     (queryName == null || queryName.equals(oSS.getName())) &&
164                     (queryDescription == null || queryDescription.equals(oSS.getDescription())) &&
165                     (queryAuthAlgorithm == null || queryAuthAlgorithm.equals(oSS.getAuthAlgorithm())) &&
166                     (queryEncryptionAlgorithm == null || queryEncryptionAlgorithm.equals(oSS.getEncryptionAlgorithm())) &&
167                     (queryPFS == null || queryPFS.equals(oSS.getPerfectForwardSecrecy())) &&
168                     (queryTransformProtocol == null || queryTransformProtocol.equals(oSS.getTransformProtocol())) &&
169                     (queryEncapsulationMode == null || queryEncapsulationMode.equals(oSS.getEncapsulationMode())) &&
170                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
171                 if (fields.size() > 0) {
172                     ans.add(extractFields(oSS,fields));
173                 } else {
174                     ans.add(oSS);
175                 }
176             }
177         }
178         //TODO: apply pagination to results
179         return Response.status(HttpURLConnection.HTTP_OK).entity(
180                 new NeutronVPNIPSECPolicyRequest(ans)).build();
181     }
182
183     /**
184      * Returns a specific VPN IPSEC Policy */
185
186     @Path("{policyID}")
187     @GET
188     @Produces({ MediaType.APPLICATION_JSON })
189     @StatusCodes({
190             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
191             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
192             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
193             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
194             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
195             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
196     public Response showVPNIPSECPolicy(
197             @PathParam("policyID") String policyUUID,
198             // return fields
199             @QueryParam("fields") List<String> fields
200             ) {
201         return show(policyUUID, fields);
202     }
203
204     /**
205      * Creates new VPN IPSEC Policy */
206     @POST
207     @Produces({ MediaType.APPLICATION_JSON })
208     @Consumes({ MediaType.APPLICATION_JSON })
209     @TypeHint(NeutronVPNIPSECPolicy.class)
210     @StatusCodes({
211             @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
212             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
213     public Response createVPNIPSECPolicy(final NeutronVPNIPSECPolicyRequest input) {
214         return create(input);
215     }
216
217     /**
218      * Updates a VPN IPSEC Policy */
219     @Path("{policyID}")
220     @PUT
221     @Produces({ MediaType.APPLICATION_JSON })
222     @Consumes({ MediaType.APPLICATION_JSON })
223     @StatusCodes({
224             @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
225             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
226     public Response updateVPNIPSECPolicy(
227             @PathParam("policyID") String policyUUID, final NeutronVPNIPSECPolicyRequest input
228             ) {
229         return update(policyUUID, input);
230     }
231
232     /**
233      * Deletes a VPN IPSEC Policy */
234
235     @Path("{policyID}")
236     @DELETE
237     @StatusCodes({
238             @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
239             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
240     public Response deleteVPNIPSECPolicy(
241             @PathParam("policyID") String policyUUID) {
242         return delete(policyUUID);
243     }
244 }