NEUTRON-208: BGPVPN network and router association
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronBgpvpnsNorthbound.java
index 3414e1eef00859e4fd98ebc1a35ceddab8f68644..42db59a982908f584bbba205212a11103ddd6863 100644 (file)
@@ -5,13 +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;
@@ -26,67 +29,50 @@ 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.INeutronBgpvpnNetworkAssociationCRUD;
+import org.opendaylight.neutron.spi.INeutronBgpvpnRouterAssociationCRUD;
 import org.opendaylight.neutron.spi.NeutronBgpvpn;
-import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
+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
+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)
@@ -114,28 +100,20 @@ public class NeutronBgpvpnsNorthbound
         INeutronBgpvpnCRUD bgpvpnInterface = getNeutronCRUD();
         List<NeutronBgpvpn> allBgpvpns = bgpvpnInterface.getAll();
         List<NeutronBgpvpn> ans = new ArrayList<>();
-        Iterator<NeutronBgpvpn> i = allBgpvpns.iterator();
-        while (i.hasNext()) {
-            NeutronBgpvpn oSN = i.next();
+        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);
                 }
             }
         }
@@ -152,8 +130,8 @@ public class NeutronBgpvpnsNorthbound
     }
 
     /**
-     * Returns a specific Bgpvpn */
-
+     * Returns a specific Bgpvpn.
+     */
     @Path("{bgpvpnUUID}")
     @GET
     @Produces({ MediaType.APPLICATION_JSON })
@@ -170,7 +148,8 @@ public class NeutronBgpvpnsNorthbound
     }
 
     /**
-     * Creates new Bgpvpns */
+     * Creates new Bgpvpns.
+     */
     @POST
     @Produces({ MediaType.APPLICATION_JSON })
     @Consumes({ MediaType.APPLICATION_JSON })
@@ -182,7 +161,8 @@ public class NeutronBgpvpnsNorthbound
     }
 
     /**
-     * Updates a Bgpvpn */
+     * Updates a Bgpvpn.
+     */
     @Override
     protected void updateDelta(String uuid, NeutronBgpvpn delta, NeutronBgpvpn original) {
         //Fill in defaults if they're missing in update
@@ -204,14 +184,31 @@ public class NeutronBgpvpnsNorthbound
     }
 
     /**
-     * Deletes a Bgpvpn */
-
+     * Deletes a Bgpvpn.
+     */
     @Path("{bgpvpnUUID}")
     @DELETE
     @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);
     }
 }