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