Merge "BUG #4044 binding:host_id is string, not UUID"
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronVPNIPSECSiteConnectionsNorthbound.java
1 /*
2  * Copyright IBM Corporation, 2013.  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_BAD_REQUEST, condition = "Bad Request"),
186             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
187             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
188             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
189     public Response createVPNIPSECSiteConnection(final NeutronVPNIPSECSiteConnectionRequest input) {
190         INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
191                 .getVPNIPSECSiteConnectionsInterface();
192         if (input.isSingleton()) {
193             NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
194             Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
195             if (instances != null) {
196                 if (instances.length > 0) {
197                     for (Object instance : instances) {
198                         INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
199                         int status = service.canCreateNeutronVPNIPSECSiteConnection(singleton);
200                         if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
201                             return Response.status(status).build();
202                         }
203                     }
204                 } else {
205                     throw new ServiceUnavailableException(NO_PROVIDERS);
206                 }
207             } else {
208                 throw new ServiceUnavailableException(NO_PROVIDER_LIST);
209             }
210             /*
211              * add ipsecSiteConnections to the cache
212              */
213             ipsecSiteConnectionsInterface.addNeutronVPNIPSECSiteConnections(singleton);
214             if (instances != null) {
215                 for (Object instance : instances) {
216                     INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
217                     service.neutronVPNIPSECSiteConnectionCreated(singleton);
218                 }
219             }
220         } else {
221
222             /*
223              * only singleton ipsecSiteConnections creates supported
224              */
225             throw new BadRequestException("Only singleton ipsecSiteConnections creates supported");
226         }
227         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
228     }
229
230     /**
231      * Updates a VPN IPSEC SiteConnection
232      */
233     @Path("{policyID}")
234     @PUT
235     @Produces({ MediaType.APPLICATION_JSON })
236     @Consumes({ MediaType.APPLICATION_JSON })
237     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
238             @ResponseCode(code = HttpURLConnection.HTTP_BAD_REQUEST, condition = "Bad Request"),
239             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
240             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
241             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
242             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
243     public Response updateVPNIPSECSiteConnection(@PathParam("policyID") String policyID,
244             final NeutronVPNIPSECSiteConnectionRequest input) {
245         INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
246                 .getVPNIPSECSiteConnectionsInterface();
247
248         NeutronVPNIPSECSiteConnection singleton = input.getSingleton();
249         NeutronVPNIPSECSiteConnection original = ipsecSiteConnectionsInterface
250                 .getNeutronVPNIPSECSiteConnections(policyID);
251
252         Object[] instances = NeutronUtil.getInstances(INeutronVPNIKEPolicyAware.class, this);
253         if (instances != null) {
254             if (instances.length > 0) {
255                 for (Object instance : instances) {
256                     INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
257                     int status = service.canUpdateNeutronVPNIPSECSiteConnection(singleton, original);
258                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
259                         return Response.status(status).build();
260                     }
261                 }
262             } else {
263                 throw new ServiceUnavailableException(NO_PROVIDERS);
264             }
265         } else {
266             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
267         }
268         /*
269          * update the ipsecSiteConnections entry and return the modified object
270          */
271         ipsecSiteConnectionsInterface.updateNeutronVPNIPSECSiteConnections(policyID, singleton);
272         NeutronVPNIPSECSiteConnection updatedVPNIKEPolicy = ipsecSiteConnectionsInterface
273                 .getNeutronVPNIPSECSiteConnections(policyID);
274         if (instances != null) {
275             for (Object instance : instances) {
276                 INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
277                 service.neutronVPNIPSECSiteConnectionUpdated(updatedVPNIKEPolicy);
278             }
279         }
280         return Response
281                 .status(HttpURLConnection.HTTP_OK)
282                 .entity(new NeutronVPNIPSECSiteConnectionRequest(ipsecSiteConnectionsInterface
283                         .getNeutronVPNIPSECSiteConnections(policyID))).build();
284     }
285
286     /**
287      * Deletes a VPN IPSEC SiteConnection
288      */
289
290     @Path("{policyID}")
291     @DELETE
292     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
293             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
294             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
295             @ResponseCode(code = HttpURLConnection.HTTP_CONFLICT, condition = "Conflict"),
296             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
297             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
298     public Response deleteVPNIPSECSiteConnection(@PathParam("policyID") String policyID) {
299         INeutronVPNIPSECSiteConnectionsCRUD ipsecSiteConnectionsInterface = getNeutronInterfaces()
300                 .getVPNIPSECSiteConnectionsInterface();
301
302         NeutronVPNIPSECSiteConnection singleton = ipsecSiteConnectionsInterface
303                 .getNeutronVPNIPSECSiteConnections(policyID);
304         Object[] instances = NeutronUtil.getInstances(INeutronVPNIPSECSiteConnectionAware.class, this);
305         if (instances != null) {
306             if (instances.length > 0) {
307                 for (Object instance : instances) {
308                     INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
309                     int status = service.canDeleteNeutronVPNIPSECSiteConnection(singleton);
310                     if (status < HTTP_OK_BOTTOM || status > HTTP_OK_TOP) {
311                         return Response.status(status).build();
312                     }
313                 }
314             } else {
315                 throw new ServiceUnavailableException(NO_PROVIDERS);
316             }
317         } else {
318             throw new ServiceUnavailableException(NO_PROVIDER_LIST);
319         }
320         ipsecSiteConnectionsInterface.removeNeutronVPNIPSECSiteConnections(policyID);
321         if (instances != null) {
322             for (Object instance : instances) {
323                 INeutronVPNIPSECSiteConnectionAware service = (INeutronVPNIPSECSiteConnectionAware) instance;
324                 service.neutronVPNIPSECSiteConnectionDeleted(singleton);
325             }
326         }
327         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
328     }
329
330 }