Add test to Holmes
[clamp.git] / src / main / java / org / onap / clamp / clds / Application.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds;
25
26 import com.att.ajsc.common.utility.SystemPropertiesLoader;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.util.ArrayList;
31 import java.util.Collection;
32
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.ClientBuilder;
35
36 import org.apache.camel.component.servlet.CamelHttpTransportServlet;
37 import org.apache.catalina.connector.Connector;
38 import org.camunda.bpm.spring.boot.starter.webapp.CamundaBpmWebappAutoConfiguration;
39 import org.onap.clamp.clds.model.prop.Holmes;
40 import org.onap.clamp.clds.model.prop.ModelProperties;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Value;
43 import org.springframework.boot.SpringApplication;
44 import org.springframework.boot.actuate.autoconfigure.ManagementWebSecurityAutoConfiguration;
45 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
46 import org.springframework.boot.autoconfigure.SpringBootApplication;
47 import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
48 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
49 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
50 import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
51 import org.springframework.boot.builder.SpringApplicationBuilder;
52 import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
53 import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
54 import org.springframework.boot.web.servlet.ServletRegistrationBean;
55 import org.springframework.boot.web.support.SpringBootServletInitializer;
56 import org.springframework.context.ApplicationContext;
57 import org.springframework.context.annotation.Bean;
58 import org.springframework.context.annotation.ComponentScan;
59 import org.springframework.scheduling.annotation.EnableAsync;
60
61 @SpringBootApplication
62 @ComponentScan(basePackages = { "org.onap.clamp.clds", "com.att.ajsc" })
63 @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, CamundaBpmWebappAutoConfiguration.class,
64         HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, SecurityAutoConfiguration.class,
65         ManagementWebSecurityAutoConfiguration.class })
66 @EnableAsync
67 public class Application extends SpringBootServletInitializer {
68
69     protected static final EELFLogger eelfLogger         = EELFManager.getInstance().getLogger(Application.class);
70
71     @Autowired
72     protected ApplicationContext      appContext;
73
74     private static final String       CAMEL_SERVLET_NAME = "CamelServlet";
75     private static final String       CAMEL_URL_MAPPING  = "/restservices/clds/v1/*";
76
77     // This settings is an additional one to Spring config,
78     // only if we want to have an additional port automatically redirected to
79     // HTTPS
80     @Value("${server.http-to-https-redirection.port:none}")
81     private String                    httpRedirectedPort;
82
83     /**
84      * This 8080 is the default port used by spring if this parameter is not
85      * specified in application.properties.
86      */
87     @Value("${server.port:8080}")
88     private String                    springServerPort;
89
90     @Value("${server.ssl.key-store:none}")
91     private String                    sslKeystoreFile;
92
93     @Override
94     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
95         return application.sources(Application.class);
96     }
97
98     public static void main(String[] args) {
99         // This is to load the system.properties file parameters
100         SystemPropertiesLoader.addSystemProperties();
101         // This is to initialize some Onap Clamp components
102         initializeComponents();
103         // Start the Spring application
104         SpringApplication.run(Application.class, args); // NOSONAR
105     }
106
107     private static void initializeComponents() {
108         ModelProperties.registerModelElement(Holmes.class, Holmes.getType());
109     }
110
111     @Bean
112     public ServletRegistrationBean servletRegistrationBean() {
113         ServletRegistrationBean registration = new ServletRegistrationBean();
114         registration.setName(CAMEL_SERVLET_NAME);
115         registration.setServlet(new CamelHttpTransportServlet());
116         Collection<String> urlMappings = new ArrayList<>();
117         urlMappings.add(CAMEL_URL_MAPPING);
118         registration.setUrlMappings(urlMappings);
119         return registration;
120     }
121
122     @Bean
123     public Client restClient() {
124         return ClientBuilder.newClient();
125     }
126
127     /**
128      * This method is used by Spring to create the servlet container factory.
129      * 
130      * @return The TomcatEmbeddedServletContainerFactory just created
131      */
132     @Bean
133     public EmbeddedServletContainerFactory getEmbeddedServletContainerFactory() {
134         TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
135         if (!"none".equals(httpRedirectedPort) && !"none".equals(sslKeystoreFile)) {
136             // Automatically redirect to HTTPS
137             tomcat = new TomcatEmbeddedServletContainerFactoryRedirection();
138             Connector newConnector = createRedirectConnector(Integer.parseInt(springServerPort));
139             if (newConnector != null) {
140                 tomcat.addAdditionalTomcatConnectors(newConnector);
141             }
142
143         }
144         return tomcat;
145
146     }
147
148     private Connector createRedirectConnector(int redirectSecuredPort) {
149         if (redirectSecuredPort <= 0) {
150             eelfLogger.warn(
151                     "HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1 (Connector disabled)");
152             return null;
153         }
154         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
155         connector.setScheme("http");
156         connector.setSecure(false);
157         connector.setPort(Integer.parseInt(httpRedirectedPort));
158         connector.setRedirectPort(redirectSecuredPort);
159         return connector;
160     }
161
162 }