Son-handler microservice seed code
[dcaegen2/services/son-handler.git] / src / main / java / com / wipro / www / sonhms / Application.java
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * pcims
4  *  ================================================================================
5  *  Copyright (C) 2018 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 package com.wipro.www.sonhms;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.wipro.www.sonhms.dmaap.DmaapClient;
25 import com.wipro.www.sonhms.restclient.PolicyRestClient;
26 import com.wipro.www.sonhms.utils.FileIo;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import javax.annotation.PostConstruct;
35
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.SpringApplication;
40 import org.springframework.boot.autoconfigure.SpringBootApplication;
41
42 @SpringBootApplication
43 public class Application {
44
45     @Autowired
46     DmaapClient dmaapClient;
47
48     @Autowired
49     MainThreadComponent mainThreadComponent;
50
51     private static Logger log = LoggerFactory.getLogger(Application.class);
52
53     /**
54      * Main method where the pci context is initially set.
55      */
56     public static void main(String[] args) {
57         SpringApplication.run(Application.class);
58
59     }
60
61     /**
62      * initialization.
63      */
64     @PostConstruct
65     void init() {
66         getConfig();
67         fetchIntialConfigFromPolicy();
68         NewNotification newNotification = new NewNotification(false);
69         dmaapClient.initClient(newNotification);
70         mainThreadComponent.init(newNotification);
71     }
72
73     /**
74      * Gets configuration from policy.
75      */
76     @SuppressWarnings("unchecked")
77     private void fetchIntialConfigFromPolicy() {
78         log.debug("fetch initial config from policy");
79         String configPolicyResponseJson = PolicyRestClient.fetchConfigFromPolicy();
80         if (configPolicyResponseJson.equals("Post failed")) {
81             log.debug("cannot fetch config from policy");
82             return;
83         }
84         ObjectMapper mapper = new ObjectMapper();
85         List<HashMap<String, Object>> configPolicyResponse = new ArrayList<>();
86         try {
87             configPolicyResponse = mapper.readValue(configPolicyResponseJson, List.class);
88         } catch (IOException e) {
89             log.debug("exception during parsing response from policy", e);
90         }
91         String configPolicyJson = null;
92         if (configPolicyResponse != null) {
93             configPolicyJson = (String) configPolicyResponse.get(0).get("config");
94         } else {
95             return;
96         }
97         Map<String, Object> configPolicyMap = new HashMap<>();
98         try {
99             configPolicyMap = mapper.readValue(configPolicyJson, HashMap.class);
100         } catch (IOException e) {
101             log.debug("exception during parsing config body from policy", e);
102         }
103         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
104         configPolicy.setConfig(configPolicyMap);
105         if (log.isDebugEnabled()) {
106             log.debug(configPolicy.toString());
107         }
108     }
109
110     /**
111      * Gets config from config.json.
112      *
113      */
114     private void getConfig() {
115         log.debug("getting initial config");
116         String configJson = FileIo.readFromFile("/etc/config.json");
117         ObjectMapper mapper = new ObjectMapper();
118         Configuration configuration = Configuration.getInstance();
119         try {
120             mapper.readerForUpdating(configuration).readValue(configJson);
121             if (log.isDebugEnabled()) {
122                 log.debug(configuration.toString());
123             }
124         } catch (IOException e) {
125             log.debug("exception during parsing configuration", e);
126         }
127     }
128 }