aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-dmaap-adapter/appc-dmaap-adapter-bundle/src/main/java/org/onap/appc/adapter/messaging/dmaap/impl/DmaapUtil.java
blob: 72e0a265baf694832fad8cb23da97c6601fdc425 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * 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.appc.adapter.messaging.dmaap.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.onap.appc.configuration.Configuration;
import org.onap.appc.configuration.ConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DmaapUtil {

    private static final char   DELIMITER             = '_';

    static final String         DMAAP_PROPERTIES_PATH = "org.onap.appc.dmaap.profile.path";

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

    private DmaapUtil() {
    }

    private static String createPreferredRouteFileIfNotExist(String topic) throws IOException {
        String topicPreferredRouteFileName;
        topicPreferredRouteFileName = topic + "preferredRoute.properties";
        File fo = new File(topicPreferredRouteFileName);
        if (!fo.exists()) {
            ClassLoader classLoader = DmaapUtil.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("preferredRoute.txt");
            Properties props = new Properties();
            props.load(inputStream);
            String fileName = topic != null ? topic + DELIMITER + "MR1" : DELIMITER + "MR1";
            props.setProperty("preferredRouteKey", fileName);
            topicPreferredRouteFileName = topic + "preferredRoute.properties";
            props.store(new FileOutputStream(topicPreferredRouteFileName),
                    "preferredRoute.properties file created on the fly for topic:" + topic + " on:"
                            + System.currentTimeMillis());
        }
        return topicPreferredRouteFileName;
    }

    public static String createConsumerPropFile(String topic, Properties props) throws IOException {
        String defaultProfFileName = "consumer.properties";

        log.debug("Creating DMaaP Consumer Property File for topic " + topic);
        return createConsumerProducerPropFile(topic, defaultProfFileName, props);
    }

    public static String createProducerPropFile(String topic, Properties props) throws IOException {
        String defaultProfFileName = "producer.properties";

        log.debug("Creating DMaaP Producer Property File for topic " + topic);
        return createConsumerProducerPropFile(topic, defaultProfFileName, props);
    }

    private static String createConsumerProducerPropFile(String topic, String defaultProfFileName, Properties props)
            throws IOException {
        Properties defaultProps = getDefaultProperties(defaultProfFileName);

        defaultProps.setProperty("topic", topic);

        String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
        if (props != null && !props.isEmpty()) {
            defaultProps.putAll(props);
        }
        defaultProps.setProperty("topic", topic);
        defaultProps.setProperty("DME2preferredRouterFilePath", preferredRouteFileName);
        String id = defaultProps.getProperty("id");
        String topicConsumerPropFileName = defaultProfFileName;
        topicConsumerPropFileName = id != null ? id + DELIMITER + topicConsumerPropFileName
                : DELIMITER + topicConsumerPropFileName;
        topicConsumerPropFileName = topic != null ? topic + DELIMITER + topicConsumerPropFileName
                : DELIMITER + topicConsumerPropFileName;

        defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName
                + " file created on the fly for topic:" + topic + " on:" + System.currentTimeMillis());
        return topicConsumerPropFileName;
    }

    private static Properties getDefaultProperties(String profileName) {
        Properties props = new Properties();

        // use appc configuration to get all properties which includes
        // appc.properties and system properties
        // allowing variable to be set in any location
        Configuration config = ConfigurationFactory.getConfiguration();
        String dmaapPropPath = config.getProperty(DMAAP_PROPERTIES_PATH);

        if (dmaapPropPath != null) {
            // load from file system

            File profileFile = new File(dmaapPropPath, profileName);
            FileInputStream inputStream = null;

            log.info("Loading DMaaP Profile from " + profileFile.getAbsolutePath());

            if (profileFile.exists()) {
                try {
                    inputStream = new FileInputStream(profileFile);
                    props.load(inputStream);
                } catch (IOException e) {
                    log.error("Exception loading DMaaP Profile from " + profileFile.getAbsolutePath(), e);
                } finally {
                    try {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException ex) {
                        log.warn("Exception closing DMaaP Profile file " + profileFile.getAbsolutePath(), ex);
                    }
                }
            }
        }
        if (props.isEmpty()) {
            // load default Profile from class
            log.info("Loading Default DMaaP Profile");

            ClassLoader classLoader = DmaapUtil.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream(profileName);
            try {
                props.load(inputStream);
            } catch (IOException e) {
                log.error("Exception loading Default DMaaP Profile", e);
            }
        }

        return props;
    }
}