b5fcf71961b15bd3773a145e0d5fe576637dbec0
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronTrunksNorthbound.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. 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.INeutronTrunkCRUD;
28 import org.opendaylight.neutron.spi.NeutronTrunk;
29
30 @Path("/trunks")
31 public final class NeutronTrunksNorthbound
32         extends AbstractNeutronNorthbound<NeutronTrunk, NeutronTrunkRequest, INeutronTrunkCRUD> {
33
34     private static final String RESOURCE_NAME = "Trunk";
35
36     @Override
37     protected String getResourceName() {
38         return RESOURCE_NAME;
39     }
40
41     /**
42      * Returns a list of all Trunks.
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 trunk attributes
54             @QueryParam("id") String queryUUID,
55             @QueryParam("tenant_id") String queryTenantID,
56             @QueryParam("status") String queryStatus,
57             @QueryParam("name") String queryName,
58             // pagination
59             @QueryParam("limit") String limit,
60             @QueryParam("marker") String marker,
61             @QueryParam("page_reverse") String pageReverse) {
62         INeutronTrunkCRUD trunkInterface = getNeutronCRUD();
63         List<NeutronTrunk> ans = new ArrayList<>();
64         for (NeutronTrunk nsg : trunkInterface.getAll()) {
65             if ((queryUUID == null || queryUUID.equals(nsg.getID()))
66                     && (queryTenantID == null || queryTenantID.equals(nsg.getTenantID()))
67                     && (queryStatus == null || queryStatus.equals(nsg.getStatus()))
68                     && (queryName == null || queryName.equals(nsg.getName()))) {
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 NeutronTrunkRequest(ans)).build();
77     }
78
79     /**
80      * Returns a specific Trunk.
81      */
82     @Path("{trunkUUID}")
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 showTrunk(@PathParam("trunkUUID") String trunkUUID,
91             @QueryParam("fields") List<String> fields) {
92         return show(trunkUUID, fields);
93     }
94
95     /**
96      * Creates new Trunk.
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 createTrunks(final NeutronTrunkRequest input) {
104         return create(input);
105     }
106
107     /**
108      * Updates a Trunk.
109      */
110     @Path("{trunkUUID}")
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 updateTrunk(@PathParam("trunkUUID") String trunkUUID,
118             final NeutronTrunkRequest input) {
119         return update(trunkUUID, input);
120     }
121
122     /**
123      * Deletes a Trunk.
124      */
125     @Path("{trunkUUID}")
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 deleteTrunk(@PathParam("trunkUUID") String trunkUUID) {
131         return delete(trunkUUID);
132     }
133 }