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.neutron.spi.INeutronTapFlowCRUD;
29 import org.opendaylight.neutron.spi.NeutronTapFlow;
30 import org.ops4j.pax.cdi.api.OsgiService;
34 public final class NeutronTapFlowNorthbound
35 extends AbstractNeutronNorthbound<NeutronTapFlow, NeutronTapFlowRequest, INeutronTapFlowCRUD> {
37 private static final String RESOURCE_NAME = "Tap Flow";
40 public NeutronTapFlowNorthbound(@OsgiService INeutronTapFlowCRUD neutronCRUD) {
45 protected String getResourceName() {
50 * Returns a list of all Tap Flows.
53 @Produces({ MediaType.APPLICATION_JSON })
54 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
55 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
56 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
57 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
58 public Response listGroups(
60 @QueryParam("fields") List<String> fields,
61 // OpenStack Tap Flow attributes
62 @QueryParam("id") String queryTapFlowUUID,
63 @QueryParam("tenant_id") String queryTapFlowTenantID,
64 @QueryParam("name") String queryTapFlowName,
65 @QueryParam("source_port") String queryTapFlowSourcePort,
66 @QueryParam("tap_service_id") String queryTapServiceID,
67 @QueryParam("direction") String queryTapFlowDirection,
69 @QueryParam("limit") String limit,
70 @QueryParam("marker") String marker,
71 @QueryParam("page_reverse") String pageReverse) {
73 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
74 List<NeutronTapFlow> ans = new ArrayList<>();
75 for (NeutronTapFlow nsg : tapFlowInterface.getAll()) {
76 if ((queryTapFlowUUID == null || queryTapFlowUUID.equals(nsg.getID()))
77 && (queryTapFlowTenantID == null || queryTapFlowTenantID.equals(nsg.getTenantID()))
78 && (queryTapFlowName == null || queryTapFlowName.equals(nsg.getName()))
79 && (queryTapServiceID == null || queryTapServiceID.equals(nsg.getTapFlowServiceID()))
80 && (queryTapFlowDirection == null || queryTapFlowDirection.equals(nsg.getTapFlowDirection()))
81 && (queryTapFlowSourcePort == null
82 || queryTapFlowSourcePort.equals(nsg.getTapFlowSourcePort()))) {
83 if (fields.size() > 0) {
84 ans.add(nsg.extractFields(fields));
90 return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronTapFlowRequest(ans)).build();
94 * Creates new Tap Flow.
96 @Path("{tapServiceUUID}/flows")
98 @Produces({ MediaType.APPLICATION_JSON })
99 @Consumes({ MediaType.APPLICATION_JSON })
100 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
101 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
102 public Response createTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
103 final NeutronTapFlowRequest input) {
105 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
107 if (input.isSingleton()) {
108 NeutronTapFlow singleton = input.getSingleton();
109 singleton.setTapFlowServiceID(tapServiceUUID);
111 tapFlowInterface.addTapFlow(singleton);
113 throw new BadRequestException("Only Singleton tapFlow creation supported");
116 return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
120 * Returns a specific Tap Flow.
122 @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
124 @Produces({ MediaType.APPLICATION_JSON })
125 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
126 @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
127 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
128 @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
129 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130 public Response showTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
131 @PathParam("tapFlowUUID") String tapFlowUUID,
132 @QueryParam("fields") List<String> fields) {
134 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
135 if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
136 throw new ResourceNotFoundException("Specified UUID does not Exist");
139 NeutronTapFlow tapFlow = tapFlowInterface.getTapFlow(tapServiceUUID, tapFlowUUID);
140 if (fields.size() > 0) {
141 return Response.status(HttpURLConnection.HTTP_OK)
142 .entity(new NeutronTapFlowRequest(tapFlow.extractFields(fields))).build();
144 return Response.status(HttpURLConnection.HTTP_OK)
145 .entity(new NeutronTapFlowRequest(tapFlow)).build();
150 * Updates a Tap Flow.
152 @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
154 @Produces({ MediaType.APPLICATION_JSON })
155 @Consumes({ MediaType.APPLICATION_JSON })
156 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
157 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
158 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
159 public Response updateTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
160 @PathParam("tapFlowUUID") String tapFlowUUID,
161 final NeutronTapFlowRequest input) {
163 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
165 if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
166 throw new ResourceNotFoundException("Specified UUID does not Exist");
169 NeutronTapFlow singleton = input.getSingleton();
170 singleton.setTapFlowServiceID(tapServiceUUID);
171 tapFlowInterface.updateTapFlow(singleton);
173 return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
177 * Deletes a Tap Flow.
179 @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
181 @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
182 @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
183 @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
184 public Response deleteTapFlow(@PathParam("tapServiceUUID") String tapServiceUUID,
185 @PathParam("tapFlowUUID") String tapFlowUUID) {
187 INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
189 if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
190 throw new ResourceNotFoundException("Specified UUID does not Exist");
193 tapFlowInterface.deleteTapFlow(tapServiceUUID, tapFlowUUID);
194 return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();