Bump upstream versions
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronBgpvpnsNorthbound.java
1 /*
2  * Copyright (c) 2015 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 com.webcohesion.enunciate.metadata.rs.ResponseCode;
11 import com.webcohesion.enunciate.metadata.rs.StatusCodes;
12 import com.webcohesion.enunciate.metadata.rs.TypeHint;
13 import java.net.HttpURLConnection;
14 import java.util.ArrayList;
15 import java.util.List;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.DELETE;
20 import javax.ws.rs.DefaultValue;
21 import javax.ws.rs.GET;
22 import javax.ws.rs.POST;
23 import javax.ws.rs.PUT;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.PathParam;
26 import javax.ws.rs.Produces;
27 import javax.ws.rs.QueryParam;
28 import javax.ws.rs.core.Context;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.core.UriInfo;
32 import org.apache.aries.blueprint.annotation.service.Reference;
33 import org.opendaylight.neutron.spi.INeutronBgpvpnCRUD;
34 import org.opendaylight.neutron.spi.INeutronBgpvpnNetworkAssociationCRUD;
35 import org.opendaylight.neutron.spi.INeutronBgpvpnRouterAssociationCRUD;
36 import org.opendaylight.neutron.spi.NeutronBgpvpn;
37 import org.opendaylight.neutron.spi.NeutronBgpvpnNetworkAssociation;
38 import org.opendaylight.neutron.spi.NeutronBgpvpnRouterAssociation;
39
40 /**
41  * Neutron Northbound REST APIs for Bgpvpn.
42  */
43 @Singleton
44 @Path("/bgpvpns")
45 public final class NeutronBgpvpnsNorthbound
46         extends AbstractNeutronNorthbound<NeutronBgpvpn, NeutronBgpvpnRequest, INeutronBgpvpnCRUD> {
47     private static final String RESOURCE_NAME = "Bgpvpn";
48
49     @Context
50     UriInfo uriInfo;
51
52     INeutronBgpvpnNetworkAssociationCRUD neutronBgpvpnNetworkAssociation;
53     INeutronBgpvpnRouterAssociationCRUD neutronBgpvpnRouterAssociation;
54
55     @Inject
56     public NeutronBgpvpnsNorthbound(@Reference INeutronBgpvpnCRUD neutronCRUD,
57                                     INeutronBgpvpnNetworkAssociationCRUD neutronBgpvpnNetworkAssociation,
58                                     INeutronBgpvpnRouterAssociationCRUD neutronBgpvpnRouterAssociation) {
59         super(neutronCRUD);
60         this.neutronBgpvpnNetworkAssociation = neutronBgpvpnNetworkAssociation;
61         this.neutronBgpvpnRouterAssociation = neutronBgpvpnRouterAssociation;
62     }
63
64     @Override
65     protected String getResourceName() {
66         return RESOURCE_NAME;
67     }
68
69     /**
70      * Returns a list of all Bgpvpns.
71      */
72     @GET
73     @Produces({ MediaType.APPLICATION_JSON })
74     //@TypeHint(OpenStackBgpvpns.class)
75     @StatusCodes({
76         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
77         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
78         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
79         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available")
80     })
81     public Response listBgpvpns(
82             // return fields
83             @QueryParam("fields") List<String> fields,
84             // note: openstack isn't clear about filtering on lists, so we aren't handling them
85             @QueryParam("id") String queryID,
86             @QueryParam("name") String queryName,
87             @QueryParam("admin_state_up") String queryAdminStateUp,
88             @QueryParam("status") String queryStatus,
89             @QueryParam("tenant_id") String queryTenantID,
90             @QueryParam("type") String queryType,
91             @QueryParam("auto_aggregate") String queryAutoAggregate,
92             // pagination
93             @QueryParam("limit") Integer limit,
94             @QueryParam("marker") String marker,
95             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
96     // sorting not supported
97     ) {
98         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
99         List<NeutronBgpvpn> allBgpvpns = bgpvpnInterface.getAll();
100         List<NeutronBgpvpn> ans = new ArrayList<>();
101         for (NeutronBgpvpn bgpvpn : allBgpvpns) {
102             //match filters: TODO provider extension
103             Boolean adminStateUp = queryAdminStateUp != null ? Boolean.valueOf(queryAdminStateUp) : null;
104             Boolean autoAggregate = queryAutoAggregate != null ? Boolean.valueOf(queryAutoAggregate) : null;
105             if ((queryID == null || queryID.equals(bgpvpn.getID()))
106                     && (queryName == null || queryName.equals(bgpvpn.getName()))
107                     && (adminStateUp == null || adminStateUp.booleanValue() == bgpvpn.isAdminStateUp())
108                     && (queryStatus == null || queryStatus.equals(bgpvpn.getStatus()))
109                     && (autoAggregate == null || autoAggregate.booleanValue() == bgpvpn.isAutoAggregate())
110                     && (queryTenantID == null || queryTenantID.equals(bgpvpn.getTenantID()))) {
111                 if (fields.size() > 0) {
112                     ans.add(bgpvpn.extractFields(fields));
113                 } else {
114                     ans.add(bgpvpn);
115                 }
116             }
117         }
118
119         if (limit != null && ans.size() > 1) {
120             // Return a paginated request
121             NeutronBgpvpnRequest request = (NeutronBgpvpnRequest) PaginatedRequestFactory.createRequest(limit, marker,
122                     pageReverse, uriInfo, ans, NeutronBgpvpn.class);
123             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
124         }
125
126         return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
127     }
128
129     /**
130      * Returns a specific Bgpvpn.
131      */
132     @Path("{bgpvpnUUID}")
133     @GET
134     @Produces({ MediaType.APPLICATION_JSON })
135     //@TypeHint(OpenStackBgpvpns.class)
136     @StatusCodes({
137         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
138         @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
139         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
140         @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
141         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available")
142     })
143     public Response showBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID,
144             // return fields
145             @QueryParam("fields") List<String> fields) {
146         return show(bgpvpnUUID, fields);
147     }
148
149     /**
150      * Creates new Bgpvpns.
151      */
152     @POST
153     @Produces({ MediaType.APPLICATION_JSON })
154     @Consumes({ MediaType.APPLICATION_JSON })
155     @TypeHint(NeutronBgpvpn.class)
156     @StatusCodes({
157         @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
158         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available")
159     })
160     public Response createBgpvpns(final NeutronBgpvpnRequest input) {
161         return create(input);
162     }
163
164     /**
165      * Updates a Bgpvpn.
166      */
167     @Override
168     protected void updateDelta(String uuid, NeutronBgpvpn delta, NeutronBgpvpn original) {
169         //Fill in defaults if they're missing in update
170         delta.initDefaults();
171         delta.setID(uuid);
172         delta.setTenantID(original.getTenantID());
173     }
174
175     @Path("{bgpvpnUUID}")
176     @PUT
177     @Produces({ MediaType.APPLICATION_JSON })
178     @Consumes({ MediaType.APPLICATION_JSON })
179     //@TypeHint(OpenStackBgpvpns.class)
180     @StatusCodes({
181         @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
182         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
183         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available")
184     })
185     public Response updateBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID, final NeutronBgpvpnRequest input) {
186         return update(bgpvpnUUID, input);
187     }
188
189     /**
190      * Deletes a Bgpvpn.
191      */
192     @Path("{bgpvpnUUID}")
193     @DELETE
194     @StatusCodes({
195         @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
196         @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
197         @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available")
198     })
199     public Response deleteBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID) {
200         NeutronBgpvpnNetworkAssociationsNorthbound netAssoNorthBound =
201                 new NeutronBgpvpnNetworkAssociationsNorthbound(neutronBgpvpnNetworkAssociation);
202         List<NeutronBgpvpnNetworkAssociation> allBgpvpnNetAssos = neutronBgpvpnNetworkAssociation.getAll();
203         for (NeutronBgpvpnNetworkAssociation bgpvpnNetAsso : allBgpvpnNetAssos) {
204             if (bgpvpnUUID != null && bgpvpnUUID.equals(bgpvpnNetAsso.getBgpvpnId())) {
205                 netAssoNorthBound.delete(bgpvpnNetAsso.getID());
206             }
207         }
208
209         NeutronBgpvpnRouterAssociationsNorthbound routeAssoNorthBound =
210                 new NeutronBgpvpnRouterAssociationsNorthbound(neutronBgpvpnRouterAssociation);
211         List<NeutronBgpvpnRouterAssociation> allBgpvpnRouteAssos = neutronBgpvpnRouterAssociation.getAll();
212         for (NeutronBgpvpnRouterAssociation bgpvpnRouteAsso : allBgpvpnRouteAssos) {
213             if (bgpvpnUUID != null && bgpvpnUUID.equals(bgpvpnRouteAsso.getBgpvpnId())) {
214                 routeAssoNorthBound.delete(bgpvpnRouteAsso.getID());
215             }
216         }
217         return delete(bgpvpnUUID);
218     }
219 }