Fix license header violations in northbound-api
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronVPNIPSECSiteConnectionsNorthbound.java
1 /*
2  * Copyright (c) 2013, 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.INeutronVPNIKEPolicyAware;
35 import org.opendaylight.neutron.spi.INeutronVPNIPSECSiteConnectionAware;
36 import org.opendaylight.neutron.spi.INeutronVPNIPSECSiteConnectionsCRUD;
37 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
38 import org.opendaylight.neutron.spi.NeutronVPNIPSECSiteConnection;
39
40 /**
41  * Neutron Northbound REST APIs for VPN IPSEC SiteConnection.<br>
42  * This class provides REST APIs for managing neutron VPN IPSEC SiteConnections
43  *
44  * <br>
45  * <br>
46  * Authentication scheme : <b>HTTP Basic</b><br>
47  * Authentication realm : <b>opendaylight</b><br>
48  * Transport : <b>HTTP and HTTPS</b><br>
49  * <br>
50  * HTTPS Authentication is disabled by default. Administrator can enable it in
51  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
52  * trusted authority.<br>
53  * More info :
54  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
55  *
56  */
57
58 @Path("/vpn/ipsecsiteconnections")
59 public class NeutronVPNIPSECSiteConnectionsNorthbound {
60
61     private static final int HTTP_OK_BOTTOM = 200;
62     private static final int HTTP_OK_TOP = 299;
63     private static final String INTERFACE_NAME = "VPNIPSECSiteConnections CRUD Interface";
64     private static final String NO_PROVIDERS = "No providers registered.  Please try again later";
65     private static final String NO_PROVIDER_LIST = "Couldn't get providers list.  Please try again later";
66
67     private NeutronVPNIPSECSiteConnection extractFields(NeutronVPNIPSECSiteConnection o, List<String> fields) {
68         return o.extractFields(fields);
69     }
70
71     private NeutronCRUDInterfaces getNeutronInterfaces() {
72         NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronVPNIPSECSiteConnectionsCRUD(this);
73         if (answer.getVPNIPSECSiteConnectionsInterface() == null) {
74             throw new ServiceUnavailableException(INTERFACE_NAME
75                 + RestMessages.SERVICEUNAVAILABLE.toString());
76         }
77         return answer;
78     }
79
80     @Context
81     UriInfo uriInfo;
82
83     /**
84      * Returns a list of all VPN IPSEC SiteConnections
85      */
86
87     @GET
88     @Produces({ MediaType.APPLICATION_JSON })
89     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
90             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
91             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
92             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
93     public Response listVPNIPSECSiteConnections(
94             // return fields
95             @QueryParam("fields") List<String> fields,
96             // filter fields
97             @QueryParam("id") String queryID, @QueryParam("tenant_id") String queryTenantID,
98             @QueryParam("name") String queryName, @QueryParam("description") String queryDescription,
99             @QueryParam("peer_address") String queryPeerAddress, @QueryParam("peer_id") String queryPeerID,
100             @QueryParam("route_mode") String queryRouteMode, @QueryParam("mtu") Integer queryMtu,
101             @QueryParam("auth_mode") String queryAuthMode, @QueryParam("psk") String queryPsk,
102             @QueryParam("initiator") String queryInitiator, @QueryParam("admin_state_up") String queryAdminStateUp,
103             @QueryParam("status") String queryStatus, @QueryParam("ikepolicy_id") String queryIkePolicyID,
104             @QueryParam("ipsecpolicy_id") String queryIpSecPolicyID,
105             @QueryParam("vpnservice_id") String queryVpnServiceID
106     // pagination and sorting are TODO
107     ) {
108         INeutronVPNIPSECSiteConnectionsCRUD labelInterface = getNeutronInterfaces()
109                 .getVPNIPSECSiteConnectionsInterface();
110         List<NeutronVPNIPSECSiteConnection> allNeutronVPNIPSECSiteConnection = labelInterface
111                 .getAllNeutronVPNIPSECSiteConnections();
112         List<NeutronVPNIPSECSiteConnection> ans = new ArrayList<NeutronVPNIPSECSiteConnection>();
113         Iterator<NeutronVPNIPSECSiteConnection> i = allNeutronVPNIPSECSiteConnection.iterator();
114         while (i.hasNext()) {
115             NeutronVPNIPSECSiteConnection oSS = i.next();
116             if ((queryID == null || queryID.equals(oSS.getID()))
117                     && (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))
118                     && (queryName == null || queryName.equals(oSS.getName()))
119                     && (queryDescription == null || queryDescription.equals(oSS.getDescription()))
120                     && (queryPeerAddress == null || queryPeerAddress.equals(oSS.getPeerAddress()))
121                     && (queryPeerID == null || queryPeerID.equals(oSS.getPeerID()))
122                     && (queryRouteMode == null || queryRouteMode.equals(oSS.getRouteMode()))
123                     && (queryMtu == null || queryMtu.equals(oSS.getMtu()))
124                     && (queryAuthMode == null || queryAuthMode.equals(oSS.getAuthMode()))
125                     && (queryPsk == null || queryPsk.equals(oSS.getPreSharedKey()))
126                     && (queryInitiator == null || queryInitiator.equals(oSS.getInitiator()))
127                     && (queryAdminStateUp == null || queryAdminStateUp.equals(oSS.getAdminStateUp()))
128                     && (queryStatus == null || queryStatus.equals(oSS.getStatus()))
129                     && (queryIkePolicyID == null || queryIkePolicyID.equals(oSS.getIkePolicyID()))
130                     && (queryIpSecPolicyID == null || queryIpSecPolicyID.equals(oSS.getIpsecPolicyID()))
131                     && (queryVpnServiceID == null || queryVpnServiceID.equals(oSS.getVpnServiceID()))) {
132                 if (fields.size() > 0) {
133                     ans.add(extractFields(oSS, fields));
134                 } else {
135                     ans.add(oSS);
136                 }
137             }
138         }
139
140         // TODO: apply pagination to results
141         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronVPNIPSECSiteConnectionRequest(ans)).build();
142     }
143
144     /**
145      * Returns a specific VPN IPSEC SiteConnection
146      */
147
148     @Path("{connectionID}")
149     @GET
150     @Produces({ MediaType.APPLICATION_JSON })
151     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
152             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
153             @ResponseCode(code = HttpURLConnection.HTTP_FORBIDDEN, condition = "Forbidden"),
154             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
155             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
156             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
157     public Response showVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
158     // return fields
159             @QueryParam("fields") List<String> fields) {
160         INeutronVPNIPSECSiteConnectionsCRUD connectionInterface = getNeutronInterfaces()
161                 .getVPNIPSECSiteConnectionsInterface();
162         if (!connectionInterface.neutronVPNIPSECSiteConnectionsExists(policyID)) {
163             throw new ResourceNotFoundException("NeutronVPNIPSECSiteConnections ID not found");
164         }
165         if (fields.size() > 0) {
166             NeutronVPNIPSECSiteConnection ans = connectionInterface.getNeutronVPNIPSECSiteConnections(policyID);
167             return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronVPNIPSECSiteConnectionRequest(extractFields(ans, fields)))
168                     .build();
169         } else {
170             return Response
171                     .status(HttpURLConnection.HTTP_OK)
172                     .entity(new NeutronVPNIPSECSiteConnectionRequest(connectionInterface
173                             .getNeutronVPNIPSECSiteConnections(policyID))).build();
174         }
175     }
176
177     /**
178      * Creates new VPN IPSEC SiteConnection
179      */
180     @POST
181     @Produces({ MediaType.APPLICATION_JSON })
182     @Consumes({ MediaType.APPLICATION_JSON })
183     @TypeHint(NeutronVPNIPSECSiteConnection.class)
184     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
185             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
186     public Response createVPNIPSECSiteConnection(final NeutronVPNIPSECSiteConnectionRequest input) {
187         INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
188                 .getVPNIPSECSiteConnectionsInterface();
189         if (input.isSingleton()) {
190             NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
191             Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
192             if (instances != null) {
193                 if (instances.length > 0) {
194                     for (Object instance : instances) {
195                         INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
196                         int status = service.canCreateNeutronVPNIPSECSiteConnection(singleton);
197                         if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
198                             return Response.status(status).build();
199                         }
200                     }
201                 } else {
202                     throw new ServiceUnavailableException(NO_PROVIDERS);
203                 }
204             } else {
205                 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
206             }
207             /*
208              * add ipsecSiteConnections to the cache
209              */
210             ipsecSiteConnectionsInterface.addNeutronVPNIPSECSiteConnections(singleton);
211             if (instances != null) {
212                 for (Object instance : instances) {
213                     INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
214                     service.neutronVPNIPSECSiteConnectionCreated(singleton);
215                 }
216             }
217         } else {
218
219             /*
220              * only singleton ipsecSiteConnections creates supported
221              */
222             throw new BadRequestException("Only singleton ipsecSiteConnections creates supported");
223         }
224         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
225     }
226
227     /**
228      * Updates a VPN IPSEC SiteConnection
229      */
230     @Path("{policyID}")
231     @PUT
232     @Produces({ MediaType.APPLICATION_JSON })
233     @Consumes({ MediaType.APPLICATION_JSON })
234     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
235             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
236     public Response updateVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
237             final NeutronVPNIPSECSiteConnectionRequest input) {
238         INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
239                 .getVPNIPSECSiteConnectionsInterface();
240
241         NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
242         NeutronVPNIPSECSiteConnection original = ipsecSiteConnectionsInterface
243                 .getNeutronVPNIPSECSiteConnections(policyID);
244
245         Object[] instances = NeutronUtil.getInstances(INeutronVPNIKEPolicyAware.class, this);
246         if (instances != null) {
247             if (instances.length > 0) {
248                 for (Object instance : instances) {
249                     INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
250                     int status = service.canUpdateNeutronVPNIPSECSiteConnection(singleton, original);
251                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
252                         return Response.status(status).build();
253                     }
254                 }
255             } else {
256                 throw new ServiceUnavailableException(NO_PROVIDERS);
257             }
258         } else {
259             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
260         }
261         /*
262          * update the ipsecSiteConnections entry and return the modified object
263          */
264         ipsecSiteConnectionsInterface.updateNeutronVPNIPSECSiteConnections(policyID, singleton);
265         NeutronVPNIPSECSiteConnection updatedVPNIKEPolicy = ipsecSiteConnectionsInterface
266                 .getNeutronVPNIPSECSiteConnections(policyID);
267         if (instances != null) {
268             for (Object instance : instances) {
269                 INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
270                 service.neutronVPNIPSECSiteConnectionUpdated(updatedVPNIKEPolicy);
271             }
272         }
273         return Response
274                 .status(HttpURLConnection.HTTP_OK)
275                 .entity(new NeutronVPNIPSECSiteConnectionRequest(ipsecSiteConnectionsInterface
276                         .getNeutronVPNIPSECSiteConnections(policyID))).build();
277     }
278
279     /**
280      * Deletes a VPN IPSEC SiteConnection
281      */
282
283     @Path("{policyID}")
284     @DELETE
285     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
286             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
287     public Response deleteVPNIPSECSiteConnection(@PathParam("policyID") String policyID) {
288         INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
289                 .getVPNIPSECSiteConnectionsInterface();
290
291         NeutronVPNIPSECSiteConnection singleton = ipsecSiteConnectionsInterface
292                 .getNeutronVPNIPSECSiteConnections(policyID);
293         Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
294         if (instances != null) {
295             if (instances.length > 0) {
296                 for (Object instance : instances) {
297                     INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
298                     int status = service.canDeleteNeutronVPNIPSECSiteConnection(singleton);
299                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
300                         return Response.status(status).build();
301                     }
302                 }
303             } else {
304                 throw new ServiceUnavailableException(NO_PROVIDERS);
305             }
306         } else {
307             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
308         }
309         ipsecSiteConnectionsInterface.removeNeutronVPNIPSECSiteConnections(policyID);
310         if (instances != null) {
311             for (Object instance : instances) {
312                 INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
313                 service.neutronVPNIPSECSiteConnectionDeleted(singleton);
314             }
315         }
316         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
317     }
318
319 }