0c3ee715a16e216154a1c84daeb836881ab69816
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronTapServiceNorthbound.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.INeutronTapServiceCRUD;
28 import org.opendaylight.neutron.spi.NeutronTapService;
29
30 @Path("/tap/services")
31 public final class NeutronTapServiceNorthbound
32         extends AbstractNeutronNorthbound<NeutronTapService, NeutronTapServiceRequest, INeutronTapServiceCRUD> {
33
34     private static final String RESOURCE_NAME = "Tap Service";
35
36     @Override
37     protected String getResourceName() {
38         return RESOURCE_NAME;
39     }
40
41     /**
42      * Returns a list of all Tap Services.
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 Service attributes
54             @QueryParam("id") String queryTapServiceUUID,
55             @QueryParam("tenant_id") String queryTapServiceTenantID,
56             @QueryParam("name") String queryTapServiceName,
57             @QueryParam("port_id") String queryTapServicePortID,
58             // pagination
59             @QueryParam("limit") String limit,
60             @QueryParam("marker") String marker,
61             @QueryParam("page_reverse") String pageReverse) {
62         INeutronTapServiceCRUD tapServiceInterface = getNeutronCRUD();
63         List<NeutronTapService> ans = new ArrayList<>();
64         for (NeutronTapService nsg : tapServiceInterface.getAll()) {
65             if ((queryTapServiceUUID == null || queryTapServiceUUID.equals(nsg.getID()))
66                     && (queryTapServiceTenantID == null || queryTapServiceTenantID.equals(nsg.getTenantID()))
67                     && (queryTapServiceName == null || queryTapServiceName.equals(nsg.getName()))
68                     && (queryTapServicePortID == null || queryTapServicePortID.equals(nsg.getTapServicePortID()))) {
69                 if (fields.size() > 0) {
70                     ans.add(nsg.extractFields(fields));
71                 } else {
72                     ans.add(nsg);
73                 }
74             }
75         }
76         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronTapServiceRequest(ans)).build();
77     }
78
79     /**
80      * Returns a specific Tap Service.
81      */
82     @Path("{tapServiceUUID}")
83     @GET
84     @Produces({ MediaType.APPLICATION_JSON })
85     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
86             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
87             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
88             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
89             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
90     public Response showTapService(@PathParam("tapServiceUUID") String tapServiceUUID,
91             @QueryParam("fields") List<String> fields) {
92         return show(tapServiceUUID, fields);
93     }
94
95     /**
96      * Creates new Tap Service.
97      */
98     @POST
99     @Produces({ MediaType.APPLICATION_JSON })
100     @Consumes({ MediaType.APPLICATION_JSON })
101     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
102             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
103     public Response createTapService(final NeutronTapServiceRequest input) {
104         return create(input);
105     }
106
107     /**
108      * Updates a Tap Service.
109      */
110     @Path("{tapServiceUUID}")
111     @PUT
112     @Produces({ MediaType.APPLICATION_JSON })
113     @Consumes({ MediaType.APPLICATION_JSON })
114     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
115             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
116             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
117     public Response updateTapService(@PathParam("tapServiceUUID") String tapServiceUUID,
118             final NeutronTapServiceRequest input) {
119         return update(tapServiceUUID, input);
120     }
121
122     /**
123      * Deletes a Tap Service.
124      */
125     @Path("{tapServiceUUID}")
126     @DELETE
127     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
128             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
129             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
130     public Response deleteTapService(@PathParam("tapServiceUUID") String tapServiceUUID) {
131         return delete(tapServiceUUID);
132     }
133 }