propagate datastore exceptions all the way to northbound
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronTapFlowNorthbound.java
1 /*
2  * Copyright (c) 2017 Intel, Corp. 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 package org.opendaylight.neutron.northbound.api;
9
10 import java.net.HttpURLConnection;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import javax.ws.rs.Consumes;
16 import javax.ws.rs.DELETE;
17 import javax.ws.rs.GET;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.PUT;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.PathParam;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.QueryParam;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26 import org.codehaus.enunciate.jaxrs.ResponseCode;
27 import org.codehaus.enunciate.jaxrs.StatusCodes;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.neutron.spi.INeutronTapFlowCRUD;
30 import org.opendaylight.neutron.spi.NeutronTapFlow;
31 import org.opendaylight.yangtools.yang.common.OperationFailedException;
32 import org.ops4j.pax.cdi.api.OsgiService;
33
34 @Singleton
35 @Path("/tap/flows")
36 public final class NeutronTapFlowNorthbound
37         extends AbstractNeutronNorthbound<NeutronTapFlow, NeutronTapFlowRequest, INeutronTapFlowCRUD> {
38
39     private static final String RESOURCE_NAME = "Tap Flow";
40
41     @Inject
42     public NeutronTapFlowNorthbound(@OsgiService INeutronTapFlowCRUD neutronCRUD) {
43         super(neutronCRUD);
44     }
45
46     @Override
47     protected String getResourceName() {
48         return RESOURCE_NAME;
49     }
50
51     /**
52      * Returns a list of all Tap Flows.
53      */
54     @GET
55     @Produces({ MediaType.APPLICATION_JSON })
56     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
57             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
58             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
59             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
60     public Response listGroups(
61             // return fields
62             @QueryParam("fields") List<String> fields,
63             // OpenStack Tap Flow attributes
64             @QueryParam("id") String queryTapFlowUUID,
65             @QueryParam("tenant_id") String queryTapFlowTenantID,
66             @QueryParam("name") String queryTapFlowName,
67             @QueryParam("source_port") String queryTapFlowSourcePort,
68             @QueryParam("tap_service_id") String queryTapServiceID,
69             @QueryParam("direction") String queryTapFlowDirection,
70             // pagination
71             @QueryParam("limit") String limit,
72             @QueryParam("marker") String marker,
73             @QueryParam("page_reverse") String pageReverse) {
74
75         INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
76         List<NeutronTapFlow> ans = new ArrayList<>();
77         for (NeutronTapFlow nsg : tapFlowInterface.getAll()) {
78             if ((queryTapFlowUUID == null || queryTapFlowUUID.equals(nsg.getID()))
79                     && (queryTapFlowTenantID == null || queryTapFlowTenantID.equals(nsg.getTenantID()))
80                     && (queryTapFlowName == null || queryTapFlowName.equals(nsg.getName()))
81                     && (queryTapServiceID == null || queryTapServiceID.equals(nsg.getTapFlowServiceID()))
82                     && (queryTapFlowDirection == null || queryTapFlowDirection.equals(nsg.getTapFlowDirection()))
83                     && (queryTapFlowSourcePort == null
84                         || queryTapFlowSourcePort.equals(nsg.getTapFlowSourcePort()))) {
85                 if (fields.size() > 0) {
86                     ans.add(nsg.extractFields(fields));
87                 } else {
88                     ans.add(nsg);
89                 }
90             }
91         }
92         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronTapFlowRequest(ans)).build();
93     }
94
95     /**
96      * Creates new Tap Flow.
97      */
98     @Path("{tapServiceUUID}/flows")
99     @POST
100     @Produces({ MediaType.APPLICATION_JSON })
101     @Consumes({ MediaType.APPLICATION_JSON })
102     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
103             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
104     public Response createTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
105                                   final NeutronTapFlowRequest input) {
106
107         INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
108
109         if (input.isSingleton()) {
110             NeutronTapFlow singleton = input.getSingleton();
111             singleton.setTapFlowServiceID(tapServiceUUID);
112
113             try {
114                 tapFlowInterface.addTapFlow(singleton);
115             } catch (OperationFailedException e) {
116                 throw new DatastoreOperationFailedWebApplicationException(e);
117             }
118         } else {
119             throw new BadRequestException("Only Singleton tapFlow creation supported");
120         }
121
122         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
123     }
124
125     /**
126      * Returns a specific Tap Flow.
127      */
128     @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
129     @GET
130     @Produces({ MediaType.APPLICATION_JSON })
131     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
132             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
133             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
134             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
135             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
136     public Response showTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
137                                 @PathParam("tapFlowUUID") String tapFlowUUID,
138                                 @QueryParam("fields") List<String> fields) {
139         try {
140             INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
141             if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
142                 throw new ResourceNotFoundException("Specified UUID does not Exist");
143             }
144
145             NeutronTapFlow tapFlow = tapFlowInterface.getTapFlow(tapServiceUUID, tapFlowUUID);
146             if (fields.size() > 0) {
147                 return Response.status(HttpURLConnection.HTTP_OK)
148                         .entity(new NeutronTapFlowRequest(tapFlow.extractFields(fields))).build();
149             } else {
150                 return Response.status(HttpURLConnection.HTTP_OK)
151                         .entity(new NeutronTapFlowRequest(tapFlow)).build();
152             }
153         } catch (ReadFailedException e) {
154             throw new DatastoreOperationFailedWebApplicationException(e);
155         }
156     }
157
158     /**
159      * Updates a Tap Flow.
160      */
161     @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
162     @PUT
163     @Produces({ MediaType.APPLICATION_JSON })
164     @Consumes({ MediaType.APPLICATION_JSON })
165     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
166             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
167             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
168     public Response updateTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
169                                   @PathParam("tapFlowUUID") String tapFlowUUID,
170                                   final NeutronTapFlowRequest input) {
171         try {
172             INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
173
174             if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
175                 throw new ResourceNotFoundException("Specified UUID does not Exist");
176             }
177
178             NeutronTapFlow singleton = input.getSingleton();
179             singleton.setTapFlowServiceID(tapServiceUUID);
180             tapFlowInterface.updateTapFlow(singleton);
181
182             return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
183         } catch (OperationFailedException e) {
184             throw new DatastoreOperationFailedWebApplicationException(e);
185         }
186     }
187
188     /**
189      * Deletes a Tap Flow.
190      */
191     @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
192     @DELETE
193     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
194             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
195             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
196     public Response deleteTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
197                                   @PathParam("tapFlowUUID") String tapFlowUUID) {
198         try {
199             INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
200
201             if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
202                 throw new ResourceNotFoundException("Specified UUID does not Exist");
203             }
204
205             tapFlowInterface.deleteTapFlow(tapServiceUUID, tapFlowUUID);
206             return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
207         } catch (OperationFailedException e) {
208             throw new DatastoreOperationFailedWebApplicationException(e);
209         }
210     }
211 }