3a13dba08aac9572012eef7942b637030a36ac46
[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 import java.util.ArrayList;
13 import java.util.List;
14 import javax.ws.rs.Consumes;
15 import javax.ws.rs.DELETE;
16 import javax.ws.rs.GET;
17 import javax.ws.rs.POST;
18 import javax.ws.rs.PUT;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.PathParam;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.QueryParam;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.codehaus.enunciate.jaxrs.ResponseCode;
26 import org.codehaus.enunciate.jaxrs.StatusCodes;
27 import org.codehaus.enunciate.jaxrs.TypeHint;
28 import org.opendaylight.neutron.spi.INeutronVpnIpSecPolicyCRUD;
29 import org.opendaylight.neutron.spi.NeutronVpnIpSecPolicy;
30
31 /**
32  * Neutron Northbound REST APIs for VPN IPSEC Policy.<br>
33  * This class provides REST APIs for managing neutron VPN IPSEC Policies
34  *
35  * <br>
36  * <br>
37  * Authentication scheme : <b>HTTP Basic</b><br>
38  * Authentication realm : <b>opendaylight</b><br>
39  * Transport : <b>HTTP and HTTPS</b><br>
40  * <br>
41  * HTTPS Authentication is disabled by default. Administrator can enable it in
42  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
43  * trusted authority.<br>
44  * More info :
45  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
46  *
47  */
48
49 @Path("/vpn/ipsecpolicies")
50 public final class NeutronVpnIpSecPoliciesNorthbound extends
51         AbstractNeutronNorthbound<NeutronVpnIpSecPolicy, NeutronVpnIpSecPolicyRequest, INeutronVpnIpSecPolicyCRUD> {
52
53     private static final String RESOURCE_NAME = "VPNIPSECPolicy";
54
55     @Override
56     protected String getResourceName() {
57         return RESOURCE_NAME;
58     }
59
60     /**
61      * Returns a list of all VPN IPSEC Policies.
62      */
63
64     @GET
65     @Produces({ MediaType.APPLICATION_JSON })
66     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
67             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
68             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
69             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
70     public Response listVpnIPSecPolicies(
71             // return fields
72             @QueryParam("fields") List<String> fields,
73             // filter fields
74             @QueryParam("id") String queryID,
75             @QueryParam("tenant_id") String queryTenantID,
76             @QueryParam("name") String queryName,
77             @QueryParam("transform_protocol") String queryTransformProtocol,
78             @QueryParam("encapsulation_mode") String queryEncapsulationMode,
79             @QueryParam("auth_algorithm") String queryAuthAlgorithm,
80             @QueryParam("encryption_algorithm") String queryEncryptionAlgorithm,
81             @QueryParam("pfs") String queryPFS
82     // pagination and sorting are TODO
83     ) {
84         INeutronVpnIpSecPolicyCRUD policyInterface = getNeutronCRUD();
85         List<NeutronVpnIpSecPolicy> allNeutronVpnIPSecPolicies = policyInterface.getAll();
86         List<NeutronVpnIpSecPolicy> ans = new ArrayList<>();
87         for (NeutronVpnIpSecPolicy policy : allNeutronVpnIPSecPolicies) {
88             if ((queryID == null || queryID.equals(policy.getID()))
89                     && (queryName == null || queryName.equals(policy.getName()))
90                     && (queryAuthAlgorithm == null || queryAuthAlgorithm.equals(policy.getAuthAlgorithm()))
91                     && (queryEncryptionAlgorithm == null
92                             || queryEncryptionAlgorithm.equals(policy.getEncryptionAlgorithm()))
93                     && (queryPFS == null || queryPFS.equals(policy.getPerfectForwardSecrecy()))
94                     && (queryTransformProtocol == null || queryTransformProtocol.equals(policy.getTransformProtocol()))
95                     && (queryEncapsulationMode == null || queryEncapsulationMode.equals(policy.getEncapsulationMode()))
96                     && (queryTenantID == null || queryTenantID.equals(policy.getTenantID()))) {
97                 if (fields.size() > 0) {
98                     ans.add(policy.extractFields(fields));
99                 } else {
100                     ans.add(policy);
101                 }
102             }
103         }
104         //TODO: apply pagination to results
105         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronVpnIpSecPolicyRequest(ans)).build();
106     }
107
108     /**
109      * Returns a specific VPN IPSEC Policy.
110      */
111
112     @Path("{policyID}")
113     @GET
114     @Produces({ MediaType.APPLICATION_JSON })
115     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
116             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
117             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
118             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
119             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
120             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
121     public Response showVpnIPSecPolicy(@PathParam("policyID") String policyUUID,
122                                        // return fields
123                                        @QueryParam("fields") List<String> fields) {
124         return show(policyUUID, fields);
125     }
126
127     /**
128      * Creates new VPN IPSEC Policy.
129      */
130     @POST
131     @Produces({ MediaType.APPLICATION_JSON })
132     @Consumes({ MediaType.APPLICATION_JSON })
133     @TypeHint(NeutronVpnIpSecPolicy.class)
134     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
135             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
136     public Response createVpnIPSecPolicy(final NeutronVpnIpSecPolicyRequest input) {
137         return create(input);
138     }
139
140     /**
141      * Updates a VPN IPSEC Policy.
142      */
143     @Path("{policyID}")
144     @PUT
145     @Produces({ MediaType.APPLICATION_JSON })
146     @Consumes({ MediaType.APPLICATION_JSON })
147     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
148             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
149             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
150     public Response updateVpnIPSecPolicy(@PathParam("policyID") String policyUUID,
151                                          final NeutronVpnIpSecPolicyRequest input) {
152         return update(policyUUID, input);
153     }
154
155     /**
156      * Deletes a VPN IPSEC Policy.
157      */
158
159     @Path("{policyID}")
160     @DELETE
161     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
162             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
163             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
164     public Response deleteVpnIPSecPolicy(@PathParam("policyID") String policyUUID) {
165         return delete(policyUUID);
166     }
167 }