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