Merge "Remove A1 Policy Management Service"
[nonrtric.git] / information-coordinator-service / src / main / java / org / oransc / ics / 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.ics;
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.ics.clients.SecurityContext;
29 import org.oransc.ics.configuration.ApplicationConfig;
30 import org.oransc.ics.controllers.r1producer.ProducerCallbacks;
31 import org.oransc.ics.repository.InfoJobs;
32 import org.oransc.ics.repository.InfoTypes;
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 InfoTypes infoTypes;
52     private InfoJobs infoJobs;
53
54     @Bean
55     public ObjectMapper mapper() {
56         return new ObjectMapper();
57     }
58
59     @Bean
60     public ServletWebServerFactory servletContainer() {
61         TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
62         if (httpPort > 0) {
63             tomcat.addAdditionalTomcatConnectors(getHttpConnector(httpPort));
64         }
65         return tomcat;
66     }
67
68     @Bean
69     public InfoJobs infoJobs(SecurityContext securityContext) {
70         if (infoJobs == null) {
71             infoJobs = new InfoJobs(getApplicationConfig(), producerCallbacks(securityContext));
72             try {
73                 infoJobs.restoreJobsFromDatabase();
74             } catch (Exception e) {
75                 logger.error("Could not restore jobs from database: {}", e.getMessage());
76             }
77         }
78         return infoJobs;
79     }
80
81     @Bean
82     public InfoTypes infoTypes() {
83         if (this.infoTypes == null) {
84             infoTypes = new InfoTypes(getApplicationConfig());
85             try {
86                 infoTypes.restoreTypesFromDatabase();
87             } catch (Exception e) {
88                 logger.error("Could not restore Information Types from database: {}", e.getMessage());
89             }
90         }
91         return infoTypes;
92     }
93
94     @Bean
95     public ProducerCallbacks producerCallbacks(SecurityContext securityContext) {
96         if (this.producerCallbacks == null) {
97             producerCallbacks = new ProducerCallbacks(getApplicationConfig(), securityContext);
98         }
99         return this.producerCallbacks;
100     }
101
102     @Bean
103     public ApplicationConfig getApplicationConfig() {
104         return this.applicationConfig;
105     }
106
107     private static Connector getHttpConnector(int httpPort) {
108         Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
109         connector.setScheme("http");
110         connector.setPort(httpPort);
111         connector.setSecure(false);
112         return connector;
113     }
114
115 }