2 * Copyright (c) 2017 Intel, Corp. and others. All rights reserved.
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
8 package org.opendaylight.neutron.northbound.api;
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;
36 public final class NeutronTapFlowNorthbound
37 extends AbstractNeutronNorthbound<NeutronTapFlow, NeutronTapFlowRequest, INeutronTapFlowCRUD> {
39 private static final String RESOURCE_NAME = "Tap Flow";
42 public NeutronTapFlowNorthbound(@OsgiService INeutronTapFlowCRUD neutronCRUD) {
47 protected String getResourceName() {
52 * Returns a list of all Tap Flows.
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(
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,
71 @QueryParam("limit") String limit,
72 @QueryParam("marker") String marker,
73 @QueryParam("page_reverse") String pageReverse) {
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));
92 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronTapFlowRequest(ans)).build();
96 * Creates new Tap Flow.
98 @Path("{tapServiceUUID}/flows")
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) {
107 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
109 if (input.isSingleton()) {
110 NeutronTapFlow singleton = input.getSingleton();
111 singleton.setTapFlowServiceID(tapServiceUUID);
114 tapFlowInterface.addTapFlow(singleton);
115 } catch (OperationFailedException e) {
116 throw new DatastoreOperationFailedWebApplicationException(e);
119 throw new BadRequestException("Only Singleton tapFlow creation supported");
122 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
126 * Returns a specific Tap Flow.
128 @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
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) {
140 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
141 if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
142 throw new ResourceNotFoundException("Specified UUID does not Exist");
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();
150 return Response.status(HttpURLConnection.HTTP_OK)
151 .entity(new NeutronTapFlowRequest(tapFlow)).build();
153 } catch (ReadFailedException e) {
154 throw new DatastoreOperationFailedWebApplicationException(e);
159 * Updates a Tap Flow.
161 @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
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) {
172 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
174 if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
175 throw new ResourceNotFoundException("Specified UUID does not Exist");
178 NeutronTapFlow singleton = input.getSingleton();
179 singleton.setTapFlowServiceID(tapServiceUUID);
180 tapFlowInterface.updateTapFlow(singleton);
182 return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
183 } catch (OperationFailedException e) {
184 throw new DatastoreOperationFailedWebApplicationException(e);
189 * Deletes a Tap Flow.
191 @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
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) {
199 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
201 if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
202 throw new ResourceNotFoundException("Specified UUID does not Exist");
205 tapFlowInterface.deleteTapFlow(tapServiceUUID, tapFlowUUID);
206 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
207 } catch (OperationFailedException e) {
208 throw new DatastoreOperationFailedWebApplicationException(e);