Update of EI Data Producer API
[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.EiTypes;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
36 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
37 import org.springframework.context.annotation.Bean;
38 import org.springframework.context.annotation.Configuration;
39
40 @Configuration
41 class BeanFactory {
42
43     @Value("${server.http-port}")
44     private int httpPort = 0;
45
46     private final ApplicationConfig applicationConfig = new ApplicationConfig();
47     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
48
49     private ProducerCallbacks producerCallbacks;
50     private EiTypes eiTypes;
51     private EiJobs eiJobs;
52
53     @Bean
54     public ObjectMapper mapper() {
55         return new ObjectMapper();
56     }
57
58     @Bean
59     public ServletWebServerFactory servletContainer() {
60         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
61         if (httpPort > 0) {
62             tomcat.addAdditionalTomcatConnectors(getHttpConnector(httpPort));
63         }
64         return tomcat;
65     }
66
67     @Bean
68     public EiJobs eiJobs() {
69         if (eiJobs == null) {
70             eiJobs = new EiJobs(getApplicationConfig(), producerCallbacks());
71             try {
72                 eiJobs.restoreJobsFromDatabase();
73             } catch (Exception e) {
74                 logger.error("Could not restore jobs from database: {}", e.getMessage());
75             }
76         }
77         return eiJobs;
78     }
79
80     @Bean
81     public EiTypes eiTypes() {
82         if (this.eiTypes == null) {
83             eiTypes = new EiTypes(getApplicationConfig());
84             try {
85                 eiTypes.restoreTypesFromDatabase();
86             } catch (Exception e) {
87                 logger.error("Could not restore EI types from database: {}", e.getMessage());
88             }
89         }
90         return eiTypes;
91     }
92
93     @Bean
94     public ProducerCallbacks producerCallbacks() {
95         if (this.producerCallbacks == null) {
96             producerCallbacks = new ProducerCallbacks(getApplicationConfig());
97         }
98         return this.producerCallbacks;
99     }
100
101     @Bean
102     public ApplicationConfig getApplicationConfig() {
103         return this.applicationConfig;
104     }
105
106     private static Connector getHttpConnector(int httpPort) {
107         Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
108         connector.setScheme("http");
109         connector.setPort(httpPort);
110         connector.setSecure(false);
111         return connector;
112     }
113
114 }