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