DmaapUtil fixes
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / onap / 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.onap.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
35     private static final char DELIMITER = '_';
36
37     private DmaapUtil() {
38     }
39
40     private static String createPreferredRouteFileIfNotExist(String topic) throws IOException {
41         String topicPreferredRouteFileName;
42         topicPreferredRouteFileName = topic+"preferredRoute.properties";
43         File fo= new File(topicPreferredRouteFileName);
44         if(!fo.exists()) {
45             ClassLoader classLoader = DmaapUtil.class.getClassLoader();
46             InputStream inputStream = classLoader.getResourceAsStream("preferredRoute.txt");
47             Properties props = new Properties();
48             props.load(inputStream);
49             String fileName = topic != null ? topic+ DELIMITER +"MR1" : DELIMITER +"MR1";
50             props.setProperty("preferredRouteKey", fileName);
51             topicPreferredRouteFileName = topic + "preferredRoute.properties";
52             props.store(new FileOutputStream(topicPreferredRouteFileName), "preferredRoute.properties file created on the fly for topic:" + topic + " on:" + System.currentTimeMillis());
53         }
54         return topicPreferredRouteFileName;
55     }
56
57     public static String createConsumerPropFile(String topic, Properties props)throws IOException {
58         String defaultProfFileName = "consumer.properties";
59         return createConsumerProducerPropFile(topic, defaultProfFileName,props);
60     }
61
62     public static String createProducerPropFile(String topic, Properties props)throws IOException {
63         String defaultProfFileName = "producer.properties";
64         return createConsumerProducerPropFile(topic, defaultProfFileName,props);
65     }
66
67     private static String createConsumerProducerPropFile(String topic, String defaultProfFileName, Properties props) throws IOException {
68         ClassLoader classLoader = DmaapUtil.class.getClassLoader();
69         InputStream inputStream = classLoader.getResourceAsStream(defaultProfFileName);
70         Properties defaultProps = new Properties();
71         defaultProps.load(inputStream);
72         defaultProps.setProperty("topic",topic);
73
74         String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
75         if(props != null && !props.isEmpty()){
76             defaultProps.putAll(props);
77         }
78         defaultProps.setProperty("topic",topic);
79         defaultProps.setProperty("DME2preferredRouterFilePath",preferredRouteFileName);
80         String id = defaultProps.getProperty("id");
81         String topicConsumerPropFileName = defaultProfFileName;
82         topicConsumerPropFileName = id != null ? id+ DELIMITER +topicConsumerPropFileName : DELIMITER +topicConsumerPropFileName;
83         topicConsumerPropFileName = topic != null ? topic+ DELIMITER +topicConsumerPropFileName : DELIMITER +topicConsumerPropFileName;
84
85         defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName+" file created on the fly for topic:"+topic+" on:"+System.currentTimeMillis());
86         return topicConsumerPropFileName;
87     }
88
89 }