Update CBS client to fetch config periodically
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / Application.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019 Wipro Limited.
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
22 package org.onap.dcaegen2.services.sonhms;
23
24 import java.time.Duration;
25
26 import javax.sql.DataSource;
27
28 import org.onap.dcaegen2.services.sonhms.controller.ConfigFetchFromCbs;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.boot.SpringApplication;
32 import org.springframework.boot.autoconfigure.SpringBootApplication;
33 import org.springframework.boot.jdbc.DataSourceBuilder;
34 import org.springframework.context.annotation.Bean;
35
36 @SpringBootApplication
37 public class Application {
38
39     private static Logger log = LoggerFactory.getLogger(Application.class);
40
41     /**
42      * Main method where the pci context is initially set.
43      */
44     public static void main(String[] args) {
45
46         ConfigFetchFromCbs configFetchFromCbs = new ConfigFetchFromCbs(Duration.ofSeconds(60));
47         Thread configFetchThread = new Thread(configFetchFromCbs);
48         configFetchThread.start();
49         try {
50             Thread.sleep(10000);
51         } catch (InterruptedException e) {
52             log.debug("InterruptedException : {}", e);
53         }
54         log.info("after 10s sleep");
55         log.info("Starting spring boot application");
56         SpringApplication.run(Application.class);
57
58     }
59
60     /**
61      * DataSource bean.
62      */
63     @Bean
64     public DataSource dataSource() {
65         Configuration configuration = Configuration.getInstance();
66
67         String url = "jdbc:postgresql://" + configuration.getPgHost() + ":" + configuration.getPgPort() + "/sonhms";
68
69         return DataSourceBuilder.create().url(url).username(configuration.getPgUsername())
70                 .password(configuration.getPgPassword()).build();
71     }
72
73 }