complete dependency inject CRUD implementations
[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 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.INeutronTrunkCRUD;
29 import org.opendaylight.neutron.spi.NeutronTrunk;
30 import org.ops4j.pax.cdi.api.OsgiService;
31
32 @Singleton
33 @Path("/trunks")
34 public final class NeutronTrunksNorthbound
35         extends AbstractNeutronNorthbound<NeutronTrunk, NeutronTrunkRequest, INeutronTrunkCRUD> {
36
37     private static final String RESOURCE_NAME = "Trunk";
38
39     @Inject
40     public NeutronTrunksNorthbound(@OsgiService INeutronTrunkCRUD 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 Trunks.
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 trunk attributes
62             @QueryParam("id") String queryUUID,
63             @QueryParam("tenant_id") String queryTenantID,
64             @QueryParam("status") String queryStatus,
65             @QueryParam("name") String queryName,
66             // pagination
67             @QueryParam("limit") String limit,
68             @QueryParam("marker") String marker,
69             @QueryParam("page_reverse") String pageReverse) {
70         INeutronTrunkCRUD trunkInterface = getNeutronCRUD();
71         List<NeutronTrunk> ans = new ArrayList<>();
72         for (NeutronTrunk nsg : trunkInterface.getAll()) {
73             if ((queryUUID == null || queryUUID.equals(nsg.getID()))
74                     && (queryTenantID == null || queryTenantID.equals(nsg.getTenantID()))
75                     && (queryStatus == null || queryStatus.equals(nsg.getStatus()))
76                     && (queryName == null || queryName.equals(nsg.getName()))) {
77                 if (fields.size() > 0) {
78                     ans.add(nsg.extractFields(fields));
79                 } else {
80                     ans.add(nsg);
81                 }
82             }
83         }
84         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronTrunkRequest(ans)).build();
85     }
86
87     /**
88      * Returns a specific Trunk.
89      */
90     @Path("{trunkUUID}")
91     @GET
92     @Produces({ MediaType.APPLICATION_JSON })
93     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
94             @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
95             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
96             @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
97             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
98     public Response showTrunk(@PathParam("trunkUUID") String trunkUUID,
99             @QueryParam("fields") List<String> fields) {
100         return show(trunkUUID, fields);
101     }
102
103     /**
104      * Creates new Trunk.
105      */
106     @POST
107     @Produces({ MediaType.APPLICATION_JSON })
108     @Consumes({ MediaType.APPLICATION_JSON })
109     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
110             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
111     public Response createTrunks(final NeutronTrunkRequest input) {
112         return create(input);
113     }
114
115     /**
116      * Updates a Trunk.
117      */
118     @Path("{trunkUUID}")
119     @PUT
120     @Produces({ MediaType.APPLICATION_JSON })
121     @Consumes({ MediaType.APPLICATION_JSON })
122     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
123             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
124             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
125     public Response updateTrunk(@PathParam("trunkUUID") String trunkUUID,
126             final NeutronTrunkRequest input) {
127         return update(trunkUUID, input);
128     }
129
130     /**
131      * Deletes a Trunk.
132      */
133     @Path("{trunkUUID}")
134     @DELETE
135     @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
136             @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
137             @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
138     public Response deleteTrunk(@PathParam("trunkUUID") String trunkUUID) {
139         return delete(trunkUUID);
140     }
141 }