2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap.utils;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Properties;
33 import org.onap.appc.configuration.Configuration;
34 import org.onap.appc.configuration.ConfigurationFactory;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
38 public class DmaapUtil {
40 private static final char DELIMITER = '_';
42 static final String DMAAP_PROPERTIES_PATH = "org.onap.appc.dmaap.profile.path";
44 private static final EELFLogger log = EELFManager.getInstance().getLogger(DmaapUtil.class);
49 private static String createPreferredRouteFileIfNotExist(String topic) throws IOException {
50 String topicPreferredRouteFileName;
51 topicPreferredRouteFileName = topic + "preferredRoute.properties";
52 File fo = new File(topicPreferredRouteFileName);
54 ClassLoader classLoader = DmaapUtil.class.getClassLoader();
55 InputStream inputStream = classLoader.getResourceAsStream("preferredRoute.txt");
56 Properties props = new Properties();
57 props.load(inputStream);
58 String fileName = topic != null ? topic + DELIMITER + "MR1" : DELIMITER + "MR1";
59 props.setProperty("preferredRouteKey", fileName);
60 topicPreferredRouteFileName = topic + "preferredRoute.properties";
61 props.store(new FileOutputStream(topicPreferredRouteFileName),
62 "preferredRoute.properties file created on the fly for topic:" + topic + " on:"
63 + System.currentTimeMillis());
65 return topicPreferredRouteFileName;
68 public static String createConsumerPropFile(String topic, Properties props) throws IOException {
69 String defaultProfFileName = "consumer.properties";
71 log.debug("Creating DMaaP Consumer Property File for topic " + topic);
72 return createConsumerProducerPropFile(topic, defaultProfFileName, props);
75 public static String createProducerPropFile(String topic, Properties props) throws IOException {
76 String defaultProfFileName = "producer.properties";
78 log.debug("Creating DMaaP Producer Property File for topic " + topic);
79 return createConsumerProducerPropFile(topic, defaultProfFileName, props);
82 private static String createConsumerProducerPropFile(String topic, String defaultProfFileName, Properties props)
84 Properties defaultProps = getDefaultProperties(defaultProfFileName);
86 defaultProps.setProperty("topic", topic);
88 String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
89 if (props != null && !props.isEmpty()) {
90 defaultProps.putAll(props);
92 defaultProps.setProperty("topic", topic);
93 defaultProps.setProperty("DME2preferredRouterFilePath", preferredRouteFileName);
94 String id = defaultProps.getProperty("id");
95 String topicConsumerPropFileName = defaultProfFileName;
96 topicConsumerPropFileName = id != null ? id + DELIMITER + topicConsumerPropFileName
97 : DELIMITER + topicConsumerPropFileName;
98 topicConsumerPropFileName = topic != null ? topic + DELIMITER + topicConsumerPropFileName
99 : DELIMITER + topicConsumerPropFileName;
101 defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName
102 + " file created on the fly for topic:" + topic + " on:" + System.currentTimeMillis());
103 return topicConsumerPropFileName;
106 private static Properties getDefaultProperties(String profileName) {
107 Properties props = new Properties();
109 // use appc configuration to get all properties which includes
110 // appc.properties and system properties
111 // allowing variable to be set in any location
112 Configuration config = ConfigurationFactory.getConfiguration();
113 String dmaapPropPath = config.getProperty(DMAAP_PROPERTIES_PATH);
115 if (dmaapPropPath != null) {
116 // load from file system
118 File profileFile = new File(dmaapPropPath, profileName);
119 FileInputStream inputStream = null;
121 log.info("Loading DMaaP Profile from " + profileFile.getAbsolutePath());
123 if (profileFile.exists()) {
125 inputStream = new FileInputStream(profileFile);
126 props.load(inputStream);
127 } catch (IOException e) {
128 log.error("Exception loading DMaaP Profile from " + profileFile.getAbsolutePath(), e);
131 if (inputStream != null) {
134 } catch (IOException ex) {
135 log.warn("Exception closing DMaaP Profile file " + profileFile.getAbsolutePath(), ex);
140 if (props.isEmpty()) {
141 // load default Profile from class
142 log.info("Loading Default DMaaP Profile");
144 ClassLoader classLoader = DmaapUtil.class.getClassLoader();
145 InputStream inputStream = classLoader.getResourceAsStream(profileName);
147 props.load(inputStream);
148 } catch (IOException e) {
149 log.error("Exception loading Default DMaaP Profile", e);