2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.messaging.dmaap.impl;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Properties;
33 public class DmaapUtil {
34 private final static String delimiter = "_";
35 private static String createPreferredRouteFileIfNotExist(String topic) throws IOException {
36 String topicPreferredRouteFileName = null;
37 topicPreferredRouteFileName = topic+"preferredRoute.properties";
38 File fo= new File(topicPreferredRouteFileName);
40 ClassLoader classLoader = DmaapUtil.class.getClassLoader();
41 InputStream inputStream = classLoader.getResourceAsStream("preferredRoute.txt");
42 Properties props = new Properties();
43 props.load(inputStream);
44 String fileName = topic != null ? topic+delimiter+"MR1" : delimiter+"MR1";
45 props.setProperty("preferredRouteKey", fileName);
46 topicPreferredRouteFileName = topic + "preferredRoute.properties";
47 props.store(new FileOutputStream(topicPreferredRouteFileName), "preferredRoute.properties file created on the fly for topic:" + topic + " on:" + System.currentTimeMillis());
49 return topicPreferredRouteFileName;
52 public static String createConsumerPropFile(String topic, Properties props)throws IOException {
53 String defaultProfFileName = "consumer.properties";
54 String topicConsumerPropFileName = createConsumerProducerPropFile(topic, defaultProfFileName,props);
55 return topicConsumerPropFileName;
58 public static String createProducerPropFile(String topic, Properties props)throws IOException {
59 String defaultProfFileName = "producer.properties";
60 String topicConsumerPropFileName = createConsumerProducerPropFile(topic, defaultProfFileName,props);
61 return topicConsumerPropFileName;
64 private static String createConsumerProducerPropFile(String topic, String defaultProfFileName, Properties props) throws IOException {
65 ClassLoader classLoader = DmaapUtil.class.getClassLoader();
66 InputStream inputStream = classLoader.getResourceAsStream(defaultProfFileName);
67 Properties defaultProps = new Properties();
68 defaultProps.load(inputStream);
69 defaultProps.setProperty("topic",topic);
71 String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
72 if(props != null && !props.isEmpty()){
73 defaultProps.putAll(props);
75 defaultProps.setProperty("topic",topic);
76 defaultProps.setProperty("DME2preferredRouterFilePath",preferredRouteFileName);
77 String id = defaultProps.getProperty("id");
78 String topicConsumerPropFileName = defaultProfFileName;
79 topicConsumerPropFileName = id != null ? id+delimiter+topicConsumerPropFileName : delimiter+topicConsumerPropFileName;
80 topicConsumerPropFileName = topic != null ? topic+delimiter+topicConsumerPropFileName : delimiter+topicConsumerPropFileName;
82 defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName+" file created on the fly for topic:"+topic+" on:"+System.currentTimeMillis());
83 return topicConsumerPropFileName;