NEUTRON-208: BGPVPN network and router association
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronNorthboundRSApplication.java
index 90e57edeea98d13a6432ec96c38cb8266708f9f6..522f1c2b5d59f07307560ca5dc279608305007b4 100644 (file)
  * 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 static java.util.Collections.emptySet;
+
+import com.google.common.collect.ImmutableSet;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.ws.rs.core.Application;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
 import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This class is an instance of javax.ws.rs.core.Application and is used to return the classes
  * that will be instantiated for JAXRS processing. This is necessary
  * because package scanning in jersey doesn't yet work in OSGi environment.
- *
  */
+@Singleton
 public final class NeutronNorthboundRSApplication extends Application {
+
+    private static final Logger LOG = LoggerFactory.getLogger(NeutronNorthboundRSApplication.class);
+
     private static final int HASHMAP_SIZE = 3;
 
+    private final NeutronNetworksNorthbound neutronNetworksNorthbound;
+    private final NeutronSubnetsNorthbound neutronSubnetsNorthbound;
+    private final NeutronPortsNorthbound neutronPortsNorthbound;
+    private final NeutronRoutersNorthbound neutronRoutersNorthbound;
+    private final NeutronFloatingIpsNorthbound neutronFloatingIpsNorthbound;
+    private final NeutronSecurityGroupsNorthbound neutronSecurityGroupsNorthbound;
+    private final NeutronSecurityRulesNorthbound neutronSecurityRulesNorthbound;
+    private final NeutronFirewallNorthbound neutronFirewallNorthbound;
+    private final NeutronFirewallPolicyNorthbound neutronFirewallPolicyNorthbound;
+    private final NeutronFirewallRulesNorthbound neutronFirewallRulesNorthbound;
+    private final NeutronLoadBalancerNorthbound neutronLoadBalancerNorthbound;
+    private final NeutronLoadBalancerListenerNorthbound neutronLoadBalancerListenerNorthbound;
+    private final NeutronLoadBalancerPoolNorthbound neutronLoadBalancerPoolNorthbound;
+    private final NeutronBgpvpnsNorthbound neutronBgpvpnsNorthbound;
+    private final NeutronL2gatewayNorthbound neutronL2gatewayNorthbound;
+    private final NeutronL2gatewayConnectionNorthbound neutronL2gatewayConnectionNorthbound;
+    private final NeutronLoadBalancerHealthMonitorNorthbound neutronLoadBalancerHealthMonitorNorthbound;
+    private final NeutronMeteringLabelsNorthbound neutronMeteringLabelsNorthbound;
+    private final NeutronMeteringLabelRulesNorthbound neutronMeteringLabelRulesNorthbound;
+    private final NeutronVpnServicesNorthbound neutronVpnServicesNorthbound;
+    private final NeutronVpnIkePoliciesNorthbound neutronVpnIkePoliciesNorthbound;
+    private final NeutronVpnIpSecPoliciesNorthbound neutronVpnIpSecPoliciesNorthbound;
+    private final NeutronVpnIpSecSiteConnectionsNorthbound neutronVpnIpSecSiteConnectionsNorthbound;
+    private final NeutronSFCFlowClassifiersNorthbound neutronSFCFlowClassifiersNorthbound;
+    private final NeutronSFCPortChainsNorthbound neutronSFCPortChainsNorthbound;
+    private final NeutronSFCPortPairGroupsNorthbound neutronSFCPortPairGroupsNorthbound;
+    private final NeutronSFCPortPairsNorthbound neutronSFCPortPairsNorthbound;
+    private final NeutronQosPolicyNorthbound neutronQosPolicyNorthbound;
+    private final NeutronTrunksNorthbound neutronTrunksNorthbound;
+    private final NeutronTapServiceNorthbound neutronTapServiceNorthbound;
+    private final NeutronTapFlowNorthbound neutronTapFlowNorthbound;
+    private final NeutronBgpvpnNetworkAssociationsNorthbound neutronBgpvpnNetworkAssociationsNorthbound;
+    private final NeutronBgpvpnRouterAssociationsNorthbound neutronBgpvpnRouterAssociationsNorthbound;
+
+    @Inject
+    public NeutronNorthboundRSApplication(
+            NeutronNetworksNorthbound neutronNetworksNorthbound,
+            NeutronSubnetsNorthbound neutronSubnetsNorthbound,
+            NeutronPortsNorthbound neutronPortsNorthbound,
+            NeutronRoutersNorthbound neutronRoutersNorthbound,
+            NeutronFloatingIpsNorthbound neutronFloatingIpsNorthbound,
+            NeutronSecurityGroupsNorthbound neutronSecurityGroupsNorthbound,
+            NeutronSecurityRulesNorthbound neutronSecurityRulesNorthbound,
+            NeutronFirewallNorthbound neutronFirewallNorthbound,
+            NeutronFirewallPolicyNorthbound neutronFirewallPolicyNorthbound,
+            NeutronFirewallRulesNorthbound neutronFirewallRulesNorthbound,
+            NeutronLoadBalancerListenerNorthbound neutronLoadBalancerListenerNorthbound,
+            NeutronLoadBalancerNorthbound neutronLoadBalancerNorthbound,
+            NeutronLoadBalancerPoolNorthbound neutronLoadBalancerPoolNorthbound,
+            NeutronBgpvpnsNorthbound neutronBgpvpnsNorthbound,
+            NeutronL2gatewayNorthbound neutronL2gatewayNorthbound,
+            NeutronL2gatewayConnectionNorthbound neutronL2gatewayConnectionNorthbound,
+            NeutronLoadBalancerHealthMonitorNorthbound neutronLoadBalancerHealthMonitorNorthbound,
+            NeutronMeteringLabelsNorthbound neutronMeteringLabelsNorthbound,
+            NeutronMeteringLabelRulesNorthbound neutronMeteringLabelRulesNorthbound,
+            NeutronVpnServicesNorthbound neutronVpnServicesNorthbound,
+            NeutronVpnIkePoliciesNorthbound neutronVpnIkePoliciesNorthbound,
+            NeutronVpnIpSecSiteConnectionsNorthbound neutronVpnIpSecSiteConnectionsNorthbound,
+            NeutronVpnIpSecPoliciesNorthbound neutronVpnIpSecPoliciesNorthbound,
+            NeutronSFCFlowClassifiersNorthbound neutronSFCFlowClassifiersNorthbound,
+            NeutronSFCPortChainsNorthbound neutronSFCPortChainsNorthbound,
+            NeutronSFCPortPairGroupsNorthbound neutronSFCPortPairGroupsNorthbound,
+            NeutronSFCPortPairsNorthbound neutronSFCPortPairsNorthbound,
+            NeutronQosPolicyNorthbound neutronQosPolicyNorthbound,
+            NeutronTrunksNorthbound neutronTrunksNorthbound,
+            NeutronTapServiceNorthbound neutronTapServiceNorthbound,
+            NeutronTapFlowNorthbound neutronTapFlowNorthbound,
+            NeutronBgpvpnNetworkAssociationsNorthbound neutronBgpvpnNetworkAssociationsNorthbound,
+            NeutronBgpvpnRouterAssociationsNorthbound neutronBgpvpnRouterAssociationsNorthbound) {
+
+        this.neutronNetworksNorthbound = neutronNetworksNorthbound;
+        this.neutronSubnetsNorthbound = neutronSubnetsNorthbound;
+        this.neutronPortsNorthbound = neutronPortsNorthbound;
+        this.neutronRoutersNorthbound = neutronRoutersNorthbound;
+        this.neutronFloatingIpsNorthbound = neutronFloatingIpsNorthbound;
+        this.neutronSecurityGroupsNorthbound = neutronSecurityGroupsNorthbound;
+        this.neutronSecurityRulesNorthbound = neutronSecurityRulesNorthbound;
+        this.neutronFirewallNorthbound = neutronFirewallNorthbound;
+        this.neutronFirewallRulesNorthbound = neutronFirewallRulesNorthbound;
+        this.neutronLoadBalancerListenerNorthbound = neutronLoadBalancerListenerNorthbound;
+        this.neutronLoadBalancerNorthbound = neutronLoadBalancerNorthbound;
+        this.neutronFirewallPolicyNorthbound = neutronFirewallPolicyNorthbound;
+        this.neutronLoadBalancerPoolNorthbound = neutronLoadBalancerPoolNorthbound;
+        this.neutronBgpvpnsNorthbound = neutronBgpvpnsNorthbound;
+        this.neutronL2gatewayNorthbound = neutronL2gatewayNorthbound;
+        this.neutronL2gatewayConnectionNorthbound = neutronL2gatewayConnectionNorthbound;
+        this.neutronLoadBalancerHealthMonitorNorthbound = neutronLoadBalancerHealthMonitorNorthbound;
+        this.neutronMeteringLabelsNorthbound = neutronMeteringLabelsNorthbound;
+        this.neutronMeteringLabelRulesNorthbound = neutronMeteringLabelRulesNorthbound;
+        this.neutronVpnServicesNorthbound = neutronVpnServicesNorthbound;
+        this.neutronVpnIkePoliciesNorthbound = neutronVpnIkePoliciesNorthbound;
+        this.neutronVpnIpSecSiteConnectionsNorthbound = neutronVpnIpSecSiteConnectionsNorthbound;
+        this.neutronVpnIpSecPoliciesNorthbound = neutronVpnIpSecPoliciesNorthbound;
+        this.neutronSFCFlowClassifiersNorthbound = neutronSFCFlowClassifiersNorthbound;
+        this.neutronSFCPortChainsNorthbound = neutronSFCPortChainsNorthbound;
+        this.neutronSFCPortPairGroupsNorthbound = neutronSFCPortPairGroupsNorthbound;
+        this.neutronSFCPortPairsNorthbound = neutronSFCPortPairsNorthbound;
+        this.neutronQosPolicyNorthbound = neutronQosPolicyNorthbound;
+        this.neutronTrunksNorthbound = neutronTrunksNorthbound;
+        this.neutronTapServiceNorthbound = neutronTapServiceNorthbound;
+        this.neutronTapFlowNorthbound = neutronTapFlowNorthbound;
+        this.neutronBgpvpnNetworkAssociationsNorthbound = neutronBgpvpnNetworkAssociationsNorthbound;
+        this.neutronBgpvpnRouterAssociationsNorthbound = neutronBgpvpnRouterAssociationsNorthbound;
+    }
+
     @Override
     public Set<Class<?>> getClasses() {
-        Set<Class<?>> classes = new HashSet<>();
-        // northbound URIs
-        classes.add(NeutronNetworksNorthbound.class);
-        classes.add(NeutronSubnetsNorthbound.class);
-        classes.add(NeutronPortsNorthbound.class);
-        classes.add(NeutronRoutersNorthbound.class);
-        classes.add(NeutronFloatingIpsNorthbound.class);
-        classes.add(NeutronSecurityGroupsNorthbound.class);
-        classes.add(NeutronSecurityRulesNorthbound.class);
-        classes.add(NeutronFirewallNorthbound.class);
-        classes.add(NeutronFirewallPolicyNorthbound.class);
-        classes.add(NeutronFirewallRulesNorthbound.class);
-        classes.add(NeutronLoadBalancerNorthbound.class);
-        classes.add(NeutronLoadBalancerListenerNorthbound.class);
-        classes.add(NeutronLoadBalancerPoolNorthbound.class);
-        classes.add(NeutronLoadBalancerHealthMonitorNorthbound.class);
-        classes.add(NeutronMeteringLabelsNorthbound.class);
-        classes.add(NeutronMeteringLabelRulesNorthbound.class);
-        classes.add(NeutronVpnServicesNorthbound.class);
-        classes.add(NeutronVpnIkePoliciesNorthbound.class);
-        classes.add(NeutronVpnIpSecPoliciesNorthbound.class);
-        classes.add(NeutronVpnIpSecSiteConnectionsNorthbound.class);
-        classes.add(NeutronBgpvpnsNorthbound.class);
-        classes.add(NeutronL2gatewayNorthbound.class);
-        classes.add(NeutronL2gatewayConnectionNorthbound.class);
-        classes.add(NeutronSFCFlowClassifiersNorthbound.class);
-        classes.add(NeutronSFCPortPairsNorthbound.class);
-        classes.add(NeutronSFCPortPairGroupsNorthbound.class);
-        classes.add(NeutronSFCPortChainsNorthbound.class);
-        classes.add(NeutronQosPolicyNorthbound.class);
-        classes.add(NeutronTrunksNorthbound.class);
-        classes.add(NeutronTapServiceNorthbound.class);
-        classes.add(NeutronTapFlowNorthbound.class);
-
-        return classes;
+        return emptySet();
     }
 
     @Override
     public Set<Object> getSingletons() {
+        return ImmutableSet.builderWithExpectedSize(34)
+                .add(getMOXyJsonProvider())
+                // Northbound URIs JAX RS Resources:
+                .add(neutronNetworksNorthbound)
+                .add(neutronSubnetsNorthbound)
+                .add(neutronPortsNorthbound)
+                .add(neutronRoutersNorthbound)
+                .add(neutronFloatingIpsNorthbound)
+                .add(neutronSecurityGroupsNorthbound)
+                .add(neutronSecurityRulesNorthbound)
+                .add(neutronFirewallNorthbound)
+                .add(neutronFirewallPolicyNorthbound)
+                .add(neutronFirewallRulesNorthbound)
+                .add(neutronLoadBalancerNorthbound)
+                .add(neutronLoadBalancerListenerNorthbound)
+                .add(neutronLoadBalancerPoolNorthbound)
+                .add(neutronLoadBalancerHealthMonitorNorthbound)
+                .add(neutronMeteringLabelsNorthbound)
+                .add(neutronMeteringLabelRulesNorthbound)
+                .add(neutronVpnServicesNorthbound)
+                .add(neutronVpnIkePoliciesNorthbound)
+                .add(neutronVpnIpSecPoliciesNorthbound)
+                .add(neutronVpnIpSecSiteConnectionsNorthbound)
+                .add(neutronBgpvpnsNorthbound)
+                .add(neutronBgpvpnNetworkAssociationsNorthbound)
+                .add(neutronBgpvpnRouterAssociationsNorthbound)
+                .add(neutronL2gatewayNorthbound)
+                .add(neutronL2gatewayConnectionNorthbound)
+                .add(neutronSFCFlowClassifiersNorthbound)
+                .add(neutronSFCPortPairsNorthbound)
+                .add(neutronSFCPortPairGroupsNorthbound)
+                .add(neutronSFCPortChainsNorthbound)
+                .add(neutronQosPolicyNorthbound)
+                .add(neutronTrunksNorthbound)
+                .add(neutronTapServiceNorthbound)
+                .add(neutronTapFlowNorthbound)
+                .add(new LoggingExceptionMapper())
+                .build();
+    }
+
+    private MOXyJsonProvider getMOXyJsonProvider() {
         MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
 
         moxyJsonProvider.setAttributePrefix("@");
@@ -81,8 +203,21 @@ public final class NeutronNorthboundRSApplication extends Application {
         moxyJsonProvider.setNamespacePrefixMapper(namespacePrefixMapper);
         moxyJsonProvider.setNamespaceSeparator(':');
 
-        Set<Object> set = new HashSet<>(1);
-        set.add(moxyJsonProvider);
-        return set;
+        return moxyJsonProvider;
+    }
+
+    // do not inline this as a lambda; for some (strange) reason, the HK2 DI thing used by Jersey (v2.25.1) does
+    // not like it: "WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
+    //  javax.ws.rs.ProcessingException: Could not find exception type for given ExceptionMapper class:
+    //  class org.opendaylight.neutron.northbound.api.NeutronNorthboundRSApplication$$Lambda$157/5987161."
+    public static class LoggingExceptionMapper implements ExceptionMapper<Exception> {
+        @Override
+        public Response toResponse(Exception exception) {
+            LOG.error("Error processing response", exception);
+            return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
+            // In order to return the exception in the Response, it would need mapping.
+            // This could be brittle, and potentially insecure to provide internal exception externally.
+            // We thus intentionally chose to only return a generic 500 with details (only) in the log.
+        }
     }
 }