39857b856846f7f01d6285466ea7e3b7f4d1703e
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / openecomp / appc / adapter / messaging / dmaap / impl / DmaapUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.adapter.messaging.dmaap.impl;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Properties;
29
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);
36         if(!fo.exists()) {
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());
45         }
46         return topicPreferredRouteFileName;
47     }
48
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;
53     }
54
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;
59     }
60
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);
67
68         String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
69         if(props != null && !props.isEmpty()){
70             defaultProps.putAll(props);
71         }
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;
78
79         defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName+" file created on the fly for topic:"+topic+" on:"+System.currentTimeMillis());
80         return topicConsumerPropFileName;
81     }
82
83 }