1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 ******************************************************************************/
21 package com.wipro.www.sonhms;
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;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
34 import javax.annotation.PostConstruct;
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;
42 @SpringBootApplication
43 public class Application {
46 DmaapClient dmaapClient;
49 MainThreadComponent mainThreadComponent;
51 private static Logger log = LoggerFactory.getLogger(Application.class);
54 * Main method where the pci context is initially set.
56 public static void main(String[] args) {
57 SpringApplication.run(Application.class);
67 fetchIntialConfigFromPolicy();
68 NewNotification newNotification = new NewNotification(false);
69 dmaapClient.initClient(newNotification);
70 mainThreadComponent.init(newNotification);
74 * Gets configuration from policy.
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");
84 ObjectMapper mapper = new ObjectMapper();
85 List<HashMap<String, Object>> configPolicyResponse = new ArrayList<>();
87 configPolicyResponse = mapper.readValue(configPolicyResponseJson, List.class);
88 } catch (IOException e) {
89 log.debug("exception during parsing response from policy", e);
91 String configPolicyJson = null;
92 if (configPolicyResponse != null) {
93 configPolicyJson = (String) configPolicyResponse.get(0).get("config");
97 Map<String, Object> configPolicyMap = new HashMap<>();
99 configPolicyMap = mapper.readValue(configPolicyJson, HashMap.class);
100 } catch (IOException e) {
101 log.debug("exception during parsing config body from policy", e);
103 ConfigPolicy configPolicy = ConfigPolicy.getInstance();
104 configPolicy.setConfig(configPolicyMap);
105 if (log.isDebugEnabled()) {
106 log.debug(configPolicy.toString());
111 * Gets config from config.json.
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();
120 mapper.readerForUpdating(configuration).readValue(configJson);
121 if (log.isDebugEnabled()) {
122 log.debug(configuration.toString());
124 } catch (IOException e) {
125 log.debug("exception during parsing configuration", e);