ead13dd1bef374992d9d4edae98599d79ba96adb
[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  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.messaging.dmaap.impl;
26
27 import java.io.File;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Properties;
32
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);
39         if(!fo.exists()) {
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());
48         }
49         return topicPreferredRouteFileName;
50     }
51
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;
56     }
57
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;
62     }
63
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);
70
71         String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
72         if(props != null && !props.isEmpty()){
73             defaultProps.putAll(props);
74         }
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;
81
82         defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName+" file created on the fly for topic:"+topic+" on:"+System.currentTimeMillis());
83         return topicConsumerPropFileName;
84     }
85
86 }