NEUTRON-208: BGPVPN network and router association
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronBgpvpnsNorthbound.java
index f85c55595fb240b1f5acf92621d5b1b24fc98188..42db59a982908f584bbba205212a11103ddd6863 100644 (file)
@@ -5,14 +5,16 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.neutron.northbound.api;
 
+import com.webcohesion.enunciate.metadata.rs.ResponseCode;
+import com.webcohesion.enunciate.metadata.rs.StatusCodes;
+import com.webcohesion.enunciate.metadata.rs.TypeHint;
 import java.net.HttpURLConnection;
 import java.util.ArrayList;
-import java.util.Iterator;
 import java.util.List;
-
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.DefaultValue;
@@ -27,76 +29,57 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
-
-import org.codehaus.enunciate.jaxrs.ResponseCode;
-import org.codehaus.enunciate.jaxrs.StatusCodes;
-import org.codehaus.enunciate.jaxrs.TypeHint;
+import org.apache.aries.blueprint.annotation.service.Reference;
 import org.opendaylight.neutron.spi.INeutronBgpvpnCRUD;
-import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
+import org.opendaylight.neutron.spi.INeutronBgpvpnNetworkAssociationCRUD;
+import org.opendaylight.neutron.spi.INeutronBgpvpnRouterAssociationCRUD;
 import org.opendaylight.neutron.spi.NeutronBgpvpn;
+import org.opendaylight.neutron.spi.NeutronBgpvpnNetworkAssociation;
+import org.opendaylight.neutron.spi.NeutronBgpvpnRouterAssociation;
+
 
 /**
- * Neutron Northbound REST APIs for Bgpvpn.<br>
- * This class provides REST APIs for managing neutron Bgpvpns
- *
- * <br>
- * <br>
- * Authentication scheme : <b>HTTP Basic</b><br>
- * Authentication realm : <b>opendaylight</b><br>
- * Transport : <b>HTTP and HTTPS</b><br>
- * <br>
- * HTTPS Authentication is disabled by default. Administrator can enable it in
- * tomcat-server.xml after adding a proper keystore / SSL certificate from a
- * trusted authority.<br>
- * More info :
- * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
- *
+ * Neutron Northbound REST APIs for Bgpvpn.
  */
-
+@Singleton
 @Path("/bgpvpns")
-public class NeutronBgpvpnsNorthbound
-    extends AbstractNeutronNorthbound<NeutronBgpvpn, NeutronBgpvpnRequest, INeutronBgpvpnCRUD> {
+public final class NeutronBgpvpnsNorthbound
+        extends AbstractNeutronNorthbound<NeutronBgpvpn, NeutronBgpvpnRequest, INeutronBgpvpnCRUD> {
+
+    private static final String RESOURCE_NAME = "Bgpvpn";
 
     @Context
     UriInfo uriInfo;
 
-    private static final String RESOURCE_NAME = "Bgpvpn";
 
-    @Override
-    protected String getResourceName() {
-        return RESOURCE_NAME;
-    }
+    INeutronBgpvpnNetworkAssociationCRUD neutronBgpvpnNetworkAssociation;
+    INeutronBgpvpnRouterAssociationCRUD neutronBgpvpnRouterAssociation;
 
-    @Override
-    protected NeutronBgpvpn extractFields(NeutronBgpvpn o, List<String> fields) {
-        return o.extractFields(fields);
-    }
+    @Inject
+    public NeutronBgpvpnsNorthbound(@Reference INeutronBgpvpnCRUD neutronCRUD,
+                                    INeutronBgpvpnNetworkAssociationCRUD neutronBgpvpnNetworkAssociation,
+                                    INeutronBgpvpnRouterAssociationCRUD neutronBgpvpnRouterAssociation) {
+        super(neutronCRUD);
+        this.neutronBgpvpnNetworkAssociation = neutronBgpvpnNetworkAssociation;
+        this.neutronBgpvpnRouterAssociation = neutronBgpvpnRouterAssociation;
 
-    @Override
-    protected NeutronBgpvpnRequest newNeutronRequest(NeutronBgpvpn o) {
-        return new NeutronBgpvpnRequest(o);
     }
 
     @Override
-    protected INeutronBgpvpnCRUD getNeutronCRUD() {
-        NeutronCRUDInterfaces answer = new NeutronCRUDInterfaces().fetchINeutronBgpvpnCRUD(this);
-        if (answer.getBgpvpnInterface() == null) {
-            throw new ServiceUnavailableException(serviceUnavailable());
-        }
-        return answer.getBgpvpnInterface();
+    protected String getResourceName() {
+        return RESOURCE_NAME;
     }
 
     /**
-     * Returns a list of all Bgpvpns */
-
+     * Returns a list of all Bgpvpns.
+     */
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackBgpvpns.class)
-    @StatusCodes({
-        @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
-        @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response listBgpvpns(
             // return fields
             @QueryParam("fields") List<String> fields,
@@ -112,87 +95,76 @@ public class NeutronBgpvpnsNorthbound
             @QueryParam("limit") Integer limit,
             @QueryParam("marker") String marker,
             @DefaultValue("false") @QueryParam("page_reverse") Boolean pageReverse
-            // sorting not supported
-            ) {
+    // sorting not supported
+    ) {
         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
         List<NeutronBgpvpn> allBgpvpns = bgpvpnInterface.getAll();
-        List<NeutronBgpvpn> ans = new ArrayList<NeutronBgpvpn>();
-        Iterator<NeutronBgpvpn> i = allBgpvpns.iterator();
-        while (i.hasNext()) {
-            NeutronBgpvpn oSN = i.next();
+        List<NeutronBgpvpn> ans = new ArrayList<>();
+        for (NeutronBgpvpn bgpvpn : allBgpvpns) {
             //match filters: TODO provider extension
-            Boolean bAdminStateUp = null;
-            Boolean bAutoAggregate = null;
-            if (queryAdminStateUp != null) {
-                bAdminStateUp = Boolean.valueOf(queryAdminStateUp);
-            }
-            if (queryAutoAggregate != null) {
-                bAutoAggregate = Boolean.valueOf(queryAutoAggregate);
-            }
-            if ((queryID == null || queryID.equals(oSN.getID())) &&
-                    (queryName == null || queryName.equals(oSN.getBgpvpnName())) &&
-                    (bAdminStateUp == null || bAdminStateUp.booleanValue() == oSN.isAdminStateUp()) &&
-                    (queryStatus == null || queryStatus.equals(oSN.getStatus())) &&
-                    (bAutoAggregate == null || bAutoAggregate.booleanValue() == oSN.isAutoAggregate()) &&
-                    (queryTenantID == null || queryTenantID.equals(oSN.getTenantID()))) {
+            Boolean adminStateUp = queryAdminStateUp != null ? Boolean.valueOf(queryAdminStateUp) : null;
+            Boolean autoAggregate = queryAutoAggregate != null ? Boolean.valueOf(queryAutoAggregate) : null;
+            if ((queryID == null || queryID.equals(bgpvpn.getID()))
+                    && (queryName == null || queryName.equals(bgpvpn.getName()))
+                    && (adminStateUp == null || adminStateUp.booleanValue() == bgpvpn.isAdminStateUp())
+                    && (queryStatus == null || queryStatus.equals(bgpvpn.getStatus()))
+                    && (autoAggregate == null || autoAggregate.booleanValue() == bgpvpn.isAutoAggregate())
+                    && (queryTenantID == null || queryTenantID.equals(bgpvpn.getTenantID()))) {
                 if (fields.size() > 0) {
-                    ans.add(extractFields(oSN,fields));
+                    ans.add(bgpvpn.extractFields(fields));
                 } else {
-                    ans.add(oSN);
+                    ans.add(bgpvpn);
                 }
             }
         }
 
         if (limit != null && ans.size() > 1) {
             // Return a paginated request
-            NeutronBgpvpnRequest request = (NeutronBgpvpnRequest) PaginatedRequestFactory.createRequest(limit,
-                    marker, pageReverse, uriInfo, ans, NeutronBgpvpn.class);
+            NeutronBgpvpnRequest request = (NeutronBgpvpnRequest) PaginatedRequestFactory.createRequest(limit, marker,
+                    pageReverse, uriInfo, ans, NeutronBgpvpn.class);
             return Response.status(HttpURLConnection.HTTP_OK).entity(request).build();
         }
 
-    return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
+        return Response.status(HttpURLConnection.HTTP_OK).entity(new NeutronBgpvpnRequest(ans)).build();
 
     }
 
     /**
-     * Returns a specific Bgpvpn */
-
+     * Returns a specific Bgpvpn.
+     */
     @Path("{bgpvpnUUID}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackBgpvpns.class)
-    @StatusCodes({
-        @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
-        @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
-        @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response showBgpvpn(
-            @PathParam("bgpvpnUUID") String bgpvpnUUID,
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAUTHORIZED, condition = "Unauthorized"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_IMPLEMENTED, condition = "Not Implemented"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    public Response showBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID,
             // return fields
-            @QueryParam("fields") List<String> fields
-            ) {
+            @QueryParam("fields") List<String> fields) {
         return show(bgpvpnUUID, fields);
     }
 
     /**
-     * Creates new Bgpvpns */
+     * Creates new Bgpvpns.
+     */
     @POST
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     @TypeHint(NeutronBgpvpn.class)
-    @StatusCodes({
-        @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_CREATED, condition = "Created"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
     public Response createBgpvpns(final NeutronBgpvpnRequest input) {
         return create(input);
     }
 
     /**
-     * Updates a Bgpvpn */
+     * Updates a Bgpvpn.
+     */
     @Override
-    protected void updateDelta(String uuid,
-                               NeutronBgpvpn delta, NeutronBgpvpn original) {
+    protected void updateDelta(String uuid, NeutronBgpvpn delta, NeutronBgpvpn original) {
         //Fill in defaults if they're missing in update
         delta.initDefaults();
         delta.setID(uuid);
@@ -204,25 +176,39 @@ public class NeutronBgpvpnsNorthbound
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
     //@TypeHint(OpenStackBgpvpns.class)
-    @StatusCodes({
-        @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response updateBgpvpn(
-            @PathParam("bgpvpnUUID") String bgpvpnUUID, final NeutronBgpvpnRequest input
-            ) {
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_OK, condition = "Operation successful"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    public Response updateBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID, final NeutronBgpvpnRequest input) {
         return update(bgpvpnUUID, input);
     }
 
     /**
-     * Deletes a Bgpvpn */
-
+     * Deletes a Bgpvpn.
+     */
     @Path("{bgpvpnUUID}")
     @DELETE
-    @StatusCodes({
-        @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
-        @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
-    public Response deleteBgpvpn(
-            @PathParam("bgpvpnUUID") String bgpvpnUUID) {
+    @StatusCodes({ @ResponseCode(code = HttpURLConnection.HTTP_NO_CONTENT, condition = "No Content"),
+            @ResponseCode(code = HttpURLConnection.HTTP_NOT_FOUND, condition = "Not Found"),
+            @ResponseCode(code = HttpURLConnection.HTTP_UNAVAILABLE, condition = "No providers available") })
+    public Response deleteBgpvpn(@PathParam("bgpvpnUUID") String bgpvpnUUID) {
+        NeutronBgpvpnNetworkAssociationsNorthbound netAssoNorthBound =
+                new NeutronBgpvpnNetworkAssociationsNorthbound(neutronBgpvpnNetworkAssociation);
+        List<NeutronBgpvpnNetworkAssociation> allBgpvpnNetAssos = neutronBgpvpnNetworkAssociation.getAll();
+        for (NeutronBgpvpnNetworkAssociation bgpvpnNetAsso : allBgpvpnNetAssos) {
+            if (bgpvpnUUID != null && bgpvpnUUID.equals(bgpvpnNetAsso.getBgpvpnId())) {
+                netAssoNorthBound.delete(bgpvpnNetAsso.getID());
+            }
+        }
+
+        NeutronBgpvpnRouterAssociationsNorthbound routeAssoNorthBound =
+                new NeutronBgpvpnRouterAssociationsNorthbound(neutronBgpvpnRouterAssociation);
+        List<NeutronBgpvpnRouterAssociation> allBgpvpnRouteAssos = neutronBgpvpnRouterAssociation.getAll();
+        for (NeutronBgpvpnRouterAssociation bgpvpnRouteAsso : allBgpvpnRouteAssos) {
+            if (bgpvpnUUID != null && bgpvpnUUID.equals(bgpvpnRouteAsso.getBgpvpnId())) {
+                routeAssoNorthBound.delete(bgpvpnRouteAsso.getID());
+            }
+        }
         return delete(bgpvpnUUID);
     }
 }