aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcaegen2/services/sonhms/Application.java
blob: 7eb9e627e63bde9ea65cc664e6f1a0c805445b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  son-handler
 *  ================================================================================
 *   Copyright (C) 2019 Wipro Limited.
 *   ==============================================================================
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *  
 *          http://www.apache.org/licenses/LICENSE-2.0
 *  
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 *     ============LICENSE_END=========================================================
 *  
 *******************************************************************************/

package org.onap.dcaegen2.services.sonhms;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.onap.dcaegen2.services.sonhms.dmaap.DmaapClient;
import org.onap.dcaegen2.services.sonhms.restclient.PolicyRestClient;
import org.onap.dcaegen2.services.sonhms.utils.FileIo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    @Autowired
    DmaapClient dmaapClient;

    @Autowired
    MainThreadComponent mainThreadComponent;

    private static Logger log = LoggerFactory.getLogger(Application.class);

    /**
     * Main method where the pci context is initially set.
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class);

    }

    /**
     * initialization.
     */
    @PostConstruct
    void init() {
        getConfig();
        fetchIntialConfigFromPolicy();
        NewNotification newNotification = new NewNotification(false);
        dmaapClient.initClient(newNotification);
        mainThreadComponent.init(newNotification);
    }

    /**
     * Gets configuration from policy.
     */
    @SuppressWarnings("unchecked")
    private void fetchIntialConfigFromPolicy() {
        log.debug("fetch initial config from policy");
        String configPolicyResponseJson = PolicyRestClient.fetchConfigFromPolicy();
        if (configPolicyResponseJson.equals("Post failed")) {
            log.debug("cannot fetch config from policy");
            return;
        }
        ObjectMapper mapper = new ObjectMapper();
        List<HashMap<String, Object>> configPolicyResponse = new ArrayList<>();
        try {
            configPolicyResponse = mapper.readValue(configPolicyResponseJson, List.class);
        } catch (IOException e) {
            log.debug("exception during parsing response from policy", e);
        }
        String configPolicyJson = null;
        if (configPolicyResponse != null) {
            configPolicyJson = (String) configPolicyResponse.get(0).get("config");
        } else {
            return;
        }
        Map<String, Object> configPolicyMap = new HashMap<>();
        try {
            configPolicyMap = mapper.readValue(configPolicyJson, HashMap.class);
        } catch (IOException e) {
            log.debug("exception during parsing config body from policy", e);
        }
        ConfigPolicy configPolicy = ConfigPolicy.getInstance();
        configPolicy.setConfig(configPolicyMap);
        if (log.isDebugEnabled()) {
            log.debug(configPolicy.toString());
        }
    }

    /**
     * Gets config from config.json.
     *
     */
    private void getConfig() {
        log.debug("getting initial config");
        String configJson = FileIo.readFromFile("/etc/config.json");
        ObjectMapper mapper = new ObjectMapper();
        Configuration configuration = Configuration.getInstance();
        try {
            mapper.readerForUpdating(configuration).readValue(configJson);
            if (log.isDebugEnabled()) {
                log.debug(configuration.toString());
            }
        } catch (IOException e) {
            log.debug("exception during parsing configuration", e);
        }
    }
}