Add an exception mapper
[neutron.git] / northbound-api / src / main / java / org / opendaylight / neutron / northbound / api / NeutronNorthboundRSApplication.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation 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 static java.util.Collections.emptySet;
11
12 import com.google.common.collect.ImmutableSet;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Set;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import javax.ws.rs.core.Application;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.ext.ExceptionMapper;
21 import org.eclipse.persistence.jaxb.rs.MOXyJsonProvider;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * This class is an instance of javax.ws.rs.core.Application and is used to return the classes
27  * that will be instantiated for JAXRS processing. This is necessary
28  * because package scanning in jersey doesn't yet work in OSGi environment.
29  */
30 @Singleton
31 public final class NeutronNorthboundRSApplication extends Application {
32
33     private static final Logger LOG = LoggerFactory.getLogger(NeutronNorthboundRSApplication.class);
34
35     private static final int HASHMAP_SIZE = 3;
36
37     private final NeutronNetworksNorthbound neutronNetworksNorthbound;
38     private final NeutronSubnetsNorthbound neutronSubnetsNorthbound;
39     private final NeutronPortsNorthbound neutronPortsNorthbound;
40     private final NeutronRoutersNorthbound neutronRoutersNorthbound;
41     private final NeutronFloatingIpsNorthbound neutronFloatingIpsNorthbound;
42     private final NeutronSecurityGroupsNorthbound neutronSecurityGroupsNorthbound;
43     private final NeutronSecurityRulesNorthbound neutronSecurityRulesNorthbound;
44     private final NeutronFirewallNorthbound neutronFirewallNorthbound;
45     private final NeutronFirewallPolicyNorthbound neutronFirewallPolicyNorthbound;
46     private final NeutronFirewallRulesNorthbound neutronFirewallRulesNorthbound;
47     private final NeutronLoadBalancerNorthbound neutronLoadBalancerNorthbound;
48     private final NeutronLoadBalancerListenerNorthbound neutronLoadBalancerListenerNorthbound;
49     private final NeutronLoadBalancerPoolNorthbound neutronLoadBalancerPoolNorthbound;
50     private final NeutronBgpvpnsNorthbound neutronBgpvpnsNorthbound;
51     private final NeutronL2gatewayNorthbound neutronL2gatewayNorthbound;
52     private final NeutronL2gatewayConnectionNorthbound neutronL2gatewayConnectionNorthbound;
53     private final NeutronLoadBalancerHealthMonitorNorthbound neutronLoadBalancerHealthMonitorNorthbound;
54     private final NeutronMeteringLabelsNorthbound neutronMeteringLabelsNorthbound;
55     private final NeutronMeteringLabelRulesNorthbound neutronMeteringLabelRulesNorthbound;
56     private final NeutronVpnServicesNorthbound neutronVpnServicesNorthbound;
57     private final NeutronVpnIkePoliciesNorthbound neutronVpnIkePoliciesNorthbound;
58     private final NeutronVpnIpSecPoliciesNorthbound neutronVpnIpSecPoliciesNorthbound;
59     private final NeutronVpnIpSecSiteConnectionsNorthbound neutronVpnIpSecSiteConnectionsNorthbound;
60     private final NeutronSFCFlowClassifiersNorthbound neutronSFCFlowClassifiersNorthbound;
61     private final NeutronSFCPortChainsNorthbound neutronSFCPortChainsNorthbound;
62     private final NeutronSFCPortPairGroupsNorthbound neutronSFCPortPairGroupsNorthbound;
63     private final NeutronSFCPortPairsNorthbound neutronSFCPortPairsNorthbound;
64     private final NeutronQosPolicyNorthbound neutronQosPolicyNorthbound;
65     private final NeutronTrunksNorthbound neutronTrunksNorthbound;
66     private final NeutronTapServiceNorthbound neutronTapServiceNorthbound;
67     private final NeutronTapFlowNorthbound neutronTapFlowNorthbound;
68
69     @Inject
70     public NeutronNorthboundRSApplication(
71             NeutronNetworksNorthbound neutronNetworksNorthbound,
72             NeutronSubnetsNorthbound neutronSubnetsNorthbound,
73             NeutronPortsNorthbound neutronPortsNorthbound,
74             NeutronRoutersNorthbound neutronRoutersNorthbound,
75             NeutronFloatingIpsNorthbound neutronFloatingIpsNorthbound,
76             NeutronSecurityGroupsNorthbound neutronSecurityGroupsNorthbound,
77             NeutronSecurityRulesNorthbound neutronSecurityRulesNorthbound,
78             NeutronFirewallNorthbound neutronFirewallNorthbound,
79             NeutronFirewallPolicyNorthbound neutronFirewallPolicyNorthbound,
80             NeutronFirewallRulesNorthbound neutronFirewallRulesNorthbound,
81             NeutronLoadBalancerListenerNorthbound neutronLoadBalancerListenerNorthbound,
82             NeutronLoadBalancerNorthbound neutronLoadBalancerNorthbound,
83             NeutronLoadBalancerPoolNorthbound neutronLoadBalancerPoolNorthbound,
84             NeutronBgpvpnsNorthbound neutronBgpvpnsNorthbound,
85             NeutronL2gatewayNorthbound neutronL2gatewayNorthbound,
86             NeutronL2gatewayConnectionNorthbound neutronL2gatewayConnectionNorthbound,
87             NeutronLoadBalancerHealthMonitorNorthbound neutronLoadBalancerHealthMonitorNorthbound,
88             NeutronMeteringLabelsNorthbound neutronMeteringLabelsNorthbound,
89             NeutronMeteringLabelRulesNorthbound neutronMeteringLabelRulesNorthbound,
90             NeutronVpnServicesNorthbound neutronVpnServicesNorthbound,
91             NeutronVpnIkePoliciesNorthbound neutronVpnIkePoliciesNorthbound,
92             NeutronVpnIpSecSiteConnectionsNorthbound neutronVpnIpSecSiteConnectionsNorthbound,
93             NeutronVpnIpSecPoliciesNorthbound neutronVpnIpSecPoliciesNorthbound,
94             NeutronSFCFlowClassifiersNorthbound neutronSFCFlowClassifiersNorthbound,
95             NeutronSFCPortChainsNorthbound neutronSFCPortChainsNorthbound,
96             NeutronSFCPortPairGroupsNorthbound neutronSFCPortPairGroupsNorthbound,
97             NeutronSFCPortPairsNorthbound neutronSFCPortPairsNorthbound,
98             NeutronQosPolicyNorthbound neutronQosPolicyNorthbound,
99             NeutronTrunksNorthbound neutronTrunksNorthbound,
100             NeutronTapServiceNorthbound neutronTapServiceNorthbound,
101             NeutronTapFlowNorthbound neutronTapFlowNorthbound) {
102
103         this.neutronNetworksNorthbound = neutronNetworksNorthbound;
104         this.neutronSubnetsNorthbound = neutronSubnetsNorthbound;
105         this.neutronPortsNorthbound = neutronPortsNorthbound;
106         this.neutronRoutersNorthbound = neutronRoutersNorthbound;
107         this.neutronFloatingIpsNorthbound = neutronFloatingIpsNorthbound;
108         this.neutronSecurityGroupsNorthbound = neutronSecurityGroupsNorthbound;
109         this.neutronSecurityRulesNorthbound = neutronSecurityRulesNorthbound;
110         this.neutronFirewallNorthbound = neutronFirewallNorthbound;
111         this.neutronFirewallRulesNorthbound = neutronFirewallRulesNorthbound;
112         this.neutronLoadBalancerListenerNorthbound = neutronLoadBalancerListenerNorthbound;
113         this.neutronLoadBalancerNorthbound = neutronLoadBalancerNorthbound;
114         this.neutronFirewallPolicyNorthbound = neutronFirewallPolicyNorthbound;
115         this.neutronLoadBalancerPoolNorthbound = neutronLoadBalancerPoolNorthbound;
116         this.neutronBgpvpnsNorthbound = neutronBgpvpnsNorthbound;
117         this.neutronL2gatewayNorthbound = neutronL2gatewayNorthbound;
118         this.neutronL2gatewayConnectionNorthbound = neutronL2gatewayConnectionNorthbound;
119         this.neutronLoadBalancerHealthMonitorNorthbound = neutronLoadBalancerHealthMonitorNorthbound;
120         this.neutronMeteringLabelsNorthbound = neutronMeteringLabelsNorthbound;
121         this.neutronMeteringLabelRulesNorthbound = neutronMeteringLabelRulesNorthbound;
122         this.neutronVpnServicesNorthbound = neutronVpnServicesNorthbound;
123         this.neutronVpnIkePoliciesNorthbound = neutronVpnIkePoliciesNorthbound;
124         this.neutronVpnIpSecSiteConnectionsNorthbound = neutronVpnIpSecSiteConnectionsNorthbound;
125         this.neutronVpnIpSecPoliciesNorthbound = neutronVpnIpSecPoliciesNorthbound;
126         this.neutronSFCFlowClassifiersNorthbound = neutronSFCFlowClassifiersNorthbound;
127         this.neutronSFCPortChainsNorthbound = neutronSFCPortChainsNorthbound;
128         this.neutronSFCPortPairGroupsNorthbound = neutronSFCPortPairGroupsNorthbound;
129         this.neutronSFCPortPairsNorthbound = neutronSFCPortPairsNorthbound;
130         this.neutronQosPolicyNorthbound = neutronQosPolicyNorthbound;
131         this.neutronTrunksNorthbound = neutronTrunksNorthbound;
132         this.neutronTapServiceNorthbound = neutronTapServiceNorthbound;
133         this.neutronTapFlowNorthbound = neutronTapFlowNorthbound;
134     }
135
136     @Override
137     public Set<Class<?>> getClasses() {
138         return emptySet();
139     }
140
141     @Override
142     public Set<Object> getSingletons() {
143         return ImmutableSet.builderWithExpectedSize(32)
144                 .add(getMOXyJsonProvider())
145                 // Northbound URIs JAX RS Resources:
146                 .add(neutronNetworksNorthbound)
147                 .add(neutronSubnetsNorthbound)
148                 .add(neutronPortsNorthbound)
149                 .add(neutronRoutersNorthbound)
150                 .add(neutronFloatingIpsNorthbound)
151                 .add(neutronSecurityGroupsNorthbound)
152                 .add(neutronSecurityRulesNorthbound)
153                 .add(neutronFirewallNorthbound)
154                 .add(neutronFirewallPolicyNorthbound)
155                 .add(neutronFirewallRulesNorthbound)
156                 .add(neutronLoadBalancerNorthbound)
157                 .add(neutronLoadBalancerListenerNorthbound)
158                 .add(neutronLoadBalancerPoolNorthbound)
159                 .add(neutronLoadBalancerHealthMonitorNorthbound)
160                 .add(neutronMeteringLabelsNorthbound)
161                 .add(neutronMeteringLabelRulesNorthbound)
162                 .add(neutronVpnServicesNorthbound)
163                 .add(neutronVpnIkePoliciesNorthbound)
164                 .add(neutronVpnIpSecPoliciesNorthbound)
165                 .add(neutronVpnIpSecSiteConnectionsNorthbound)
166                 .add(neutronBgpvpnsNorthbound)
167                 .add(neutronL2gatewayNorthbound)
168                 .add(neutronL2gatewayConnectionNorthbound)
169                 .add(neutronSFCFlowClassifiersNorthbound)
170                 .add(neutronSFCPortPairsNorthbound)
171                 .add(neutronSFCPortPairGroupsNorthbound)
172                 .add(neutronSFCPortChainsNorthbound)
173                 .add(neutronQosPolicyNorthbound)
174                 .add(neutronTrunksNorthbound)
175                 .add(neutronTapServiceNorthbound)
176                 .add(neutronTapFlowNorthbound)
177                 .add(new LoggingExceptionMapper())
178                 .build();
179     }
180
181     private MOXyJsonProvider getMOXyJsonProvider() {
182         MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
183
184         moxyJsonProvider.setAttributePrefix("@");
185         moxyJsonProvider.setFormattedOutput(true);
186         moxyJsonProvider.setIncludeRoot(false);
187         moxyJsonProvider.setMarshalEmptyCollections(true);
188         moxyJsonProvider.setValueWrapper("$");
189
190         Map<String, String> namespacePrefixMapper = new HashMap<>(HASHMAP_SIZE);
191         // FIXME: fill in next two with XSD
192         namespacePrefixMapper.put("router", "router");
193         namespacePrefixMapper.put("provider", "provider");
194         namespacePrefixMapper.put("binding", "binding");
195         moxyJsonProvider.setNamespacePrefixMapper(namespacePrefixMapper);
196         moxyJsonProvider.setNamespaceSeparator(':');
197
198         return moxyJsonProvider;
199     }
200
201     // do not inline this as a lambda; for some (strange) reason, the HK2 DI thing used by Jersey (v2.25.1) does
202     // not like it: "WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
203     //  javax.ws.rs.ProcessingException: Could not find exception type for given ExceptionMapper class:
204     //  class org.opendaylight.neutron.northbound.api.NeutronNorthboundRSApplication$$Lambda$157/5987161."
205     public static class LoggingExceptionMapper implements ExceptionMapper<Exception> {
206         @Override
207         public Response toResponse(Exception exception) {
208             LOG.error("Error processing response", exception);
209             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
210             // In order to return the exception in the Response, it would need mapping.
211             // This could be brittle, and potentially insecure to provide internal exception externally.
212             // We thus intentionally chose to only return a generic 500 with details (only) in the log.
213         }
214     }
215 }