2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.messaging.dmaap.impl;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Properties;
30 public class DmaapUtil {
31 private final static String delimiter = "_";
32 private static String createPreferredRouteFileIfNotExist(String topic) throws IOException {
33 String topicPreferredRouteFileName = null;
34 topicPreferredRouteFileName = topic+"preferredRoute.properties";
35 File fo= new File(topicPreferredRouteFileName);
37 ClassLoader classLoader = DmaapUtil.class.getClassLoader();
38 InputStream inputStream = classLoader.getResourceAsStream("preferredRoute.txt");
39 Properties props = new Properties();
40 props.load(inputStream);
41 String fileName = topic != null ? topic+delimiter+"MR1" : delimiter+"MR1";
42 props.setProperty("preferredRouteKey", fileName);
43 topicPreferredRouteFileName = topic + "preferredRoute.properties";
44 props.store(new FileOutputStream(topicPreferredRouteFileName), "preferredRoute.properties file created on the fly for topic:" + topic + " on:" + System.currentTimeMillis());
46 return topicPreferredRouteFileName;
49 public static String createConsumerPropFile(String topic, Properties props)throws IOException {
50 String defaultProfFileName = "consumer.properties";
51 String topicConsumerPropFileName = createConsumerProducerPropFile(topic, defaultProfFileName,props);
52 return topicConsumerPropFileName;
55 public static String createProducerPropFile(String topic, Properties props)throws IOException {
56 String defaultProfFileName = "producer.properties";
57 String topicConsumerPropFileName = createConsumerProducerPropFile(topic, defaultProfFileName,props);
58 return topicConsumerPropFileName;
61 private static String createConsumerProducerPropFile(String topic, String defaultProfFileName, Properties props) throws IOException {
62 ClassLoader classLoader = DmaapUtil.class.getClassLoader();
63 InputStream inputStream = classLoader.getResourceAsStream(defaultProfFileName);
64 Properties defaultProps = new Properties();
65 defaultProps.load(inputStream);
66 defaultProps.setProperty("topic",topic);
68 String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
69 if(props != null && !props.isEmpty()){
70 defaultProps.putAll(props);
72 defaultProps.setProperty("topic",topic);
73 defaultProps.setProperty("DME2preferredRouterFilePath",preferredRouteFileName);
74 String id = defaultProps.getProperty("id");
75 String topicConsumerPropFileName = defaultProfFileName;
76 topicConsumerPropFileName = id != null ? id+delimiter+topicConsumerPropFileName : delimiter+topicConsumerPropFileName;
77 topicConsumerPropFileName = topic != null ? topic+delimiter+topicConsumerPropFileName : delimiter+topicConsumerPropFileName;
79 defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName+" file created on the fly for topic:"+topic+" on:"+System.currentTimeMillis());
80 return topicConsumerPropFileName;