summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/CompositionEngine.java
blob: c633c59b144de274f337f8447c510c2b5a4a4b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.onap.sdc.dcae.composition;

import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.common.onaplog.OnapLoggerDebug;
import org.onap.sdc.common.onaplog.OnapLoggerError;
import org.onap.sdc.dcae.composition.util.SystemProperties;
import org.onap.sdc.dcae.errormng.ErrConfMgr;
import org.onap.sdc.dcae.errormng.ErrorConfigurationLoader;
import org.onap.sdc.dcae.filter.LoggingFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

@Configuration
@EnableScheduling
@SpringBootApplication
@ComponentScan("org.onap.sdc.dcae")
@EnableAutoConfiguration
@PropertySource("file:${jetty.base}/config/dcae-be/application.properties")
public class CompositionEngine extends SpringBootServletInitializer implements CommandLineRunner{
	private static final String SPECIFICATION_VERSION = "Specification-Version";
	@Autowired
	ServletContext servletContext;
	private static final String MANIFEST_FILE_NAME = "/META-INF/MANIFEST.MF";
	private static String dcaeVersion;
	private OnapLoggerError errLogger = OnapLoggerError.getInstance();
	private OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();

	public static void main(String[] args) {
		SpringApplication.run(CompositionEngine.class, args);
	}
	
	/**
	 * Creates and returns a new instance of a {@link SystemProperties} class.
	 * 
	 * @return New instance of {@link SystemProperties}.
	 */
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CompositionEngine.class);
    }
   
	
	@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                		.allowedOrigins("*")
                		.allowedHeaders("*")
                		.allowedMethods("GET", "POST", "OPTIONS", "PUT")
                		.allowCredentials(false)
                		.maxAge(3600);
                		
            }
        };
    }

	@Override
	public void run(String... args) throws Exception {

		ErrorConfigurationLoader errorConfigurationLoader = new ErrorConfigurationLoader(System.getProperty("jetty.base"));
		ErrConfMgr instance = ErrConfMgr.INSTANCE;
		InputStream inputStream = servletContext.getResourceAsStream(MANIFEST_FILE_NAME);

		//setLogbackXmlLocation();

		String version = null;
		try {
			Manifest mf = new Manifest(inputStream);
			Attributes atts = mf.getMainAttributes();
			version = atts.getValue(SPECIFICATION_VERSION);
			if (version == null || version.isEmpty()) {
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), "failed to read DCAE version from MANIFEST.");
			} else {
				debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "DCAE version from MANIFEST is {}", version);
				dcaeVersion = version;
			}

		} catch (IOException e) {
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), "failed to read DCAE version from MANIFEST: {}", e.getMessage());
		}
		
	}

	private void setLogbackXmlLocation() throws Exception {
		String jettyBase = System.getProperty("config.home");
		Properties props = System.getProperties();
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Current System Properties are: {}", props);
		if (jettyBase == null) {
			String msg = "Couldn't resolve config.home environmental variable";
			errLogger.log(LogLevel.ERROR, this.getClass().getName(), msg);
			throw new Exception(msg + ". Failed to configure logback.xml location... aborting.");
		}
		String logbackXmlLocation = jettyBase+"/dcae-be/logback.xml";
		props.setProperty("logback.configurationFile", logbackXmlLocation);
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Successfuly set the logback.xml location to {}", logbackXmlLocation);
	}

    @Bean
    public FilterRegistrationBean contextLifecycleFilter() {
        Collection<String> urlPatterns = new ArrayList<>();
        urlPatterns.add("/*");

        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new LoggingFilter());
        filterRegistrationBean.setUrlPatterns(urlPatterns);

        return filterRegistrationBean;
    }

	public static String getDcaeVersion() {
		return dcaeVersion;
	}
	
}