ee8429c833c3a07dc5410f166c2af26ad53d8109
[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.neutron.spi.INeutronTapFlowCRUD;
29 import org.opendaylight.neutron.spi.NeutronTapFlow;
30 import org.ops4j.pax.cdi.api.OsgiService;
31
32 @Singleton
33 @Path("/tap/flows")
34 public final class NeutronTapFlowNorthbound
35         extends AbstractNeutronNorthbound<NeutronTapFlow, NeutronTapFlowRequest, INeutronTapFlowCRUD> {
36
37     private static final String RESOURCE_NAME = "Tap Flow";
38
39     @Inject
40     public NeutronTapFlowNorthbound(@OsgiService INeutronTapFlowCRUD neutronCRUD) {
41         super(neutronCRUD);
42     }
43
44     @Override
45     protected String getResourceName() {
46         return RESOURCE_NAME;
47     }
48
49     /**
50      * Returns a list of all Tap Flows.
51      */
52     @GET
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(
59             // return fields
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,
68             // pagination
69             @QueryParam("limit") String limit,
70             @QueryParam("marker") String marker,
71             @QueryParam("page_reverse") String pageReverse) {
72
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));
85                 } else {
86                     ans.add(nsg);
87                 }
88             }
89         }
90         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronTapFlowRequest(ans)).build();
91     }
92
93     /**
94      * Creates new Tap Flow.
95      */
96     @Path("{tapServiceUUID}/flows")
97     @POST
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) {
104
105         INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
106
107         if (input.isSingleton()) {
108             NeutronTapFlow singleton = input.getSingleton();
109             singleton.setTapFlowServiceID(tapServiceUUID);
110
111             tapFlowInterface.addTapFlow(singleton);
112         } else {
113             throw new BadRequestException("Only Singleton tapFlow creation supported");
114         }
115
116         return Response.status(HttpURLConnection.HTTP_CREATED).entity(input).build();
117     }
118
119     /**
120      * Returns a specific Tap Flow.
121      */
122     @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
123     @GET
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) {
133
134         INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
135         if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
136             throw new ResourceNotFoundException("Specified UUID does not Exist");
137         }
138
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();
143         } else {
144             return Response.status(HttpURLConnection.HTTP_OK)
145                     .entity(new NeutronTapFlowRequest(tapFlow)).build();
146         }
147     }
148
149     /**
150      * Updates a Tap Flow.
151      */
152     @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
153     @PUT
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) {
162
163         INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
164
165         if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
166             throw new ResourceNotFoundException("Specified UUID does not Exist");
167         }
168
169         NeutronTapFlow singleton = input.getSingleton();
170         singleton.setTapFlowServiceID(tapServiceUUID);
171         tapFlowInterface.updateTapFlow(singleton);
172
173         return Response.status(HttpURLConnection.HTTP_OK).entity(input).build();
174     }
175
176     /**
177      * Deletes a Tap Flow.
178      */
179     @Path("{tapServiceUUID}/flows/{tapFlowUUID}")
180     @DELETE
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) {
186
187         INeutronTapFlowCRUD tapFlowInterface = getNeutronCRUD();
188
189         if (!tapFlowInterface.tapFlowExists(tapServiceUUID, tapFlowUUID)) {
190             throw new ResourceNotFoundException("Specified UUID does not Exist");
191         }
192
193         tapFlowInterface.deleteTapFlow(tapServiceUUID, tapFlowUUID);
194         return Response.status(HttpURLConnection.HTTP_NO_CONTENT).build();
195     }
196 }