NEUTRON-208: BGPVPN network and router association
[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     private final NeutronBgpvpnNetworkAssociationsNorthbound neutronBgpvpnNetworkAssociationsNorthbound;
69     private final NeutronBgpvpnRouterAssociationsNorthbound neutronBgpvpnRouterAssociationsNorthbound;
70
71     @Inject
72     public NeutronNorthboundRSApplication(
73             NeutronNetworksNorthbound neutronNetworksNorthbound,
74             NeutronSubnetsNorthbound neutronSubnetsNorthbound,
75             NeutronPortsNorthbound neutronPortsNorthbound,
76             NeutronRoutersNorthbound neutronRoutersNorthbound,
77             NeutronFloatingIpsNorthbound neutronFloatingIpsNorthbound,
78             NeutronSecurityGroupsNorthbound neutronSecurityGroupsNorthbound,
79             NeutronSecurityRulesNorthbound neutronSecurityRulesNorthbound,
80             NeutronFirewallNorthbound neutronFirewallNorthbound,
81             NeutronFirewallPolicyNorthbound neutronFirewallPolicyNorthbound,
82             NeutronFirewallRulesNorthbound neutronFirewallRulesNorthbound,
83             NeutronLoadBalancerListenerNorthbound neutronLoadBalancerListenerNorthbound,
84             NeutronLoadBalancerNorthbound neutronLoadBalancerNorthbound,
85             NeutronLoadBalancerPoolNorthbound neutronLoadBalancerPoolNorthbound,
86             NeutronBgpvpnsNorthbound neutronBgpvpnsNorthbound,
87             NeutronL2gatewayNorthbound neutronL2gatewayNorthbound,
88             NeutronL2gatewayConnectionNorthbound neutronL2gatewayConnectionNorthbound,
89             NeutronLoadBalancerHealthMonitorNorthbound neutronLoadBalancerHealthMonitorNorthbound,
90             NeutronMeteringLabelsNorthbound neutronMeteringLabelsNorthbound,
91             NeutronMeteringLabelRulesNorthbound neutronMeteringLabelRulesNorthbound,
92             NeutronVpnServicesNorthbound neutronVpnServicesNorthbound,
93             NeutronVpnIkePoliciesNorthbound neutronVpnIkePoliciesNorthbound,
94             NeutronVpnIpSecSiteConnectionsNorthbound neutronVpnIpSecSiteConnectionsNorthbound,
95             NeutronVpnIpSecPoliciesNorthbound neutronVpnIpSecPoliciesNorthbound,
96             NeutronSFCFlowClassifiersNorthbound neutronSFCFlowClassifiersNorthbound,
97             NeutronSFCPortChainsNorthbound neutronSFCPortChainsNorthbound,
98             NeutronSFCPortPairGroupsNorthbound neutronSFCPortPairGroupsNorthbound,
99             NeutronSFCPortPairsNorthbound neutronSFCPortPairsNorthbound,
100             NeutronQosPolicyNorthbound neutronQosPolicyNorthbound,
101             NeutronTrunksNorthbound neutronTrunksNorthbound,
102             NeutronTapServiceNorthbound neutronTapServiceNorthbound,
103             NeutronTapFlowNorthbound neutronTapFlowNorthbound,
104             NeutronBgpvpnNetworkAssociationsNorthbound neutronBgpvpnNetworkAssociationsNorthbound,
105             NeutronBgpvpnRouterAssociationsNorthbound neutronBgpvpnRouterAssociationsNorthbound) {
106
107         this.neutronNetworksNorthbound = neutronNetworksNorthbound;
108         this.neutronSubnetsNorthbound = neutronSubnetsNorthbound;
109         this.neutronPortsNorthbound = neutronPortsNorthbound;
110         this.neutronRoutersNorthbound = neutronRoutersNorthbound;
111         this.neutronFloatingIpsNorthbound = neutronFloatingIpsNorthbound;
112         this.neutronSecurityGroupsNorthbound = neutronSecurityGroupsNorthbound;
113         this.neutronSecurityRulesNorthbound = neutronSecurityRulesNorthbound;
114         this.neutronFirewallNorthbound = neutronFirewallNorthbound;
115         this.neutronFirewallRulesNorthbound = neutronFirewallRulesNorthbound;
116         this.neutronLoadBalancerListenerNorthbound = neutronLoadBalancerListenerNorthbound;
117         this.neutronLoadBalancerNorthbound = neutronLoadBalancerNorthbound;
118         this.neutronFirewallPolicyNorthbound = neutronFirewallPolicyNorthbound;
119         this.neutronLoadBalancerPoolNorthbound = neutronLoadBalancerPoolNorthbound;
120         this.neutronBgpvpnsNorthbound = neutronBgpvpnsNorthbound;
121         this.neutronL2gatewayNorthbound = neutronL2gatewayNorthbound;
122         this.neutronL2gatewayConnectionNorthbound = neutronL2gatewayConnectionNorthbound;
123         this.neutronLoadBalancerHealthMonitorNorthbound = neutronLoadBalancerHealthMonitorNorthbound;
124         this.neutronMeteringLabelsNorthbound = neutronMeteringLabelsNorthbound;
125         this.neutronMeteringLabelRulesNorthbound = neutronMeteringLabelRulesNorthbound;
126         this.neutronVpnServicesNorthbound = neutronVpnServicesNorthbound;
127         this.neutronVpnIkePoliciesNorthbound = neutronVpnIkePoliciesNorthbound;
128         this.neutronVpnIpSecSiteConnectionsNorthbound = neutronVpnIpSecSiteConnectionsNorthbound;
129         this.neutronVpnIpSecPoliciesNorthbound = neutronVpnIpSecPoliciesNorthbound;
130         this.neutronSFCFlowClassifiersNorthbound = neutronSFCFlowClassifiersNorthbound;
131         this.neutronSFCPortChainsNorthbound = neutronSFCPortChainsNorthbound;
132         this.neutronSFCPortPairGroupsNorthbound = neutronSFCPortPairGroupsNorthbound;
133         this.neutronSFCPortPairsNorthbound = neutronSFCPortPairsNorthbound;
134         this.neutronQosPolicyNorthbound = neutronQosPolicyNorthbound;
135         this.neutronTrunksNorthbound = neutronTrunksNorthbound;
136         this.neutronTapServiceNorthbound = neutronTapServiceNorthbound;
137         this.neutronTapFlowNorthbound = neutronTapFlowNorthbound;
138         this.neutronBgpvpnNetworkAssociationsNorthbound = neutronBgpvpnNetworkAssociationsNorthbound;
139         this.neutronBgpvpnRouterAssociationsNorthbound = neutronBgpvpnRouterAssociationsNorthbound;
140     }
141
142     @Override
143     public Set<Class<?>> getClasses() {
144         return emptySet();
145     }
146
147     @Override
148     public Set<Object> getSingletons() {
149         return ImmutableSet.builderWithExpectedSize(34)
150                 .add(getMOXyJsonProvider())
151                 // Northbound URIs JAX RS Resources:
152                 .add(neutronNetworksNorthbound)
153                 .add(neutronSubnetsNorthbound)
154                 .add(neutronPortsNorthbound)
155                 .add(neutronRoutersNorthbound)
156                 .add(neutronFloatingIpsNorthbound)
157                 .add(neutronSecurityGroupsNorthbound)
158                 .add(neutronSecurityRulesNorthbound)
159                 .add(neutronFirewallNorthbound)
160                 .add(neutronFirewallPolicyNorthbound)
161                 .add(neutronFirewallRulesNorthbound)
162                 .add(neutronLoadBalancerNorthbound)
163                 .add(neutronLoadBalancerListenerNorthbound)
164                 .add(neutronLoadBalancerPoolNorthbound)
165                 .add(neutronLoadBalancerHealthMonitorNorthbound)
166                 .add(neutronMeteringLabelsNorthbound)
167                 .add(neutronMeteringLabelRulesNorthbound)
168                 .add(neutronVpnServicesNorthbound)
169                 .add(neutronVpnIkePoliciesNorthbound)
170                 .add(neutronVpnIpSecPoliciesNorthbound)
171                 .add(neutronVpnIpSecSiteConnectionsNorthbound)
172                 .add(neutronBgpvpnsNorthbound)
173                 .add(neutronBgpvpnNetworkAssociationsNorthbound)
174                 .add(neutronBgpvpnRouterAssociationsNorthbound)
175                 .add(neutronL2gatewayNorthbound)
176                 .add(neutronL2gatewayConnectionNorthbound)
177                 .add(neutronSFCFlowClassifiersNorthbound)
178                 .add(neutronSFCPortPairsNorthbound)
179                 .add(neutronSFCPortPairGroupsNorthbound)
180                 .add(neutronSFCPortChainsNorthbound)
181                 .add(neutronQosPolicyNorthbound)
182                 .add(neutronTrunksNorthbound)
183                 .add(neutronTapServiceNorthbound)
184                 .add(neutronTapFlowNorthbound)
185                 .add(new LoggingExceptionMapper())
186                 .build();
187     }
188
189     private MOXyJsonProvider getMOXyJsonProvider() {
190         MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
191
192         moxyJsonProvider.setAttributePrefix("@");
193         moxyJsonProvider.setFormattedOutput(true);
194         moxyJsonProvider.setIncludeRoot(false);
195         moxyJsonProvider.setMarshalEmptyCollections(true);
196         moxyJsonProvider.setValueWrapper("$");
197
198         Map<String, String> namespacePrefixMapper = new HashMap<>(HASHMAP_SIZE);
199         // FIXME: fill in next two with XSD
200         namespacePrefixMapper.put("router", "router");
201         namespacePrefixMapper.put("provider", "provider");
202         namespacePrefixMapper.put("binding", "binding");
203         moxyJsonProvider.setNamespacePrefixMapper(namespacePrefixMapper);
204         moxyJsonProvider.setNamespaceSeparator(':');
205
206         return moxyJsonProvider;
207     }
208
209     // do not inline this as a lambda; for some (strange) reason, the HK2 DI thing used by Jersey (v2.25.1) does
210     // not like it: "WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
211     //  javax.ws.rs.ProcessingException: Could not find exception type for given ExceptionMapper class:
212     //  class org.opendaylight.neutron.northbound.api.NeutronNorthboundRSApplication$$Lambda$157/5987161."
213     public static class LoggingExceptionMapper implements ExceptionMapper<Exception> {
214         @Override
215         public Response toResponse(Exception exception) {
216             LOG.error("Error processing response", exception);
217             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
218             // In order to return the exception in the Response, it would need mapping.
219             // This could be brittle, and potentially insecure to provide internal exception externally.
220             // We thus intentionally chose to only return a generic 500 with details (only) in the log.
221         }
222     }
223 }