785ddfc799c04e9d8cc47dc68a519108866f398f
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / BeanFactory.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24
25 import java.lang.invoke.MethodHandles;
26
27 import org.apache.catalina.connector.Connector;
28 import org.oransc.enrichment.configuration.ApplicationConfig;
29 import org.oransc.enrichment.controllers.producer.ProducerCallbacks;
30 import org.oransc.enrichment.repository.EiJobs;
31 import org.oransc.enrichment.repository.EiProducers;
32 import org.oransc.enrichment.repository.EiTypes;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
37 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
38 import org.springframework.context.annotation.Bean;
39 import org.springframework.context.annotation.Configuration;
40
41 @Configuration
42 class BeanFactory {
43
44     @Value("${server.http-port}")
45     private int httpPort = 0;
46
47     private final ApplicationConfig applicationConfig = new ApplicationConfig();
48     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49
50     private ProducerCallbacks producerCallbacks;
51     private EiTypes eiTypes;
52     private EiJobs eiJobs;
53     private EiProducers eiProducers;
54
55     @Bean
56     public ObjectMapper mapper() {
57         return new ObjectMapper();
58     }
59
60     @Bean
61     public ServletWebServerFactory servletContainer() {
62         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
63         if (httpPort > 0) {
64             tomcat.addAdditionalTomcatConnectors(getHttpConnector(httpPort));
65         }
66         return tomcat;
67     }
68
69     @Bean
70     public EiJobs eiJobs() {
71         if (eiJobs == null) {
72             eiJobs = new EiJobs(getApplicationConfig(), producerCallbacks());
73             try {
74                 eiJobs.restoreJobsFromDatabase();
75             } catch (Exception e) {
76                 logger.error("Could not restore jobs from database: {}", e.getMessage());
77             }
78         }
79         return eiJobs;
80     }
81
82     @Bean
83     public EiTypes eiTypes() {
84         if (this.eiTypes == null) {
85             eiTypes = new EiTypes(getApplicationConfig());
86             try {
87                 eiTypes.restoreTypesFromDatabase();
88             } catch (Exception e) {
89                 logger.error("Could not restore EI types from database: {}", e.getMessage());
90             }
91         }
92         return eiTypes;
93     }
94
95     @Bean
96     public ProducerCallbacks producerCallbacks() {
97         if (this.producerCallbacks == null) {
98             producerCallbacks = new ProducerCallbacks(getApplicationConfig());
99         }
100         return this.producerCallbacks;
101     }
102
103     @Bean
104     public ApplicationConfig getApplicationConfig() {
105         return this.applicationConfig;
106     }
107
108     private static Connector getHttpConnector(int httpPort) {
109         Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
110         connector.setScheme("http");
111         connector.setPort(httpPort);
112         connector.setSecure(false);
113         return connector;
114     }
115
116 }